1. Linux环境下Nginx的完整安装指南
作为一名长期在Linux环境下部署Web服务的老兵,我深知Nginx作为高性能Web服务器的重要性。今天我将分享在Linux系统上从零开始安装配置Nginx的全过程,包含那些官方文档不会告诉你的实战细节。
Nginx以其高并发、低内存占用和模块化设计著称,特别适合作为反向代理和负载均衡器。与Apache相比,Nginx采用事件驱动架构,能够轻松应对C10K问题(即单机同时处理1万个连接)。在开始安装前,请确保你拥有一个干净的Linux环境(本文以CentOS 7为例,但原理适用于大多数发行版)。
需要模型API调用? 免费领10W Token,多模型网关一键接入 Claude、DeepSeek 等主流模型。
2. 环境准备与依赖安装
2.1 远程连接Linux服务器
首先需要通过SSH连接到目标服务器。这里有个小技巧:使用-A参数启用认证代理转发,可以避免重复输入密码:
bash复制ssh -A root@your_server_ip
注意:生产环境中建议使用普通用户登录后再切换root,而非直接root登录
连接后,建议先更新系统基础软件包,避免后续安装出现版本冲突:
bash复制yum update -y && yum upgrade -y
2.2 安装PCRE库
PCRE(Perl Compatible Regular Expressions)是Nginx实现URL重写等功能的必备依赖。安装时有两个选择:
- 通过yum快速安装(适合新手):
bash复制yum install pcre pcre-devel -y
- 源码编译安装(适合需要特定版本的情况):
bash复制# 下载特定版本(这里以8.32为例)
wget https://sourceforge.net/projects/pcre/files/pcre/8.32/pcre-8.32.tar.gz
tar zxvf pcre-8.32.tar.gz
cd pcre-8.32
./configure --prefix=/usr/local/pcre
make && make install
验证安装是否成功:
bash复制pcre-config --version
# 应输出类似:8.32
2.3 安装其他必要依赖
这些是Nginx编译和运行的基石:
bash复制yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
经验之谈:如果服务器位于国内,建议先配置阿里云或腾讯云的yum镜像源,可以显著提高下载速度
3. Nginx安装与配置
3.1 获取Nginx源码包
建议从官网获取稳定版本(当前最新稳定版为1.20.1):
bash复制wget http://nginx.org/download/nginx-1.20.1.tar.gz
tar zxvf nginx-1.20.1.tar.gz
cd nginx-1.20.1
3.2 编译配置选项
执行configure时,推荐添加以下实用模块:
bash复制./configure \
--prefix=/usr/local/nginx \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_stub_status_module \
--with-pcre=/usr/local/pcre
关键参数说明:
--with-http_ssl_module:启用HTTPS支持--with-http_stub_status_module:启用状态监控页面--with-pcre:指定PCRE库路径
3.3 编译与安装
bash复制make -j$(nproc) # 使用所有CPU核心加速编译
make install
安装完成后,关键目录结构:
code复制/usr/local/nginx/
├── sbin/nginx # 主程序
├── conf/nginx.conf # 主配置文件
└── logs/ # 日志目录
4. 防火墙与SELinux配置
4.1 防火墙放行HTTP/HTTPS
bash复制firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --reload
4.2 SELinux策略调整(如启用)
bash复制setsebool -P httpd_can_network_connect 1
semanage port -a -t http_port_t -p tcp 8080 # 如需自定义端口
5. Nginx服务管理
5.1 手动启动与测试
bash复制/usr/local/nginx/sbin/nginx -t # 测试配置
/usr/local/nginx/sbin/nginx # 启动
curl http://localhost # 验证
5.2 配置systemd服务(推荐)
创建服务文件/usr/lib/systemd/system/nginx.service:
ini复制[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.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
启用服务:
bash复制systemctl daemon-reload
systemctl start nginx
systemctl enable nginx
6. Nginx配置文件深度解析
6.1 配置文件结构
Nginx配置文件采用模块化设计,主要分为三大块:
nginx复制# 全局块
user nginx;
worker_processes auto; # 自动匹配CPU核心数
# events块
events {
worker_connections 1024; # 每个worker的最大连接数
use epoll; # Linux高性能事件模型
}
# http块
http {
include 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"';
server {
listen 80;
server_name example.com;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
}
}
6.2 性能调优参数
nginx复制http {
sendfile on; # 启用零拷贝传输
tcp_nopush on; # 优化数据包发送
tcp_nodelay on; # 禁用Nagle算法
keepalive_timeout 65; # 长连接超时
gzip on; # 启用压缩
gzip_types text/plain text/css application/json;
}
7. 常见问题排查指南
7.1 端口占用问题
bash复制netstat -tulnp | grep :80
# 如果被占用,可以:
# 1. 停止占用进程
# 2. 修改Nginx监听端口
7.2 权限问题
bash复制chown -R nginx:nginx /usr/local/nginx/html
chmod -R 755 /usr/local/nginx
7.3 配置语法检查
bash复制/usr/local/nginx/sbin/nginx -t
# 输出示例:
# nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
# nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
8. 进阶配置技巧
8.1 多站点配置
nginx复制http {
server {
listen 80;
server_name site1.example.com;
root /var/www/site1;
}
server {
listen 80;
server_name site2.example.com;
root /var/www/site2;
}
}
8.2 日志分割(通过logrotate)
创建/etc/logrotate.d/nginx:
code复制/usr/local/nginx/logs/*.log {
daily
missingok
rotate 30
compress
delaycompress
notifempty
create 640 nginx adm
sharedscripts
postrotate
[ -f /usr/local/nginx/logs/nginx.pid ] && kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`
endscript
}
8.3 性能监控
启用stub_status模块:
nginx复制location /nginx_status {
stub_status;
allow 127.0.0.1;
deny all;
}
访问输出示例:
code复制Active connections: 3
server accepts handled requests
10 10 20
Reading: 0 Writing: 1 Waiting: 2
在实际生产环境中,我通常会结合这些基础配置,根据业务需求添加缓存策略、负载均衡等高级功能。对于初次接触Nginx的朋友,建议先掌握这些核心配置,再逐步探索更复杂的应用场景。
