源碼編譯安裝 LAMP 可精確控制版本、模組與優化選項,適合學習原理或特定版本需求。生產環境**強烈建議使用發布版官方倉庫 (dnf/yum/apt) 或官方二進制包**,享受安全更新與依賴管理。本文以 CentOS 7/8 為例,整理標準化編譯流程。
# 關閉 SELinux (編譯/安裝階段臨時關閉,生產建議啟用並配置策略)
setenforce 0
sed -i 's/^SELINUX=enforcing/SELINUX=permissive/' /etc/selinux/config
# 安裝編譯工具鏈與依賴庫
yum groupinstall -y "Development Tools"
yum install -y \
openssl-devel pcre-devel zlib-devel libxml2-devel \
libcurl-devel libjpeg-devel libpng-devel freetype-devel \
libicu-devel oniguruma-devel libzip-devel sqlite-devel \
libxslt-devel gmp-devel libargon2-devel \
ncurses-devel readline-devel libedit-devel \
libaio-devel numactl-libs
cd /usr/local/src
wget https://dlcdn.apache.org/httpd/httpd-2.4.58.tar.gz
wget https://dlcdn.apache.org/apr/apr-1.7.4.tar.gz
wget https://dlcdn.apache.org/apr/apr-util-1.6.3.tar.gz
tar xzf httpd-2.4.58.tar.gz
tar xzf apr-1.7.4.tar.gz
tar xzf apr-util-1.6.3.tar.gz
cd apr-1.7.4
./configure --prefix=/usr/local/apr
make -j$(nproc) && make install
cd ../apr-util-1.6.3
./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
make -j$(nproc) && make install
cd ../httpd-2.4.58
./configure \
--prefix=/usr/local/apache \
--with-apr=/usr/local/apr \
--with-apr-util=/usr/local/apr-util \
--enable-so \
--enable-mods-shared=all \
--enable-ssl \
--enable-rewrite \
--enable-proxy \
--enable-proxy-http \
--enable-proxy-balancer \
--enable-lbmethod-byrequests \
--with-pcre \
--with-zlib \
--enable-mpms-shared=all \
--with-mpm=event
make -j$(nproc)
make install
# 建立系統用戶
useradd -r -s /sbin/nologin -M www
# 編輯 httpd.conf 關鍵項
vi /usr/local/apache/conf/httpd.conf
# User www
# Group www
# ServerName localhost:80
# Include conf/extra/httpd-vhosts.conf
# LoadModule php_module modules/libphp.so (PHP 編譯後添加)
# systemd 服務檔
cat > /etc/systemd/system/httpd.service <<'EOF'
[Unit]
Description=The Apache HTTP Server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/usr/local/apache/logs/httpd.pid
ExecStartPre=/usr/local/apache/bin/apachectl configtest
ExecStart=/usr/local/apache/bin/apachectl start
ExecReload=/usr/local/apache/bin/apachectl graceful
ExecStop=/usr/local/apache/bin/apachectl stop
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable --now httpd
# 防火牆開放
firewall-cmd --permanent --add-service=http --add-service=https
firewall-cmd --reload
強烈建議使用官方 RPM/YUM 倉庫安裝:
# MariaDB (CentOS 內建)
yum install -y mariadb-server mariadb
systemctl enable --now mariadb
mysql_secure_installation
# 或 MySQL 官方倉庫
wget https://dev.mysql.com/get/mysql80-community-release-el7-5.noarch.rpm
yum localinstall -y mysql80-community-release-el7-5.noarch.rpm
yum install -y mysql-community-server
systemctl enable --now mysqld
# 臨時密碼在 /var/log/mysqld.log
mysql_secure_installation
若必須源碼編譯 (cmake) 參考官方文檔,關鍵選項:
cmake . \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_DATADIR=/data/mysql \
-DSYSCONFDIR=/etc \
-DWITH_INNODB_STORAGE_ENGINE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DDEFAULT_CHARSET=utf8mb4 \
-DDEFAULT_COLLATION=utf8mb4_general_ci \
-DWITH_BOOST=boost_1_77_0
cd /usr/local/src
wget https://www.php.net/distributions/php-8.2.18.tar.gz
tar xzf php-8.2.18.tar.gz
cd php-8.2.18
./configure \
--prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/etc \
--with-config-file-scan-dir=/usr/local/php/etc/php.d \
--enable-fpm \
--with-fpm-user=www \
--with-fpm-group=www \
--enable-mbstring \
--enable-intl \
--with-openssl \
--with-zlib \
--with-bz2 \
--with-curl \
--enable-gd \
--with-freetype \
--with-jpeg \
--with-webp \
--enable-bcmath \
--enable-sockets \
--enable-pcntl \
--enable-shmop \
--enable-soap \
--enable-opcache \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-pdo-sqlite \
--with-zip \
--disable-debug \
--disable-rpath \
--enable-inline-optimization \
--with-apxs2=/usr/local/apache/bin/apxs # 若需 Apache 模組模式
make -j$(nproc)
make test
make install
cp php.ini-production /usr/local/php/etc/php.ini
# 編輯關鍵項
# date.timezone = Asia/Taipei
# expose_php = Off
# display_errors = Off
# log_errors = On
# error_log = /var/log/php-fpm/error.log
# opcache.enable=1
# opcache.memory_consumption=128
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
# 編輯 www.conf: listen = 127.0.0.1:9000 或 unix:/run/php-fpm.sock
# systemd 服務
cat > /etc/systemd/system/php-fpm.service <<'EOF'
[Unit]
Description=The PHP FastCGI Process Manager
After=network.target
[Service]
Type=simple
PIDFile=/usr/local/php/var/run/php-fpm.pid
ExecStart=/usr/local/php/sbin/php-fpm --nodaemonize --fpm-config /usr/local/php/etc/php-fpm.conf
ExecReload=/bin/kill -USR2 $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable --now php-fpm
# httpd.conf 確認載入
LoadModule php_module modules/libphp.so
# PHP 8.2 編譯需 --with-apxs2 生成 libphp.so
SetHandler application/x-httpd-php
DirectoryIndex index.php index.html
需將 MPM 切換為 prefork:--with-mpm=prefork 重新編譯 Apache。
# httpd.conf
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
# 虛擬主機配置
SetHandler "proxy:fcgi://127.0.0.1:9000"
# 或 Unix socket: proxy:unix:/run/php-fpm.sock|fcgi://localhost
# 建立測試頁
echo '' > /usr/local/apache/htdocs/info.php
curl https://localhost/info.php | grep -E "PHP Version|Server API|Loaded Configuration File"
# 檢查模組
php -m | grep -E "mysqli|pdo_mysql|gd|opcache|mbstring|bcmath|zip|intl"
httpd -M | grep -E "proxy|rewrite|ssl|mpm"
mysql --version
| 問題 | 解決 |
|---|---|
| configure: error: libxml2 not found | yum install libxml2-devel / apt install libxml2-dev |
| make 失敗: undefined reference to `libiconv_open' | yum install libiconv-devel 或 ./configure --with-iconv=/usr/local/libiconv |
| PHP 啟動警告: Unable to load dynamic library | extension_dir 路徑錯誤、缺少 .so 檔、版本不匹配 |
| Apache 啟動錯誤: AH00558: Could not reliably determine the server's fully qualified domain name | 設定 ServerName localhost:80 |
| MySQL 無法啟動: [ERROR] InnoDB: Operating system error number 13 | datadir 權限錯誤,chown -R mysql:mysql /data/mysql |
源碼編譯 LAMP 過程繁瑣但可控。生產環境優先考慮:**發布版官方套件 (dnf/yum/apt) → 官方二進制包 → 容器映像 (Docker/Podman) → 源碼編譯**。若必須編譯,請將上述流程封裝為 Ansible Role 或 Shell 腳本,實現基礎設施即代碼管理。