Nginx是广为流行的轻量级Web服务器软件。它开源,短小精悍,简单易用,深受广大互联网企业以及IT运维人员所喜爱。很多时候,我们在生产环境基于编译方式安装Nginx后,Nginx需要手工配置自启动服务,以确保服务器异常宕机后自动重启该服务。以下描述的是基于CentOS 7下来配置自启动服务,供大家参考。
一、yum 安装方式Nginx自启动
当前环境
[root@node142 ~]# more /etc/redhat-release CentOS Linux release 7.2.1511 (Core)
查看是否保护nginx rpm包
[root@node142 ~]# rpm -qa|grep nginxnginx-mod-http-geoip-1.12.2-2.el7.x86_64nginx-1.12.2-2.el7.x86_64nginx-filesystem-1.12.2-2.el7.noarchnginx-mod-http-xslt-filter-1.12.2-2.el7.x86_64nginx-mod-stream-1.12.2-2.el7.x86_64nginx-mod-http-perl-1.12.2-2.el7.x86_64nginx-mod-http-image-filter-1.12.2-2.el7.x86_64nginx-all-modules-1.12.2-2.el7.noarchnginx-mod-mail-1.12.2-2.el7.x86_64
查看是否存在相应的服务,如下,有nginx.service
[root@node142 ~]# systemctl list-unit-files |grep nginxnginx.service disabled
将其配置为自动
[root@node142 ~]# systemctl enable nginx.service
查看nginx.service文件
[root@node142 ~]# more /lib/systemd/system/nginx.service[Unit]Description=The nginx HTTP and reverse proxy serverAfter=network.target remote-fs.target nss-lookup.target[Service]Type=forkingPIDFile=/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=1268621ExecStartPre=/usr/bin/rm -f /run/nginx.pidExecStartPre=/usr/sbin/nginx -tExecStart=/usr/sbin/nginxExecReload=/bin/kill -s HUP $MAINPIDKillSignal=SIGQUITTimeoutStopSec=5KillMode=processPrivateTmp=true[Install]WantedBy=multi-user.target
上述配置文件中的内容和官网提供的一模一样
https://www.nginx.com/resources/wiki/start/topics/examples/systemd/
二、编译安装后的自启动配置
由于是编译安装,因此,对于这个自启动的脚本我们需要自行配制。
具体则是参考上面的链接或者上面给出的nginx.service文件内容。
然后将其保存为 /lib/systemd/system/nginx.service。
看下面的例子
# more /etc/redhat-release CentOS Linux release 7.4.1708 (Core) # ps -ef|grep nginxroot 10092 10014 0 16:23 pts/0 00:00:00 grep --color=auto nginxroot 20791 1 0 Mar20 ? 00:00:00 nginx: master process ./sbin/nginx -c /u01/app/nginx/nginx.confnobody 20792 20791 0 Mar20 ? 00:00:44 nginx: worker processnobody 20793 20791 0 Mar20 ? 00:00:42 nginx: worker processnobody 20794 20791 0 Mar20 ? 00:00:50 nginx: worker processnobody 20795 20791 0 Mar20 ? 00:00:44 nginx: worker processnobody 20796 20791 0 Mar20 ? 00:00:43 nginx: worker processnobody 20797 20791 0 Mar20 ? 00:00:43 nginx: worker processnobody 20798 20791 0 Mar20 ? 00:00:37 nginx: worker processnobody 20799 20791 0 Mar20 ? 00:00:48 nginx: worker processnobody 20800 20791 0 Mar20 ? 00:00:04 nginx: cache manager process#
新闻热点
疑难解答