Ubuntu apt安装nginx

Ubuntu 系统 中,如果你不想用源码 tar 包编译,而是直接用 APT 包管理器安装 Nginx,步骤就非常简单了:


Ubuntu 使用 apt 安装 Nginx

1. 更新软件源

bash 复制代码
sudo apt update

2. 安装 Nginx

bash 复制代码
sudo apt install -y nginx

3. 启动并设置开机自启

bash 复制代码
sudo systemctl start nginx
sudo systemctl enable nginx

4. 检查 Nginx 状态

bash 复制代码
systemctl status nginx

如果看到 active (running) 就说明 Nginx 启动成功。


5. 测试访问

浏览器访问:

复制代码
http://服务器IP/

会看到 Welcome to nginx! 默认页面。


6. 配置文件路径

APT 安装的 Nginx 路径和源码版不同,主要目录如下:

复制代码
/etc/nginx/              # 主配置目录
/etc/nginx/nginx.conf    # 主配置文件
/etc/nginx/sites-available/  # 可用的站点配置
/etc/nginx/sites-enabled/    # 启用的站点配置(软链接到 sites-available)
/var/www/html/           # 默认站点根目录
/var/log/nginx/          # 日志目录

7. 常用命令

bash 复制代码
# 重启 Nginx
sudo systemctl restart nginx

# 重新加载配置(推荐)
sudo systemctl reload nginx

# 测试配置文件是否正确
sudo nginx -t