插件窝 干货文章 Linux系统中怎么启动nginx

Linux系统中怎么启动nginx

Nginx sudo bash systemctl 115    来源:    2025-04-24

在 Linux 系统中启动 Nginx 的步骤如下,具体操作可能因发行版和安装方式略有不同:


1. 检查 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


2. 启动 Nginx

使用 systemd(推荐)

sudo systemctl start nginx

使用 service(旧版系统)

sudo service nginx start

3. 验证 Nginx 是否运行

  • 检查进程状态bash sudo systemctl status nginxbash ps aux | grep nginx
  • 测试访问: 在浏览器输入服务器 IP 地址(如 http://localhosthttp://your_server_ip),看到 Nginx 欢迎页即表示成功。

4. 设置开机自启

sudo systemctl enable nginx

5. 其他常用命令

操作 命令
停止 Nginx sudo systemctl stop nginx
重启 Nginx sudo systemctl restart nginx
重新加载配置(不中断服务) sudo systemctl reload nginx
检查配置文件语法 sudo nginx -t

常见问题

  1. 端口冲突
    如果其他服务(如 Apache)占用了 80 端口,需先停止冲突服务或修改 Nginx 配置(/etc/nginx/nginx.conf)。

  2. 权限问题
    确保 Nginx 用户(通常为 www-datanginx)对相关目录有访问权限。

  3. 防火墙未放行

    • 开放 80 端口: 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