nginx 1.12安装

准备工作
使用root用户安装。
到nginx官网下载Linux源码或者执行:wget http://nginx.org/download/nginx-1.12.2.tar.gz。
到pcre站点下载zlilb源码或者执行:wget https://ftp.pcre.org/pub/pcre/pcre-8.41.tar.gz。
到zlib官网下载zlilb源码或者执行:wget https://ncu.dl.sourceforge.net/project/libpng/zlib/1.2.11/zlib-1.2.11.tar.gz。
到openssl官网下载openssl源码或者执行:wget https://www.openssl.org/source/openssl-1.1.0g.tar.gz。
一般编译nginx时,都要先安装pcre、zlib、openssl等,然后编译安装nginx时指定这些程序的安装目录,这样nginx在每次启动的时候,就会去动态加载这些程序了。但是也可以将这些程序静态编译到nginx里面,这样nginx启动时就不用动态加载了,这种方式效率更高,也更方面替换这些程序的版本,因为像zlib会被很多程序依赖,不好升级。因此下载完以上4个tar.gz后,全部解压缩到/home/test:
tar -zxvf nginx-1.12.2.tar.gz -C /home/test
tar -zxvf pcre-8.41.tar.gz -C /home/test
tar -zxvf zlib-1.2.11.tar.gz -C /home/test
ar -zxvf openssl-1.1.0g.tar.gz -C /home/test
 
构建和安装
构建时使用configure命令进行配置,它定义了系统的各个方面,包括nginx被允许用于连接处理的方法。最后,它创建了一个Makefile。configure支持的参数可以使用./configure --help查看:
--prefix:定义一个将保存服务器文件的目录。此目录也将用于configure命令配置的(除库源文件路径外)和nginx.conf配置文件中的所有相对路径。默认路径为/usr/local/nginx。
--sbin-path=path:设置nginx可执行文件的路径。此路径仅在安装期间使用。默认路径为prefix/ sbin/nginx。
--conf-path=path:设置nginx.conf的路径。如有必要,总是可以 在nginx启动时通过命令行参数-c file指定一个不同的配置文件路径。 默认路径为prefix/conf/nginx.conf。
--pid-path=path:设置nginx.pid文件的路径,这个文件将存储主进程的进程ID。安装完成后,此路径可以在nginx.conf文件中使用pid指令更改。默认路径为prefix/ logs/nginx. pid。
--error-log-path=path:设置记录主要错误、警告以及诊断文件的路径。安装完成后, 该路径总是可以在nginx.conf文件中用 error_log 指令更改。 默认路径为prefix/logs/error.log。
--http-log-path=path:设置记录HTTP服务器主要请求日志的完整路径。安装完成后, 该路径总是可以在nginx.conf文件用 access_log 指令来修改。 默认路径为prefix/logs/access.log。
--build=name:设置一个可选的nginx构建名称。
--user=name: 设置工作进程以某非特权用户的身份运行。安装完成后,该用户名总是可以在 nginx.conf文件用user指令来修改。 默认值为nobody。
--group=name:设置工作进程以某非特权用户组的身份运行。安装完成后,该用户名总是可以在 nginx.conf用user指令来修改。 默认名称与--user相同。
--with-select_module:启用或禁用构建一个允许服务器使用select()方法的模块。如果平台不支持更合适的方法比如 kqueue,epoll,/dev/poll,则该模块是自动构建的。
--with-poll_module:启用或禁用构建一个允许服务器使用poll()方法的模块。如果平台不支持更合适的方法比如 kqueue,epoll,/dev/poll,则该模块是自动构建的。
--without-http_gzip_module:不构建HTTP服务器的压缩响应模块ngx_http_gzip_module。该模块需要zlib库才能构建和运行。
--without-http_rewrite_module:不构建允许HTTP服务器重定向请求和更改请求的URI的模块ngx_http_rewrite_module。该模块需要PCRE库才能构建和运行。
--without-http_proxy_module:不构建HTTP服务器代理模块ngx_http_proxy_module。
--with-http_ssl_module:构建一个让HTTP服务器支持https协议的模块,这个模块默认不会构建。该模块需要OpenSSL库才能构建和运行。
--with-pcre=path:设置PCRE库源文件的路径。PCRE库的发行版(version 4.4 — 8.41)需要先从PCRE站点下载并解压缩。接下来的操作由nginx的./configure和make命令来完成。该库用于location指令的正则表达式支持以及ngx_http_rewrite_module模块。
--with-pcre-jit:构建PCRE库以支持即时编译。
--with-zlib=path:设置zlib库源文件的路径。zlib库的发行版(version 1.1.3 — 1.2.11)需要先从zlib站点下载并解压缩。接下来的操作由nginx的./configure和make命令来完成。该库用于ngx_http_gzip_module模块。
--with-openssl=path:设置openssl库源文件的路径。openssl库的发行版需要先从openssl站点下载并解压缩。接下来的操作由nginx的./configure和make命令来完成。该库用于ngx_http_gzip_module模块。
--with-cc-opt=parameters:设置将添加到CFLAGS变量中的额外参数。当在FreeBSD下使用系统PCRE库时,需要指定--with-cc-opt="-I /usr/local/include"。 如果需要增加select()方法所支持的文件数量,也可以参照如下方式指定:--with-cc-opt="-D FD_SETSIZE=2048"。
--with-ld-opt=parameters: 设置将会在链接过程中使用的额外参数。当在FreeBSD下使用系统PCRE库时,需要指定--with-ld-opt="-L /usr/local/lib"。
--with-http_stub_status_module:ngx_http_stub_status_module模块提供了对基本状态信息的访问。
 
步骤:
1.cd /home/test/nginx-1.12.2
2../configure --sbin-path=/usr/local/nginx/nginx --conf-path=/usr/local/nginx/nginx.conf --pid-path=/usr/local/nginx/nginx.pid --with-http_ssl_module --with-pcre=/home/test/pcre-8.41 --with-zlib=/home/test/zlib-1.2.11 --with-openssl=/home/test/openssl-1.1.0g --with-http_stub_status_module
3.make
4.make install
 
配置服务
sysvinit
参考脚本地址:https://www.nginx.com/resources/wiki/start/topics/examples/redhatnginxinit/。
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig: 35 85 15
# description: NGINX is an HTTP(S) server, HTTP(S) reverse
# proxy and IMAP/POP3 proxy server
 
# Source function library.
. /etc/rc.d/init.d/functions
 
# Source networking configuration.
. /etc/sysconfig/network
 
# Check that networking is up.
test "$NETWORKING" = "no" && exit 0
 
nginx="/usr/sbin/nginx"
prog=$(basename $nginx)
 
NGINX_CONF_FILE="/etc/nginx/nginx.conf"
 
test -f /etc/sysconfig/nginx && . /etc/sysconfig/nginx
 
lockfile=/var/lock/subsys/nginx
 
make_dirs() {
# make required directories
user=`$nginx -V 2>&1 | grep "configure arguments:.*--user=" | sed 's/[^*]*--user=([^ ]*).*/1/g' -`
if test -n "$user" ; then
if test -z "`grep $user /etc/passwd`" ; then
useradd -M -s /bin/nologin $user
fi
options=`$nginx -V 2>&1 | grep 'configure arguments:'`
for opt in $options; do
if test `echo $opt | grep '.*-temp-path'` ; then
value=`echo $opt | cut -d "=" -f 2`
if test ! -d "$value" ; then
# echo "creating" $value
mkdir -p $value && chown -R $user $value
fi
fi
done
fi
}
 
start() {
test -x $nginx || exit 5
test -f $NGINX_CONF_FILE || exit 6
make_dirs
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
test $retval -eq 0 && touch $lockfile
return $retval
}
 
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
test $retval -eq 0 && rm -f $lockfile
return $retval
}
 
restart() {
configtest || return $?
stop
sleep 1
start
}
 
reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}
 
force_reload() {
restart
}
 
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
 
rh_status() {
status $prog
}
 
rh_status_q() {
rh_status > /dev/null 2>&1
}
 
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac
将以上脚本保存到/etc/rc.d/init.d/nginx文件。nginx="/usr/sbin/nginx"修改为正确的nginx文件路径。NGINX_CONF_FILE="/etc/nginx/nginx.conf"修改为正确的nginx.conf文件路径。
35代表在3和5运行级别下开机启动,85代表启动时脚本的执行顺序,15代表关闭时脚本的执行顺序。master进程由root用户启动,worker进程由配置文件的user指令指定,默认是nobody。
 
让所有用户可以执行这个服务:chmod a+x /etc/rc.d/init.d/nginx
添加nginx服务:chkconfig --add nginx
查看nginx服务:chkconfig --list nginx
开启nginx服务:service nginx start
关闭nginx服务:service nginx restart
 
如何卸载?
1.service nginx stop停止服务
2.chkconfig --del nginx删除服务
3.删除/etc/rc.d/init.d/nginx文件
4.删除/usr/local/nginx/目录
 
CentOS7
参考脚本地址:https://www.nginx.com/resources/wiki/start/topics/examples/systemd/。
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target
 
[Service]
Type=forking
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
 
[Install]
WantedBy=multi-user.target
将以上脚本保存到/usr/lib/systemd/system/nginx.service。ExecStartPre=/usr/sbin/nginx -t
和ExecStart=/usr/sbin/nginx 修改为正确的nginx文件路径。master进程由root用户启动,worker进程由配置文件的user指令指定,默认是nobody。
#启动,服务名为 nginx.service 文件名中的前半部分即“nginx”
systemctl start nginx
#检测状态
systemctl status nginx
#设置为开机启动
systemctl enable nginx
#检测是否开机启动
systemctl is-enabled nginx
 
如何卸载?
1.systemctl stop nginx 停止服务
2.systemctl disenable nginx 禁止开机启动
3.删除/usr/lib/systemd/system/nginx.service文件
4.删除/usr/local/nginx/目录
 
原文地址:https://www.cnblogs.com/gjb724332682/p/8610690.html