1. 程式人生 > >[樂意黎轉載]Centos 7 nginx 服務安裝及配置

[樂意黎轉載]Centos 7 nginx 服務安裝及配置

前言

nginx 跟 Apache 的作用差不多,都是提供 WEB 服務,但 nginx 相對於 Apache 來說,在效能上有更好的體現,而 Apache 有穩定性方面會比 nginx 好一些,所以要用哪一個,自己喜歡就好。下面就給大家分享下 nginx 的安裝及配置方法。如果你覺得下面記錄的東西有點亂,你可以直接看本文後面的總結也是可以的,更省時,更省心。我在這裡

小插曲

如果你直接執行命令

  1. # yum install nginx

你會得到這樣的提示

Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.yun-idc.com
* extras: mirrors.tuna.tsinghua.edu.cn
* rpmforge: mirrors.neusoft.edu.cn
* updates: mirrors.tuna.tsinghua.edu.cn
No package nginx available.
Error: Nothing to do

No package nginx available.(找不到 nginx 安裝包),所以我們得配置下,讓系統自動去幫你去下載,執行如下程式碼

  1. # vim /etc/yum.repos.d/nginx.repo

如果沒有這個檔案(一般沒有),系統會自動為你新建一個,按下鍵盤i進入編輯模式,複製下面的內容貼上到檔案裡,按Esc鍵退出編輯模式,按 :wq 儲存退出。

  1. [nginx]
  2. name=nginx repo
  3. baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
  4. gpgcheck=0
  5. enabled=1

還果你的系統是centos 7.2以上的,那麼你就直接執行如下命令列就可以了

  1. # yum install nginx

nginx 安裝配置

現在直接輸入下面的命令列進行安裝,你就會如入無人之境,一路暢通無阻。

  1. # yum install nginx

安裝記錄

下面就是 nginx 的安裝過程的全記錄

  1. [[email protected] ~]# yum install nginx
  2. Loaded plugins: fastestmirror
  3. nginx |2.9 kB 00:00
  4. nginx/7/x86_64/primary_db |14 kB 00:01
  5. Loading mirror speeds from cached hostfile
  6. *base
    : mirrors.yun-idc.com
  7. * extras: mirrors.tuna.tsinghua.edu.cn
  8. * rpmforge: mirrors.neusoft.edu.cn
  9. * updates: mirrors.tuna.tsinghua.edu.cn
  10. ResolvingDependencies
  11. -->Running transaction check
  12. --->Package nginx.x86_64 1:1.10.1-1.el7.ngx will be installed
  13. -->FinishedDependencyResolution
  14. DependenciesResolved
  15. ================================================================================
  16. PackageArchVersionRepositorySize
  17. ================================================================================
  18. Installing:
  19. nginx x86_64 1:1.10.1-1.el7.ngx nginx 640 k
  20. TransactionSummary
  21. ================================================================================
  22. Install1Package
  23. Total download size:640 k
  24. Installed size:2.1 M
  25. Isthis ok [y/d/N]: y
  26. Downloading packages:
  27. nginx-1.10.1-1.el7.ngx.x86_64.rpm |640 kB 01:03
  28. Running transaction check
  29. Running transaction test
  30. Transaction test succeeded
  31. Running transaction
  32. Installing:1:nginx-1.10.1-1.el7.ngx.x86_64 1/1
  33. ----------------------------------------------------------------------
  34. Thanksforusing nginx!
  35. Please find the official documentation for nginx here:
  36. * http://nginx.org/en/docs/
  37. Commercial subscriptions for nginx are available on:
  38. * http://nginx.com/products/
  39. ----------------------------------------------------------------------
  40. Verifying:1:nginx-1.10.1-1.el7.ngx.x86_64 1/1
  41. Installed:
  42. nginx.x86_64 1:1.10.1-1.el7.ngx
  43. Complete!

至此 nginx 已經安裝完成,如果你覺得上面的程式碼太多太亂,也不要緊我為你精煉出來

安裝詳情

  1. # yum install nginx

不多久你要你輸入 y 確認完成安裝

  1. Isthis ok [y/d/N]: y

輸入完 y 後坐等 “Complete!”就行了。

瀏覽器輸入主機IP測試,但顯示無法訪問此網頁,為什麼?因為你忘記了啟動 nginx 服務了,就下面的一行命令

  1. # systemctl start nginx.service

啟動服務之後,你重新整理下頁面就會出現如下賀電:

Welcome to nginx!

If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.

配置命令

隨伺服器啟動

  1. # systemctl enable nginx.service

重啟 nginx 服務

  1. # systemctl restart nginx.service

停止 nginx 服務

  1. # systemctl stop nginx.service

檢視 nginx 執行狀態

  1. # systemctl status nginx.service

到這裡,你就可以使用 nginx 服務了。你可以通過如下命令在站點根目錄下新建一個index.html檔案(站點下預設會有一個,覆蓋就好)

  1. # echo "我是站點預設根目錄下的測試頁面" >> index.html

瀏覽器輸入 IP 訪問時,頁面出現亂碼了,這是什麼原因呢?是我們沒有給頁面設定編碼格式,因為一般頁面都會有形如

  1. <!DOCTYPE html>
  2. <htmllang="en">
  3. <head>
  4. <metacharset="UTF-8">
  5. <title>nginx 配置</title>
  6. </head>
  7. <body>
  8. 我是站點預設根目錄下的測試頁面
  9. </body>
  10. </html>

其中 <meta charset=”UTF-8″> 就是宣告頁面編碼格式。我們先找到剛才編輯的index.html

  1. # vi /usr/share/nginx/html/index.html

i進入編輯模式,先清空剛才的文字,然後把上面一段 HTML 程式碼複製到 index.html 就可以了。重新整理瀏覽器頁面時就不會出現亂碼了。

nginx 預設的根目錄為/usr/share/nginx/html,如果你不喜歡你可以更改成自己設定的目錄,例如:/home/www/nginx.yunkus.com

1.建立站點目錄

  1. # mkdir -p /home/www/nginx.yunkus.com

目錄建立好後,我們可以在站點目錄裡新建一個index.html 作為測試。

  1. # echo "我是 nginx 自定義站點根目錄" >> /home/www/nginx.yunkus.com/index.html