插件窝 干货文章 nginx怎么启用

nginx怎么启用

Nginx sudo 配置 bash 584    来源:    2025-04-07

启用Nginx的步骤指南

Nginx是一个高性能的HTTP和反向代理服务器,以下是启用Nginx的基本步骤:

1. 安装Nginx

在Linux系统上安装:

Ubuntu/Debian:

sudo apt update
sudo apt install nginx

CentOS/RHEL:

sudo yum install epel-release
sudo yum install nginx

2. 启动Nginx服务

# 启动Nginx
sudo systemctl start nginx

# 设置开机自启
sudo systemctl enable nginx

3. 检查Nginx状态

sudo systemctl status nginx

4. 验证Nginx是否运行

打开浏览器访问服务器IP地址或域名,应该能看到Nginx欢迎页面。

或者使用命令行:

curl -I 127.0.0.1

5. 基本操作命令

  • 停止Nginx:

    sudo systemctl stop nginx
    
  • 重启Nginx:

    sudo systemctl restart nginx
    
  • 重新加载配置(不中断服务):

    sudo systemctl reload nginx
    

6. 配置防火墙(如果需要)

# 允许HTTP(80)和HTTPS(443)流量
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload

7. 配置文件位置

  • 主配置文件: /etc/nginx/nginx.conf
  • 站点配置文件: /etc/nginx/conf.d//etc/nginx/sites-available/
  • 默认网站根目录: /usr/share/nginx/html

注意事项

  1. 修改配置后记得测试配置语法是否正确:

    sudo nginx -t
    
  2. 如果遇到端口冲突(如80端口被占用),需要先停止占用该端口的服务。

  3. 在生产环境中,建议配置SSL/TLS证书以启用HTTPS。

下一篇:nginx怎么跳转