前端部署nginx服務器


linux版本:CentOS7 64位

1. 命令下載式安裝

  (1)安裝前先查看nginx有無安裝:yum list | grep nginx,或者yum list installed | grep "nginx",如果已經安裝了,則會出現@的標識

    未安裝情況下:

    

    已安裝情況下:

    

  (2)安裝nginx:yum install nginx(手動選擇),或者yum -y install nginx(自動選擇,跳過Is this OK[y/d/N),然后nginx -v,查看版本號

  (3)相關文件夾查看:rpm -ql nginx,查看 Nginx 被安裝到了什么地方,有哪些相關目錄,其中位於 /etc 目錄下的主要是配置文件,還有一些文件見下圖:

    

  (4)主要關注的文件夾有兩個:

    /etc/nginx/conf.d/ 文件夾,是我們進行子配置的配置項存放處,/etc/nginx/nginx.conf 主配置文件會默認把這個文件夾中所有子配置項都引入;      /usr/share/nginx/html/ 文件夾,通常靜態文件都放在這個文件夾,也可以根據你自己的習慣放其他地方;

  (5)安裝之后開啟 Nginx,如果系統開啟了防火牆,那么需要設置一下在防火牆中加入需要開放的端口,下面列舉幾個常用的防火牆操作(沒開啟的話不用管這個):

    systemctl start firewalld # 開啟防火牆
    systemctl stop firewalld # 關閉防火牆
    systemctl status firewalld # 查看防火牆開啟狀態,顯示running則是正在運行
    firewall-cmd --reload # 重啟防火牆,永久打開端口需要reload一下

    # 添加開啟端口,--permanent表示永久打開,不加是臨時打開重啟之后失效
    firewall-cmd --permanent --zone=public --add-port=8888/tcp

    # 查看防火牆,添加的端口也可以看到
    firewall-cmd --list-all

  (6)設置 Nginx 的開機啟動:systemctl enable nginx

  (7)啟動 Nginx :systemctl start nginx,然后訪問你的 IP,這時候就可以看到 Nginx 的歡迎頁面了~ Welcome to nginx!

  (8)Nginx操作常用命令:

    nginx -s reload # 向主進程發送信號,重新加載配置文件,熱重啟
    nginx -s reopen # 重啟 Nginx
    nginx -s stop # 快速關閉
    nginx -s quit # 等待工作進程處理完成后關閉
    nginx -T # 查看當前 Nginx 最終的配置
    nginx -t -c <配置路徑> # 檢查配置是否有問題,如果已經在配置目錄,則不需要-c

   (9)systemctl 是 Linux 系統應用管理工具 systemd 的主命令,用於管理系統,我們也可以用它來對 Nginx 進行管理,相關命令如下:

    systemctl start nginx # 啟動 Nginx
    systemctl stop nginx # 停止 Nginx
    systemctl restart nginx # 重啟 Nginx
    systemctl reload nginx # 重新加載 Nginx,用於修改配置后
    systemctl enable nginx # 設置開機啟動 Nginx
    systemctl disable nginx # 關閉開機啟動 Nginx
    systemctl status nginx # 查看 Nginx 運行狀態

  (10)卸載Nginx:

    停止Nginx:service nginx stop
    刪除Nginx的自動啟動:chkconfig nginx off
    刪除Nginx本地文件:
      rm -rf /usr/sbin/nginx
      rm -rf /etc/nginx
      rm -rf /etc/init.d/nginx
    yum清理:yum remove ngin

2. 源碼下載安裝

  (1)在安裝nginx前首先要確認系統中安裝了gcc、pcre-devel、zlib-devel、openssl-devel,

      安裝命令:

      yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel

  (2)nginx下載地址:https://nginx.org/download/,下載“nginx-1.9.9.tar.gz”,移動到/usr/local/下  

    ## 解壓
    tar -zxvf nginx-1.9.9.tar.gz
    ##進入nginx目錄     cd nginx-1.9.9
    ## 配置
    ./configure --prefix=nginx安裝路徑 --with-http_ssl_module
    例如:./configure --prefix=/usr/local/nginx

    ## 安裝
    make
    make install
    或者:make && make install
 
 (3)測試是否安裝成功
    # cd到剛才配置的安裝目錄/usr/loca/nginx/
    ./sbin/nginx -t

    錯誤信息

    nginx: [alert] could not open error log file: open() "/usr/local/nginx/logs/error.log" failed (2: No such file or directory)
    2016/09/13 19:08:56 [emerg] 6996#0: open() "/usr/local/nginx/logs/access.log" failed (2: No such file or directory)

    原因分析:nginx/目錄下沒有logs文件夾

    解決方法

    mkdir logs
    chmod 700 logs

   正常情況的信息輸出:

    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 

    cd /usr/local/nginx/sbin
    ./nginx //啟動nginx

 

  (4)nginx開機自啟動參考鏈接:https://blog.csdn.net/J080624/article/details/79240685?utm_source=blogxgwz5,          https://blog.csdn.net/qq_40406061/article/details/83040462
  (5)如果nginx安裝目錄與配置目錄不在一個地方,則啟動時用下面的命令:

    啟動代碼格式:nginx安裝目錄地址 -c nginx配置文件地址
    例如:/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

  (6)驗證nginx配置文件是否正確

    方法一:進入nginx安裝目錄 /usr/local/nginx/sbin/ 下,輸入命令./nginx -t看到如下顯示

  nginx.conf syntax is ok
  nginx.conf test is successful

    方法二:在啟動命令-c前加-t,例如:/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
  (7)重啟Nginx服務
      進入nginx可執行目錄 /usr/local/nginx/sbin/ 下,輸入命令 ./nginx -s reload即可
  (8)強制停止 pkill -9 nginx
    

 

參考鏈接:

  命令式安裝參考鏈接:https://juejin.im/post/5ea931866fb9a043815146fb

  Windows服務器nginx安裝與配置參考鏈接:https://www.jianshu.com/p/129378a49245

  源碼式安裝參考鏈接:https://www.jianshu.com/p/e9f120ea3a5f


免責聲明!

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



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