Compile Apache 2.4.x (Centos 7)

最後更新: 2021-10-04



  1. yum 需要的 Package
  2. 下載 Apache Source
  3. 設定 (./configure)
  4. Compile & Install
  5. 系統設定

1. 安裝需要的 Package

# Make sure you have APR and APR-Util already installed on your system.

# 如果不 yum 安裝它們, 就要到 下載, 並放在

# /httpd_source_tree_root/srclib/apr and /httpd_source_tree_root/srclib/apr-util

# ./configure --with-included-apr

yum install apr apr-util apr-devel apr-util-devel

# Perl-Compatible Regular Expressions Library (PCRE)

yum install pcre pcre-devel

# 行 https

yum install openssl-devel

# mod_mime_magic

yum install file-devel -y

# 獲得 htpasswd

yum install httpd-tools -y

2. 下載 Apache Source

mkdir /usr/src/httpd; cd /usr/src/httpd



tar -zxf httpd-${V}.tar.gz

cd httpd-${V}

3. 設定 (./configure)

# 優化

CFLAGS="-march=x86-64 -O3 -pipe"



# worker MPM

./configure --prefix=/opt/httpd \
  --sysconfdir=/etc/httpd \
  --enable-ssl \
  --enable-proxy-fcgi \
  --enable-rewrite \

configure: summary of build options:

    Server Version: 2.4.52
    Install prefix: /opt/httpd
    C compiler:     gcc -std=gnu11
    CFLAGS:         -march=x86-64 -O3 -pipe -pthread
    CPPFLAGS:       -march=x86-64 -O3 -pipe -DLINUX -D_REENTRANT -D_GNU_SOURCE
    C preprocessor: gcc -E

4. Compile & Install

make -j

make install

ln -s /opt/httpd/bin/apachectl /usr/sbin


apachectl -t

Syntax OK

查看有什麼 module

apachectl -M

Loaded Modules:
 core_module (static)
 so_module (static)
 http_module (static)
 mpm_event_module (static)
 authn_file_module (shared)

5. 系統設定

groupadd --gid 48 apache; useradd -u 48 -g 48 apache -d /var/www --shell /sbin/nologin -m

useradd -r apache -d /var/www --shell /sbin/nologin -m

mv /etc/httpd /etc/httpd.bak

mkdir /etc/httpd/vhosts /etc/httpd/conf.d /var/www/html /var/log/httpd -p

mv /etc/httpd.bak/{magic,mime.types} /etc/httpd

chmod 770 /var/log/httpd

ln -s /opt/httpd/modules/ /etc/httpd


# MyApacheConfig
ServerRoot "/etc/httpd"
Listen 80
PidFile /var/run/

DocumentRoot "/var/www/html"

# 不要放到 conf.d 內 !
Include modules.conf

User  apache
Group apache

AddDefaultCharset UTF-8
DirectoryIndex index.php index.html index.htm

EnableMMAP      on
EnableSendfile  on
KeepAlive       Off
Timeout         60

ServerTokens    Prod
ServerSignature Off
HostnameLookups Off
RequestReadTimeout header=20-40,MinRate=500 body=20,MinRate=500

# worker MPM
StartServers             3
MinSpareThreads         75
MaxSpareThreads        200
ThreadsPerChild         25
MaxRequestWorkers      400
MaxConnectionsPerChild 999

Include conf.d/*.conf

# Vhost Settings
IncludeOptional vhosts/*.conf


# User / Group 設定
LoadModule unixd_module modules/
LoadModule authn_file_module modules/
LoadModule authn_core_module modules/
LoadModule authz_host_module modules/
LoadModule authz_user_module modules/
LoadModule authz_groupfile_module modules/
LoadModule authz_core_module modules/
LoadModule access_compat_module modules/
LoadModule auth_basic_module modules/
LoadModule ratelimit_module modules/
LoadModule reqtimeout_module modules/
LoadModule deflate_module modules/
LoadModule filter_module modules/
LoadModule mime_module modules/
LoadModule mime_magic_module modules/
LoadModule log_config_module modules/
LoadModule env_module modules/
LoadModule expires_module modules/
LoadModule headers_module modules/
LoadModule setenvif_module modules/
LoadModule version_module modules/
# PHP 要用佢
LoadModule proxy_module modules/
LoadModule proxy_fcgi_module modules/
# https
LoadModule ssl_module modules/
LoadModule socache_shmcb_module modules/
LoadModule status_module modules/
LoadModule dir_module modules/
LoadModule alias_module modules/
LoadModule rewrite_module modules/
# Disable
#LoadModule autoindex_module modules/


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

ErrorLog "/var/log/httpd/error_log"
CustomLog "/var/log/httpd/access_log" combined


<Files ".ht*">
    Require all denied
<Directory />
    AllowOverride none
    Options -Indexes
    Require all denied
<Directory "/var/www/html">
    Options -Indexes +FollowSymLinks
    AllowOverride None
    Require all granted
#### Addon Settings
<FilesMatch "^\.">
    Order allow,deny
    Deny from all
<DirectoryMatch "^\.|\/\.">
    Order allow,deny
    Deny from all
<FilesMatch "composer\.(json|phar|lock)$">
    deny from all
<Files ~ "\.(ini|bak|old|sql|swp|log|sh|md)$">
    deny from all


TypesConfig   mime.types
MIMEMagicFile magic

AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz
AddType application/javascript .js
AddType text/css .css

AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/javascript


Listen                  443 https
SSLSessionCache         shmcb:/run/httpd/sslcache(512000)
SSLSessionCacheTimeout  300
SSLRandomSeed startup   file:/dev/urandom  256
SSLRandomSeed connect   builtin
SSLCryptoDevice         builtin
SSLCipherSuite          HIGH:MEDIUM:!MD5:!RC4:!3DES
SSLProxyCipherSuite     HIGH:MEDIUM:!MD5:!RC4:!3DES
SSLProtocol             TLSv1.2


/var/log/httpd/*log {
        /bin/systemctl reload httpd.service > /dev/null 2>/dev/null || true


Description=The Apache HTTP Server

ExecStart=/opt/httpd/bin/apachectl -k start
ExecReload=/opt/httpd/bin/apachectl -k graceful
ExecStop=/opt/httpd/bin/apachectl -k graceful-stop


systemctl daemon-reload

systemctl start httpd

systemctl enable httpd


php 有關 Setting



# php80
<FilesMatch ".+\.php$">
        SetHandler application/x-httpd-php
        SetHandler "proxy:fcgi://"
ProxyTimeout 60

chgrp apache /var/opt/remi/php80/lib/php -R

ls -l /var/opt/remi/php80/lib/php

total 0
drwxrwx--- 2 root apache 6 Jan 18 22:25 opcache
drwxrwx--- 2 root apache 6 Jan 18 22:25 session
drwxrwx--- 2 root apache 6 Jan 18 22:25 wsdlcache