20 / 11 / 03
本教程适用于 Ubuntu 系统安装 Nginx
sudo apt update sudo apt install nginx
安装之后,Nginx 服务会自动开启。可以执行下面命令查看服务状态
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 Tue 2020-11-03 15:24:30 CST; 3h 4min ago Docs: man:nginx(8) Process: 6059 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS) Process: 6071 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS) Main PID: 6072 (nginx) Tasks: 2 (limit: 1111) Memory: 2.9M CGroup: /system.slice/nginx.service ├─6072 nginx: master process /usr/sbin/nginx -g daemon on; master_process on; └─6073 nginx: worker process
Ubuntu 系统默认会安装 UFW 防火墙组件。我们基于它来配置 Nginx 的防火墙设置。
# 添加规则 sudo ufw allow 'Nginx Full' # ufw 默认是关闭状态,需要开启防火墙 sudo ufw enable # 可选,开启 22 端口以允许 ssh 访问 sudo ufw allow 22 # 查看 ufw 状态 sudo ufw status
当看到下面的输出,说明你已经配置成功了
Status: active To Action From -- ------ ---- Nginx Full ALLOW Anywhere 22 ALLOW Anywhere Nginx Full (v6) ALLOW Anywhere (v6) 22 (v6) ALLOW Anywhere (v6)
到此,你已经安装完 Nginx,且配置了防火墙。如果你想了解更多相关知识点,可以继续阅读。
你可以通过 sudo ufw app list
来查看可用的应用配置
Available applications: Nginx Full Nginx HTTP Nginx HTTPS OpenSSH
是的,Nginx 提供了三个配置文件:
nginx -t
:检查 Nginx 配置文件格式是否正确service nginx restart
:重启 Nginx