一、实验环境
操作系统: CentOS7.2 Minimal
IP: 192.168.1.105
nginx版本: 1.15.3
二、安装编译环境
nginx源码都是用C/C++写的,所以需要在编译用的CentOS 7服务器上安装gcc和gcc-c++等相关软件包。
# yum -y install vim epel-release wget
安装编译工具
# yum -y install gcc gcc-c++ autoconf automake make
安装依赖库
# yum -y install openssl openssl-devel libxml2-devel libxslt-devel perl-devel perl-ExtUtils-Embed libtool zlib zlib-devel pcre pcre-devel patch
三、创建用户及用户组
# groupadd -r nginx
# useradd -r -g nginx -s/sbin/nologin -d /usr/local/nginx -M nginx
四、创建相关目录
1.创建编译后的nginx存放目录
# mkdir -pv /usr/local/nginx
2. 创建缓存目录
# mkdir -pv /usr/local/nginx/tmp/{client_body,proxy,fastcgi,uwsgi,scgi}
3. 创建logs、etc 目录
# mkdir -pv /usr/local/nginx/{logs,etc}
五、软件下载及解压
# wget http://nginx.org/download/nginx-1.15.3.tar.gz
# tar -zxvf nginx-1.15.3.tar.gz
nginx第三方模块:nginx-sticky-module,基于cookie的会话保持
# wget https://bitbucket.org/nginx-goodies/nginx-sticky-module-ng/get/master.tar.gz
# tar -zxvf /master.tar.gz
# mv nginx-goodies-nginx-sticky-module-ng-08a395c66e42/ nginx-sticky-module/
六、编译
# cd nginx-1.15.3
############################################
# ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--sbin-path=/usr/local/nginx/sbin/nginx \
--conf-path=/usr/local/nginx/etc/nginx.conf \
--error-log-path=/usr/local/nginx/logs/error.log \
--http-log-path=/usr/local/nginx/logs/access.log \
--pid-path=/var/run/nginx.pid \
--lock-path=/usr/local/nginx/nginx.lock \
--http-client-body-temp-path=/usr/local/nginx/tmp/client_body \
--http-proxy-temp-path=/usr/local/nginx/tmp/proxy \
--http-fastcgi-temp-path=/usr/local/nginx/tmp/fastcgi \
--http-uwsgi-temp-path=/usr/local/nginx/tmp/uwsgi \
--http-scgi-temp-path=/usr/local/nginx/tmp/scgi \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_xslt_module \
--with-http_stub_status_module \
--with-http_sub_module \
--with-http_random_index_module \
--with-http_degradation_module \
--with-http_secure_link_module \
--with-http_gzip_static_module \
--with-http_perl_module \
--add-module=/root/nginx-sticky-module \
--with-debug \
--with-file-aio \
--with-mail \
--with-mail_ssl_module \
--with-stream \
--with-ld-opt="-Wl,-E"
#########################################
编译选项说明
# make && make install
# ll -R /usr/local/nginx
查看nginx版本和编译参数
# /usr/local/nginx/sbin/nginx -V 2>&1 | sed 's/ --/\n--/g' | egrep --color '.*path.*|$'
七、编写启动脚本或者unit文件,二选一
启动脚本
# vim /etc/init.d/nginx
####################################################
#! /bin/bash
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig: - 85 15
# description: The nginx HTTP and reverse proxy server
#
#
# processname: nginx
# config: /etc/nginx/nginx.conf
# pidfile: /var/run/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/usr/local/nginx/etc/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile="/var/lock/subsys/nginx.lock"
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
echo -n "Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n "Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $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
#####################################################
unit文件
# vim /etc/systemd/system/nginx.service
##############################################
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/run/nginx.pid
# Nginx will fail to start if /run/nginx.pid already exists but has the wrong
# SELinux context. This might happen when running `nginx -t` from the cmdline.
# https://bugzilla.redhat.com/show_bug.cgi?id=1268621
ExecStartPre=/usr/bin/rm -f /run/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true
[Install]
WantedBy=multi-user.target
##############################################
注: /var/run/ --------> /run/
八、启动nginx 服务
关闭selinux
# setenforce 0
# sed -i 's/^SELINUX=.*/SELINUX=permissive/g' /etc/selinux/config
如果选用启动脚本
# chmod +x /etc/init.d/nginx
# chkconfig --add nginx
# chkconfig nginx on
# service nginx start
# service nginx status
如果选用unit文件
# systemctl daemon-reload
# systemctl enable nginx
# systemctl start nginx
# systemctl status nginx
九、用非root用户启动nginx的问题
用编译好的rpm包安装的nginx,master进程的运行用户是root,无法更改,worker进程的运行用户默认是nginx(可以更改),可以监听1024以下端口。
如果需要以sysV init 或者systemd启动nginx,那么编译nginx时,pid文件路径有要求 --pid-path=/var/run/nginx.pid ,或者更改配置文件中的pid文件生成的默认路径,否则用启动脚本或者unit文件启动服务失败!
# vim /usr/local/nginx/etc/nginx.conf
# systemctl restart nginx
# systemctl status nginx
如果因为安全加固原因,nginx服务的所有进程需要以非root用户运行,那么必须使用编译的nginx
例如以nginx用户,那么需要注意:
1 创建的nginx用户需要能被切换过去,也就是 shell不能为 /sbin/nologin,否则无法执行 su - nginx -c "/usr/local/nginx/sbin.nginx"
# groupadd nginx
# useradd nginx -d /home/nginx
2 相应的文件目录属主属组更改为nginx用户 , 需 chown -R nginx:nginx /usr/local/nginx
3. nginx配置文件中,监听端口需大于1024,否则会报权限拒绝
4 编译nginx时,pid文件路径能自定义 ,一般无需编译时指定,默认为logs/nginx.pid
# vim /usr/local/nginx/etc/nginx.conf
# su - nginx -c "/usr/local/nginx/sbin/nginx -c /usr/local/nginx/etc/nginx.conf"
# ps aux | grep nginx
# ss -tan
十、参考
CentOS 7.5 编译安装 Nginx 1.15.3
https://segmentfault.com/a/1190000016498647
nginx最新稳定版的rpm安装
https://www.jianshu.com/p/c2f579c44055?tdsourcetag=s_pcqq_aiomsg
nginx会话保持之nginx-sticky-module模块
https://www.jianshu.com/p/6d973db47ee4
nginx 编译参数详解
http://www.ttlsa.com/nginx/nginx-configure-descriptions
nginx作为web服务以及nginx.conf详解
http://www.cnblogs.com/f-ck-need-u/p/7576137.html#nginx
How to Install Nginx on CentOS 7 / RHEL 7
https://webhostinggeeks.com/howto/how-to-install-nginx-on-centos-7-rhel-7