Nginx编译安装

 

Nginx编译安装

 

 

Linux 7为例子,前边OS的配置或者优化项比如关闭防火墙就忽略不写了。

主要写Nginx的编译安装。

 

官方下载网址:

Nginx

Tengine

 

一般我们都需要先装pcre, zlib,ssl,pcre为了重写rewrite,zlib为了gzip压缩,ssl 功能需要openssl库。 

可以使用yum安装,想使用最新版本的pcre,zlib以及ssl可以使用源码安装。

源码官网下载:pcrepcrezlib

ssl参考我另外的博客:Linux升级openssh过程

不过为了方便这里示例是使用yum进行安装的。

 

依赖包安装

yum install -y gcc gcc-c++ make automake autoconf libtool pcre pcre-devel openssl-devel zlib zlib-devel openssl

 

 

配置环境变量

vi ~/.bash_profile
export PATH=$PATH:/usr/local/nginx/sbin

source生效或者重新开启新新会话生效。

 

解压编译安装

tar -zxvf nginx-1.20.1.tar.gz
cd nginx-1.20.1
./configure --prefix=/usr/local/nginx \
--conf-path=/usr/local/nginx/conf/nginx.conf \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--with-file-aio \
--with-http_realip_module \
--with-http_ssl_module
make
make install
echo $?

[root@dm8 soft]# tar -zxvf nginx-1.20.1.tar.gz 
[root@dm8 soft]# cd nginx-1.20.1
[root@dm8 nginx-1.20.1]# ./configure --prefix=/usr/local/nginx \
> --conf-path=/usr/local/nginx/conf/nginx.conf \
> --with-http_gzip_static_module \
> --with-http_stub_status_module \
> --with-file-aio \
> --with-http_realip_module \
> --with-http_ssl_module

[root@dm8 nginx-1.20.1]# make
[root@dm8 nginx-1.20.1]# make install
[root@dm8 nginx-1.20.1]# echo $?

依赖包都安装的话基本不会遇到问题。

另外,如果开始pcre,zlib,ssl使用源码安装,则对应的需要的依赖包和configure使用如下:

yum install -y gcc gcc-c++ make automake autoconf libtool

./configure --prefix=/usr/local/nginx \
--sbin-path=/usr/local/nginx/sbin/nginx \
--conf-path=/usr/local/nginx/conf/nginx.conf \
--pid-path=/usr/local/nginx/logs/nginx.pid \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--with-file-aio \
--with-http_realip_module \
--with-http_ssl_module \
--with-pcre=/soft/pcre-8.45 \
--with-zlib=/soft/zlib-1.2.11 \
--with-openssl=/soft/openssl-1.1.1k

 

--with-pcre=/soft/pcre-8.45 指的是pcre-8.45的源码路径。

--with-zlib=/soft/zlib-1.2.11指的是zlib-1.2.11的源码路径。

--with-openssl=/soft/openssl-1.1.1k指的是openssl-1.1.1k的源码路径。

建议使用/usr/local/src/目录代替/soft。

 

配置开机自启

Nginx官网选择NGINX Init Scripts启动脚本,根据OS类型为RedHat/CentOS选择Red Hat NGINX Init Script

 

添加nginx启动脚本

  1 vi /etc/init.d/nginx
  2 #!/bin/sh
  3 #
  4 # nginx - this script starts and stops the nginx daemon
  5 #
  6 # chkconfig:   - 85 15
  7 # description:  NGINX is an HTTP(S) server, HTTP(S) reverse \
  8 #               proxy and IMAP/POP3 proxy server
  9 # processname: nginx
 10 # config:      /etc/nginx/nginx.conf
 11 # config:      /etc/sysconfig/nginx
 12 # pidfile:     /usr/local/nginx/logs/nginx.pid
 13 
 14 # Source function library.
 15 . /etc/rc.d/init.d/functions
 16 
 17 # Source networking configuration.
 18 . /etc/sysconfig/network
 19 
 20 # Check that networking is up.
 21 [ "$NETWORKING" = "no" ] && exit 0
 22 
 23 nginx="/usr/local/nginx/sbin/nginx"
 24 prog=$(basename $nginx)
 25 
 26 NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
 27 
 28 [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
 29 
 30 lockfile=/var/lock/subsys/nginx
 31 
 32 make_dirs() {
 33    # make required directories
 34    user=`$nginx -V 2>&1 | grep "configure arguments:.*--user=" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
 35    if [ -n "$user" ]; then
 36       if [ -z "`grep $user /etc/passwd`" ]; then
 37          useradd -M -s /bin/nologin $user
 38       fi
 39       options=`$nginx -V 2>&1 | grep 'configure arguments:'`
 40       for opt in $options; do
 41           if [ `echo $opt | grep '.*-temp-path'` ]; then
 42               value=`echo $opt | cut -d "=" -f 2`
 43               if [ ! -d "$value" ]; then
 44                   # echo "creating" $value
 45                   mkdir -p $value && chown -R $user $value
 46               fi
 47           fi
 48        done
 49     fi
 50 }
 51 
 52 start() {
 53     [ -x $nginx ] || exit 5
 54     [ -f $NGINX_CONF_FILE ] || exit 6
 55     make_dirs
 56     echo -n $"Starting $prog: "
 57     daemon $nginx -c $NGINX_CONF_FILE
 58     retval=$?
 59     echo
 60     [ $retval -eq 0 ] && touch $lockfile
 61     return $retval
 62 }
 63 
 64 stop() {
 65     echo -n $"Stopping $prog: "
 66     killproc $prog -QUIT
 67     retval=$?
 68     echo
 69     [ $retval -eq 0 ] && rm -f $lockfile
 70     return $retval
 71 }
 72 
 73 restart() {
 74     configtest || return $?
 75     stop
 76     sleep 1
 77     start
 78 }
 79 
 80 reload() {
 81     configtest || return $?
 82     echo -n $"Reloading $prog: "
 83     killproc $prog -HUP
 84     retval=$?
 85     echo
 86 }
 87 
 88 force_reload() {
 89     restart
 90 }
 91 
 92 configtest() {
 93   $nginx -t -c $NGINX_CONF_FILE
 94 }
 95 
 96 rh_status() {
 97     status $prog
 98 }
 99 
100 rh_status_q() {
101     rh_status >/dev/null 2>&1
102 }
103 
104 case "$1" in
105     start)
106         rh_status_q && exit 0
107         $1
108         ;;
109     stop)
110         rh_status_q || exit 0
111         $1
112         ;;
113     restart|configtest)
114         $1
115         ;;
116     reload)
117         rh_status_q || exit 7
118         $1
119         ;;
120     force-reload)
121         force_reload
122         ;;
123     status)
124         rh_status
125         ;;
126     condrestart|try-restart)
127         rh_status_q || exit 0
128             ;;
129     *)
130         echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
131         exit 2
132 esac

 说明:这段脚本分别修改了第12,23,26行。

其中第12行为nginx.pid位置,虽然是#注释的,不过会影响启动时候pid的判定位置,导致卡住。

在使用service nginx start的之后会卡住并生成/run/systemd/generator.late/nginx.service文件,该文件中PIDFile的值根据这第12行的位置来生成。

 

比如如果不修改12行,默认是

# pidfile:     /var/run/nginx.pid

此时有如下问题

[root@dm8 init.d]# service nginx start
Starting nginx (via systemctl):  Warning: nginx.service changed on disk. Run 'systemctl daemon-reload' to reload units.
卡住......

使用systemctl看状态有:

[root@dm8 ~]# systemctl status nginx
● nginx.service - SYSV: NGINX is an HTTP(S) server, HTTP(S) reverse proxy and IMAP/POP3 proxy server
   Loaded: loaded (/etc/rc.d/init.d/nginx; bad; vendor preset: disabled)
   Active: activating (start) since Fri 2021-08-27 06:17:14 EDT; 18s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 1627 ExecStart=/etc/rc.d/init.d/nginx start (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/nginx.service
           ├─1287 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
           └─1289 nginx: worker process

Aug 27 06:17:14 dm8 systemd[1]: Starting SYSV: NGINX is an HTTP(S) server, HTTP(S) reverse proxy and IMAP/POP3 proxy server...
Aug 27 06:17:14 dm8 systemd[1]: Can't open PID file /var/run/nginx.pid (yet?) after start: No such file or directory

看最后一行的路径就有问题。

并且有

[root@dm8 ~]# cat /run/systemd/generator.late/nginx.service
# Automatically generated by systemd-sysv-generator

[Unit]
Documentation=man:systemd-sysv-generator(8)
SourcePath=/etc/rc.d/init.d/nginx
Description=SYSV: NGINX is an HTTP(S) server, HTTP(S) reverse proxy and IMAP/POP3 proxy server

[Service]
Type=forking
Restart=no
TimeoutSec=5min
IgnoreSIGPIPE=no
KillMode=process
GuessMainPID=no
RemainAfterExit=no
PIDFile=/var/run/nginx.pid
ExecStart=/etc/rc.d/init.d/nginx start
ExecStop=/etc/rc.d/init.d/nginx stop
ExecReload=/etc/rc.d/init.d/nginx reload

 

 

授权启动脚本

chmod 755 /etc/init.d/nginx

 

 

启动Nginx并设置开机启动

确认当前Nginx没在运行。

[root@dm8 init.d]# service nginx start
Starting nginx (via systemctl):                            [  OK  ]

 

此时,会有如下文件产生。

[root@dm8 conf]# ll /run/systemd/generator.late/nginx.service 
-rw-r--r--. 1 root root 510 Aug 27 07:04 /run/systemd/generator.late/nginx.service

 

将之拷贝至如下目录。

cp /run/systemd/generator.late/nginx.service /usr/lib/systemd/system/

 

之后的/usr/lib/systemd/system/nginx.service内容如下(有修改)。

[root@dm8 conf]# cat /usr/lib/systemd/system/nginx.service 
# Automatically generated by systemd-sysv-generator

[Unit]
Documentation=man:systemd-sysv-generator(8)
SourcePath=/etc/rc.d/init.d/nginx
Description=SYSV: NGINX is an HTTP(S) server, HTTP(S) reverse proxy and IMAP/POP3 proxy server

[Service]
Type=forking
Restart=no
TimeoutSec=5min
IgnoreSIGPIPE=no
KillMode=process
GuessMainPID=no
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
RemainAfterExit=no
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/etc/rc.d/init.d/nginx start
ExecStop=/etc/rc.d/init.d/nginx stop
ExecReload=/etc/rc.d/init.d/nginx reload
ExecStartPost=/bin/sleep 0.1

[Install]
WantedBy=multi-user.target

 

如果不添加

ExecStartPost=/bin/sleep 0.1

systemctl status nginx的话有如下问题,根据Systemd Error "systemd[1]: Failed to read PID from file /var/run/iscsid.pid: Invalid argument" (文档 ID 2335138.1)类似的情形,可以忽略:

Aug 27 03:39:43 dm8 systemd[1]: Can't open PID file /usr/local/nginx/logs/nginx.pid (yet?) after start: No such file or directory
或者
Aug 27 10:15:51 dm8 systemd[1]: Failed to parse PID from file /usr/local/nginx/logs/nginx.pid: Invalid argument

如果不添加

[Install]
WantedBy=multi-user.target

则无法被enable自启动。

另外的解决方法:Failed to parse PID from file

 

也可以参考官网NGINX systemd service file做修改,正常不需要。

重载且设置开机自启。

[root@dm8 ~]# systemctl daemon-reload
[root@dm8 ~]# systemctl enable nginx
[root@dm8 ~]# systemctl restart nginx
[root@dm8 ~]# systemctl status nginx

 

另外也可以不需要/etc/init.d/nginx脚本(兼容Linux 6),直接参考NGINX systemd service file创建配置nginx.server。

 

参考链接

https://www.nginx.com/resources/wiki/start/topics/examples/initscripts/

https://www.nginx.com/resources/wiki/start/topics/examples/redhatnginxinit/

https://www.kancloud.cn/tinywan/nginx_tutorial/753832

https://www.nginx.cn/install

https://www.cnblogs.com/skynet/p/4146083.html

Systemd Error "systemd[1]: Failed to read PID from file /var/run/iscsid.pid: Invalid argument" (文档 ID 2335138.1)

posted @ 2021-08-26 23:34  PiscesCanon  阅读(409)  评论(0编辑  收藏  举报