雖然套件管理器提供便利安裝,但原始碼編譯部署能精確控制版本、優化編譯參數、減少攻擊面,是生產環境定製化部署的必備技能。本文記錄 Apache 2.4、PHP 8.x、MySQL 8.0、phpMyAdmin 完整編譯安裝與整合流程。
/usr/local/apache2、/usr/local/php、/usr/local/mysql
# CentOS/RHEL
yum groupinstall "Development Tools" -y
yum install -y libxml2-devel openssl-devel bzip2-devel libcurl-devel \
libpng-devel libjpeg-devel freetype-devel gmp-devel libzip-devel \
oniguruma-devel libicu-devel sqlite-devel readline-devel \
libaio-devel ncurses-devel cmake
# Ubuntu/Debian
apt-get update && apt-get install -y build-essential libxml2-dev libssl-dev \
libbz2-dev libcurl4-openssl-dev libpng-dev libjpeg-dev libfreetype6-dev \
libgmp-dev libzip-dev libonig-dev libicu-dev libsqlite3-dev \
libreadline-dev libaio-dev libncurses5-dev cmake
cd /usr/local/src
wget https://dlcdn.apache.org/httpd/httpd-2.4.59.tar.gz
wget https://dlcdn.apache.org/httpd/httpd-2.4.59.tar.gz.sha256
sha256sum -c httpd-2.4.59.tar.gz.sha256
tar -zxvf httpd-2.4.59.tar.gz
cd httpd-2.4.59
# 關鍵:下載 APR、APR-util 並放入 srclib 目錄(或使用系統庫)
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 -zxvf apr-1.7.4.tar.gz -C srclib && mv srclib/apr-1.7.4 srclib/apr
tar -zxvf apr-util-1.6.3.tar.gz -C srclib && mv srclib/apr-util-1.6.3 srclib/apr-util
./configure \
--prefix=/usr/local/apache2 \
--enable-modules=all \
--enable-mods-shared=all \
--enable-ssl \
--enable-http2 \
--enable-proxy \
--enable-proxy-fcgi \
--enable-rewrite \
--enable-deflate \
--enable-headers \
--enable-expires \
--with-mpm=event \
--with-included-apr
make -j$(nproc)
make install
# 建立 systemd 服務單元
cat > /etc/systemd/system/httpd.service << 'EOF'
[Unit]
Description=Apache HTTP Server
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/apache2/logs/httpd.pid
ExecStart=/usr/local/apache2/bin/apachectl start
ExecReload=/usr/local/apache2/bin/apachectl graceful
ExecStop=/usr/local/apache2/bin/apachectl stop
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable --now httpd
編輯 /usr/local/apache2/conf/httpd.conf:
ServerName localhost:80DirectoryIndex index.php index.htmlLoadModule mpm_event_module modules/mod_mpm_event.so、LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so、LoadModule rewrite_module modules/mod_rewrite.so、LoadModule deflate_module modules/mod_deflate.so、LoadModule headers_module modules/mod_headers.soFilesMatch 區塊):
SetHandler "proxy:unix:/usr/local/php/var/run/php-fpm.sock|fcgi://localhost"
cd /usr/local/src
wget https://www.php.net/distributions/php-8.3.6.tar.gz
tar -zxvf php-8.3.6.tar.gz
cd php-8.3.6
./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-mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-openssl \
--with-zlib \
--with-bz2 \
--with-curl \
--enable-gd \
--with-freetype \
--with-jpeg \
--enable-mbstring \
--enable-xml \
--enable-bcmath \
--enable-sockets \
--enable-zip \
--enable-soap \
--enable-intl \
--enable-opcache \
--with-pear \
--enable-pcntl \
--enable-ftp \
--with-xsl \
--with-gettext \
--with-gmp \
--with-sodium
make -j$(nproc)
make install
# 配置檔案
cp php.ini-production /usr/local/php/etc/php.ini
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
# 調整 php-fpm 池配置(www.conf)
# listen = /usr/local/php/var/run/php-fpm.sock
# listen.owner = www
# listen.group = www
# pm = dynamic
# pm.max_children = 50
# pm.start_servers = 5
# pm.min_spare_servers = 5
# pm.max_spare_servers = 35
# 建立 www 使用者
useradd -r -s /sbin/nologin www
# systemd 服務
cat > /etc/systemd/system/php-fpm.service << 'EOF'
[Unit]
Description=PHP FastCGI Process Manager
After=network.target
[Service]
Type=notify
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
cd /usr/local/src
wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.36.tar.gz
tar -zxvf mysql-8.0.36.tar.gz
cd mysql-8.0.36
mkdir build && cd build
cmake .. \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_DATADIR=/data/mysql \
-DSYSCONFDIR=/etc \
-DWITH_INNODB_STORAGE_ENGINE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DWITH_FEDERATED_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DENABLED_LOCAL_INFILE=1 \
-DDEFAULT_CHARSET=utf8mb4 \
-DDEFAULT_COLLATION=utf8mb4_general_ci \
-DWITH_SSL=system \
-DWITH_ZLIB=system \
-DWITH_BOOST=../boost_1_83_0
make -j$(nproc)
make install
# 初始化資料目錄
useradd -r -s /sbin/nologin mysql
mkdir -p /data/mysql /var/log/mysql
chown -R mysql:mysql /data/mysql /var/log/mysql /usr/local/mysql
/usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql \
--basedir=/usr/local/mysql --datadir=/data/mysql
# SSL 憑證生成
/usr/local/mysql/bin/mysql_ssl_rsa_setup --datadir=/data/mysql
# systemd 服務
cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /etc/systemd/system/
# 修正 Basedir、Datadir、User、Group
systemctl daemon-reload
systemctl enable --now mysqld
# 設定 root 密碼
/usr/local/mysql/bin/mysqladmin -u root password 'StrongRootPass123!'
cd /usr/local/src
wget https://www.phpmyadmin.net/downloads/phpMyAdmin-latest-all-languages.tar.gz
tar -zxvf phpMyAdmin-latest-all-languages.tar.gz
mv phpMyAdmin-*-all-languages /usr/local/apache2/htdocs/phpmyadmin
# 配置
cd /usr/local/apache2/htdocs/phpmyadmin
cp config.sample.inc.php config.inc.php
# 編輯 config.inc.php
# $cfg['blowfish_secret'] = '32chars_random_string_here';
# $cfg['Servers'][$i]['host'] = 'localhost';
# $cfg['Servers'][$i]['port'] = '3306';
# $cfg['Servers'][$i]['auth_type'] = 'cookie';
# firewalld
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --reload
# SELinux(若啟用)
setsebool -P httpd_can_network_connect 1
setsebool -P httpd_can_network_connect_db 1
semanage fcontext -a -t httpd_sys_content_t "/usr/local/apache2/htdocs(/.*)?"
restorecon -Rv /usr/local/apache2/htdocs
/usr/local/apache2/htdocs/info.php:https://server-ip/info.php 確認 PHP 模組載入https://server-ip/phpmyadmin 登入測試php.ini:opcache.enable=1、opcache.memory_consumption=256、date.timezone=Asia/Taipeimy.cnf:innodb_buffer_pool_size 設為實體記憶體 70%原始碼編譯部署雖耗時,卻能深度理解元件相依關係與效能調優參數。生產環境建議配合 Ansible Playbook 將上述流程自動化,實現不可變基礎設施交付。