如何在 Debian 上安装 Nginx
介绍
Nginx
在本指南中
先决条件
在开始本指南之前
当你有可用的账户时
步骤 1 - 安装 Nginx
因为 Nginx 在 Debian 的默认仓库中可用apt
由于这是我们在本会话中与系统的第一次交互
sudo apt update
我们现在开始安装nginx
sudo apt install nginx
当系统提示你确认安装时
步骤 2 – 调整防火墙
在测试
查看
sudo ufw app list
你应该会获得应用程序配置文件的列表如下
Output
Available applications:
...
Nginx Full
Nginx HTTP
Nginx HTTPS
...
如你所见
- Nginx Full
此配置文件同时打开: 80
正常( 未加密的, 和) 443
TLS / SSL( ) - Nginx HTTP
此配置文件仅打开: 80
正常( 未加密的, ) - Nginx HTTPS
此配置文件仅打开: 443
TLS / SSL( )
建议你启用限制性最强的配置文件
你可以通过键入以下内容来启用此功能
sudo ufw allow 'Nginx HTTP'
你可以通过键入以下内容来验证更改
sudo ufw status
你应该会在显示的输出中看到允许的 HTTP 流量
Output
Status: active
To Action From
-- ------ ----
OpenSSH ALLOW Anywhere
Nginx HTTP ALLOW Anywhere
OpenSSH (v6) ALLOW Anywhere (v6)
Nginx HTTP (v6) ALLOW Anywhere (v6)
步骤 3 – 检查 Web 服务器
在安装过程结束后
我们可以使用 init 系统进行检查
systemctl status nginx
Output
● 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 Wed 2019-07-03 12:52:54 UTC; 4min 23s ago
Docs: man:nginx(8)
Main PID: 3942 (nginx)
Tasks: 3 (limit: 4719)
Memory: 6.1M
CGroup: /system.slice/nginx.service
├─3942 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
├─3943 nginx: worker process
└─3944 nginx: worker process
如上所示
你可以访问默认的
ip addr show eth0 | grep inet | awk '{ print $2; }' | sed 's/\/.*$//'
当你拥有服务器的 IP 地址时
http://your_server_ip
你应该会看到默认的
此页面是
步骤 4 - 管理 Nginx 进程
现在
要停止 Web 服务器
sudo systemctl stop nginx
要启动 Web 服务器
sudo systemctl start nginx
要重启 Web 服务器
sudo systemctl restart nginx
如果只是进行配置更改
sudo systemctl reload nginx
默认情况下
sudo systemctl disable nginx
要重新启用开机自启
sudo systemctl enable nginx
步骤 5 – 设置服务器块
使用
Debian 10 上的 Nginx 默认启用了一个服务器块/var/www/html
/var/www/html
按如下所示创建 your_domain 网站-p
sudo mkdir -p /var/www/your_domain/html
接下来$USER
sudo chown -R $USER:$USER /var/www/your_domain/html
如果你尚未修改umask
sudo chmod -R 755 /var/www/your_domain
接下来
nano /var/www/your_domain/html/index.html
在里面添加以下示例
<html>
<head>
<title>Welcome to your_domain</title>
</head>
<body>
<h1>Success! Your Nginx server is successfully configured for <em>your_domain</em>. </h1>
<p>This is a sample page.</p>
</body>
</html>
完成后保存并关闭该文件
为了让/etc/nginx/sites-available/your_domain
sudo nano /etc/nginx/sites-available/your_domain
粘贴以下配置块
server {
listen 80;
listen [::]:80;
root /var/www/your_domain/html;
index index.html index.htm index.nginx-debian.html;
server_name your_domain www.your_domain;
location / {
try_files $uri $uri/ =404;
}
}
注意看root
server_name
然后让我们通过建立一个软链接把 sites-available
的配置指向 sites-enabled
来启用此服务器块sites-enabled
sudo ln -s /etc/nginx/sites-available/your_domain /etc/nginx/sites-enabled/
接下来
sudo nginx -t
如果没有任何问题
Output
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
配置测试通过后
sudo systemctl restart nginx
现在你应该可以通过http://your_domain
步骤 6 - 熟悉重要的 Nginx 文件和目录
现在你已经知道如何管理
网页内容
/var/www/html
实际的: 默认情况下仅由你之前看到的默认, 从网站根目录中提供, 这可以通过更改。 。
服务器配置
/etc/nginx
Nginx 配置目录: 所有。 。 /etc/nginx/nginx.conf
主: 可以对此进行修改以更改。 。 /etc/nginx/sites-available/
可以存储每个站点服务器块的目录: Nginx。 除非它们链接到, sites-enabled
通常。 所有服务器块配置都在此目录中完成, 然后通过链接到其他目录来启用, 。 /etc/nginx/sites-enabled/
存储已启用的站点服务器块的目录: 通常。 它们是通过链接, sites-available
。 /etc/nginx/snippets
这个目录主要可以包含在其它: 。
服务器日志
/var/log/nginx/access.log
对: 除非你为, 。 /var/log/nginx/error.log
任何: 。
完结
现在