如何在Ubuntu 16.04上安裝和管理Nginx


本文將學習如何在 Ubuntu 16.04 上安裝 Nginx,以及如何允許防火牆訪問 Nginx。此外,我們還將學習如何控制 Nginx。

Nginx 是一款流行的 Web 伺服器,現實世界中大部分網站都託管在 Nginx 上,它比 Apache 更友好,也可以用作反向代理。

先決條件

在本示例中,我們需要一個具有 sudo 許可權的非 root 使用者。

在 Ubuntu 16.04 上安裝 Nginx

預設情況下,Nginx 可透過預設的 Ubuntu 倉庫獲得,因此我們可以直接從命令列安裝 Nginx,我們也可以使用 apt 安裝軟體包,但在安裝 Nginx 之前,我們將更新並安裝 Nginx。

$ sudo apt-get update
OutPut:
sudo apt-get update
Hit:1 http://in.archive.ubuntu.com/ubuntu xenial InRelease
Hit:2 http://security.ubuntu.com/ubuntu xenial-security InRelease
Get:3 http://in.archive.ubuntu.com/ubuntu xenial-updates InRelease [95.7 kB]
Hit:4 http://ppa.launchpad.net/ansible/ansible/ubuntu xenial InRelease
Hit:5 http://in.archive.ubuntu.com/ubuntu xenial-backports InRelease
Get:6 http://in.archive.ubuntu.com/ubuntu xenial-updates/main amd64 Packages [37 3 kB]
Get:7 http://in.archive.ubuntu.com/ubuntu xenial-updates/main i386 Packages [368 kB]
Get:8 http://in.archive.ubuntu.com/ubuntu xenial-updates/universe amd64 Packages [319 kB]
Get:9 http://in.archive.ubuntu.com/ubuntu xenial-updates/universe i386 Packages [316 kB]
Fetched 1,471 kB in 4s (325 kB/s).
Reading package lists... Done
Now we will install the Nginx using the below command -

$ sudo apt-get install nginx
Output:
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
libyaml-0-2 python-crypto python-ecdsa python-jinja2 python-markupsafe python-paramiko python-pkg-resources
python-setuptools python-six python-yaml sshpass
Use 'sudo apt autoremove' to remove them.
The following NEW packages will be installed: nginx
0 upgraded, 1 newly installed, 0 to remove and 13 not upgraded.
Need to get 3,498 B of archives.
After this operation, 37.9 kB of additional disk space will be used.
Get:1 http://in.archive.ubuntu.com/ubuntu xenial-updates/main amd64 nginx all 1.10.0-0ubuntu0.16.04.2 [3,498 B]
Fetched 3,498 B in 0s (11.1 kB/s)
Selecting previously unselected package nginx.
(Reading database ... 92777 files and directories currently installed.)
Preparing to unpack .../nginx_1.10.0-0ubuntu0.16.04.2_all.deb ...
Unpacking nginx (1.10.0-0ubuntu0.16.04.2) ...
Setting up nginx (1.10.0-0ubuntu0.16.04.2) ...

重要的 Nginx 檔案和資料夾

  • /var/www/html: – Nginx 的所有預設內容都位於此資料夾中。/etc/nginx: Nginx 配置資料夾,所有 Nginx 配置檔案都儲存在此處。
  • etc/nginx/nginx.conf: 這是主要的 Nginx 配置檔案,也稱為 Nginx 全域性配置。
  • /etc/nginx/sites-available: 這是儲存所有單個伺服器塊的位置,除非將配置檔案連結到 sites-enabled 資料夾,否則 Nginx 不會使用這些配置檔案。
  • /etc/nginx/sites-enabled/: 這是儲存已啟用站點或伺服器塊的資料夾,這些檔案是透過將伺服器塊從 site-available 資料夾連結建立的。
  • /etc/nginx/snippets: 這是儲存可在配置中重複使用的配置檔案或片段的資料夾,這些片段稱為程式碼片段。
  • /var/log/nginx/access.log: 所有對 Nginx Web 伺服器的請求都記錄在此日誌中,否則如果我們更改 Nginx 配置以儲存在其他資料夾中。
  • /var/log/nginx/error.log: 所有與 Nginx Web 伺服器相關的錯誤都儲存在此日誌中。

調整防火牆以訪問 Nginx

在從 Web 瀏覽器訪問 Nginx 之前,我們將允許防火牆從外部訪問服務。

要檢視防火牆允許訪問的配置檔案列表,請執行以下操作:

$ sudo ufw app list
Output:
Available applications:
Nginx Full
Nginx HTTP
Nginx HTTPS
OpenSSH
  • Nginx 全部:這將允許埠 80 和 443。
  • Nginx HTTP:這將只允許埠 80。
  • Nginx HTTPS:這將只允許埠 443。

在本示例中,我們允許 HTTP 80 和 HTTPS 443,以下是允許 80 和 443 協議的命令

$ sudo ufw allow 'Nginx HTTP'
Rule added
Rule added (v6)

如果我們檢查防火牆的狀態,現在可以看到 Nginx 全部都允許來自 IPv4 和 IPv6。

$ sudo ufw status
Status: active
To Action From
-- ------ ----
OpenSSH ALLOW Anywhere
Nginx Full ALLOW Anywhere
OpenSSH (v6) ALLOW Anywhere (v6)
Nginx Full (v6) ALLOW Anywhere (v6)

檢查 Nginx 服務狀態

預設情況下,在安裝 Ubuntu 後,它會啟動 Nginx,我們可以使用以下命令檢查 Nginx 的狀態:

$ systemctl status nginx
nginx.service - A high performance web server and a reverse proxy server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
Active: active (running) since Sun 2016-08-21 18:50:36 IST; 11min ago
Process: 4370 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Process: 4286 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS
Main PID: 4387 (nginx)
Tasks: 2
Memory: 8.0M
CPU: 152ms
CGroup: /system.slice/nginx.service
├─4387 nginx: master process /usr/sbin/nginx -g daemon on; master_process on
└─4389 nginx: worker process
Aug 21 18:50:35 ubuntu16 systemd[1]: Starting A high performance web server and a reverse proxy server...
Aug 21 18:50:36 ubuntu16 systemd[1]: nginx.service: Failed to read PID from file /run/nginx.pid: Invalid argument
Aug 21 18:50:36 ubuntu16 systemd[1]: Started A high performance web server and a reverse proxy server.

我們也可以使用 curl 命令檢查 Nginx 伺服器是否正在執行,以下是使用伺服器的 IP 地址檢查伺服器狀態的命令。

$ curl -4 192.168.0.9
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
   body {
      width: 35em;
      margin: 0 auto;
      font-family: Tahoma, Verdana, Arial, sans-serif;
   }
</style>
</head>
<body>
   <h1>Welcome to nginx!</h1>
   <p>If you see this page, the nginx web server is successfully installed and
   working. Further configuration is required.</p>
   <p>For online documentation and support please refer to
   <a href="http://nginx.org/">nginx.org</a>.<br/>
   Commercial support is available at
   <a href="http://nginx.com/">nginx.com</a>.</p>
   <p><em>Thank you for using nginx.</em></p>
</body>
</html>

我們現在可以從任何瀏覽器訪問 Nginx 預設頁面,以確認 Nginx 正在執行並可從伺服器外部訪問。

http://伺服器域名或IP

使用命令管理 Nginx 程序

停止 Nginx

$ sudo systemctl stop nginx

啟動 Nginx

$ sudo systemctl start nginx

重啟 Nginx

$ sudo systemctl restart nginx

重新載入配置,無需斷開現有連線

$ sudo systemctl reload nginx

在啟動時啟動 Nginx

$ sudo systemctl enable nginx

停用或停止在啟動時啟動 Nginx。

$ sudo systemctl disable nginx

透過本文和設定,我們能夠安裝 Nginx Web 伺服器,允許防火牆訪問 Nginx,並管理 Nginx 的啟動、停止、啟用、停用和重新載入。

更新於:2020年1月23日

162 次檢視

開啟你的職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.