内容简介:本文风哥教程参考Linux官方文档、Red Hat Enterprise Linux官方文档、Ansible Automation Platform官方文档、Docker官方文档、Kubernetes官方文档和Podman官方文档等内容,详细介绍了相关技术的配置和使用方法。
本文档详细介绍Apache HTTP服务器的安装、配置和管理方法。
from PG视频:www.itpux.com
Part01-Apache安装
1.1 安装Apache服务
$ sudo dnf install -y httpd
Last metadata expiration check: 0:45:23 ago on Thu 03 Apr 2026 22:00:15 AM CST.
Dependencies resolved.
================================================================================
Package Architecture Version Repository Size
================================================================================
Installing:
httpd x86_64 2.4.53-11.el9 appstream 1.4 M
Installing dependencies:
apr x86_64 1.7.0-11.el9 appstream 123 k
apr-util x86_64 1.6.1-20.el9 appstream 95 k
httpd-filesystem
noarch 2.4.53-11.el9 appstream 15 k
httpd-tools x86_64 2.4.53-11.el9 appstream 81 k
Transaction Summary
================================================================================
Install 5 Packages
Total download size: 1.更多学习教程公众号风哥教程itpux_com7 M
Installed size: 5.1 M
Downloading Packages:
(1/5): apr-1.7.0-11.el9.x86_64.rpm 123 kB/s | 123 kB 00:01
(2/5): apr-util-1.6.1-20.el9.x86_64.rpm 95 kB/s | 95 kB 00:01
(3/5): httpd-2.4.53-11.el9.x86_64.rpm 1.4 MB/s | 1.4 MB 00:01
(4/5): httpd-filesystem-2.4.53-11.el9.noarch.rpm 15 kB/s | 15 kB 00:01
(5/5): httpd-tools-2.4.53-11.el9.x86_64.rpm 81 kB/s | 81 kB 00:01
——————————————————————————–
Total 1.7 MB/s | 1.7 MB 00:01
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Installing : apr-1.7.0-11.el9.更多视频教程www.fgedu.net.cnx86_64 1/5
Installing : apr-util-1.6.1-20.el9.x86_64 2/5
Installing : httpd-tools-2.4.53-11.el9.x86_64 3/5
Installing : httpd-filesystem-2.4.53-11.el9.noarch 4/5
Installing : httpd-2.4.53-11.el9.x86_64 5/5
Running scriptlet: httpd-2.4.53-11.el9.x86_64 5/5
Verifying : apr-1.7.0-11.el9.x86_64 1/5
Verifying : apr-util-1.6.1-20.el9.x86_64 2/5
Verifying : httpd-2.4.53-11.el9.x86_64 3/5
Verifying : httpd-filesystem-2.4.53-11.el9.noarch 4/5
Verifying : httpd-tools-2.4.53-11.el9.x86_64 5/5
Installed:
apr-1.7.0-11.el9.x86_64
apr-util-1.6.1-20.el9.x86_64
httpd-2.4.53-11.el9.x86_64
httpd-filesystem-2.4.53-11.el9.noarch
httpd-tools-2.4.53-11.el9.x86_64
Complete!
# 启动Apache服务
$ sudo systemctl start httpd
# 设置开机自启动
$ sudo systemctl enable httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
# 查看服务状态
$ sudo systemctl status httpd
● httpd.service – The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; preset: disabled)
Active: active (running) since Thu 2026-04-03 22:00:00 CST; 10s ago
Docs: man:httpd.service(8)
Main PID: 12345 (httpd)
Status: “Started, listening on: port 80”
Tasks: 213 (limit: 49152)
Memory: 25.5M
CPU: 100ms
CGroup: /system.slice/httpd.service
├─12345 /usr/sbin/httpd -DFOREGROUND
├─12346 /usr/sbin/httpd -DFOREGROUND
├─12347 /usr/sbin/httpd -DFOREGROUND
├─12348 /usr/sbin/httpd -DFOREGROUND
└─12349 /usr/sbin/httpd -DFOREGROUND
Apr 03 22:00:00 rhel10 systemd[1]: Starting The Apache HTTP Server…
Apr 03 22:00:00 rhel10 httpd[12345]: Server configured, listening on: port 80
Apr 03 22:00:00 rhel10 systemd[1]: Started The Apache HTTP Server.
# 配置防火墙
$ sudo firewall-cmd –permanent –add-service=http
success
$ sudo firewall-cmd –permanent –add-service=https
success
$ sudo firewall-cmd –reload
success
# 测试访问
$ curl http://localhost
Welcome to httpd!
This is
风哥提示:
the学习交流加群风哥微信: itpux-com default welcome page.
Part02-Apache配置文件
2.1 主配置文件
$ cat /etc/httpd/conf/httpd.conf
ServerRoot “/etc/httpd”
Listen 80
Include conf.modules.d/*.conf
User apache
Group apache
ServerAdmin root@localhost
ServerName localhost:80
DocumentRoot “/var/www/html”
DirectoryIndex index.html
ErrorLog “logs/error_log”
LogLevel warn
LogFormat “%h %l %u %t \”%r\” %>s %b” common
CustomLog “logs/access_log” common
# 查看配置文件目录结构
$ tree /etc/httpd/
/etc/httpd/
├── conf
│ ├── httpd.conf
│ └── magic
├── conf.d
│ ├── autoindex.conf
│ ├── README
│ ├── userdir.conf
│ └── welcome.conf
├── conf.modules.d
│ ├── 00-base.conf
│ ├── 00-dav.conf
│ ├── 00-lua.conf
│ ├── 00-mpm.conf
│ ├── 00-optional.conf
│ ├── 00-proxy.conf
│ ├── 00-systemd.conf
│ └── 01-cgi.conf
├── logs -> ../../var/log/httpd
├── modules -> ../../usr/lib64/httpd/modules
├── run -> /run/httpd
└── state -> /var/lib/httpd
# 查看网站根目录
$ ls -l /var/www/html/
total 0
-rw-r–r–. 1 root root 0 Nov 3 2024 index.html
# 创建测试页面
$ echo “
Hello Apache
” | sudo tee /var/www/html/index.html
Hello Apache
# 测试访问
$ curl http://localhost
Hello Apache
Part03-虚拟主机配置
3.1 配置基于域名的虚拟主机
$ sudo mkdir -p /var/www/fgedu.net.cn/publ学习交流加群风哥QQ113257174ic_html
$ sudo mkdir -p /var/www/fgedu.net.cn/logs
# 创建测试页面
$ echo “
Welcome to fgedu.net.cn
” | sudo tee /var/www/fgedu.net.cn/public_html/index.html
# 创建虚拟主机配置文件
$ sudo tee /etc/httpd/conf.d/fgedu.net.cn.conf << EOF
ServerName fgedu.net.cn
ServerAlias www.fgedu.net.cn
ServerAdmin webmaster@fgedu.net.cn
DocumentRoot /var/www/fgedu.net.cn/public_html
ErrorLog /var/www/fgedu.net.cn/logs/error.log
CustomLog /var/www/fgedu.net.cn/logs/access.log combined
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
EOF
# 检查配置语法
$ sudo apachectl configtest
Syntax OK
# 重启Apache服务
$ sudo systemctl restart httpd
# 测试访问
$ curl -H “Host: fgedu.net.cn” http://localhost
Welcome to fgedu.net.cn
# 配置第二个虚拟主机
$ sudo mkdir -p /var/www/test.com/public_html
$ echo “
Welcome to test.com
” | sudo tee /var/www/test.com/public_html/index.html
$ sudo tee /etc/httpd/conf.d/test.com.conf << EOF
ServerName test.com
ServerAlias www.test.com
ServerAdmin webmaster@test.com
DocumentRoot /var/www/test.com/public_html
ErrorLog /var/log/httpd/test.com-error.log
CustomLog /var/log/httpd/test.com-access.log combined
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
EOF
$ sudo systemctl restart httpd
Part04-HTTPS配置
4.1 配置SSL证书
$ sudo dnf install -y mod_ssl
# 生成自签名证书
$ sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
-keyout /etc/pki/tls/private/fgedu.net.cn.key \
-out /etc/pki/tls/certs/fgedu.net.cn.crt \
-subj “/C=CN/ST=Beijing/L=Beijing/O=Example/CN=fgedu.net.cn”
Generating a RSA private key
……………….+++++
…………+++++
writing new private key to ‘/etc/pki/tls/private/fgedu.net.cn.key’
—–
# 配置HTTPS虚拟主机
$ sudo tee /etc/httpd/conf.d/fgedu.net.cn-ssl.conf << EOF
ServerName fgedu.net.cn
ServerAlias www.fgedu.net.cn
ServerAdmin webmaster@fgedu.net.cn
DocumentRoot /var/www/fgedu.net.cn/public_html
SSLEngine on
SSLCertificateFile /etc/pki/tls/certs/fgedu.net.cn.crt
SSLCertificateKeyFile /etc/pki/tls/private/fgedu.net.cn.key
ErrorLog /var/log/httpd/fgedu.net.cn-ssl-error.log
CustomLog /var/log/httpd/fgedu.net.cn-ssl-access.log combined
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
EOF
# 检查配置
$ sudo apachectl configtest
Syntax OK
# 重启服务
$ sudo systemctl restart httpd
# 测试HTTPS访问
$ curl -k https://localhost
Welcome to fgedu.net.cn
# 配置HTTP到HTTPS重定向
$ sudo tee -a /etc/httpd/conf.d/fgedu.net.cn.conf << EOF
RewriteEngine on
RewriteCond %{SERVER_NAME} =fgedu.net.cn [OR]
RewriteCond %{SERVER_NAME} =www.fgedu.net.cn
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
EOF
$ sudo systemctl restart httpd
Part05-Apache性能优化
5.1 性能优化配置
$ sudo httpd -V | grep MPM
AH00558: httpd: Could not reliably determine the server’s fully qualified domain name, using rhel10.fgedu.net.cn. Set the ‘ServerName’ directive globally to suppress this message
Server MPM: event
threaded: yes (fixed thread count)
forked: yes (variable process count)
# 配置MPM参数
$ sudo tee /etc/httpd/conf.modules.d/00-mpm.conf << EOF
LoadModule mpm_event_module modules/mod_mpm_event.so
ServerLimit 250
StartServers 10
MinSpareThreads 75
MaxSpareThreads 250
ThreadLimit 64
ThreadsPerChild 25
MaxRequestWorkers 400
MaxConnectionsPerChild 0
EOF
# 启用压缩
$ sudo tee /etc/httpd/conf.d/compression.conf << EOF
SetOutputFilter DEFLATE
AddOutputFilterByType DEFLATE text/html text/plain text/xml
AddOutputFilterByType DEFLATE text/css text/javascript
AddOutputFilterByType DEFLATE application/javascript application/json
AddOutputFilterByType DEFLATE application/xml application/xhtml+xml
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
DeflateCompressionLevel 6
EOF
# 启用缓存
$ sudo tee /etc/httpd/conf.d/cache.conf << EOF
CacheQuickHandler off
CacheLock on
CacheLockPath /tmp/cachelock
CacheLockMaxAge 5
CacheIgnoreHeaders Set-Cookie
CacheRoot /var/cache/httpd
CacheDirLevels 2
CacheDirLength 1
CacheMaxFileSize 10000000
CacheMinFileSize 1
EOF
# 启用KeepAlive
$ sudo tee -a /etc/httpd/conf/httpd.conf << EOF
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5
EOF
# 重启服务
$ sudo systemctl restart httpd
# 查看Apache状态
$ sudo systemctl status httpd
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; preset: disabled)
Active: active (running) since Thu 2026-04-03 22:10:00 CST; 10s ago
1. 使用虚拟主机管理多个网站
2. 配置HTTPS提高安全性
3. 启用压缩和缓存提高性能
4. 定期检查日志文件
5. 及时更新安全补丁
本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html
