如何在 Ubuntu 16.04 上安裝和配置 Nginx


在本文中,我們將學習如何安裝和配置 Nginx Web 伺服器。Nginx 是最流行的 Web 伺服器,一些最受歡迎和流量最大的網站都託管在上面,它比 Apache 更友好、更安全,也可用作 Web 伺服器。

先決條件

在機器上安裝 Ubuntu 16.04,並擁有一個具有 sudo 許可權的非 root 使用者。

在機器上安裝 Nginx

我們可以安裝 Nginx,它在 Ubuntu 的預設儲存庫中可用,因此可以直接使用 apt 命令安裝 Nginx。

在安裝 Nginx 之前,我們需要使用以下命令更新機器

$ sudo apt-get update
Get:1 http://security.ubuntu.com/ubuntu xenial-security InRelease [94.5 kB]
Hit:2 http://in.archive.ubuntu.com/ubuntu xenial InRelease
Get:3 http://in.archive.ubuntu.com/ubuntu xenial-updates InRelease [95.7 kB]
Hit:4 http://in.archive.ubuntu.com/ubuntu xenial-backports InRelease
Fetched 190 kB in 12s (15.5 kB/s)
Reading package lists... Done
Building dependency tree
Reading state information... Done
19 packages can be upgraded. Run 'apt list --upgradable' to see them.

更新 Linux 後,我們需要使用以下命令安裝 Nginx

$ sudo apt-get install nginx
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
fontconfig-config fonts-dejavu-core libfontconfig1 libgd3 libjbig0
libjpeg-turbo8 libjpeg8 libtiff5 libvpx3 libxpm4 libxslt1.1 nginx-common
nginx-core
Suggested packages:
libgd-tools fcgiwrap nginx-doc ssl-cert
The following NEW packages will be installed:
fontconfig-config fonts-dejavu-core libfontconfig1 libgd3 libjbig0
libjpeg-turbo8 libjpeg8 libtiff5 libvpx3 libxpm4 libxslt1.1 nginx
nginx-common nginx-core
0 upgraded, 14 newly installed, 0 to remove and 19 not upgraded.
Need to get 2,994 kB of archives.
After this operation, 9,789 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://in.archive.ubuntu.com/ubuntu xenial/main amd64 libjpeg-turbo8 amd6 1.4.2-0ubuntu3 [111 kB]
Get:2 http://in.archive.ubuntu.com/ubuntu xenial/main amd64 libxpm4 amd64 1:3.511-1 [33.1 kB]
Get:3 http://in.archive.ubuntu.com/ubuntu xenial/main amd64 libjbig0 amd64 2.1-.1 [26.6 kB]
Get:4 http://in.archive.ubuntu.com/ubuntu xenial/main amd64 fonts-dejavu-core al 2.35-1 [1,039 kB]
Get:5 http://in.archive.ubuntu.com/ubuntu xenial/main amd64 fontconfig-config al 2.11.94-0ubuntu1 [49.9 kB]…
…
..
Setting up libjpeg-turbo8:amd64 (1.4.2-0ubuntu3) ...
Setting up libxpm4:amd64 (1:3.5.11-1) ...
Setting up libjbig0:amd64 (2.1-3.1) ...
Setting up fonts-dejavu-core (2.35-1) ...
Setting up fontconfig-config (2.11.94-0ubuntu1) ...
Setting up libfontconfig1:amd64 (2.11.94-0ubuntu1) ...
Setting up libjpeg8:amd64 (8c-2ubuntu8) ...
Setting up libtiff5:amd64 (4.0.6-1) ...
Setting up libvpx3:amd64 (1.5.0-2ubuntu1) ...
Setting up libgd3:amd64 (2.1.1-4ubuntu0.16.04.3) ...
Setting up libxslt1.1:amd64 (1.1.28-2.1) ...
Setting up nginx-common (1.10.0-0ubuntu0.16.04.2) ...
Setting up nginx-core (1.10.0-0ubuntu0.16.04.2) ...
Setting up nginx (1.10.0-0ubuntu0.16.04.2) ...
Processing triggers for libc-bin (2.23-0ubuntu3) ...
Processing triggers for systemd (229-4ubuntu7) ...
Processing triggers for ureadahead (0.100.0-19) ...
Processing triggers for ufw (0.35-0ubuntu2) ...

配置防火牆以允許 Nginx

我們需要配置防火牆以允許 **Nginx** 服務,可以使用 **ufw** 允許 Nginx 訪問。我們可以檢查防火牆知道的應用程式配置列表,使用以下命令列出應用程式 –

$ sudo ufw app list
Available applications:
Nginx Full
Nginx HTTP
Nginx HTTPS
OpenSSH

我們需要使用以下命令啟用 Nginx

$ sudo ufw allow 'Nginx HTTP'
Rules updated
Rules updated (v6)
root@ubuntu16:~# sudo ufw status
Status: active
To                      Action                      From
--                      ------                      ----
Nginx HTTP             ALLOW                      Anywhere
OpenSSH                ALLOW                      Anywhere
OpenSSH (v6)           ALLOW                      Anywhere (v6)
Nginx HTTP (v6)        ALLOW                      Anywhere (v6)

檢查 Nginx Web 伺服器

我們將使用以下命令檢查 Nginx Web 伺服器的狀態

$ systemctl status
nginxnginx.service - A high performance web server and a reverse proxy server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: e
Active: active (running) since Sun 2016-08-14 16:44:20 IST; 8min ago
Main PID: 4983 (nginx)
CGroup: /system.slice/nginx.service
├─4983 nginx: master process /usr/sbin/nginx -g daemon on; master_pr
└─4984 nginx: worker process
Aug 14 16:44:19 ubuntu16 systemd[1]: Starting A high performance web server and
Aug 14 16:44:20 ubuntu16 systemd[1]: nginx.service: Failed to read PID from fil
Aug 14 16:44:20 ubuntu16 systemd[1]: Started A high performance web server and lines 1-11/11 (END)

如我們從以上輸出中看到的,Nginx 服務已成功啟動並執行,我們可以在 Web 瀏覽器中透過 http://伺服器的IP地址 開啟 nginx。

管理 Nginx 程序

我們可以使用以下命令管理 Nginx Web 伺服器

要停止 Nginx Web 伺服器,請使用以下命令

$ sudo systemctl stop nginx

要啟動 Nginx Web 伺服器,請使用以下命令

$ sudo systemctl start nginx

要重新啟動 Nginx Web 伺服器,請使用以下命令

$ sudo systemctl restart nginx

要載入配置更改而不中斷現有連線,我們可以使用以下命令

$ sudo systemctl reload nginx

要在啟動時啟動/停止 Nginx Web 伺服器,我們可以使用以下命令

啟動

$ sudo systemctl enable nginx

停止

$ sudo systemctl disable nginx

Nginx Web 伺服器的重要檔案和目錄

檔案位置

Nginx Web 伺服器文件根目錄的預設位置是 /var/www/html,我們可以透過更改配置檔案來更改位置。

Nginx Web 伺服器配置檔案

/etc/nginx : The nginx configuration folders, as all the configuration files are located at this place.
/etc/nginx/nginx.conf : This configuration file is the main files where we can change the global configuration of the Nginx Web Server.
/etc/nginx/sites-available: This folder is for to configure the per site i.e., Server Blocks and the configuration files are found in the folders unless they are linked to sites-enabled folder.
/etc/nginx/sites-enabled: This folders is used to enable the per-site i.e., Server blocks and links are created by using the files in the sites-available folder.
/etc/nignx/snippets: Ths snipest folder contains the configuration included in the Nginx, these are repeatable configurations segments on refactoring into snippets.

Nginx Web 伺服器日誌位置

/var/log/nignx/access.log : All the request for the Nginx Web Server are recorded in the log file, untile we change the location in the Nginx configuration file.
/var/log/nignx/error.log: Any Nginx Web Server errors are recorded in the log file.

在本文中,我們學習瞭如何配置 Nginx Web 伺服器,如何更改配置,配置檔案的位置以及如何透過 ufw 防火牆允許 Nginx Web 伺服器,並且我們還有許多其他選項來服務技術。

更新於: 2020年1月20日

690 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告