WSL 安装 Debian 12 后,Linux 如何安装 nginx ?
在 WSL 的 Debian 12 中安装 Nginx 的步骤如下:
1. 更新系统软件包
sudo apt update && sudo apt upgrade -y
2. 安装 Nginx
sudo apt install nginx -y
3. 管理 Nginx 服务
▶ 启动 Nginx
sudo service nginx start # 如果使用 systemd 可能需改用:sudo systemctl start nginx
▶ 设置开机自启(仅对支持 systemd 的 WSL 有效)
sudo systemctl enable nginx
▶ 验证服务状态
sudo service nginx status
# 或
sudo systemctl status nginx
4. 验证安装
打开浏览器访问:
http://localhost
或使用终端命令:
curl 127.0.0.1
如果看到 “Welcome to nginx!” 页面,说明安装成功。
5. 配置说明
- 配置文件目录:
/etc/nginx/
- 默认网站目录:
/var/www/html/
- 日志文件:
/var/log/nginx/
6. 常见问题解决
🔸 端口被占用
检查 80 端口是否被 Windows 程序占用:
# 在 Windows PowerShell 中执行
netstat -ano | findstr :80
🔸 WSL 不支持 systemd
如果是旧版 WSL,需手动启动服务:
sudo service nginx restart
或启用 systemd 支持:
- 以管理员身份打开 PowerShell
- 创建配置文件:
code "%USERPROFILE%\.wslconfig"
- 添加以下内容后保存:
[boot] systemd=true
- PowerShell 重启 WSL:
wsl --shutdown
后续操作建议
- 配置防火墙(如有需要):
sudo ufw allow 80/tcp
- 创建自定义网站配置:
sudo nano /etc/nginx/sites-available/my-site
完成上述步骤后,你的 Nginx 服务器即可正常运行。