如何在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="https://nginx.ac.cn/">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 的启动、停止、启用、禁用和重新加载。
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP