中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

編譯安裝nginx時配置開機自啟

發布時間:2020-07-16 16:31:50 來源:網絡 閱讀:270 作者:wx5c4682be18b52 欄目:系統運維

詳細編譯安裝nginx請參考【Nginx目錄結構與配置文件詳解】以及【Nginx安裝部署】,在這里就進行簡單安裝

安裝Nginx

環境介紹

操作系統:

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

nginx軟件版本: nginx-1.17.6.tar.gz

安裝依賴

注意:編譯安裝一定要安裝開發工具,否則無法進行安裝或安裝報錯

[root@localhost opt]# yum -y install openssl openssl-devel zlib zlib-devel pcre pcre-devel make gcc gcc-c++

安裝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]# ls
auto  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

啟動測試nginx

[root@localhost nginx-1.17.6]# cd /usr/local/nginx/
[root@localhost nginx]# ls
conf  html  logs  sbin
[root@localhost nginx]# cd sbin/
[root@localhost sbin]# ./nginx 
[root@localhost sbin]# netstat -anpl | grep nginx       //查看端口
tcp        0      0 0.0.0.0:80              0.0.0.0:*               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      //查看進程
jia       5496  0.0  0.0 302400   852 ?        Sl   10:58   0:00 /usr/libexec/ibus-engine-simple
root     11881  0.0  0.0  20560   620 ?        Ss   11:23   0:00 nginx: master process ./nginx
nobody   11882  0.0  0.1  23080  1632 ?        S    11:23   0:00 nginx: worker process
root     11896  0.0  0.1 112728   988 pts/0    S+   11:24   0:00 grep --color=auto ngin

設置為系統命令

[root@localhost sbin]# ln nginx /usr/local/sbin/
[root@localhost ~]# nginx -t        //檢查nginx語法
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost ~]# nginx -s stop       //停止nginx
[root@localhost ~]# which nginx     //查看啟動程序位置
/usr/local/sbin/nginx

方法一利用rc.local腳本

rc.local是啟動加載文件,在linux中要把一個程序加入開機啟動,一般可以通過修改rc.local來完成,這個文件時開機就要加載的文件,所以我們就可以利用linux這個文件設置nginx開機自啟動

[root@localhost ~]# cat /etc/rc.local       //文件存放在/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

利用這個文件可以設置自己想在開機時啟動的命令,直接把自己想執行的命令寫到rc.local中就可以了
我們把nginx啟動命令加入此文件中

[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
如果你上面把nginx設置為系統命令那你就可以直接寫命令就好了
nginx

然后讓我們重啟系統再次查看端口和進程

[root@localhost ~]# reboot
重啟后發現nginx自動啟動了
[root@localhost ~]# netstat -anpl | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               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 nginx
root      4847  0.0  0.0  20560   612 ?        Ss   11:45   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody    4848  0.0  0.1  23080  1388 ?        S    11:45   0:00 nginx: worker process
root      4860  0.0  0.1 112724   988 pts/0    S+   11:45   0:00 grep --color=auto nginx
[root@localhost ~]# 

方法二設置系統服務

推薦設置開機自啟

配置啟動生成pid文件

pid文件是進程文件里面存放的是程序運行的進程ID也就是進程號
nginx生成pid文件需要修改配置文件
修改內容如下:

默認配置文件有這一條,如果沒有請在nginx.conf中找到這一條然后將前面注釋刪除就可以了
pid        logs/nginx.pid;

在/usr/lib/systemd/system路徑下添加nginx.service文件

/usr/lib/systemd/system 此目錄是用來存放一些系統服務的
nginx文件內容:

[root@localhost system]# cat nginx.service 
[Unit]
Description=nginx       //描述
After=syslog.target network.target remote-fs.target nss-lookup.target       \\描述服務類別

[Service]
Type=forking        //設置運行方式,后臺運行
PIDFile=/usr/local/nginx/logs/nginx.pid     //設置PID文件
ExecStart=/usr/local/nginx/sbin/nginx       //啟動命令
ExecReload=/bin/kill -s HUP $MAINPID        //重啟命令
ExecStop=/bin/kill -s QUIT $MAINPID         //關閉命令
PrivateTmp=true     //分配獨立的臨時空間
*注意命令需要寫絕對路徑
[Install]       ///服務安裝的相關設置,可設置為多用戶
WantedBy=multi-user.target      

注意:此文件需要754的權限

測試啟動關閉

[root@localhost ~]# systemctl start nginx       //啟動服務
[root@localhost ~]# netstat -anpl | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               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            //關閉服務
[root@localhost ~]# netstat -anpl | grep nginx
[root@localhost ~]# systemctl restart nginx         //重新啟動服務
[root@localhost ~]# netstat -anpl | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      5289/nginx: master  
unix  3      [ ]         STREAM     CONNECTED     45346    5289/nginx: master   
unix  3      [ ]         STREAM     CONNECTED     45347    5289/nginx: master   

需要注意的是使用之前執行腳本來啟動服務的,無法使用此方法關閉服務

設置開機自啟動

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

重啟看效果

[root@localhost ~]# netstat -anpl | grep nginx
tcp        0      0 0.0.0.0: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設置開機啟動

init啟動方式在centos7系統版本已經不推薦使用了

在/etc/init.d目錄中創建啟動文件nginx

文件內容如下:

#!/bin/bash
# chkconfig: 345 80 20  //啟動順序
# description: start the nginx deamon       //說明
# Source function library
. /etc/rc.d/init.d/functions

prog=nginx
# 根據自己的路徑改寫CATALANA_HOME
CATALANA_HOME=/usr/local/nginx
export CATALINA_HOME

case "$1" in
start)
    echo "Starting nginx..."
    $CATALANA_HOME/sbin/nginx
    ;;

stop)
    echo "Stopping nginx..."
    $CATALANA_HOME/sbin/nginx -s stop
    ;;

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}"
    ;;
esac
exit 0

設置權限

[root@localhost ~]# chmod +x /etc/init.d/nginx      //設置執行權限

測試啟動

[root@localhost init.d]# service nginx start            //啟動nginx
Starting nginx (via systemctl):                            [  確定  ]
[root@localhost init.d]# netstat -anpl | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               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         //關閉nginx
Stopping nginx (via systemctl):                            [  確定  ]
[root@localhost init.d]# netstat -anpl | grep nginx
[root@localhost init.d]# service nginx restart          //重新啟動nginx
Restarting nginx (via systemctl):                          [  確定  ]
[root@localhost init.d]# netstat -anpl | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      5304/nginx: master  
unix  3      [ ]         STREAM     CONNECTED     43218    5304/nginx: master   
unix  3      [ ]         STREA  

在centos7中init.d中的服務默認也會在system目錄中

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

嘉黎县| 江达县| 双柏县| 洮南市| 高陵县| 基隆市| 大厂| 永春县| 辽阳市| 桐梓县| 田东县| 石景山区| 西林县| 大兴区| 会昌县| 尼勒克县| 盐城市| 社旗县| 织金县| 江华| 岗巴县| 乌兰浩特市| 务川| 宕昌县| 北京市| 高雄市| 共和县| 金川县| 博乐市| 平远县| 宜春市| 军事| 绥宁县| 临泉县| 宁城县| 福海县| 新化县| 德兴市| 和静县| 昌图县| 屯昌县|