Apache HTTP Server安装配置-Apache Web服务器安装配置_升级迁移详细过程
1. Apache概述与环境规划
Apache HTTP Server是Apache软件基金会开发的开源Web服务器,是世界上使用最广泛的Web服务器之一。它功能强大、模块丰富、稳定可靠,支持多种操作系统和编程语言。更多学习教程www.fgedu.net.cn
1.1 Apache版本说明
Apache目前主要版本为2.4,本教程以Apache 2.4为例进行详细讲解。
$ httpd -v
Server version: Apache/2.4.58 (Red Hat Enterprise Linux)
Server built: Oct 26 2023 00:00:00
# 查看编译参数
$ httpd -V
Server version: Apache/2.4.58 (Red Hat Enterprise Linux)
Server built: Oct 26 2023 00:00:00
Server’s Module Magic Number: 20120211:124
Server loaded: APR 1.7.0, APR-UTIL 1.6.1
Compiled using: APR 1.7.0, APR-UTIL 1.6.1
Architecture: 64-bit
Server MPM: event
threaded: yes (fixed thread count)
forked: yes (variable process count)
# 检查配置语法
$ httpd -t
Syntax OK
1.2 环境规划
本次安装环境规划如下:
IP地址:192.168.1.51
HTTP端口:80
HTTPS端口:443
安装目录:/etc/httpd
日志目录:/var/log/httpd
配置目录:/etc/httpd/conf.d
网站根目录:/data/httpd/html
Apache版本:2.4.58
MPM模式:event
OpenSSL版本:3.0.7
1.3 Apache核心特性
1. 模块化:丰富的模块支持,可动态加载
2. 虚拟主机:支持基于IP、端口、域名的虚拟主机
3. SSL/TLS:支持HTTPS和SSL证书
4. URL重写:强大的mod_rewrite模块
5. 反向代理:支持负载均衡和代理
6. CGI/FastCGI:支持多种编程语言
7. 认证授权:支持多种认证方式
8. 日志管理:灵活的日志配置
2. 硬件环境要求与检查
在安装Apache之前,需要对服务器硬件环境进行全面检查。学习交流加群风哥微信: itpux-com
2.1 最低硬件要求
CPU:1核心
内存:256MB
磁盘:2GB
推荐配置(生产环境):
CPU:2核心以上
内存:2GB以上
磁盘:20GB以上
高并发配置:
CPU:4核心以上
内存:8GB以上
磁盘:50GB以上(SSD)
2.2 系统环境检查
# cat /etc/redhat-release
Red Hat Enterprise Linux release 8.8 (Ootpa)
# 检查内核版本
# uname -r
4.18.0-477.27.1.el8_8.x86_64
# 检查内存信息
# free -h
total used free shared buff/cache available
Mem: 15Gi 1.0Gi 13Gi 256Mi 1.0Gi 14Gi
Swap: 7Gi 0B 7Gi
# 检查磁盘空间
# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/mapper/vg_system-lv_root 100G 5.0G 95G 5% /
/dev/mapper/vg_data-lv_data 500G 50G 450G 10% /data
2.3 依赖包检查
# rpm -qa | grep -E “httpd|apr|pcre|openssl”
httpd-2.4.58-1.el8.x86_64
apr-1.7.0-12.el8.x86_64
apr-util-1.6.1-9.el8.x86_64
pcre-8.45-1.el8.x86_64
openssl-3.0.7-24.el8.x86_64
# 安装依赖包
# yum install -y httpd httpd-devel apr apr-devel apr-util apr-util-devel pcre pcre-devel openssl openssl-devel
# 输出示例:
Last metadata expiration check: 0:00:00 ago on Sat Apr 4 10:00:00 2026.
Dependencies resolved.
Complete!
3. Apache安装步骤
本节详细介绍Apache 2.4的安装过程。学习交流加群风哥QQ113257174
3.1 创建用户和目录
# 检查用户
# id apache
uid=48(apache) gid=48(apache) 组=48(apache)
# 创建目录
# mkdir -p /data/httpd/{html,ssl,logs}
# mkdir -p /var/log/httpd
# 设置目录权限
# chown -R apache:apache /data/httpd
# chown -R apache:apache /var/log/httpd
3.2 安装Apache
# yum install -y httpd
# 输出示例:
Installed:
httpd-2.4.58-1.el8.x86_64
Complete!
# 方法2:源码编译安装
# cd /usr/local/src
# wget https://dlcdn.apache.org/httpd/httpd-2.4.58.tar.gz
# tar -xzf httpd-2.4.58.tar.gz
# cd httpd-2.4.58
# 配置编译选项
# ./configure \
–prefix=/usr/local/apache2 \
–enable-so \
–enable-ssl \
–enable-rewrite \
–enable-mods-shared=all \
–with-ssl=/usr/local/openssl \
–with-apr=/usr/local/apr \
–with-apr-util=/usr/local/apr-util \
–with-pcre=/usr/local/pcre
# 编译安装
# make -j$(nproc)
# make install
# 验证安装
$ httpd -v
Server version: Apache/2.4.58 (Red Hat Enterprise Linux)
3.3 创建配置文件
# vi /etc/httpd/conf/httpd.conf
ServerRoot “/etc/httpd”
Listen 80
ServerName fgedudb01.fgedu.net.cn:80
User apache
Group apache
ServerAdmin root@fgedu.net.cn
DocumentRoot “/data/httpd/html”
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
DirectoryIndex index.html index.htm
ErrorLog “logs/error_log”
LogLevel warn
LogFormat “%h %l %u %t \”%r\” %>s %b \”%{Referer}i\” \”%{User-Agent}i\”” combined
CustomLog “logs/access_log” combined
# 包含模块配置
Include conf.modules.d/*.conf
# 包含虚拟主机配置
IncludeOptional conf.d/*.conf
# 验证配置
$ httpd -t
Syntax OK
# 创建测试页面
# echo “Welcome to Apache on fgedudb01.fgedu.net.cn” > /data/httpd/html/index.html
3.4 启动Apache服务
# systemctl start httpd
# 设置开机自启
# systemctl enable httpd
# 检查状态
# systemctl status httpd
# 输出示例:
● httpd.service – The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
Drop-In: /usr/lib/systemd/system/httpd.service.d
└─php-fpm.conf
Active: active (running) since Sat 2026-04-04 10:00:00 CST; 1s ago
Main PID: 12345 (httpd)
Status: “Running, listening on: port 80”
Tasks: 213 (limit: 49134)
Memory: 25.5M
CGroup: /system.slice/httpd.service
├─12345 /usr/sbin/httpd -DFOREGROUND
├─12346 /usr/sbin/httpd -DFOREGROUND
└─12347 /usr/sbin/httpd -DFOREGROUND
# 检查端口
# netstat -tlnp | grep httpd
tcp6 0 0 :::80 :::* LISTEN 12345/httpd
3.5 配置防火墙
# firewall-cmd –permanent –add-service=http
success
# firewall-cmd –permanent –add-service=https
success
# firewall-cmd –reload
success
# 验证安装
$ curl -I http://192.168.1.51
# 输出示例:
HTTP/1.1 200 OK
Date: Sat, 04 Apr 2026 02:00:00 GMT
Server: Apache/2.4.58 (Red Hat Enterprise Linux)
Last-Modified: Sat, 04 Apr 2026 01:00:00 GMT
ETag: “32-5f7b8c1d3c840”
Content-Length: 50
Content-Type: text/html; charset=UTF-8
4. Apache参数配置
Apache参数配置是性能优化的关键步骤,直接影响系统性能。更多学习教程公众号风哥教程itpux_com
4.1 MPM配置
$ httpd -V | grep MPM
Server MPM: event
# 配置MPM(event模式)
# vi /etc/httpd/conf.modules.d/00-mpm.conf
LoadModule mpm_event_module modules/mod_mpm_event.so
ServerLimit 1000
StartServers 4
MinSpareThreads 25
MaxSpareThreads 75
ThreadLimit 64
ThreadsPerChild 25
MaxRequestWorkers 1000
MaxConnectionsPerChild 10000
KeepAlive On
KeepAliveTimeout 5
MaxKeepAliveRequests 100
# 配置MPM(worker模式)
# LoadModule mpm_worker_module modules/mod_mpm_worker.so
ServerLimit 1000
StartServers 4
MinSpareThreads 25
MaxSpareThreads 75
ThreadLimit 64
ThreadsPerChild 25
MaxRequestWorkers 1000
MaxConnectionsPerChild 10000
# 配置MPM(prefork模式)
# LoadModule mpm_prefork_module modules/mod_mpm_prefork.so
ServerLimit 1000
StartServers 8
MinSpareServers 5
MaxSpareServers 20
MaxRequestWorkers 1000
MaxConnectionsPerChild 10000
# 重载配置
# systemctl reload httpd
4.2 性能优化配置
# vi /etc/httpd/conf.d/performance.conf
# 启用压缩
LoadModule deflate_module modules/mod_deflate.so
DeflateCompressionLevel 6
AddOutputFilterByType DEFLATE text/html text/plain text/xml
AddOutputFilterByType DEFLATE text/css text/javascript
AddOutputFilterByType DEFLATE application/json application/javascript
DeflateFilterNote Input instream
DeflateFilterNote Output outstream
DeflateFilterNote Ratio ratio
# 启用缓存
LoadModule cache_module modules/mod_cache.so
LoadModule cache_disk_module modules/mod_cache_disk.so
CacheQuickHandler on
CacheLock on
CacheLockPath /tmp/cachelock
CacheLockMaxAge 5
CacheIgnoreHeaders Set-Cookie
CacheRoot /var/cache/httpd
CacheDirLevels 2
CacheDirLength 1
CacheMaxFileSize 10000000
CacheMinFileSize 1
# 启用过期控制
LoadModule expires_module modules/mod_expires.so
ExpiresActive On
ExpiresByType text/html “access plus 1 hour”
ExpiresByType text/css “access plus 7 days”
ExpiresByType text/javascript “access plus 7 days”
ExpiresByType image/jpeg “access plus 30 days”
ExpiresByType image/png “access plus 30 days”
ExpiresByType image/gif “access plus 30 days”
# 重载配置
# systemctl reload httpd
5. 虚拟主机配置
Apache支持基于IP、端口和域名的虚拟主机配置,本节介绍常用的配置方法。from:www.itpux.com
5.1 基于域名的虚拟主机
# vi /etc/httpd/conf.d/vhost.conf
NameVirtualHost *:80
ServerName www.fgedu.net.cn
ServerAlias fgedu.net.cn
DocumentRoot /data/httpd/html/fgedu
ServerAdmin webmaster@fgedu.net.cn
ErrorLog /var/log/httpd/fgedu_error.log
CustomLog /var/log/httpd/fgedu_access.log combined
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
ServerName api.fgedu.net.cn
DocumentRoot /data/httpd/html/api
ServerAdmin webmaster@fgedu.net.cn
ErrorLog /var/log/httpd/api_error.log
CustomLog /var/log/httpd/api_access.log combined
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
# 创建网站目录
# mkdir -p /data/httpd/html/fgedu
# echo “Welcome to www.fgedu.net.cn” > /data/httpd/html/fgedu/index.html
# 验证并重载配置
$ httpd -t
Syntax OK
# systemctl reload httpd
5.2 基于端口的虚拟主机
# vi /etc/httpd/conf/httpd.conf
Listen 80
Listen 8080
Listen 9090
# 创建基于端口的虚拟主机
# vi /etc/httpd/conf.d/port.conf
ServerName localhost
DocumentRoot /data/httpd/html/8080
ErrorLog /var/log/httpd/port8080_error.log
CustomLog /var/log/httpd/port8080_access.log combined
ServerName localhost
DocumentRoot /data/httpd/html/9090
ErrorLog /var/log/httpd/port9090_error.log
CustomLog /var/log/httpd/port9090_access.log combined
# 创建网站目录
# mkdir -p /data/httpd/html/8080
# echo “Welcome to port 8080” > /data/httpd/html/8080/index.html
# 重载配置
# systemctl reload httpd
# 测试访问
$ curl http://192.168.1.51:8080
Welcome to port 8080
5.3 HTTPS虚拟主机
# mkdir -p /data/httpd/ssl
# openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
-keyout /data/httpd/ssl/fgedu.key \
-out /data/httpd/ssl/fgedu.crt \
-subj “/C=CN/ST=BJ/L=BJ/O=FGedu/OU=IT/CN=www.fgedu.net.cn”
# 安装SSL模块
# yum install -y mod_ssl
# 配置HTTPS虚拟主机
# vi /etc/httpd/conf.d/ssl.conf
Listen 443 https
ServerName www.fgedu.net.cn
DocumentRoot /data/httpd/html/fgedu
SSLEngine on
SSLCertificateFile /data/httpd/ssl/fgedu.crt
SSLCertificateKeyFile /data/httpd/ssl/fgedu.key
SSLProtocol all -SSLv2 -SSLv3
SSLCipherSuite HIGH:!aNULL:!MD5
ErrorLog /var/log/httpd/ssl_error.log
CustomLog /var/log/httpd/ssl_access.log combined
# HTTP重定向到HTTPS
# vi /etc/httpd/conf.d/redirect.conf
ServerName www.fgedu.net.cn
Redirect permanent / https://www.fgedu.net.cn/
# 重载配置
# systemctl reload httpd
# 测试HTTPS
$ curl -k https://www.fgedu.net.cn
Welcome to www.fgedu.net.cn
6. 反向代理配置
Apache支持强大的反向代理功能,本节介绍常用的配置方法。更多学习教程www.fgedu.net.cn
6.1 基本反向代理
# vi /etc/httpd/conf.modules.d/00-proxy.conf
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
LoadModule proxy_wstunnel_module modules/mod_proxy_wstunnel.so
# 反向代理配置
# vi /etc/httpd/conf.d/proxy.conf
ServerName api.fgedu.net.cn
ProxyPreserveHost On
ProxyPass / http://192.168.1.51:8080/
ProxyPassReverse / http://192.168.1.51:8080/
ErrorLog /var/log/httpd/proxy_error.log
CustomLog /var/log/httpd/proxy_access.log combined
# 重载配置
# systemctl reload httpd
# 测试代理
$ curl http://api.fgedu.net.cn/api/users
6.2 负载均衡配置
# vi /etc/httpd/conf.d/lb.conf
BalancerMember “http://192.168.1.51:8080” loadfactor=3
BalancerMember “http://192.168.1.52:8080” loadfactor=2
BalancerMember “http://192.168.1.53:8080” loadfactor=1
ProxySet lbmethod=byrequests
ProxySet stickysession=JSESSIONID
ServerName lb.fgedu.net.cn
ProxyPreserveHost On
ProxyPass / balancer://fgedu_cluster/
ProxyPassReverse / balancer://fgedu_cluster/
# 健康检查
SetHandler balancer-manager
Require ip 192.168.1.0/24
ErrorLog /var/log/httpd/lb_error.log
CustomLog /var/log/httpd/lb_access.log combined
# 重载配置
# systemctl reload httpd
# 访问负载均衡管理页面
$ curl http://lb.fgedu.net.cn/balancer-manager
6.3 AJP代理配置
# vi /etc/httpd/conf.modules.d/00-proxy.conf
LoadModule proxy_ajp_module modules/mod_proxy_ajp.so
# AJP代理配置
# vi /etc/httpd/conf.d/ajp.conf
ServerName tomcat.fgedu.net.cn
ProxyPreserveHost On
ProxyPass / ajp://192.168.1.51:8009/
ProxyPassReverse / ajp://192.168.1.51:8009/
ErrorLog /var/log/httpd/ajp_error.log
CustomLog /var/log/httpd/ajp_access.log combined
# 重载配置
# systemctl reload httpd
7. 安全配置
Apache安全配置是保护Web服务的重要措施,本节介绍常用的安全配置方法。学习交流加群风哥微信: itpux-com
7.1 基本安全配置
# vi /etc/httpd/conf.d/security.conf
# 隐藏版本号
ServerTokens Prod
ServerSignature Off
# 禁止目录列表
Options -Indexes
# 禁止访问敏感文件
Require all denied
Require all denied
# 安全头部
Header set X-Content-Type-Options “nosniff”
Header set X-Frame-Options “SAMEORIGIN”
Header set X-XSS-Protection “1; mode=block”
Header set Referrer-Policy “strict-origin-when-cross-origin”
# 限制请求方法
Require all denied
# 重载配置
# systemctl reload httpd
7.2 访问控制
# vi /etc/httpd/conf.d/access.conf
Options Indexes FollowSymLinks
AllowOverride None
# 允许指定IP访问
Require ip 192.168.1.0/24
Require ip 10.0.0.0/8
# 基于密码的访问控制
# vi /etc/httpd/conf.d/auth.conf
AuthType Basic
AuthName “Restricted Area”
AuthUserFile /etc/httpd/.htpasswd
Require valid-user
# 创建密码文件
# htpasswd -c /etc/httpd/.htpasswd admin
New password:
Re-type new password:
Adding password for user admin
# 重载配置
# systemctl reload httpd
7.3 mod_security配置
# yum install -y mod_security mod_security_crs
# 启用mod_security
# vi /etc/httpd/conf.d/mod_security.conf
SecRuleEngine On
SecRequestBodyAccess On
SecResponseBodyAccess Off
SecRequestBodyLimit 13107200
SecRequestBodyNoFilesLimit 131072
# OWASP核心规则集
Include /usr/share/mod_modsecurity_crs/*.conf
Include /usr/share/mod_modsecurity_crs/base_rules/*.conf
# 自定义规则
SecRule REQUEST_URI “select.*from” “id:1001,deny,status:403,msg:’SQL Injection Detected'”
SecRule ARGS “
