1. 程式人生 > >010 Linux 下通過yum、apt、dnf方式安裝和配置Nginx伺服器

010 Linux 下通過yum、apt、dnf方式安裝和配置Nginx伺服器

一、安裝

Centos系統

1、安裝Nginx源。
Centos7下:#rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
Centos6下:#rpm -Uvh http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm
Centos5下:#rpm -Uvh http://nginx.org/packages/centos/5/noarch/RPMS/nginx-release-centos-5-0.el5.ngx.noarch.rpm

2、使用yum命令安裝Nginx。
yum install nginx

Ubuntu系統

 使用以下命令直接安裝Nginx
      sudo apt-get install nginx

Fedora27

使用以下命令直接安裝Nginx
      sudo dnf   install nginx

二 、啟動並查詢配置檔案位置

systemctl start nginx.service #啟動
systemctl status nginx.service#檢視狀態、檢視啟動過程,從最下幾排能看到使用的配置檔名

● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
   Active: active (running)

since Fri 2018-01-12 14:13:49 EST; 1min 34s ago
  Process: 5906 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
  Process: 5905 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
  Process: 5904 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
 Main PID: 5907 (nginx)
    Tasks: 2 (limit: 4915)
   CGroup: /system.slice/nginx.service
           ├─5907 nginx: master process /usr/sbin/nginx
           └─5909 nginx: worker process

1月 12 14:13:49 Xin-Water systemd[1]: Starting The nginx HTTP and reverse proxy server...
1月 12 14:13:49 Xin-Water nginx[5905]: nginx: [warn] could not build optimal types_hash, you should increase either types_hash_max_size
1月 12 14:13:49 Xin-Water nginx[5905]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
1月 12 14:13:49 Xin-Water nginx[5905]: nginx: configuration file
/etc/nginx/nginx.conf test is successful
1月 12 14:13:49 Xin-Water nginx[5906]: nginx: [warn] could not build optimal types_hash, you should increase either types_hash_max_size
1月 12 14:13:49 Xin-Water systemd[1]: nginx.service: Failed to read PID from file /run/nginx.pid: Invalid argument
1月 12 14:13:49 Xin-Water systemd[1]: Started The nginx HTTP and reverse proxy server.


此時如果在圖形介面下,可輸入:127.0.0.1 檢視nginx伺服器歡迎介面,上面也會有nginx家目錄位置和配置檔案位置:


三、更改nginx訪問根目錄

1)修改配置檔案:

fedora27:vi /etc/nginx/nginx.conf

centos7: vi /etc/nginx/conf.d/default.conf  

其他版本看nginx啟動日誌。


找到:

server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        }

要改根目錄為:/var/www/html/,則配置修改如下:

方式1:更改root 後為:/var/www/html;

方式2:刪除root行,修改location /{

}

為以下內容:

  location / {

            root   /var/www/html;     #修改http://locakhost:80/對映地址,此時對應本機訪問路徑為 :/var/www/html。
            index  index.jpg index.html index.htm;

        }

2)修改根目錄訪問許可權
如果許可權設定不對,就沒法讀檔案了,一讀就是403錯誤!

目錄許可權1              目錄執行許可權:x                                   可進入目錄

目錄許可權2              目錄寫許可權:w                                     可複製、貼上、刪除、移動該目錄下檔案

目錄許可權4              目錄讀許可權:r                                       可列出目錄下所含檔案

 

檔案許可權1              檔案執行許可權:x                                   可執行檔案

檔案許可權2              檔案寫許可權:w                                     可修改檔案裡面內容

檔案許可權4              檔案讀許可權:r                                       可檢視檔案內容


所以:要檢視目錄內容,最低許可權為:5(要先能夠進入目錄,再有列出檔案許可權),要檢視檔案內容,最低許可權為4

最後,設定人們對該目錄和裡面檔案的讀寫許可權為:
        chmod  751  /var/www/html/                  #   其他使用者只能進入目錄,但看不到目錄下有哪些檔案
        chmod  -R 755  /var/www/html/*           #   -R 是遞迴遍歷子目錄,設定改目錄下的東西為所有人可檢視。

3)重啟nginx服務生效

systemctl restart nginx.service


四、設定防火牆

1)開放80埠、開放http服務,重啟防火牆

 sudo  firewall-cmd --list-all  #檢視防火牆情況
FedoraWorkstation (active)
  target: default
  icmp-block-inversion: no
  interfaces: enp0s17
  sources:
  services: dhcpv6-client  samba-client mdns
  ports: 1025-65535/udp 1025-65535/tcp
  protocols:
  masquerade: no
  forward-ports:
  source-ports:
  icmp-blocks:
  rich rules:

知道防火牆活躍區域為:FedoraWorkstation,則命令為:

sudo  firewall-cmd --zone=FedoraWorkstation --add-port=80/tcp  --permanent

sudo  firewall-cmd --zone=FedoraWorkstation --add-service=http  --permanent

sudo  firewall-cmd --reload

sudo  firewall-cmd --list-all #檢視開放服務、埠中是否有http服務和80埠。

FedoraWorkstation (active)
  target: default
  icmp-block-inversion: no
  interfaces: enp0s17
  sources:
  services: dhcpv6-client  samba-client mdns http
  ports: 1025-65535/udp 1025-65535/tcp 80/tcp
  protocols:
  masquerade: no
  forward-ports:
  source-ports:
  icmp-blocks:
  rich rules:

如果防火牆出現問題導致設定不成功,可關閉防火牆:(我在fedora上遇到前2步提示執行成功,但檢視開發埠和服務是確看不到的問題,後來安裝firewalld圖形化工具firewall-config,在圖形介面下來設定)

五、nginx使用命令

1 啟動服務:systemctl start nginx.service   # 開啟瀏覽器輸入127.0.0.1如果出現歡迎頁面,說明啟動成功。
2 檢視、顯示服務當前狀態:systemctl status nginx.service
3 設定開機自啟動:systemctl enable nginx.service
4 停止開機自啟動:systemctl disable nginx.service
5 重新啟動服務:    systemctl restart nginx.service
6 停止ftp服務:        systemctl  stop  nginx.service

 

    Centos下解除安裝Nginx:yum remove nginx
    Ubuntu下解除安裝Nginx:sudo apt-get remove nginx
    Fedora27下解除安裝Nginx:sudo dnf remove nginx