Nginx

Nginx发音的“engine x”是一个免费的开源高性能HTTP和反向代理服务器,负责处理互联网上一些最大的网站的负载。 本教程将概述在Ubuntu 16.04机器上安装和管理Nginx的步骤。

安装Nginx

Nginx的软件包在Ubuntu默认软件仓库中可用。 安装非常简单,只需键入以下命令:
sudo apt update
sudo apt install nginx

安装完成后,请检查Nginx服务状态和版本:

  • 服务状态
1
2
3
4
5
6
7
8
9
10
11
12
root@vultr:~# sudo 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 2020-11-08 18:11:00 CST; 13s ago
Main PID: 19268 (nginx)
CGroup: /system.slice/nginx.service
├─19268 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
└─19269 nginx: worker process

Nov 08 18:11:00 vultr.guest systemd[1]: Starting A high performance web server and a reverse proxy server...
Nov 08 18:11:00 vultr.guest systemd[1]: nginx.service: Failed to parse PID from file /run/nginx.pid: Invalid argument
Nov 08 18:11:00 vultr.guest systemd[1]: Started A high performance web server and a reverse proxy server.
  • 版本
1
2
root@vultr:~# sudo nginx -v
nginx version: nginx/1.10.3 (Ubuntu)

配置防火墙

  • 如果您正在运行防火墙,则还需要打开端口80和443。
1
2
3
root@vultr:~# sudo ufw allow 'Nginx Full'
Rules updated
Rules updated (v6)
  • 您可以通过以下方式验证更改:
1
2
root@vultr:~# sudo ufw status
Status: inactive
  • 如果是显示inactive,说明未启用,启用即可
1
2
3
4
5
6
7
8
9
10
11
12
root@vultr:~# sudo ufw enable
Command may disrupt existing ssh connections. Proceed with operation (y|n)? y
Firewall is active and enabled on system startup
root@vultr:~# sudo ufw status
Status: active

To Action From
-- ------ ----
Nginx Full ALLOW Anywhere
Nginx Full (v6) ALLOW Anywhere (v6)


管理Nginx服务

  • 您可以像任何其他systemd单位一样管理Nginx服务。 要停止Nginx服务,请运行:

sudo systemctl stop nginx

  • 要再次启动,请键入:

sudo systemctl start nginx

  • 重新启动Nginx服务:

sudo systemctl restart nginx

  • 在进行一些配置更改后重新加载Nginx服务:

$sudo systemctl reload nginx`

  • 如果你想禁用Nginx服务在启动时启动:

$sudo systemctl disable nginx`

  • 并重新启用它:

$sudo systemctl enable nginx`