$ yum install gcc-c++
$ yum install pcre pcre-devel
$ yum install zlib zlib-devel
$ yum install openssl openssl--devel
mkdir -p /usr/local/nginx #创建一个安装目录
cd /usr/local/nginx #切换到创建好的目录
wget http://nginx.org/download/nginx-1.10.3.tar.gz #下载安装包
tar -zxvf nginx-1.10.3.tar.gz #解压
cd nginx-1.10.3 #到解压目录下编译
./configure --prefix=/usr/local/nginx --with-pcre --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-http_realip_module
#在编译的时候可能遇到以下报错:
/configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option. #没有安装pcre-devel
./configure: error: SSL modules require the OpenSSL library.
You can either do not enable the modules, or install the OpenSSL library
into the system, or build the OpenSSL library statically from the source
with nginx by using --with-openssl=<path> option #没有安装openssl-devel
#针对上诉的报错情况,执行下面的安装命令,然后就可以正常编译
yum -y install pcre-devel openssl openssl-devel
make && make install #安装
安装完成之后把Nginx添加成系统服务
vim /etc/init.d/nginx #在 /etc/init.d下创建一个nginx文件,并输入以下代码
#! /bin/bash
chkconfig: - 85 15
PATH=/usr/local/nginx #Nginx的安装路径
DESC="nginx daemon"
NAME=nginx
DAEMON=$PATH/sbin/$NAME
CONFIGFILE=$PATH/conf/$NAME.conf
PIDFILE=$PATH/logs/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
set -e
[ -x "$DAEMON" ] || exit 0
do_start() {
$DAEMON -c $CONFIGFILE || echo -n "nginx already running"
}
do_stop() {
$DAEMON -s stop || echo -n "nginx not running"
}
do_reload() {
$DAEMON -s reload || echo -n "nginx can‘t reload"
}
case "$1" in
start)
echo -n "Starting $DESC: $NAME"
do_start
echo "."
;;
stop)
echo -n "Stopping $DESC: $NAME"
do_stop
echo "."
;;
reload|graceful)
echo -n "Reloading $DESC configuration..."
do_reload
echo "."
;;
restart)
echo -n "Restarting $DESC: $NAME"
do_stop
do_start
echo "."
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|reload|restart}" >&2
exit 3
;;
esac
exit 0
chmod a+x /etc/init.d/nginx #给执行权限
chkconfig --add nginx #注册成服务
chkconfig nginx on #设置开机启动
添加成系统服务之后可以使用以下命令控制Nginx服务
systemctl start nginx.service #启动nginx服务
systemctl stop nginx.service #停止nginx服务
systemctl restart nginx.service #重启nginx服务
systemctl reload nginx.service #重新读取nginx配置(这个最常用, 不用停止nginx服务就能使修改的配置生效)
原文地址:http://blog.51cto.com/888888hfer/2064474