如何在 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:所有单独的服务器块都存储于此位置。Nginx 只有在这些配置文件链接到 sites-enabled 文件夹后才会使用它们。
- /etc/nginx/sites-enabled/:已启用的站点或服务器块存储于此文件夹,这些服务器块是通过链接 site-available 文件夹中的服务器块创建的。
- /etc/nginx/snippets:配置文件或片段(称为代码片段)存储于此文件夹,这些片段在配置中可以重复使用。
- /var/log/nginx/access.log:记录所有对 nginx 的请求。 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 Full:允许 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 Full 防火墙已允许来自 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 的启动、停止、启用、禁用和重新加载。

