主题
Nginx服务部署指南
🚀 Nginx安装步骤
📋 1. 系统环境准备
在开始安装Nginx之前,确保系统满足基本要求:
bash
# 检查系统版本
cat /etc/redhat-release
# 更新系统软件包
yum update -y1
2
3
4
2
3
4
🔧 2. 安装Nginx
安装之前先执行:更换源方法
bash
# 安装EPEL仓库
yum install epel-release -y
# 安装Nginx
yum install nginx -y
#如果安装失败,可以尝试使用如下
wget https://nginx.org/packages/centos/7/x86_64/RPMS/nginx-1.26.1-1.el7.ngx.x86_64.rpm
rpm -ivh nginx-1.26.1-1.el7.ngx.x86_64.rpm
# 安装好后,查看Nginx版本
nginx -v1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
🚀 3. 启动并配置Nginx服务
bash
# 启动Nginx服务
systemctl start nginx
# 设置Nginx开机自启
systemctl enable nginx
# 检查Nginx服务状态
systemctl status nginx
# 检查Nginx版本和配置
nginx -v
nginx -t1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
🔥 4. 配置防火墙
bash
# 开放HTTP端口(80)
firewall-cmd --permanent --add-service=http
# 开放HTTPS端口(443)
firewall-cmd --permanent --add-service=https
# 重新加载防火墙配置
firewall-cmd --reload
# 查看防火墙规则
firewall-cmd --list-all1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
或者直接完全关闭防火墙
bash
systemctl stop firewalld
systemctl disable firewalld
setenforce 01
2
3
2
3
✅ 5. 验证安装
bash
# 检查Nginx进程
ps aux | grep nginx
# 检查端口监听
netstat -tlnp | grep :80
# 测试本地访问
curl -I http://localhost
# 获取Nginx默认页面
curl http://localhost1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
🚀 Nginx配置步骤
📋 6. 基础配置文件结构
Nginx的主要配置文件位于 /etc/nginx/ 目录:
bash
# 查看Nginx配置目录结构
ls -la /etc/nginx/
# 主要配置文件:
# nginx.conf - 主配置文件
# conf.d/ - 额外配置文件目录
# sites-available/ - 可用站点配置(Ubuntu/Debian风格)
# sites-enabled/ - 启用站点配置(Ubuntu/Debian风格)1
2
3
4
5
6
7
8
2
3
4
5
6
7
8

🔧 2. 修改主配置文件
备份并编辑主配置文件:
bash
# 备份原始配置
cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.backup
# 编辑主配置文件
vim /etc/nginx/nginx.conf1
2
3
4
2
3
4
推荐的主配置文件内容:
nginx
# /etc/nginx/nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
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;
# 基础性能优化
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# Gzip压缩
gzip on;
gzip_vary on;
gzip_min_length 1024;
gzip_types text/plain text/css text/xml text/javascript
application/javascript application/xml+rss
application/json application/xml;
# 包含站点配置
include /etc/nginx/conf.d/*.conf;
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
🌐 3. 创建虚拟主机配置
创建默认站点配置:
bash
# 创建站点配置目录(如果不存在)
mkdir -p /etc/nginx/conf.d
# 创建默认站点配置
vim /etc/nginx/conf.d/default.conf1
2
3
4
2
3
4
默认站点配置内容:
nginx
# /etc/nginx/conf.d/default.conf
server {
listen 80;
server_name 192.168.38.130 localhost;
# 网站根目录
root /usr/share/nginx/html;
index index.html index.htm index.php;
# 访问日志
access_log /var/log/nginx/default_access.log;
error_log /var/log/nginx/default_error.log;
# 主页面配置
location / {
try_files $uri $uri/ /index.html;
}
# 错误页面
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# 静态文件缓存
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires 7d;
add_header Cache-Control "public, max-age=604800";
}
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
🎯 4. 创建自定义网页
创建测试网页:
bash
# 创建网站根目录(如果不存在)
mkdir -p /usr/share/nginx/html
# 备份原始index.html
mv /usr/share/nginx/html/index.html /usr/share/nginx/html/index.html.backup
# 创建自定义首页
tee /usr/share/nginx/html/index.html > /dev/null <<'EOF'
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Nginx测试页面</title>
</head>
<body>
<div class="container">
<h1>🚀 Nginx服务器</h1>
<div class="status">✅ 运行正常</div>
</div>
</body>
</html>
EOF
# 设置正确的权限
chown -R nginx:nginx /usr/share/nginx/html/
chmod -R 755 /usr/share/nginx/html/1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
✅ 7. 测试配置并重启服务
bash
# 测试Nginx配置语法
nginx -t
# 如果测试通过,重启Nginx服务
systemctl restart nginx
# 检查服务状态
systemctl status nginx
# 查看Nginx进程
ps aux | grep nginx1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
🧪 网页测试方法
🌐 8. 本地测试
在服务器上测试:
bash
# 使用curl测试本地访问
curl -I http://localhost
# 使用curl获取页面内容
curl http://localhost
# 使用wget获取然后在控制台输出前20行
wget -q -O - http://localhost | head -201
2
3
4
5
6
7
8
2
3
4
5
6
7
8
🌍 9. 远程测试
在虚拟机(服务器)上的命令行输入
bash
ifconfig #查看ip地址,注意ens33那条是,记录下服务器IP地址1
在windows宿主机测试:
1.命令行测试 按下win+R,输入cmd,回车,打开命令行窗口,在命令行输入如下,
bash
# 替换YOUR_SERVER_IP为实际服务器IP
curl -I 服务器IP地址 # 即上面记录的ip地址1
2
2
2.打开电脑浏览器,在浏览器中访问, http://服务器IP地址,即上面记录的ip地址,