centos7下安裝、配置Nginx、設置Nginx開機自啟動


測試環境:

[root@centos-linux ~]# cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)

[root@centos-linux ~]# php --version

PHP 7.2.32 (cli)

 

首先安裝必要的庫(nginx 中gzip模塊需要 zlib 庫,rewrite模塊需要 pcre 庫,ssl 功能需要openssl庫)

1、安裝相關的依賴包。

yum install -y gcc-c++

yum install -y pcre pcre-devel

yum install -y zlib zlib-devel

yum install -y openssl openssl-devel

 

2、下載Nginx

wget http://nginx.org/download/nginx-1.18.0.tar.gz

 

3、解壓安裝包

tar zxvf nginx-1.18.0.tar.gz 

 

4、進入安裝包目錄

cd nginx-1.18.0/

 

5、編譯安裝

./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

make

make install

 

6、創建 Nginx 運行使用的用戶 www:

/usr/sbin/groupadd www

/usr/sbin/useradd -g www www

 

7、nginx.conf最小配置(/usr/local/nginx/conf/nginx.conf),紅色為新增內容

user www www; worker_processes 1; error_log /usr/local/nginx/logs/nginx_error.log crit; #日志位置和日志級別 pid /usr/local/nginx/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name localhost; index index.html index.htm index.php; root /usr/local/nginx/html;#站點目錄 location / { root html; index index.html index.htm; } error_page 500 502 503 504  /50x.html; location = /50x.html { root html; } } }

 

8、檢查配置文件正確性的命令

# /usr/local/nginx/sbin/nginx -t
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

  

9、啟動Nginx

/usr/local/nginx/sbin/nginx

  

10、站點訪問

  

 11、Nginx常用命令

/usr/local/webserver/nginx/sbin/nginx -s reload # 重新載入配置文件

/usr/local/webserver/nginx/sbin/nginx -s reopen # 重啟Nginx

/usr/local/webserver/nginx/sbin/nginx -s stop # 停止 Nginx

 

12、Nginx調用PHP

先啟動php-fpm

systemctl start php-fpm  // 需要加入systemctl服務后才可以

  在配置文件中增加如下內容

location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
         # include fastcgi.conf;  #或用此句替代以上兩句。不通版本的nginx不一定都包含該文件,如果沒有該配置文件,則使用上面兩句話。
}

  

13、使用yum安裝Nignx

// yum -y install epel-release  // 如果需要添加CentOS EPEL倉庫
yum -y install nginx

啟動Nginx

systemctl start nginx

14、卸載Nginx

查找nginx相關文件

find / -name nginx*

從源頭刪除

rm -rf /usr/sbin/nginx
rm -rf /etc/nginx
rm -rf /etc/init.d/nginx

在使用yum清理

yum remove nginx

 

15、設置Nginx開機自啟動

在/lib/systemd/system目錄下,創建nginx.service文件

編輯nginx.service文件,增加如下內容

[Unit]
Description=nginx service
After=network.target 
   
[Service] 
Type=forking 
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true 
   
[Install] 
WantedBy=multi-user.target
[Unit]:服務的說明
Description:描述服務
After:描述服務類別
[Service]服務運行參數的設置
Type=forking是后台運行的形式
ExecStart為服務的具體運行命令
ExecReload為重啟命令
ExecStop為停止命令
PrivateTmp=True表示給服務分配獨立的臨時空間
注意:[Service]的啟動、重啟、停止命令全部要求使用絕對路徑
[Install]運行級別下服務安裝的相關設置,可設置為多用戶,即系統運行級別為3
 
服務的啟動/停止/刷新配置文件/查看狀態 
# systemctl start nginx.service          // 啟動nginx服務
# systemctl stop nginx.service           // 停止服務
# systemctl restart nginx.service        // 重新啟動服務
# systemctl list-units --type=service     // 查看所有已啟動的服務
# systemctl status nginx.service          // 查看服務當前狀態
# systemctl enable nginx.service          // 設置開機自啟動
# systemctl disable nginx.service         //停止開機自啟動

 

可能遇到的錯誤提示:

Warning: nginx.service changed on disk. Run 'systemctl daemon-reload' to reload units.
直接按照提示執行命令systemctl daemon-reload 即可。
 




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM