源码编译安装nginx及设置开机启动项
1、上传nginx文档;解压到/data目录下,并安装依赖包
tar xf nginx-1.20.1.tar.gz -C /data/
cd /data/nginx-1.20.1/ && ll
依赖关系安装,执行如下:
yum -y install zlib pcre pcre-devel openssl openssl-devel
2、自定义安装nginx
./configure --prefix=/data/nginx-1.20.1 --conf-path=/data/nginx-1.20.1/nginx.conf --error-log-path=/data/nginx-1.20.1/logs/error.log --http-log-path=/data/nginx-1.20.1/logs/access.log --with-http_stub_status_module --pid-path=/data/nginx-1.20.1/nginx.pid --without-http_rewrite_module
make && make install
3、启动nginx
(1)常规方式:
cd /usr/local/nginx /sbin/nginx -c conf/nginx.conf
(2)编辑脚本方式:
源码编译安装nginx是没有nginx服务的,对于这种情况的解决方案有:
编辑脚本,如下示例:
vi /root/nginx_lxc #!/bin/bash if [ "$1" = "start" ] then if [ -f /data/nginx-1.20.1/nginx.pid -a -s /data/nginx-1.20.1/nginx.pid ] then : else /data/nginx-1.20.1/sbin/nginx -c /data/nginx-1.20.1/nginx.conf echo "nginx is running..." fi elif [ "$1" = "stop" ] then if [ -f /data/nginx-1.20.1/nginx.pid -a -s /data/nginx-1.20.1/nginx.pid ] then kill `cat /data/nginx-1.20.1/nginx.pid` echo "nginx is killed!" else echo "Failed to stop nginx Unit nginx not loaded." exit 1 fi elif [ "$1" = "restart" ] then /data/nginx-1.20.1/sbin/nginx -s reload echo "nginx is reload" else echo "Usage: sh $0 {start|stop|restart}" fi
保存退出后,授执行权限:
chmod +x /root/nginx_lxc
(3)设置开机启动项方式1:
在linux6.X或centos6.X系统下设置开机启动项,在/etc/init.d目录下添加nginx的自动启动脚本,如下示例:(说明:在linux7.x或centos7.x系统下也可这样设置)
#!/bin/bash # # chkconfig: 2345 85 95 # description: Nginx is a World Wide Web server. # processname: nginx if [ "$1" = "start" ] then if [ -f /data/nginx-1.20.1/nginx.pid -a -s /data/nginx-1.20.1/nginx.pid ] then : else /data/nginx-1.20.1/sbin/nginx -c /data/nginx-1.20.1/nginx.conf echo "nginx is running..." fi # /data/nginx-1.20.1/sbin/nginx -c /data/nginx-1.20.1/nginx.conf # echo "nginx is running..." elif [ "$1" = "stop" ] then if [ -f /data/nginx-1.20.1/nginx.pid -a -s /data/nginx-1.20.1/nginx.pid ] then kill `cat /data/nginx-1.20.1/nginx.pid` echo "nginx is killed!" else echo "Failed to stop nginx Unit nginx not loaded." exit 1 fi elif [ "$1" = "restart" ] then /data/nginx-1.20.1/sbin/nginx -s reload echo "nginx is reload" else echo "Usage: sh $0 {start|stop|restart}" fi 脚本编辑好后,添加执行权限 chmod 775 nginx
命令执行:
启动Nginx
service nginx start
停止Nginx
service nginx stop
重启Nginx
service nginx restart
最后设置开机自启动,执行:
chkconfig --add nginx
chkconfig nginx on
查询是否设置开启自启动成功,执行:
chkconfig --list nginx
(4)设置开机启动项方式2:
在linux7.x或centos7.x系统使用systemctl管理nginx服务。
说明:由于linux7系统没有抛弃chkconfig方式管理服务,这里延续上面示例中设置的启动、停止和重启路径,如未完成之前设置,可改为nginx程序路径下的启动路径。
在/usr/lib/systemd/system目录下,创建nginx.service脚本,如下示例:
[Unit] Description=Nginx service After=network.target [Service] Type=forking PIDFile=/data/nginx-1.20.1/nginx.pid ExecStart=/etc/init.d/nginx start ExecReload=/etc/init.d/nginx restart ExecStop=/etc/init.d/nginx stop PrivateTmp=true [Install] WantedBy=multi-user.target
保存退出 设置开机启动: systemctl enable nginx.service
其他操作
systemctl start nginx
systemctl stop nginx
systemctl status nginx.service
systemctl disable nginx
查看是否已添加开机自启动服务
systemctl list-unit-files |grep nginx
4、nginx服务及端口查看
ss -antlup |grep nginx
ps -ef |grep nginx
lsof -i :80