1. 首页 > 软件安装教程 > 正文

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为例进行详细讲解。

# 查看Apache版本
$ 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 环境规划

本次安装环境规划如下:

主机名:fgedudb01.fgedu.net.cn
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 创建用户和目录

# Apache默认使用apache用户
# 检查用户
# 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

# 方法1:使用yum安装
# 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服务

# 启动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

风哥提示:Apache安装建议使用yum方式,便于后续升级维护。源码编译安装可以自定义模块和功能。

4. Apache参数配置

Apache参数配置是性能优化的关键步骤,直接影响系统性能。更多学习教程公众号风哥教程itpux_com

4.1 MPM配置

# 查看当前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

生产环境建议:生产环境建议使用event MPM模式,性能最优。根据服务器资源调整MaxRequestWorkers参数。

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虚拟主机

# 生成SSL证书
# 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代理配置

# 启用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

风哥提示:Apache反向代理配置建议启用ProxyPreserveHost保持原始主机头。负载均衡建议使用byrequests算法。

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 访问控制

# IP访问控制
# 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配置

# 安装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 “

生产环境建议:生产环境建议配置安全头部、访问控制和WAF防护,保护Web服务免受攻击。

8. 监控与日志

Apache提供了完善的监控和日志功能,本节介绍常用的监控配置方法。更多学习教程公众号风哥教程itpux_com

8.1 状态监控

# 启用状态模块
# vi /etc/httpd/conf.modules.d/00-status.conf
LoadModule status_module modules/mod_status.so

# 配置状态页面
# vi /etc/httpd/conf.d/status.conf


SetHandler server-status
Require ip 192.168.1.0/24
Require ip 127.0.0.1

ExtendedStatus On


SetHandler server-info
Require ip 192.168.1.0/24

# 重载配置
# systemctl reload httpd

# 访问状态页面
$ curl http://192.168.1.51/server-status?auto

# 输出示例:
Total Accesses: 10000
Total kBytes: 50000
CPULoad: .001234
Uptime: 3600
ReqPerSec: 2.77778
BytesPerSec: 14222.2
BytesPerReq: 5120
BusyWorkers: 5
IdleWorkers: 45

8.2 日志配置

# 日志配置
# vi /etc/httpd/conf/httpd.conf

# 错误日志
ErrorLog “logs/error_log”
LogLevel warn

# 访问日志格式
LogFormat “%h %l %u %t \”%r\” %>s %b \”%{Referer}i\” \”%{User-Agent}i\”” combined
LogFormat “%h %l %u %t \”%r\” %>s %b” common
LogFormat “{\”time\”:\”%{%Y-%m-%dT%H:%M:%S}t\”,\”remote\”:\”%h\”,\”method\”:\”%m\”,\”uri\”:\”%U\”,\”status\”:%>s,\”size\”:%b}” json

CustomLog “logs/access_log” combined

# 虚拟主机独立日志

ServerName www.fgedu.net.cn
DocumentRoot /data/httpd/html/fgedu

ErrorLog /var/log/httpd/fgedu_error.log
CustomLog /var/log/httpd/fgedu_access.log json

# 查看日志
$ tail -f /var/log/httpd/access_log

# 输出示例:
192.168.1.100 – – [04/Apr/2026:10:00:00 +0800] “GET / HTTP/1.1” 200 615 “-” “Mozilla/5.0”

# 日志轮转配置
# vi /etc/logrotate.d/httpd

/var/log/httpd/*log {
daily
missingok
rotate 30
compress
delaycompress
notifempty
create 0640 apache apache
sharedscripts
postrotate
systemctl reload httpd > /dev/null 2>&1 || true
endscript
}

8.3 日志分析

# 统计访问量
$ cat /var/log/httpd/access_log | wc -l
10000

# 统计IP访问量
$ awk ‘{print $1}’ /var/log/httpd/access_log | sort | uniq -c | sort -rn | head -10

# 输出示例:
500 192.168.1.100
300 192.168.1.101
200 192.168.1.102

# 统计HTTP状态码
$ awk ‘{print $9}’ /var/log/httpd/access_log | sort | uniq -c | sort -rn

# 输出示例:
8000 200
1000 304
500 404
200 500

# 统计请求路径
$ awk ‘{print $7}’ /var/log/httpd/access_log | sort | uniq -c | sort -rn | head -10

# 输出示例:
3000 /
1500 /api/users
1000 /api/products

风哥提示:生产环境建议配置JSON格式日志便于分析。定期检查日志发现异常访问,及时调整安全策略。

9. 升级与迁移

Apache升级和迁移是运维工作中的重要环节,需要仔细规划和执行。from:www.itpux.com

9.1 版本升级

# 查看当前版本
$ httpd -v
Server version: Apache/2.4.57 (Red Hat Enterprise Linux)

# 备份配置
# cp -r /etc/httpd /backup/httpd_conf_$(date +%Y%m%d)

# 升级Apache
# yum update httpd

# 输出示例:
Upgraded:
httpd-2.4.58-1.el8.x86_64

Complete!

# 验证版本
$ httpd -v
Server version: Apache/2.4.58 (Red Hat Enterprise Linux)

# 验证配置
$ httpd -t
Syntax OK

# 重启服务
# systemctl restart httpd

9.2 配置迁移

# 备份配置
# tar -czf httpd_backup_$(date +%Y%m%d).tar.gz \
/etc/httpd \
/var/log/httpd \
/data/httpd

# 迁移到新服务器
# scp httpd_backup_*.tar.gz root@newserver:/backup/

# 在新服务器解压
# tar -xzf httpd_backup_*.tar.gz -C /

# 验证配置
$ httpd -t
Syntax OK

# 启动服务
# systemctl start httpd

生产环境建议:升级前必须进行完整备份。Apache配置向后兼容,但建议验证配置后再重启服务。

10. 生产环境实战案例

本节提供一个完整的生产环境配置案例,帮助读者更好地理解Apache的实际应用。更多学习教程www.fgedu.net.cn

10.1 生产环境完整配置

# 生产环境主配置
# vi /etc/httpd/conf/httpd.conf

ServerRoot “/etc/httpd”
Listen 80
Listen 443 https

User apache
Group apache

ServerName fgedudb01.fgedu.net.cn:80
ServerAdmin root@fgedu.net.cn
ServerTokens Prod
ServerSignature Off

DocumentRoot “/data/httpd/html”


Options -Indexes +FollowSymLinks
AllowOverride All
Require all granted

DirectoryIndex index.html index.htm index.php

# MPM配置
Include conf.modules.d/00-mpm.conf


ServerLimit 1000
StartServers 4
MinSpareThreads 25
MaxSpareThreads 75
ThreadsPerChild 25
MaxRequestWorkers 1000
MaxConnectionsPerChild 10000

# 日志配置
ErrorLog “/var/log/httpd/error_log”
LogLevel warn
LogFormat “{\”time\”:\”%{%Y-%m-%dT%H:%M:%S}t\”,\”remote\”:\”%h\”,\”method\”:\”%m\”,\”uri\”:\”%U\”,\”status\”:%>s,\”size\”:%b}” json
CustomLog “/var/log/httpd/access_log” json

# 模块配置
Include conf.modules.d/*.conf
IncludeOptional conf.d/*.conf

10.2 高可用负载均衡

# 高可用负载均衡配置
# vi /etc/httpd/conf.d/ha-lb.conf


BalancerMember “http://192.168.1.51:8080” loadfactor=3 retry=5
BalancerMember “http://192.168.1.52:8080” loadfactor=2 retry=5
BalancerMember “http://192.168.1.53:8080” loadfactor=1 retry=5 status=+H

ProxySet lbmethod=byrequests
ProxySet failonstatus=500,502,503,504
ProxySet failontimeout=on


ServerName lb.fgedu.net.cn

ProxyPreserveHost On
ProxyPass / balancer://ha_cluster/
ProxyPassReverse / balancer://ha_cluster/


SetHandler balancer-manager
Require ip 192.168.1.0/24

ErrorLog /var/log/httpd/ha_lb_error.log
CustomLog /var/log/httpd/ha_lb_access.log json

10.3 性能调优实战

# 操作系统优化
# vi /etc/sysctl.d/99-httpd.conf

net.core.somaxconn = 65535
net.core.netdev_max_backlog = 65535
net.ipv4.tcp_max_syn_backlog = 65535
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_fin_timeout = 30
fs.file-max = 2097152

# 使配置生效
# sysctl -p /etc/sysctl.d/99-httpd.conf

# 用户限制
# vi /etc/security/limits.d/httpd.conf

apache soft nofile 65535
apache hard nofile 65535

# 压力测试
$ ab -n 100000 -c 1000 http://192.168.1.51/

# 输出示例:
Server Software: Apache/2.4.58
Server Hostname: 192.168.1.51
Server Port: 80

Concurrency Level: 1000
Time taken for tests: 10.000 seconds
Complete requests: 100000
Failed requests: 0
Requests per second: 10000.00 [#/sec] (mean)
Time per request: 100.000 [ms] (mean)

风哥提示:Apache作为成熟的Web服务器,功能强大、模块丰富。建议根据业务场景选择合适的MPM模式和模块配置,并配置完善的安全策略。

本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html

联系我们

在线咨询:点击这里给我发消息

微信号:itpux-com

工作日:9:30-18:30,节假日休息