1. 安装Nginx
在CentOS上安装Nginx有两种常见方式:YUM源安装(快速便捷,推荐新手)和源码编译安装(灵活自定义,适合有特定需求的情况)。
YUM源安装步骤:
sudo yum update -y
sudo yum install epel-release -y
sudo yum install nginx -y
sudo systemctl start nginx
sudo systemctl enable nginx
源码编译安装步骤(可选):
sudo yum install -y gcc make pcre-devel openssl-devel zlib-devel
wget http://nginx.org/download/nginx-1.25.3.tar.gz
tar -zxvf nginx-1.25.3.tar.gz
cd nginx-1.25.3
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-pcre
make && sudo make install
sudo nano /etc/systemd/system/nginx.service
内容如下:[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=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true
[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reload
sudo systemctl start nginx
sudo systemctl enable nginx
2. 基础配置
Nginx的主配置文件位于/etc/nginx/nginx.conf(YUM安装)或/usr/local/nginx/conf/nginx.conf(源码安装),虚拟主机配置通常放在/etc/nginx/conf.d/(YUM)或/usr/local/nginx/conf/conf.d/(源码)目录下。
核心配置说明:
user nginx; # 运行用户(默认nginx)
worker_processes auto; # 工作进程数(auto自动匹配CPU核心数)
error_log /var/log/nginx/error.log warn; # 错误日志路径和级别
pid /var/run/nginx.pid; # 主进程PID文件路径
events {
worker_connections 1024; # 每个工作进程最大并发连接数
use epoll; # 事件模型(Linux下推荐epoll)
multi_accept on; # 允许一个进程同时接受多个连接
}
http {
include /etc/nginx/mime.types; # 引入MIME类型文件
default_type application/octet-stream; # 默认MIME类型
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; # 开启高效文件传输
keepalive_timeout 65; # 长连接超时时间(秒)
gzip on; # 启用GZIP压缩
include /etc/nginx/conf.d/*.conf; # 包含虚拟主机配置
}
server {
listen 80; # 监听80端口(HTTP)
server_name example.com www.example.com; # 域名(可多个,空格分隔)
root /var/www/html/example.com; # 网站根目录
index index.html index.htm; # 默认首页文件
location / {
try_files $uri $uri/ =404; # 尝试查找文件,不存在则返回404
}
error_page 404 /404.html; # 自定义404错误页面
location = /404.html {
root /var/www/html; # 错误页面路径
}
}
3. 虚拟主机配置
Nginx支持基于域名的虚拟主机(最常用),通过不同server_name区分多个网站。
步骤:
sudo mkdir -p /var/www/html/example.com
sudo echo "Welcome to Example.com" > /var/www/html/example.com/index.html
example.com.conf):sudo nano /etc/nginx/conf.d/example.com.conf
粘贴上述server块配置,修改server_name和root为你的域名和目录。sudo nginx -t # 检查配置是否有语法错误
sudo systemctl reload nginx # 重载配置(不中断服务)
4. 防火墙设置
如果服务器启用了防火墙(如firewalld),需要开放HTTP(80)和HTTPS(443)端口:
sudo firewall-cmd --permanent --add-service=http # 永久开放HTTP
sudo firewall-cmd --permanent --add-service=https # 永久开放HTTPS
sudo firewall-cmd --reload # 重载防火墙规则
若需临时关闭防火墙(仅测试用,生产环境不推荐):
sudo systemctl stop firewalld
5. SSL配置(可选,启用HTTPS)
使用Let’s Encrypt免费获取SSL证书,步骤如下:
sudo yum install -y certbot python3-certbot-nginx
example.com为你的域名):sudo certbot --nginx -d example.com -d www.example.com
sudo certbot renew --dry-run # 测试续期
6. 常用命令
sudo systemctl start nginxsudo systemctl stop nginxsudo systemctl reload nginx(不中断服务)sudo systemctl status nginxsudo nginx -tsudo tail -f /var/log/nginx/error.log通过以上步骤,你可以在CentOS上完成Nginx的安装、基础配置及虚拟主机设置,满足常见的Web服务需求。