目录
一、Apache 服务的搭建与配置
1. 安装Apache
2. Apache目录介绍
3. Apache 主配置文件
4. 案例-网站没有主页
5. 部署单个网站【不建议】
(1)开启防火墙并添加apache服务
(2) 准备测试页面
(3)一站式雅思学习:AI定制专属首页
6. 部署多个网站
(1)基于域名【推荐】
1)部署第一个网站
2)部署第二个网站
(2) 基于端口【不建议】
7. 更换虚拟主机配置文件的位置
二、Nginx服务搭建与配置
1. nginx安装
2. nginx 目录介绍
3. nginx 配置文件详解
4. Nginx虚拟主机
(1) 基于域名【推荐】
(2)基于端口【不建议】
一、Apache 服务的搭建与配置
Apache HTTP Server(简称Apache)是Apache软件基金会的一个开放源码的网页服务器,Apache是世界使用排名第一的Web服务器软件。它可以运行在几乎所有广泛使用的计算机平台上,由于其跨平台和安全性被广泛使用,是最流行的Web服务器端软件之一。
1. 安装Apache
[root@apache ~]# yum -y install httpd
[root@apache ~]# systemctl enable --now httpd
[root@apache ~]# ps -ef | grep httpd
root 3133 1 0 11:02 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 3134 3133 0 11:02 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 3135 3133 0 11:02 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 3136 3133 0 11:02 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 3137 3133 0 11:02 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 3138 3133 0 11:02 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
root 3220 2728 0 11:04 pts/0 00:00:00 grep --color=auto httpd
浏览器访问测试页面
从客户端浏览器通过IP或域名访问测试
由于当前网站主目录/var/www/html没有内容,默认访问的为欢迎页面/etc/httpd/conf.d/wel***e.conf,建议删除该页面
[root@apache httpd]# rm -rf /etc/httpd/conf.d/wel***e.conf
[root@apache httpd]# systemctl restart httpd
再次浏览器访问测试页面
2. Apache目录介绍
[root@apache httpd]# tree /etc/httpd/
/etc/httpd/
├── conf # 主配置目录 - 核心配置文件存放位置
│ ├── httpd.conf # Apache 主配置文件 - 全局设置、监听端口、基本参数等
│ └── magic # MIME 类型识别辅助文件 - 用于 mod_mime_magic 模块
├── conf.d # 附加配置目录 - 用户自定义和第三方软件配置
│ ├── autoindex.conf # 自动索引配置 - 目录列表显示设置
│ ├── README # 说明文档 - 目录使用说明
│ └── userdir.conf # 用户目录配置 - 用户家目录访问设置(如 ~username)
├── conf.modules.d # 模块配置目录 - 动态模块加载管理
│ ├── 00-base.conf # 基础模块配置 - 核心功能模块
│ ├── 00-dav.conf # WebDAV 模块配置 - 分布式创作和版本控制
│ ├── 00-lua.conf # Lua 模块配置 - Lua 脚本支持
│ ├── 00-mpm.conf # MPM 多处理模块配置 - 处理模型(prefork/worker/event)
│ ├── 00-proxy.conf # 代理模块配置 - 正向/反向代理功能
│ ├── 00-systemd.conf # systemd 集成配置 - 与系统服务管理器集成
│ └── 01-cgi.conf # CGI 配置 - 通用网关接口支持
├── logs -> ../../var/log/httpd # 日志目录符号链接 - 指向实际日志存储位置
├── modules -> ../../usr/lib64/httpd/modules # 模块文件符号链接 - 指向共享库文件位置
└── run -> /run/httpd # 运行时目录符号链接 - 存放 PID 文件等运行时数据
3. Apache 主配置文件
[root@apache httpd]# sed '/^\s*#/d;/^$/d' /etc/httpd/conf/httpd.conf > ~/httpd.conf
[root@apache httpd]# cat ~/httpd.conf
ServerRoot "/etc/httpd" # 指定Apache的根配置目录
Listen 80 # 监听80端口
Include conf.modules.d/*.conf # 加载所有模块配置文件
User apache # 运行Apache进程的用户
Group apache # 运行Apache进程的组
ServerAdmin root@localhost # 管理员邮箱
# 根目录权限设置 - 默认拒绝所有访问
<Directory />
AllowOverride none
Require all denied
</Directory>
DocumentRoot "/var/www/html" # 网站根目录路径
# 网站目录基本权限设置
<Directory "/var/www">
AllowOverride None
Require all granted
</Directory>
# 网站根目录详细设置
<Directory "/var/www/html">
Options Indexes FollowSymLinks # 允许目录索引和符号链接
AllowOverride None
Require all granted
</Directory>
# 默认索引文件设置
<IfModule dir_module>
DirectoryIndex index.html
</IfModule>
# 保护.hta***ess等隐藏文件
<Files ".ht*">
Require all denied
</Files>
# 错误日志设置
ErrorLog "logs/error_log"
LogLevel warn
# 访问日志格式定义
<IfModule log_config_module>
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" ***bined
LogFormat "%h %l %u %t \"%r\" %>s %b" ***mon
CustomLog "logs/a***ess_log" ***bined # 使用***bined格式记录访问日志
</IfModule>
# CGI脚本设置
<IfModule alias_module>
ScriptAlias /cgi-bin/ "/var/www/cgi-bin/" # CGI脚本目录映射
</IfModule>
# MIME类型配置
<IfModule mime_module>
TypesConfig /etc/mime.types # MIME类型配置文件
AddDefaultCharset UTF-8 # 默认字符集
</IfModule>
EnableSendfile on # 启用高效文件传输
IncludeOptional conf.d/*.conf # 加载额外的配置文件
4. 案例-网站没有主页
[root@apache httpd]# ls /var/www/html/
[root@apache httpd]# cp -rf /etc/hosts /var/www/html/
[root@apache httpd]# cp -rf /etc/services /var/www/html/
[root@apache httpd]# ls /var/www/html/
hosts services
查看文件:
如果不希望文件索引:
[root@tianyun ~]# vim /etc/httpd/conf/httpd.conf
<Directory "/var/www/html">
#Options Indexes FollowSymLinks # 将该行注释掉
Options FollowSymLinks # 当没有主页文件时,不支持索引
AllowOverride None
Require all granted
</Directory>
[root@tianyun ~]# systemctl restart httpd
[root@apache httpd]# curl -I http://192.168.159.220/
HTTP/1.1 403 Forbidden
Date: Sat, 27 Sep 2025 03:46:18 GMT
Server: Apache/2.4.6 (CentOS)
Content-Type: text/html; charset=iso-8859-1
5. 部署单个网站【不建议】
(1)开启防火墙并添加apache服务
[root@apache ~]# systemctl enable --now firewalld
[root@apache ~]# firewall-cmd --permanent --add-service=http
su***ess
[root@apache ~]# firewall-cmd --reload
su***ess
[root@apache ~]# firewall-cmd --list-services
dhcpv6-client http ssh
[root@apache ~]# getenforce
Disabled
(2) 准备测试页面
[root@apache ~]# cd /var/www/html/
[root@apache html]# ls
hosts services
[root@apache html]# rm -rf *
[root@apache html]# echo 'Test Apache!' > /var/www/html/index.html
[root@apache html]# ls
index.html
[root@apache html]# cat index.html
Test Apache!
[root@apache html]# curl http://192.168.159.220/
Test Apache!
[root@apache html]# curl -I http://192.168.159.220/
HTTP/1.1 200 OK # 返回的状态码为200
Date: Sat, 27 Sep 2025 04:05:39 GMT
Server: Apache/2.4.6 (CentOS)
Last-Modified: Sat, 27 Sep 2025 04:03:56 GMT
ETag: "d-63fc07dc672e3"
A***ept-Ranges: bytes
Content-Length: 13
Content-Type: text/html; charset=UTF-8
(3)一站式雅思学习:AI定制专属首页
[root@apache html]# vim index.html
[root@apache html]# chown apache:apache /var/www/html/index.html
[root@apache html]# chmod 644 /var/www/html/index.html
[root@apache html]# systemctl restart httpd
6. 部署多个网站
虚拟主机:一台网站服务器上同时部署多个网站
三种技术:
1. 基于域名(多个域名对应同1个IP)
2. 基于端口(每个站点对应不同的端口)
3. 基于IP(每个站点不同的IP)
一个虚拟主机,一个独立的配置文件
(1)基于域名【推荐】
1)部署第一个网站
[root@apache ~]# mkdir -p /apache/penink # 准备每个网站的主目录
[root@apache ~]# echo "This is an apache web" > /apache/penink/index.html
[root@apache apache]# vim /etc/httpd/conf.d/penink.conf # 实际文件中不能出现如下注释,否则报错
<VirtualHost *:80> # 所有IP的80端口
DocumentRoot "/apache/penink" # 网站主目录
ServerName www.penink.*** # 网站名
ServerAlias penink.*** # 网站别名
CustomLog "logs/penink_a***ess_log" ***bined # 访问日志文件
ErrorLog "logs/penink_error_log" # 错误日志文件
</VirtualHost>
<Directory "/apache/penink"> # 设置目录的属性和访问权限
AllowOverride None # None没有任何属性
Require all granted # 授权所有主机访问
</Directory>
# 配置本地主机映射
[root@apache ~]# vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.159.221 www.penink.***
192.168.159.221 penink.***
# 重启apache
[root@apache ~]# systemctl restart httpd
# 验证
[root@apache conf.d]# curl www.penink.***
This is an apache web
[root@apache conf.d]# curl penink.***
This is an apache web
2)部署第二个网站
[root@apache ~]# cd /apache/
[root@apache apache]# mkdir baby
[root@apache apache]# echo 'Wel***e to home, baby!' > baby/index.html
[root@apache apache]# cd /etc/httpd/conf.d/
[root@apache conf.d]# ls
autoindex.conf penink.conf README userdir.conf
[root@apache conf.d]# cp penink.conf baby.conf
[root@apache conf.d]# vim baby.conf
<VirtualHost *:80>
DocumentRoot "/apache/baby"
ServerName www.baby.***
ServerAlias baby.***
CustomLog "logs/baby_a***ess_log" ***bined
ErrorLog "logs/baby_error_log"
</VirtualHost>
<Directory "/apache/baby">
AllowOverride None
Require all granted
</Directory>
# 配置本地主机映射
[root@apache conf.d]# vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.159.221 www.penink.***
192.168.159.221 penink.***
192.168.159.221 www.baby.***
192.168.159.221 baby.***
# 重启apache
[root@apache conf.d]# systemctl restart httpd
# 验证
[root@apache ~]# curl www.baby.***
Wel***e to home, baby!
[root@apache ~]# curl baby.***
Wel***e to home, baby!
后续网站部署步骤类似,不再赘述 !
(2) 基于端口【不建议】
# 第一个网站部署
[root@apache ~]# cd /etc/httpd/conf.d/
[root@apache conf.d]# ls
autoindex.conf baby.conf penink.conf README userdir.conf
# 在“基于域名”的基础上,改变端口号即可
[root@apache conf.d]# vim penink.conf
Listen 81
<VirtualHost *:81>
DocumentRoot "/apache/penink"
ServerName www.penink.***
ServerAlias penink.***
CustomLog "logs/penink_a***ess_log" ***bined
ErrorLog "logs/penink_error_log"
</VirtualHost>
<Directory "/apache/penink">
AllowOverride None
Require all granted
</Direc
[root@apache conf.d]# lsof -i:81
***MAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
httpd 58218 root 6u IPv6 146500 0t0 TCP *:81 (LISTEN)
httpd 58219 apache 6u IPv6 146500 0t0 TCP *:81 (LISTEN)
httpd 58220 apache 6u IPv6 146500 0t0 TCP *:81 (LISTEN)
httpd 58221 apache 6u IPv6 146500 0t0 TCP *:81 (LISTEN)
httpd 58222 apache 6u IPv6 146500 0t0 TCP *:81 (LISTEN)
httpd 58223 apache 6u IPv6 146500 0t0 TCP *:81 (LISTEN)tory>
[root@apache conf.d]# systemctl restart httpd
[root@apache conf.d]# curl 192.168.159.221:81
This is an apache web
# 第二个网站部署
[root@apache conf.d]# vim penink.conf
Listen 82
<VirtualHost *:82>
DocumentRoot "/apache/baby"
ServerName www.baby.***
ServerAlias baby.***
CustomLog "logs/baby_a***ess_log" ***bined
ErrorLog "logs/baby_error_log"
</VirtualHost>
<Directory "/apache/baby">
AllowOverride None
Require all granted
</Directory>
[root@apache conf.d]# systemctl restart httpd
[root@apache conf.d]# ss -tnulp | grep :82
tcp LISTEN 0 128 [::]:82 [::]:* users:(("httpd",pid=58608,fd=6),("httpd",pid=58607,fd=6),("httpd",pid=58606,fd=6),("httpd",pid=58605,fd=6),("httpd",pid=58604,fd=6),("httpd",pid=58603,fd=6))
[root@apache conf.d]#
[root@apache conf.d]# curl 192.168.159.221:82
Wel***e to home, baby!
7. 更换虚拟主机配置文件的位置
[root@apache /]# mkdir /web
[root@apache /]# mv /etc/httpd/conf.d/penink.conf /web/
[root@apache /]# vim /etc/httpd/conf/httpd.conf
...内容省略...
IncludeOptional /web # 将自义定目录包含进来
[root@apache /]# systemctl restart httpd
[root@apache /]# ***stat -tnlp | grep httpd
tcp6 0 0 :::80 :::* LISTEN 60486/httpd
tcp6 0 0 :::81 :::* LISTEN 60486/httpd
tcp6 0 0 :::82 :::* LISTEN 60486/httpd
tcp6 0 0 :::83 :::* LISTEN 60486/httpd
# 验证
[root@apache /]# curl 192.168.159.221:81
This is an apache web
二、Nginx服务搭建与配置
Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,由俄罗斯的程序设计师Igor Sysoev所开发,其特点是占有内存少,并发能力强。事实上nginx的并发能力确实在同类型的网页服务器中表现较好。
1. nginx安装
# 关闭防火墙与selinux
[root@nginx ~]# systemctl disable firewalld
[root@apache ~]# systemctl stop firewalld
[root@nginx ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
安装步骤:https://nginx.org/ ----> documentation ----> Installing nginx ----> Installation on Linux( packages) ----> RHEL and derivatives
一键安装nginx:
[root@nginx ~]# yum list nginx
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.***
* extras: mirrors.aliyun.***
* updates: mirrors.aliyun.***
Available Packages
nginx.x86_64 1:1.20.1-10.el7 epel
[root@nginx ~]# yum install yum-utils
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.***
* extras: mirrors.aliyun.***
* updates: mirrors.aliyun.***
Package yum-utils-1.1.31-54.el7_8.noarch already installed and latest version
Nothing to do
[root@nginx ~]# vim /etc/yum.repos.d/nginx.repo
[root@nginx ~]# yum list nginx
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.***
* extras: mirrors.aliyun.***
* updates: mirrors.aliyun.***
Available Packages
nginx.x86_64 1:1.26.1-2.el7.ngx nginx-stable
[root@nginx ~]# yum install nginx
[root@nginx ~]# systemctl enable --now nginx
[root@nginx ~]# lsof -i:80
***MAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 61342 root 6u IPv4 177221 0t0 TCP *:http (LISTEN)
nginx 61343 nginx 6u IPv4 177221 0t0 TCP *:http (LISTEN)
nginx 61344 nginx 6u IPv4 177221 0t0 TCP *:http (LISTEN)
nginx 61345 nginx 6u IPv4 177221 0t0 TCP *:http (LISTEN)
nginx 61346 nginx 6u IPv4 177221 0t0 TCP *:http (LISTEN)
[root@nginx ~]# curl -I 192.168.159.221
HTTP/1.1 200 OK
Server: nginx/1.26.1
Date: Sat, 27 Sep 2025 12:50:47 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Wed, 29 May 2024 19:07:19 GMT
Connection: keep-alive
ETag: "66577ce7-267"
A***ept-Ranges: bytes
2. nginx 目录介绍
[root@nginx ~]# tree /etc/nginx/
/etc/nginx/
├── conf.d
│ └── default.conf
├── fastcgi_params
├── mime.types
├── modules -> ../../usr/lib64/nginx/modules
├── nginx.conf
├── scgi_params
└── uwsgi_params
2 directories, 6 files
/etc/nginx/
├── conf.d/ # ⚙️ 虚拟主机配置目录(核心配置)
│ └── default.conf # 🖥️ 默认服务器配置文件(可修改为站点专属配置)
├── fastcgi_params # 🐘 FastCGI协议参数文件(PHP/Python后端通信)
├── mime.types # 📄 文件扩展名与MIME类型映射表(如text/css)
├── modules -> ../../usr/lib64/nginx/modules # 🔌 动态模块软链接(.so文件存放处)
├── nginx.conf # 🧠 主配置文件(全局参数、事件模型、HTTP块)
├── scgi_params # 🐍 SCGI协议参数(Python/Ruby传统后端协议)
└── uwsgi_params # 🐍 uWSGI协议参数(Python Django/Flask专用)
🔍 关键文件说明:
nginx.conf - 包含worker进程数、日志路径、include指令等全局设置
conf.d/ - 推荐将不同站点的配置拆分为独立文件(如example.***.conf)
mime.types - 新增类型示例:application/wasm wasm;
3. nginx 配置文件详解
# 主配置文件
[root@nginx ~]# egrep -v '^.*#|^$' /etc/nginx/nginx.conf
# 运行身份设置
user nginx; # 进程运行用户(权限隔离)
worker_processes auto; # 工作进程数(auto=CPU核心数)
error_log /var/log/nginx/error.log notice; # 错误日志路径和级别
pid /var/run/nginx.pid; # 进程ID存储位置
# 连接处理配置
events {
worker_connections 1024; # 单进程最大连接数
}
# HTTP核心配置块
http {
include /etc/nginx/mime.types; # MIME类型映射文件
default_type application/octet-stream; # 默认Content-Type
# 日志格式定义(包含客户端IP、时间、请求信息等)
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
a***ess_log /var/log/nginx/a***ess.log main; # 访问日志路径
sendfile on; # 启用零拷贝传输(性能优化)
keepalive_timeout 65; # 长连接超时时间(秒)
include /etc/nginx/conf.d/*.conf; # 加载虚拟主机配置
}
# 默认服务器配置文件
[root@nginx ~]# grep -Ev '^.*#|^$' /etc/nginx/conf.d/default.conf
server {
listen 80; # 监听80端口
server_name localhost; # 服务器名称,匹配域名
location / { # 处理根路径请求
root /usr/share/nginx/html; # 网站根目录路径
index index.html index.htm; # 默认索引文件顺序
}
error_page 500 502 503 504 /50x.html; # 定义错误页面映射
location = /50x.html { # 精确匹配50x错误页面
root /usr/share/nginx/html; # 错误页面所在目录
}
}
4. Nginx虚拟主机
虚拟主机:一台网站服务器上同时部署多个网站
三种技术:
1. 基于域名(多个域名对应同1个IP)
2. 基于端口(每个站点对应不同的端口)
3. 基于IP(每个站点不同的IP)
一个虚拟主机,一个独立的配置文件
(1) 基于域名【推荐】
# 准备网站的主目录
[root@nginx ~]# mkdir -p /web/hello
[root@nginx ~]# echo "Hello World" > /web/hello/index.html
[root@nginx ~]# cat /web/hello/index.html
Hello World
[root@nginx ~]# tree /web/
/web/
└── hello
└── index.html
1 directory, 1 file
[root@nginx ~]# cd /etc/nginx/conf.d/
[root@nginx conf.d]# ls
default.conf
# 创建网站的配置文件
[root@nginx conf.d]# egrep -v '^.*#|^$' /etc/nginx/conf.d/default.conf > hello.conf
[root@nginx conf.d]# vim hello.conf
server {
listen 80;
server_name www.hello.***;
a***ess_log /var/log/nginx/hello.***_a***ess.log main;
location / {
root /web/hello;
index index.html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
# 配置本地主机映射
[root@nginx ~]# vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.159.221 www.hello.***
[root@nginx conf.d]# systemctl restart nginx
# 测试
[root@nginx conf.d]# curl www.hello.***
Hello World
(2)基于端口【不建议】
[root@nginx web]# pwd
/web
# 准备网站的主目录
[root@nginx web]# mkdir love
[root@nginx web]# echo "爱与被爱都很幸运" > love/index.html
[root@nginx web]# cat love/index.html
爱与被爱都很幸运
[root@nginx web]# tree
.
├── hello
│ └── index.html
└── love
└── index.html
2 directories, 2 files
[root@nginx conf.d]# pwd
/etc/nginx/conf.d
# 创建网站的配置文件
[root@nginx conf.d]# cp hello.conf love.conf
[root@nginx conf.d]# vim love.conf
[root@nginx conf.d]# vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.159.221 www.hello.***
192.168.159.221 love.***
[root@nginx conf.d]# systemctl restart nginx
# 测试
[root@nginx conf.d]# curl love.***:88
爱与被爱都很幸运