linux 部署Nginx服務(編譯安裝方式,且步驟完整)


本次編譯安裝最大亮點:  文件路徑 參照 "官方Nginx源yum安裝"后各文件的配置路徑
 
1 # 安裝依賴包
[root@lb01 ~]# yum install openssl openssl-devel zlib zlib-devel -y
 
2 # 從官網下載Nginx源代碼包
 
3 # 解壓
[root@lb01 ~]# tar -xf nginx-1.20.2.tar.gz
 
4 # 進入源代碼目錄
[root@lb01 ~]# cd nginx-1.20.2
 
5 # 設置編譯參數
[root@lb01 nginx-1.20.2]# ./configure  --with-http_gzip_static_module    --with-stream     --with-http_ssl_module
 
6 # 編譯
[root@lb01 nginx-1.20.2]# make
 
7 # 安裝
[root@lb01 nginx-1.20.2]# make install
 
---> 8~11 中關於各個文件存放位置是參照"用官方nginx源yum安裝nginx后"各個文件的位置
 
8 - 關於配置文件的操作
 
8.1 把nginx的配置文件移動到/etc/nginx
 
8.11 創建目錄
[root@lb01 ~]# mkdir /etc/nginx
8.12 移動
[root@lb01 ~]# mv /usr/local/nginx/conf/* /etc/nginx/
8.13 修改配置文件
vi /etc/nginx/nginx.conf
注意是vi,不是vim;因為vim方式下粘貼復制內容會把注釋帶上
修改內容
- 復制其他服務器/虛擬機上'用官方源安裝nginx軟件'的"/etc/nginx/nginx.conf "內容
user  www;
worker_processes  10;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

 

8.2 創建軟連接(原因見步驟14)
[root@lb01 ~]# ln -s /etc/nginx/nginx.conf /usr/local/nginx/conf/nginx.conf
 
8.3 創建外部配置文件的目錄
此目錄為/etc/nginx/nginx.conf加載外部配置文件的目錄
[root@lb01 ~]# mkdir /etc/nginx/conf.d
 
8.4 創建配置文件(為測試本次安裝是否成功)
[root@lb02 ~]# cd /etc/nginx/conf.d
[root@lb02 conf.d]# touch test_install.conf
[root@lb02 conf.d]# vim test_install.conf
server {
        listen 80;
        server_name _;
        location / {
                root /usr/share/nginx/html/;
                index index.html;
        }
}

 

 
[root@lb02 conf.d]# echo "bian yi an zhuang ok" > /usr/share/nginx/html/index.html
 
9  關於日志的操作
創建日志目錄
[root@lb01 ~]# mkdir /var/log/nginx
 
10 創建nginx啟動用戶(在8.13的配置文件里用到)
用戶來源:  8.13中"/etc/nginx/nginx.conf "中user 對應的用戶
[root@lb01 ~]# groupadd www -g 666
[root@lb01 ~]# useradd www -u 666 -g 666 -M -r -s /sbin/nologin
 
11 關於可執行文件操作
移動nginx可執行文件到新位置(因為可執行文件一般放在/usr/sbin/路徑下)
[root@lb01 ~]# mv /usr/local/nginx/sbin/nginx /usr/sbin/
 
12 新增nginx.service
[root@lb01 ~]# vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
 
 
[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/sh -c "/bin/kill -s HUP $(/bin/cat /var/run/nginx.pid)"
ExecStop=/bin/sh -c "/bin/kill -s TERM $(/bin/cat /var/run/nginx.pid)"
 
 
[Install]
WantedBy=multi-user.target

 

 
13 重載服務的配置文件
systemctl daemon-reload
 
 
14 檢查配置文件
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
 
補充
解決nginx -t 報錯中找不見配置文件,所以才有步驟8.2
 
15 啟動Nginx
[root@lb01 ~]# systemctl start nginx
 
16 查看Nginx版本號和配置項 及安裝的所有模塊
[root@lb01 ~]# nginx -V
 
17 瀏覽器訪問測試
輸入本機ip測試
頁面出現"bian yi an zhuang ok"表示安裝成功
 
 
 
 


免責聲明!

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



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