Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Detailed explanation of Nginx self-startup based on CentOS 7 configuration

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Nginx is a popular lightweight Web server software. It is open source, short, easy to use, and is loved by the majority of Internet enterprises and IT operation and maintenance personnel. In many cases, after we install Nginx in a production environment based on compilation, Nginx needs to manually configure the self-starting service to ensure that the service is automatically restarted after an abnormal server downtime. The following description is based on CentOS 7 to configure the self-startup service for your reference.

First, yum installation mode Nginx self-startup

Current environment

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

Check to see if nginx rpm packages are protected

[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.x86room64nginx- 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

Check to see if the corresponding service exists, as follows, there is nginx.service

[root@node142 ~] # systemctl list-unit-files | grep nginxnginx.service disabled

Configure it to be automatic

[root@node142 ~] # systemctl enable nginx.service

View the nginx.service file

[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

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-startup configuration after compilation and installation

Since it is a compiled installation, we need to configure this self-starting script.

Specifically, refer to the link above or the content of the nginx.service file given above.

Then save it as / lib/systemd/system/nginx.service.

Look at the following example

# 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 10 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 207910 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#

There is no corresponding rpm package. The following query is used for compilation and installation.

# rpm-qa | grep nginx

No corresponding self-starting service has been added.

# systemctl list-unit-files | grep nginx

Nginx version

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

Get the nginx compilation module, and then look at the binary location such as pid and record it to modify the startup file

# / u01/app/nginx/sbin/nginx-Vnginx version: nginx/1.8.1built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) built with OpenSSL 1.0.2k-fips 26 Jan 2017TLS SNI support enabledconfigure 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

Let's generate a new nginx.service file

# vim / lib/systemd/system/ nginx.service [Unit] Description=The NGINX HTTP and reverse proxy serverAfter=syslog.target network.target remote-fs.target nss- lookup.target [Service] Type=forkingPIDFile=/u01/app/nginx/nginx.pidExecStartPre=/u01/app/nginx/sbin/nginx-tExecStart=/u01/app/nginx/sbin/nginx ExecReload=/bin/kill-s HUP $MAINPIDExecStop=/bin/kill-s QUIT $MAINPIDPrivateTmp= true [install] WantedBy=multi-user.target

Let's stop nginx manually.

# / u01/app/nginx/sbin/nginx-s stop

Configure self-startup

# systemctl enable nginx.service

Start the nginx service using the systemctl tool

# 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-29 16: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 processMar 29 16:37:47 ydq-std systemd [1]: Starting The NGINX HTTP and reverse proxy server...Mar 29 16:37:47 ydq-std nginx [10586]: nginx: the configuration file / u01/app/nginx/nginx.conf syntax is okMar 29 16:37:47 ydq-std nginx [10586]: nginx: configuration file / u01/app/nginx/nginx.conf test is successfulMar 29 16:37:47 ydq-std systemd [1]: Started The NGINX HTTP and reverse proxy server.

The above is the whole content of this article, I hope it will be helpful to your study, and I also hope that you will support it.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report