详解基于CentOS 7配置Nginx自启动
Nginx是广为流行的轻量级Web服务器软件。它开源,短小精悍,简单易用,深受广大互联网企业以及IT运维人员所喜爱。很多时候,我们在生产环境基于编译方式安装Nginx后,Nginx需要手工配置自启动服务,以确保服务器异常宕机后自动重启该服务。以下描述的是基于CentOS7下来配置自启动服务,供大家参考。
一、yum安装方式Nginx自启动
当前环境
[root@node142~]#more/etc/redhat-release CentOSLinuxrelease7.2.1511(Core)
查看是否保护nginxrpm包
[root@node142~]#rpm-qa|grepnginx nginx-mod-http-geoip-1.12.2-2.el7.x86_64 nginx-1.12.2-2.el7.x86_64 nginx-filesystem-1.12.2-2.el7.noarch nginx-mod-http-xslt-filter-1.12.2-2.el7.x86_64 nginx-mod-stream-1.12.2-2.el7.x86_64 nginx-mod-http-perl-1.12.2-2.el7.x86_64 nginx-mod-http-image-filter-1.12.2-2.el7.x86_64 nginx-all-modules-1.12.2-2.el7.noarch nginx-mod-mail-1.12.2-2.el7.x86_64
查看是否存在相应的服务,如下,有nginx.service
[root@node142~]#systemctllist-unit-files|grepnginx nginx.servicedisabled
将其配置为自动
[root@node142~]#systemctlenablenginx.service
查看nginx.service文件
[root@node142~]#more/lib/systemd/system/nginx.service [Unit] Description=ThenginxHTTPandreverseproxyserver After=network.targetremote-fs.targetnss-lookup.target [Service] Type=forking PIDFile=/run/nginx.pid #Nginxwillfailtostartif/run/nginx.pidalreadyexistsbuthasthewrong #SELinuxcontext.Thismighthappenwhenrunning`nginx-t`fromthecmdline. #https://bugzilla.redhat.com/show_bug.cgi?id=1268621 ExecStartPre=/usr/bin/rm-f/run/nginx.pid ExecStartPre=/usr/sbin/nginx-t ExecStart=/usr/sbin/nginx ExecReload=/bin/kill-sHUP$MAINPID KillSignal=SIGQUIT TimeoutStopSec=5 KillMode=process PrivateTmp=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 CentOSLinuxrelease7.4.1708(Core) #ps-ef|grepnginx root1009210014016:23pts/000:00:00grep--color=autonginx root2079110Mar20?00:00:00nginx:masterprocess./sbin/nginx-c/u01/app/nginx/nginx.conf nobody20792207910Mar20?00:00:44nginx:workerprocess nobody20793207910Mar20?00:00:42nginx:workerprocess nobody20794207910Mar20?00:00:50nginx:workerprocess nobody20795207910Mar20?00:00:44nginx:workerprocess nobody20796207910Mar20?00:00:43nginx:workerprocess nobody20797207910Mar20?00:00:43nginx:workerprocess nobody20798207910Mar20?00:00:37nginx:workerprocess nobody20799207910Mar20?00:00:48nginx:workerprocess nobody20800207910Mar20?00:00:04nginx:cachemanagerprocess #
无相应的rpm包,如下查询,此处为编译安装
#rpm-qa|grepnginx
也没有添加相应的自启动服务
#systemctllist-unit-files|grepnginx
nginx版本
#/u01/app/nginx/sbin/nginx-v nginxversion:nginx/1.8.1
获取nginx编译模块,然后查看诸如pid,二进制位置并记录以便修改启动文件
#/u01/app/nginx/sbin/nginx-V nginxversion:nginx/1.8.1 builtbygcc4.8.520150623(RedHat4.8.5-16)(GCC) builtwithOpenSSL1.0.2k-fips26Jan2017 TLSSNIsupportenabled configurearguments:--prefix=/u01/app/nginx--sbin-path=/u01/app/nginx/sbin/nginx --conf-path=/u01/app/nginx/nginx.conf--error-log-path=/u01/app/nginx/log/error.log --http-log-path=/u01/app/nginx/log/access.log--pid-path=/u01/app/nginx/nginx.pid --lock-path=/u01/app/nginx/nginx.lock--http-client-body-temp-path=/u01/app/nginx/client_temp --http-proxy-temp-path=/u01/app/nginx/proxy_temp --http-fastcgi-temp-path=/u01/app/nginx/fastcgi_temp --http-uwsgi-temp-path=/u01/app/nginx/uwsgi_temp--http-scgi-temp-path=/u01/app/nginx/scgi_temp --user=nginx--group=nginx--with-http_ssl_module--with-http_realip_module --with-http_addition_module--with-http_sub_module--with-http_dav_module --with-http_flv_module--with-http_mp4_module--with-http_gunzip_module --with-http_gzip_static_module--with-http_random_index_module--with-http_secure_link_module --with-http_stub_status_module--with-http_auth_request_module--with-mail--with-mail_ssl_module --with-file-aio--with-http_spdy_module--with-ipv6
下面我们生成一个新的nginx.service文件
#vim/lib/systemd/system/nginx.service [Unit] Description=TheNGINXHTTPandreverseproxyserver After=syslog.targetnetwork.targetremote-fs.targetnss-lookup.target [Service] Type=forking PIDFile=/u01/app/nginx/nginx.pid ExecStartPre=/u01/app/nginx/sbin/nginx-t ExecStart=/u01/app/nginx/sbin/nginx ExecReload=/bin/kill-sHUP$MAINPID ExecStop=/bin/kill-sQUIT$MAINPID PrivateTmp=true [Install] WantedBy=multi-user.target
下面我们先手工停止nginx
#/u01/app/nginx/sbin/nginx-sstop
配置自启动
#systemctlenablenginx.service
使用systemctl工具启动nginx服务
#systemctlstartnginx.service #systemctlstatusnginx.service ●nginx.service-TheNGINXHTTPandreverseproxyserver Loaded:loaded(/usr/lib/systemd/system/nginx.service;disabled;vendorpreset:disabled) Active:active(running)sinceThu2018-03-2916:37:47CST;6sago Process:10588ExecStart=/u01/app/nginx/sbin/nginx(code=exited,status=0/SUCCESS) Process:10586ExecStartPre=/u01/app/nginx/sbin/nginx-t(code=exited,status=0/SUCCESS) MainPID:10590(nginx) CGroup:/system.slice/nginx.service ├─10590nginx:masterprocess/u01/app/nginx/sbin/nginx ├─10591nginx:workerprocess#Author:Leshami ├─10592nginx:workerprocess#Blog:https://blog.csdn.net/leshami ├─10593nginx:workerprocess ├─10594nginx:workerprocess ├─10595nginx:workerprocess ├─10596nginx:workerprocess ├─10597nginx:workerprocess ├─10598nginx:workerprocess ├─10599nginx:cachemanagerprocess └─10600nginx:cacheloaderprocess Mar2916:37:47ydq-stdsystemd[1]:StartingTheNGINXHTTPandreverseproxyserver... Mar2916:37:47ydq-stdnginx[10586]:nginx:theconfigurationfile/u01/app/nginx/nginx.confsyntaxisok Mar2916:37:47ydq-stdnginx[10586]:nginx:configurationfile/u01/app/nginx/nginx.conftestissuccessful Mar2916:37:47ydq-stdsystemd[1]:StartedTheNGINXHTTPandreverseproxyserver.
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。