在Ubuntu系统上安装Nginx服务器的步骤如下:
sudo apt update
sudo apt install nginx -y
sudo systemctl start nginx
sudo systemctl enable nginx
sudo systemctl status nginx # 检查服务状态
curl -I 127.0.0.1 # 测试HTTP响应
访问服务器IP或域名应看到Nginx欢迎页。
sudo apt install curl gnupg2 ca-certificates lsb-release ubuntu-keyring -y
curl https://nginx.org/keys/nginx_signing.key | gpg --dearmor | sudo tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null
echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] http://nginx.org/packages/ubuntu $(lsb_release -cs) nginx" | sudo tee /etc/apt/sources.list.d/nginx.list
sudo apt update
sudo apt install nginx -y
功能 | 命令 |
---|---|
启动Nginx | sudo systemctl start nginx |
停止Nginx | sudo systemctl stop nginx |
重启Nginx | sudo systemctl restart nginx |
检查配置 | sudo nginx -t |
重载配置 | sudo nginx -s reload |
sudo ufw allow 'Nginx Full' # 允许HTTP/HTTPS
sudo ufw enable # 启用防火墙
sudo ufw status # 查看规则
/etc/nginx/nginx.conf
/etc/nginx/conf.d/
或 /etc/nginx/sites-available/
/var/www/html
/var/log/nginx/
端口冲突
检查80/443端口是否被占用:
sudo ss -tulnp | grep ':80'
修改Nginx默认端口:编辑 /etc/nginx/sites-enabled/default
中的 listen
指令。
权限问题
确保Nginx用户(通常为www-data
)有网站目录的读取权限:
sudo chown -R www-data:www-data /var/www/html
测试配置语法
sudo nginx -t # 检查配置是否正确
通过以上步骤,你可以在Ubuntu上快速部署Nginx服务器。如需HTTPS支持,可使用Let's Encrypt免费证书(通过certbot
工具)。