Nginx 基础安装与配置

Basic Installation and Configuration

概述

Nginx的安装和基础配置是掌握Nginx的第一步。本文将详细介绍在不同操作系统上安装Nginx,以及进行基础配置的方法。

1. 安装Nginx

1.1 在Ubuntu/Debian上安装

# 更新包管理器
sudo apt update

# 安装Nginx
sudo apt install nginx

# 启动Nginx服务
sudo systemctl start nginx

# 设置开机自启
sudo systemctl enable nginx

# 检查状态
sudo systemctl status nginx

1.2 在CentOS/RHEL上安装

# 安装EPEL仓库
sudo yum install epel-release

# 安装Nginx
sudo yum install nginx

# 启动服务
sudo systemctl start nginx
sudo systemctl enable nginx

# 检查防火墙
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload

1.3 源码编译安装

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

# 下载源码
wget http://nginx.org/download/nginx-1.24.0.tar.gz
tar -xzf nginx-1.24.0.tar.gz
cd nginx-1.24.0

# 配置编译选项
./configure \
    --prefix=/etc/nginx \
    --sbin-path=/usr/sbin/nginx \
    --conf-path=/etc/nginx/nginx.conf \
    --error-log-path=/var/log/nginx/error.log \
    --http-log-path=/var/log/nginx/access.log \
    --pid-path=/var/run/nginx.pid \
    --lock-path=/var/run/nginx.lock \
    --with-http_ssl_module \
    --with-http_realip_module \
    --with-http_gzip_static_module

# 编译和安装
make && sudo make install

2. 基础配置结构

2.1 配置文件层次结构

/etc/nginx/
├── nginx.conf              # 主配置文件
├── conf.d/                  # 包含的配置文件目录
│   └── default.conf
├── sites-available/         # 可用站点配置
├── sites-enabled/           # 启用站点配置
└── modules-enabled/         # 启用模块配置

2.2 主配置文件结构

# nginx.conf 基本结构
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# 事件模块
events {
    worker_connections 1024;
    use epoll;
}

# HTTP模块
http {
    # 基础设置
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    # 日志格式
    log_format main '$remote_addr - $remote_user [$time_local] "$request" '
                   '$status $body_bytes_sent "$http_referer" '
                   '"$http_user_agent" "$http_x_forwarded_for"';

    access_log /var/log/nginx/access.log main;

    # 性能设置
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;

    # 包含其他配置文件
    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

3. 基本服务器配置

3.1 简单的Web服务器

server {
    listen 80;
    listen [::]:80;
    server_name example.com www.example.com;
    root /var/www/html;
    index index.html index.htm;

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

    # 静态文件缓存
    location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
        expires 1y;
        add_header Cache-Control "public, immutable";
    }

    # 安全设置
    location ~ /\.ht {
        deny all;
    }

    # 错误页面
    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }
}

3.2 PHP应用服务器配置

server {
    listen 80;
    server_name php-app.example.com;
    root /var/www/php-app;
    index index.php index.html;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.(?!well-known) {
        deny all;
    }
}

4. 基本管理命令

4.1 服务管理

# 启动Nginx
sudo systemctl start nginx

# 停止Nginx
sudo systemctl stop nginx

# 重启Nginx
sudo systemctl restart nginx

# 重新加载配置
sudo systemctl reload nginx

# 检查状态
sudo systemctl status nginx

4.2 配置测试

# 测试配置文件语法
sudo nginx -t

# 测试并显示配置
sudo nginx -T

# 检查配置文件路径
sudo nginx -t -c /etc/nginx/nginx.conf

4.3 进程管理

# 优雅停止
sudo nginx -s quit

# 快速停止
sudo nginx -s stop

# 重新加载配置
sudo nginx -s reload

# 重新打开日志文件
sudo nginx -s reopen

5. 常见配置参数

5.1 基础参数

# 工作进程数
worker_processes auto;  # 自动检测CPU核心数

# 工作进程连接数
worker_connections 1024;

# 文件句柄数限制
worker_rlimit_nofile 65535;

# 进程优先级
worker_priority -5;

5.2 HTTP基础设置

# 文件传输优化
sendfile on;
tcp_nopush on;
tcp_nodelay on;

# 连接超时
keepalive_timeout 65;
client_header_timeout 60;
client_body_timeout 60;
send_timeout 60;

# 缓冲区大小
client_max_body_size 100m;
client_body_buffer_size 128k;
client_header_buffer_size 1k;
large_client_header_buffers 4 4k;

6. 目录权限设置

6.1 基本权限配置

# 创建网站目录
sudo mkdir -p /var/www/example.com

# 设置所有者
sudo chown -R www-data:www-data /var/www/example.com

# 设置权限
sudo chmod -R 755 /var/www/example.com

# 设置SELinux上下文(CentOS/RHEL)
sudo setsebool -P httpd_can_network_connect 1
sudo chcon -R -t httpd_exec_t /var/www/example.com

6.2 日志目录权限

# 确保日志目录存在
sudo mkdir -p /var/log/nginx

# 设置权限
sudo chown -R nginx:nginx /var/log/nginx
sudo chmod -R 640 /var/log/nginx

7. 验证安装

7.1 检查安装版本

# 查看版本信息
nginx -v
nginx -V  # 详细信息包括编译参数

# 检查模块
nginx -V 2>&1 | grep -o with-[a-z_]*

7.2 测试基本功能

# 创建测试页面
echo "<h1>Nginx is working!</h1>" | sudo tee /var/www/html/index.html

# 访问测试
curl http://localhost
curl -I http://localhost  # 只查看头部信息

8. 故障排除

8.1 常见问题

# 检查端口占用
sudo netstat -tulpn | grep :80
sudo ss -tulpn | grep :80

# 检查防火墙
sudo ufw status  # Ubuntu
sudo firewall-cmd --list-all  # CentOS

# 检查SELinux
sudo sestatus
sudo getsebool -a | grep httpd

8.2 日志查看

# 查看错误日志
sudo tail -f /var/log/nginx/error.log

# 查看访问日志
sudo tail -f /var/log/nginx/access.log

# 按时间查看日志
sudo journalctl -u nginx -f

小结

掌握Nginx的基础安装和配置是使用Nginx的第一步。通过本文的学习,你应该能够:

  1. 在不同操作系统上成功安装Nginx
  2. 理解Nginx的配置文件结构
  3. 创建基本的Web服务器配置
  4. 使用基本的管理命令
  5. 解决常见的安装和配置问题

下一篇文章将深入介绍Nginx的核心模块和指令。

powered by Gitbook© 2025 编外计划 | 最后修改: 2025-08-29 15:40:15

results matching ""

    No results matching ""