# 添加EPEL仓库
sudo yum install epel-release
# 安装Nginx
sudo yum install nginx
# 启动Nginx服务
sudo systemctl start nginx
# 设置开机自启
sudo systemctl enable nginx
# 安装编译依赖
sudo yum install gcc pcre-devel zlib-devel make
# 下载最新稳定版Nginx
wget http://nginx.org/download/nginx-x.x.x.tar.gz
tar -zxvf nginx-x.x.x.tar.gz
cd nginx-x.x.x
# 配置编译选项
./configure --prefix=/usr/local/nginx \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_realip_module
# 编译并安装
make && sudo make install
# 创建systemd服务文件
sudo vi /etc/systemd/system/nginx.service
/etc/nginx/nginx.conf
/usr/local/nginx/conf/nginx.conf
user nginx;
worker_processes auto; # 根据CPU核心数自动设置
events {
worker_connections 1024;
use epoll;
multi_accept on;
}
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;
error_log /var/log/nginx/error.log warn;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
gzip on;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
include /etc/nginx/conf.d/*.conf;
}
sudo vi /etc/nginx/conf.d/example.com.conf
server {
listen 80;
server_name example.com www.example.com;
root /var/www/example.com/html;
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
error_page 404 /404.html;
location = /404.html {
internal;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
internal;
}
}
# 安装Certbot
sudo yum install certbot python3-certbot-nginx
# 获取并安装证书
sudo certbot --nginx -d example.com -d www.example.com
# 设置自动续期
sudo certbot renew --dry-run
server {
listen 443 ssl;
server_name example.com;
ssl_certificate /etc/nginx/ssl/example.com.crt;
ssl_certificate_key /etc/nginx/ssl/example.com.key;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384';
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
# 其他配置...
}
# 检查配置语法
sudo nginx -t
# 重新加载配置(不中断服务)
sudo systemctl reload nginx
# 重启Nginx
sudo systemctl restart nginx
# 停止Nginx
sudo systemctl stop nginx
# 查看状态
sudo systemctl status nginx
# 查看版本和编译参数
nginx -V
# 查看访问日志
tail -f /var/log/nginx/access.log
# 查看错误日志
tail -f /var/log/nginx/error.log
# 日志轮转配置
sudo vi /etc/logrotate.d/nginx
# 开放HTTP和HTTPS端口
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
按照以上步骤配置完成后,您的Nginx服务器应该可以正常运行并服务网站内容了。根据实际需求,您可能需要进一步调整配置参数。