本测试环境为:CentOS Linux release 8.2.2004 (Core)
Step1:安装编译所需的软件包:
[root@LFS ~]# yum install gcc-c++ make automake
[root@LFS ~]# yum install -y pcre pcre-devel
[root@LFS ~]# yum install -y zlib zlib-devel
[root@LFS ~]# yum install -y openssl openssl-devel
Step2:下载源代码包:
[root@LFS lfs]# wget http://nginx.org/download/nginx-1.19.2.tar.gz
[root@LFS lfs]# tar zxvf nginx-1.19.2.tar.gz
[root@LFS lfs]# cd nginx-1.19.2
Step3:编译安装:
[root@LFS lfs]# cd nginx-1.19.2 [root@LFS nginx-1.19.2]# ./configure --prefix=/usr/local/nginx
--conf-path=/usr/local/nginx/etc/nginx.conf
--user=nginx --group=nginx
--error-log-path=/usr/local/nginx/nginxlog/error.log
--http-log-path=/usr/local/nginx/nginxlog/access.log
--pid-path=/usr/local/nginx/pids/nginx.pid
--lock-path=/usr/local/nginx/locks/nginx.lock
--with-http_ssl_module
--with-http_stub_status_module
--with-http_gzip_static_module
--http-client-body-temp-path=/usr/local/nginx/tmp/client
--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
[root@LFS nginx-1.19.2]# mkdir -pv /usr/local/nginx/tmp/{client,proxy,fastcgi,uwsgi,scgi}
mkdir: created directory '/usr/local/nginx/tmp'
mkdir: created directory '/usr/local/nginx/tmp/client'
mkdir: created directory '/usr/local/nginx/tmp/proxy'
mkdir: created directory '/usr/local/nginx/tmp/fastcgi'
mkdir: created directory '/usr/local/nginx/tmp/uwsgi'
mkdir: created directory '/usr/local/nginx/tmp/scgi'
Step4:启动服务:
[root@LFS nginx-1.19.2]# /usr/local/nginx/sbin/nginx
nginx: [emerg] getpwnam("nginx") failed
解决方法:
[root@LFS nginx-1.19.2]# useradd -s /sbin/nologin -M nginx
Step5:配置开机启动:
[root@LFS nginx-1.19.2]# vim /etc/init.d/nginx
#!/bin/bash # nginx This shell script takes care of starting and stopping # nginx # # chkconfig: - 13 68 # description: nginx is a web server ### BEGIN INIT INFO # Provides: $named # Short-Description: start|stop|status|restart|configtest ### END INIT INFO #variables NGINX_BIN="/usr/local/nginx/sbin/nginx" NGINX_CONF="/usr/local/nginx/conf/nginx.conf" NGINX_PID="/usr/local/nginx/logs/nginx.pid" NETSTAT="/bin/netstat" alter=$1 prog=nginx #load system function . /etc/rc.d/init.d/functions #function:echo ok or error function if_no { if [ $2 == 0 ]; then echo -n $"$1 ${prog}:" && success && echo else echo -n $"$1 ${prog}:" && failure && echo fi } #start nginx function start { rm -f ${NGINX_PID} 2>/dev/null if [ -s ${NGINX_PID} ]; then echo "nginx already running" else if [ `${NETSTAT} -tnpl | grep nginx | wc -l` -eq 0 ]; then rm -f ${NGINX_PID} 2>/dev/null ${NGINX_BIN} -c ${NGINX_CONF} if_no start $? else ${NETSTAT} -tnpl | grep nginx | awk '{ print $7}' | cut -d '/' -f 1 > ${NGINX_PID} if_no start $? fi fi } #stp nginx function stop { if [ -s ${NGINX_PID} ]; then cat ${NGINX_PID} | xargs kill -QUIT if_no stop $? else if [ `${NETSTAT} -tnpl | grep nginx | wc -l` -eq 0 ]; then rm -f ${NGINX_PID} 2>/dev/null if_no stop 0 else rm -f ${NGINX_PID} 2>/dev/null kill `${NETSTAT} -tnpl | grep nginx | awk '{ print $7}' | cut -d '/' -f 1` if_no stop $? fi fi } function restart { if [ -s ${NGINX_PID} ]; then cat ${NGINX_PID} | xargs kill -HUP if_no restart $? else stop sleep 1 #!/bin/bash # nginx This shell script takes care of starting and stopping # nginx # # chkconfig: - 13 68 # description: nginx is a web server ### BEGIN INIT INFO # Provides: $named # Short-Description: start|stop|status|restart|configtest ### END INIT INFO #variables NGINX_BIN="/usr/local/nginx/sbin/nginx" NGINX_CONF="/usr/local/nginx/etc/nginx.conf" NGINX_PID="/usr/local/nginx/pids/nginx.pid" NETSTAT="/bin/netstat" alter=$1 prog=nginx #load system function . /etc/rc.d/init.d/functions #function:echo ok or error function if_no { if [ $2 == 0 ]; then echo -n $"$1 ${prog}:" && success && echo else echo -n $"$1 ${prog}:" && failure && echo fi } #start nginx function start { rm -f ${NGINX_PID} 2>/dev/null
Step6:修改启动项
[root@LFS nginx-1.19.2]# chmod +x /etc/init.d/nginx
[root@LFS nginx-1.19.2]# chkconfig --add nginx
[root@LFS nginx-1.19.2]# chkconfig --level 35 nginx on
执行启动、状态、关闭、重启操作
[root@LFS nginx-1.19.2]#systemctl start nginx.service
[root@LFS nginx-1.19.2]#systemctl status nginx.service
[root@LFS nginx-1.19.2]#systemctl stop nginx.service
[root@LFS nginx-1.19.2]#systemctl restart nginx.service