主题
Apache服务部署指南
🚀 Apache安装步骤
📋 1. 系统环境准备
在开始安装Apache之前,确保系统满足基本要求:
bash
# 检查系统版本
cat /etc/redhat-release
# 更新系统软件包
yum update -y1
2
3
4
2
3
4
🔧 2. 安装Apache
安装之前先执行:更换源方法
bash
# 安装Apache HTTP服务器
yum install httpd -y
# 查看Apache版本
httpd -v
# 或者
apachectl -v
# 查看安装的包
rpm -qa | grep httpd1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
🚀 3. 启动并配置Apache服务
bash
# 启动Apache服务
systemctl start httpd
# 设置Apache开机自启
systemctl enable httpd
# 检查Apache服务状态
systemctl status httpd
# 检查Apache配置语法
apachectl configtest
# 查看Apache版本和模块
httpd -M1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
🔥 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
# 检查Apache进程
ps aux | grep httpd
# 检查端口监听
netstat -tlnp | grep :80
ss -tlnp | grep :80
# 测试本地访问
curl -I http://localhost
# 获取Apache默认页面
curl http://localhost1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
🚀 Apache配置步骤
📋 6. 基础配置文件结构
Apache的主要配置文件位于 /etc/httpd/ 目录:
bash
# 查看Apache配置目录结构
ls -la /etc/httpd/
# 主要配置文件:
# httpd.conf - 主配置文件
# conf.d/ - 额外配置文件目录
# conf.modules.d/ - 模块配置目录
# logs/ - 日志目录(软链接到/var/log/httpd/)1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
🔧 7. 修改主配置文件
备份并编辑主配置文件:
bash
# 备份原始配置
cp /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf.backup
# 编辑主配置文件
vim /etc/httpd/conf/httpd.conf1
2
3
4
2
3
4
推荐的主配置文件关键配置:
apache
# /etc/httpd/conf/httpd.conf
# 基础服务器配置
ServerRoot "/etc/httpd"
PidFile run/httpd.pid
Timeout 60
KeepAlive On
MaxKeepAliveRequests 500
KeepAliveTimeout 5
# 服务器信息
ServerAdmin root@localhost
ServerName 192.168.4.128:80
# 模块加载
LoadModule authz_core_module modules/mod_authz_core.so
LoadModule authz_host_module modules/mod_authz_host.so
LoadModule log_config_module modules/mod_log_config.so
LoadModule mime_module modules/mod_mime.so
LoadModule dir_module modules/mod_dir.so
LoadModule alias_module modules/mod_alias.so
# 主服务器配置
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>
# 日志配置
ErrorLog "logs/error_log"
CustomLog "logs/access_log" combined
# 默认语言设置
AddLanguage zh-CN .zhcn
LanguagePriority zh-CN en
ForceLanguagePriority Prefer Fallback
# 默认MIME类型
TypesConfig /etc/mime.types
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz
AddType text/html .shtml
AddOutputFilter INCLUDES .shtml1
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
42
43
44
45
46
47
48
49
50
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
42
43
44
45
46
47
48
49
50
🌐 8. 创建虚拟主机配置
创建默认站点配置:
bash
# 创建站点配置目录
mkdir -p /etc/httpd/conf.d
# 创建默认站点配置
vim /etc/httpd/conf.d/default.conf1
2
3
4
2
3
4
默认站点配置内容:
apache
# /etc/httpd/conf.d/default.conf
<VirtualHost *:80>
ServerName 192.168.4.128
ServerAdmin root@localhost
DocumentRoot "/var/www/html"
# 错误和访问日志
ErrorLog "/var/log/httpd/default_error.log"
CustomLog "/var/log/httpd/default_access.log" combined
# 目录权限
<Directory "/var/www/html">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
# 默认首页
DirectoryIndex index.html index.htm index.php
# 错误页面
ErrorDocument 404 /404.html
ErrorDocument 500 /500.html
</VirtualHost>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
🎯 9. 创建自定义网页
创建测试网页:
bash
# 创建网站根目录(如果不存在)
mkdir -p /var/www/html
# 备份原始index.html
mv /var/www/html/index.html /var/www/html/index.html.backup 2>/dev/null || true
# 创建自定义首页
tee /var/www/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>Apache测试页面</title>
<style>
body { font-family: Arial, sans-serif; background: #f5f5f5; margin: 0; padding: 20px; }
.container { max-width: 800px; margin: 0 auto; background: white; padding: 40px; border-radius: 10px; box-shadow: 0 2px 10px rgba(0,0,0,0.1); }
h1 { color: #333; text-align: center; margin-bottom: 30px; }
.status { background: #4CAF50; color: white; padding: 15px; border-radius: 5px; text-align: center; font-size: 18px; }
.info { margin-top: 30px; padding: 20px; background: #f8f9fa; border-left: 4px solid #007bff; }
</style>
</head>
<body>
<div class="container">
<h1>🌟 Apache HTTP服务器</h1>
<div class="status">✅ 服务器运行正常</div>
<div class="info">
<h3>服务器信息</h3>
<p><strong>Web服务器:</strong> Apache HTTP Server</p>
<p><strong>状态:</strong> 正常运行</p>
<p><strong>时间:</strong> <script>document.write(new Date().toLocaleString());</script></p>
</div>
</div>
</body>
</html>
EOF
# 创建错误页面
tee /var/www/html/404.html > /dev/null <<'EOF'
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>404 - 页面未找到</title>
</head>
<body>
<h1>404 - 页面未找到</h1>
<p>抱歉,您访问的页面不存在。</p>
<a href="/">返回首页</a>
</body>
</html>
EOF
# 设置正确的权限
chown -R apache:apache /var/www/html/
chmod -R 755 /var/www/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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
✅ 10. 测试配置并重启服务
bash
# 测试Apache配置语法
apachectl configtest
# 如果显示 Syntax OK,继续
# 重新加载Apache配置
systemctl reload httpd
# 或者重启Apache服务
systemctl restart httpd
# 检查服务状态
systemctl status httpd
# 查看Apache进程
ps aux | grep httpd
# 检查端口监听
netstat -tlnp | grep :801
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
🧪 网页测试方法
🌐 11. 本地测试
在服务器上测试:
bash
# 使用curl测试本地访问
curl -I http://localhost
# 使用curl获取页面内容
curl http://localhost
# 使用wget获取然后在控制台输出前20行
wget -q -O - http://localhost | head -20
# 使用lynx文本浏览器测试(如果已安装)
lynx -dump http://localhost | head -201
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
🌍 12. 远程测试
在虚拟机(服务器)上的命令行输入
bash
ip addr show ens33 # 查看IP地址,记录下服务器IP地址
# 或者使用
ifconfig | grep inet1
2
3
2
3
在Windows宿主机测试:
命令行测试 按下win+R,输入cmd,回车,打开命令行窗口,在命令行输入如下,
bash# 替换YOUR_SERVER_IP为实际服务器IP curl -I 服务器IP地址 # 即上面记录的IP地址1
2浏览器测试 打开电脑浏览器,在浏览器中访问:
http://服务器IP地址1Telnet端口测试
cmdtelnet 服务器IP地址 801
📊 13. 性能和状态监控
bash
# 查看Apache状态(需要安装mod_status)
curl http://localhost/server-status
# 查看服务器信息
curl http://localhost/server-info
# 查看Apache模块
apachectl -M
# 查看Apache编译参数
apachectl -V
# 实时监控访问日志
tail -f /var/log/httpd/access_log
# 实时监控错误日志
tail -f /var/log/httpd/error_log1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
📊 服务状态检查
📋 14. 运行状态检查
bash
# 检查Apache服务状态
systemctl status httpd
systemctl is-active httpd
# 检查Apache进程和资源使用
ps aux | grep httpd
top -p $(pgrep httpd | tr '\n' ',' | sed 's/,$//')
# 检查端口监听
netstat -tlnp | grep :80
lsof -i :80
# 检查日志文件
ls -la /var/log/httpd/
tail -20 /var/log/httpd/error_log
tail -20 /var/log/httpd/access_log
# 检查配置文件语法
apachectl configtest1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
🔧 故障排除
🛠️ 15. 常见问题解决
15.1 服务启动失败
bash
# 检查配置文件语法
apachectl configtest
# 查看详细错误信息
journalctl -u httpd
cat /var/log/httpd/error_log
# 检查端口占用
netstat -tlnp | grep :80
lsof -i :801
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
15.2 无法访问网页
bash
# 检查防火墙设置
systemctl status firewalld
firewall-cmd --list-all
# 检查网络连通性
ping 192.168.4.128
telnet 192.168.4.128 80
# 检查SELinux状态
getenforce
sestatus
# 如果SELinux开启,设置Apache相关布尔值
setsebool -P httpd_can_network_connect on
setsebool -P httpd_can_network_relay on1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
15.3 权限问题
bash
# 检查网站目录权限
ls -la /var/www/html/
# 检查SELinux上下文
ls -Z /var/www/html/
# 修复SELinux上下文
restorecon -R /var/www/html/
# 设置正确的文件权限
chown -R apache:apache /var/www/html/
chmod -R 755 /var/www/html/1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
15.4 虚拟主机不生效
bash
# 检查虚拟主机配置语法
apachectl -S
# 检查配置文件是否包含
grep -r "IncludeOptional" /etc/httpd/conf/httpd.conf
# 确保conf.d目录被包含
ls -la /etc/httpd/conf.d/1
2
3
4
5
6
7
8
2
3
4
5
6
7
8