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

Configure boot self-boot when compiling and installing nginx

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

For more details on compiling and installing nginx, please refer to [detailed description of Nginx directory structure and configuration files] and [Nginx installation and deployment]. Here is a simple installation.

Introduction to installing Nginx environment

Operating system:

[root@localhost ~] # cat / etc/redhat-release CentOS Linux release 7.6.1810 (Core) [root@localhost ~] # uname-aLinux localhost.localdomain 3.10.0-957.el7.x86_64 # 1 SMP Thu Nov 8 23:39:32 UTC 2018 x86 "64 GNU/Linux

Nginx software version: nginx-1.17.6.tar.gz

Installation dependency

Note: when compiling and installing, you must install the development tools, otherwise you can't install or report an error.

[root@localhost opt] # yum-y install openssl openssl-devel zlib zlib-devel pcre pcre-devel make gcc gcc-c++ install nginx [root @ localhost ~] # cd / opt/ [root@localhost opt] # wget http://nginx.org/download/nginx-1.17.6.tar.gz[root@localhost opt] # tar zxf nginx-1.17.6.tar.gz [root@localhost opt] # cd nginx-1.17.6/ [root@localhost opt] # cd nginx-1.17 .6 / [root@localhost nginx-1.17.6] # [root@localhost nginx-1.17.6] # lsauto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src [root@localhost nginx-1.17.6] #. / configure-- prefix=/usr/local/nginx & & make & & make install launch Test nginx [root@localhost nginx-1.17.6] # cd / usr/local/nginx/ [root@localhost nginx] # lsconf html logs sbin [root@localhost nginx] # Cd sbin/ [root@localhost sbin] #. / nginx [root@localhost sbin] # netstat-anpl | grep nginx / / View port tcp 00 0.0.0.0 nginx 80 0.0.0.0 11881/nginx * LISTEN 11881/nginx: master unix 3 [] STREAM CONNECTED 53405 11881/nginx: master unix 3 [] STREAM CONNECTED 53404 11881/nginx: master [root@localhost sbin] # ps aux | grep nginx / / View process jia 5496 302400? Sl 10:58 0:00 / usr/libexec/ibus-engine-simpleroot 11881 0.0 0.0 20560 620? Ss 11:23 0:00 nginx: master process. / nginxnobody 11882 0.0 0.1 23080 1632? S 11:23 0:00 nginx: worker processroot 11896 0.0 0.1 112728 988 pts/0 S+ 11:24 0:00 grep-- color=auto ngin is set to the system command [root@localhost sbin] # ln nginx/ usr/local/sbin/ [root@localhost ~] # nginx-t / / check nginx syntax nginx: the configuration file / usr/local/nginx/conf/nginx.conf syntax is oknginx: configuration file / usr/local/nginx/conf/nginx .conf test is successful [root@localhost ~] # nginx-s stop / / stop nginx [root@localhost ~] # which nginx / / View the location of the launcher / usr/local/sbin/nginx method I use the rc.local script

Rc.local is a boot load file. If you want to add a program to boot in linux, you can generally modify rc.local. This file will be loaded when you boot, so we can use the linux file to set nginx boot self-startup.

[root@localhost ~] # cat / etc/rc.local / / the contents of the rc.local file when the file is stored in / etc directory: #! / bin/bash# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES## It is highly advisable to create own systemd services or udev rules# to run scripts during boot instead of using this file.## In contrast to previous versions due to parallel execution during boot# this script will NOT be run after all other services.## Please note that you must run 'chmod + x / etc/ Rc.d/rc.local' to ensure# that this script will be executed during boot.touch / var/lock/subsys/local

Using this file, you can set the commands you want to start when you boot, and write the commands you want to execute directly into rc.local.

We add the nginx startup command to this file

[root@localhost ~] # echo sh / usr/local/nginx/sbin/nginx > > / etc/rc.local [root@localhost ~] # cat / etc/rc.local #! / bin/bash# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES## It is highly advisable to create own systemd services or udev rules# to run scripts during boot instead of using this file.## In contrast to previous versions due to parallel execution during boot# this script will NOT be run after all other services.## Please note that you must run 'chmod + x / etc/rc D / rc.local' to ensure# that this script will be executed during boot.touch / var/lock/subsys/local/usr/local/nginx/sbin/nginx if you set nginx as the system command above, you can write the command directly, nginx.

Then let's restart the system and check the port and process again.

[root@localhost ~] # reboot restart found that nginx automatically started [root@localhost ~] # netstat-anpl | grep nginxtcp 00 0.0.0.0 nginx 80 0.0.0.0 4847/nginx * LISTEN 4847/nginx: master unix 3 [] STREAM CONNECTED 39265 4847/nginx: master unix 3 [] STREAM CONNECTED 39264 4847/nginx: master [root@localhost ~] # ps aux | grep nginxroot 4847 20560? Ss 11:45 0:00 nginx: master process / usr/local/nginx/sbin/nginxnobody 4848 0.0 0.1 23080 1388? S 11:45 0:00 nginx: worker processroot 4860 0.00.1 112724 988 pts/0 S+ 11:45 0:00 grep-- color=auto nginx [root@localhost ~] # method 2 set system Service recommendation Settings Boot configuration Boot generates pid File

The pid file is the process file in which the process ID that the program is running is stored, that is, the process number

Nginx needs to modify the configuration file to generate the pid file

The modifications are as follows:

The default configuration file has this entry. If not, please find this entry in nginx.conf and delete the previous comments. Pid logs/nginx.pid; adds the nginx.service file under the / usr/lib/systemd/system path.

/ usr/lib/systemd/system this directory is used to store some system services

Contents of nginx file:

[root@localhost system] # cat nginx.service [Unit] Description=nginx / / description After=syslog.target network.target remote-fs.target nss-lookup.target\\ describe service category [Service] Type=forking / / set operation mode Run PIDFile=/usr/local/nginx/logs/nginx.pid / / set PID file ExecStart=/usr/local/nginx/sbin/nginx / / start command ExecReload=/bin/kill-s HUP $MAINPID / / restart command ExecStop=/bin/kill-s QUIT $MAINPID / / close command PrivateTmp=true / / allocate independent temporary space * Note that the command needs to write the absolute path [Install] / related settings for service installation Can be set to multi-user WantedBy=multi-user.target

Note: this file requires 754 permissions

Test startup / shutdown [root@localhost ~] # systemctl start nginx / / start service [root@localhost ~] # netstat-anpl | grep nginxtcp 00 0.0.0.0 root@localhost 80 0.0.0.0 anpl * LISTEN 5249/nginx: master unix 3 [] STREAM CONNECTED 42458 5249/nginx: master unix 3 [] STREAM CONNECTED 42459 5249/nginx: master [root@localhost ~] # systemctl stop nginx / / shut down the service [root@localhost ~] # netstat-anpl | grep nginx [root@localhost ~] # systemctl restart nginx / / restart the service [root@localhost ~] # netstat-anpl | grep nginxtcp 00 0.0.0.0master 80 0.0.0.0systemctl stop nginx * LISTEN 5289/nginx: master unix 3 [] STREAM CONNECTED 45346 5289/nginx: master unix 3 [] STREAM CONNECTED 45347 5289/nginx: master

It should be noted that if you use the script to start the service, you cannot use this method to shut down the service.

Set [root@localhost] # systemctl enable nginxCreated symlink from / etc/systemd/system/multi-user.target.wants/nginx.service to / usr/lib/systemd/system/nginx.service. [root@localhost ~] # systemctl enable nginx

Restart to see the effect

[root@localhost ~] # netstat-anpl | grep nginxtcp 00 0.0.0.0 grep nginxtcp 80 0.0.0.0 LISTEN 4081/nginx: master unix 3 [] STREAM CONNECTED 32429 4081/nginx: master unix 3 [] STREAM CONNECTED 32428 4081/nginx: master init.d setting boot

Init boot mode is no longer recommended in the centos7 system version.

Create the startup file nginx in the / etc/init.d directory

The contents of the document are as follows:

#! / bin/bash# chkconfig: 345 80 20 / / Startup sequence # description: start the nginx deamon / / description # Source function library. / etc/rc.d/init.d/functionsprog=nginx# rewrites CATALANA_HOMECATALANA_HOME=/usr/local/nginxexport CATALINA_HOMEcase "$1" instart) echo "Starting nginx..." $CATALANA_HOME/sbin/nginx;; stop) echo "Stopping nginx..." $CATALANA_HOME/sbin/nginx-s stop according to its own path Restart) echo "Stopping nginx..." $CATALANA_HOME/sbin/nginx-s stop sleep 2 echo echo "Starting nginx..." $CATALANA_HOME/sbin/nginx;; *) echo "Usage: $prog {start | stop | restart}" Esacexit 0 set permissions [root@localhost ~] # chmod + x / etc/init.d/nginx / / set execution permissions Test start [root@localhost init.d] # service nginx start / / start nginxStarting nginx (via systemctl): [OK] [root@localhost init.d] # netstat-anpl | grep nginxtcp 00 0.0.0.0 etc/init.d/nginx 80 0.0.0.0 STREAM CONNECTED * LISTEN 4081/nginx: master unix 3 [] STREAM CONNECTED 38534 4081/nginx: master unix 3 [] STREAM CONNECTED 38535 4081/nginx: master [root@localhost init.d] # service nginx stop / / close nginxStopping nginx (via systemctl): [OK] [root@localhost init.d] # netstat-anpl | grep nginx [root@localhost init.d] # service nginx restart / / restart nginxRestarting nginx (via systemctl): [OK] [root@localhost init.d] # netstat-anpl | grep nginxtcp 0 0 0. 0 0. 0. LISTEN 5304/nginx: master unix 3 [] STREAM CONNECTED 43218 5304/nginx: master unix 3 [] STREA

Services in init.d in centos7 will also be in the system directory by default

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