在 Linux 系统中启动 Nginx 的步骤如下,具体操作可能因发行版和安装方式略有不同:
nginx -v
如果显示版本号(如 nginx/1.18.0
),则已安装;如果未安装,需先通过包管理器安装:
- Ubuntu/Debian:
bash
sudo apt update && sudo apt install nginx
- CentOS/RHEL:
bash
sudo yum install epel-release && sudo yum install nginx
sudo systemctl start nginx
sudo service nginx start
bash
sudo systemctl status nginx
或
bash
ps aux | grep nginx
http://localhost
或 http://your_server_ip
),看到 Nginx 欢迎页即表示成功。sudo systemctl enable nginx
操作 | 命令 |
---|---|
停止 Nginx | sudo systemctl stop nginx |
重启 Nginx | sudo systemctl restart nginx |
重新加载配置(不中断服务) | sudo systemctl reload nginx |
检查配置文件语法 | sudo nginx -t |
端口冲突:
如果其他服务(如 Apache)占用了 80 端口,需先停止冲突服务或修改 Nginx 配置(/etc/nginx/nginx.conf
)。
权限问题:
确保 Nginx 用户(通常为 www-data
或 nginx
)对相关目录有访问权限。
防火墙未放行:
bash
sudo ufw allow 80/tcp # Ubuntu
sudo firewall-cmd --add-port=80/tcp --permanent && sudo firewall-cmd --reload # CentOS
通过以上步骤,Nginx 应能正常启动并提供服务。如果遇到错误,可通过日志排查:
sudo tail -f /var/log/nginx/error.log