Configure Nginx to start automatically based on CentOS 7

Nginx is a popular lightweight web server software. It is open source, short and powerful, simple and easy to use, and is popular among Internet companies and IT operation and maintenance personnel. Many times, after we install Nginx in a production environment based on the compilation method, Nginx needs to manually configure the auto-start service to ensure that the service is automatically restarted after the server goes down abnormally. The following describes the configuration of the auto-start service based on CentOS 7 for your reference.

One, yum installation method Nginx self-starting##

Current environment
[ root@node142 ~]# more /etc/redhat-release 
CentOS Linux release 7.2.1511(Core) 

Check whether to protect the nginx rpm package
[ root@node142 ~]# rpm -qa|grep nginx
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

Check if there are corresponding services, as follows, there is nginx.service
[ root@node142 ~]# systemctl list-unit-files |grep nginx
nginx.service                              disabled

Configure it to automatic
[ root@node142 ~]# systemctl enable nginx.service

View nginx.service file
[ root@node142 ~]# more /lib/systemd/system/nginx.service
[ Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target

[ Service]
Type=forking
PIDFile=/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=1268621
ExecStartPre=/usr/bin/rm -f /run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true[Install]
WantedBy=multi-user.target

The content in the above configuration file is exactly the same as that provided on the official website
https://www.nginx.com/resources/wiki/start/topics/examples/systemd/

Second, the self-starting configuration after compiling and installing##

Since it is a compilation and installation, we need to prepare this self-starting script by ourselves.
Specifically, refer to the above link or the content of the nginx.service file given above.
Then save it as /lib/systemd/system/nginx.service.

See the example below

# more /etc/redhat-release 
CentOS Linux release 7.4.1708(Core) 

# ps -ef|grep nginx
root    1009210014016:23 pts/000:00:00 grep --color=auto nginx
root    2079110 Mar20 ?00:00:00 nginx: master process ./sbin/nginx -c /u01/app/nginx/nginx.conf
nobody  20792207910 Mar20 ?00:00:44 nginx: worker process
nobody  20793207910 Mar20 ?00:00:42 nginx: worker process
nobody  20794207910 Mar20 ?00:00:50 nginx: worker process
nobody  20795207910 Mar20 ?00:00:44 nginx: worker process
nobody  20796207910 Mar20 ?00:00:43 nginx: worker process
nobody  20797207910 Mar20 ?00:00:43 nginx: worker process
nobody  20798207910 Mar20 ?00:00:37 nginx: worker process
nobody  20799207910 Mar20 ?00:00:48 nginx: worker process
nobody  20800207910 Mar20 ?00:00:04 nginx: cache manager process
# 

No corresponding rpm package, query as follows, here is compiling and installing
# rpm -qa|grep nginx

Also did not add the corresponding self-starting service
# systemctl list-unit-files |grep nginx

nginx version
# /u01/app/nginx/sbin/nginx -v
nginx version: nginx/1.8.1

Obtain the nginx compiled module, and then check such as pid, binary location and record to modify the startup file
# /u01/app/nginx/sbin/nginx -V
nginx version: nginx/1.8.1
built by gcc 4.8.520150623(Red Hat 4.8.5-16)(GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments:--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

Below we generate a new nginx.service file

# vim /lib/systemd/system/nginx.service

[ Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-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 -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true[Install]
WantedBy=multi-user.target

Let's stop nginx manually first
# /u01/app/nginx/sbin/nginx -s stop

Configure auto-start
# systemctl enable nginx.service

Use systemctl tool to start nginx service
# systemctl start nginx.service

# systemctl status nginx.service
● nginx.service - The NGINX HTTP and reverse proxy server
 Loaded:loaded(/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
 Active:active(running) since Thu 2018-03-2916:37:47 CST; 6s ago
 Process:10588 ExecStart=/u01/app/nginx/sbin/nginx(code=exited, status=0/SUCCESS)
 Process:10586 ExecStartPre=/u01/app/nginx/sbin/nginx -t(code=exited, status=0/SUCCESS)
Main PID:10590(nginx)
 CGroup:/system.slice/nginx.service
   ├─10590 nginx: master process /u01/app/nginx/sbin/nginx
   ├─10591 nginx: worker process # Author : Leshami
   ├─10592 nginx: worker process # Blog : https://blog.csdn.net/leshami
   ├─10593 nginx: worker process
   ├─10594 nginx: worker process
   ├─10595 nginx: worker process
   ├─10596 nginx: worker process
   ├─10597 nginx: worker process
   ├─10598 nginx: worker process
   ├─10599 nginx: cache manager process
   └─10600 nginx: cache loader process

Mar 2916:37:47 ydq-std systemd[1]: Starting The NGINX HTTP and reverse proxy server...
Mar 2916:37:47 ydq-std nginx[10586]: nginx: the configuration file /u01/app/nginx/nginx.conf syntax is ok
Mar 2916:37:47 ydq-std nginx[10586]: nginx: configuration file /u01/app/nginx/nginx.conf test is successful
Mar 2916:37:47 ydq-std systemd[1]: Started The NGINX HTTP and reverse proxy server.

Three, more reference##

Install, compile and install Nginx under Linux 6Install Nginx in yum mode under CentOS 7Configure Nginx reverse proxy based on CentOS 7Configure Nginx load balancing based on CentOS 7

Recommended Posts

Configure Nginx to start automatically based on CentOS 7
Configure Nginx reverse proxy based on CentOS 7
Configure Nginx load balancing based on CentOS 7
Configure Nginx forward proxy based on CentOS 7
How to install Nginx on CentOS 8
Build Nginx based on Centos 7 (including virtual host)
How to install and configure Elasticsearch on CentOS 7
Configure Ocserv on CentOS 6
How to install and configure VNC on CentOS 8
How to install and configure Redis on CentOS 8
How to install and configure phpMyAdmin on CentOS 6
How to install and configure Owncloud on CentOS 8
How to install and configure Redmine on CentOS 8
How to configure FTP server with Vsftpd on CentOS 8
How to configure a fixed IP based on Ubuntu 18.04
How to install and configure NFS server on CentOS 8
How to configure FTP server with Vsftpd on CentOS 8
How to use Let's Encrypt to protect Nginx on CentOS 8
Configure swap space on CentOS7
Install Zabbix 3.4 based on CentOS 7
Install Nginx server on CentOS 7
How to install and configure Postfix mail server on CentOS8
How to install jdk1.8 on centOS7
How to install MySQL on CentOS 8
How to install R on CentOS 8
How to install FFmpeg on CentOS 8
How to install Virtualbox on CentOS 8
Configure rsyslog log client on CentOS
How to install Nginx on Ubuntu 20.04
How to install TensorFlow on CentOS 8
How to Update to gcc4.9.x on Centos7
How to install TeamViewer on CentOS 8
How to install Gradle on CentOS 8
How to install Elasticsearch on CentOS 8
How to install Jenkins on CentOS 8
How to install Go on CentOS 8
How to install GCC on CentOS 8
How to install Yarn on CentOS 8
How to install Asterisk on CentOS 7
How to install Jenkins on CentOS 8
How to install Vagrant on CentOS 8
How to install Python 3.8 on CentOS 8
How to install Tomcat 9 on CentOS 8
How to install Webmin on CentOS 8
How to install Ruby on CentOS 8
How to install Skype on CentOS 8
How to install htop on CentOS 8
How to install Python on CentOS 8
Build Elasticsearch 6.2.4 (centos) based on docker
How to install Elasticsearch on CentOS 8
How to secure Nginx on Ubuntu 14.04
Build Nginx environment on Linux (CentOS)
How to install Postgresql on CentOS 8
How to install Wordpress on Centos
How to install Nginx on Ubuntu 20.04
How to install htop on CentOS 8
Configure python3 environment on centos7 and
Build Discuz Forum based on CentOS
How to install TeamViewer on CentOS 8
How to add swap on CentOS 7
How to install MariaDB on CentOS 8