插件窝 干货文章 nginx安装实例代码分析

nginx安装实例代码分析

Nginx sudo systemctl 安装 82    来源:    2025-04-26

Nginx安装实例代码分析

基本安装方法

Ubuntu/Debian系统安装

# 更新软件包列表
sudo apt update

# 安装Nginx
sudo apt install nginx -y

# 启动Nginx服务
sudo systemctl start nginx

# 设置开机自启
sudo systemctl enable nginx

# 检查状态
sudo systemctl status nginx

CentOS/RHEL系统安装

# 添加EPEL仓库(CentOS/RHEL 7及以下)
sudo yum install epel-release -y

# 安装Nginx
sudo yum install nginx -y

# 启动Nginx服务
sudo systemctl start nginx

# 设置开机自启
sudo systemctl enable nginx

# 检查状态
sudo systemctl status nginx

源码编译安装

# 安装依赖
sudo apt install build-essential libpcre3 libpcre3-dev zlib1g zlib1g-dev libssl-dev -y

# 下载Nginx源码
wget https://nginx.org/download/nginx-1.23.3.tar.gz
tar -zxvf nginx-1.23.3.tar.gz
cd nginx-1.23.3

# 配置编译选项
./configure \
    --prefix=/usr/local/nginx \
    --with-http_ssl_module \
    --with-http_v2_module \
    --with-http_realip_module \
    --with-http_stub_status_module

# 编译并安装
make
sudo make install

# 创建服务文件(以systemd为例)
echo '[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target' | sudo tee /etc/systemd/system/nginx.service

# 重载systemd并启动
sudo systemctl daemon-reload
sudo systemctl start nginx
sudo systemctl enable nginx

Docker安装Nginx

# 拉取官方Nginx镜像
docker pull nginx:latest

# 运行Nginx容器
docker run --name mynginx -p 80:80 -d nginx

# 使用自定义配置运行
docker run --name mynginx \
    -v /path/to/nginx.conf:/etc/nginx/nginx.conf \
    -v /path/to/html:/usr/share/nginx/html \
    -p 80:80 \
    -d nginx

常见配置示例

基本HTTP服务器配置

server {
    listen 80;
    server_name example.com www.example.com;

    root /var/www/html;
    index index.html index.htm;

    location / {
        try_files $uri $uri/ =404;
    }

    error_page 404 /404.html;
    location = /404.html {
        internal;
    }
}

HTTPS配置

server {
    listen 443 ssl http2;
    server_name example.com www.example.com;

    ssl_certificate /etc/ssl/certs/example.com.crt;
    ssl_certificate_key /etc/ssl/private/example.com.key;

    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers EECDH+AESGCM:EDH+AESGCM;
    ssl_prefer_server_ciphers on;

    root /var/www/html;
    index index.html index.htm;

    location / {
        try_files $uri $uri/ =404;
    }
}

反向代理配置

server {
    listen 80;
    server_name api.example.com;

    location / {
        proxy_pass http://localhost:3000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

常见问题排查

  1. 检查Nginx配置语法

    sudo nginx -t
    
  2. 查看错误日志

    tail -f /var/log/nginx/error.log
    
  3. 检查端口占用

    sudo netstat -tulnp | grep :80
    
  4. 重新加载配置

    sudo nginx -s reload
    # 或
    sudo systemctl reload nginx
    
  5. 完全重启

    sudo systemctl restart nginx
    

以上代码涵盖了Nginx从安装到基本配置的常见场景,根据实际需求选择适合的安装和配置方式。