本文記錄 CentOS 7 下從源碼編譯安裝 PHP 7.0(FPM 模式),並安裝 Redis、Swoole 擴展的完整流程,適合需要特定版本、自定義編譯參數的生產環境。
yum install -y gcc gcc-c++ make libxml2-devel openssl-devel curl-devel \
libjpeg-devel libpng-devel freetype-devel libmcrypt-devel \
libxslt-devel libzip-devel sqlite-devel oniguruma-devel \
libicu-devel bzip2-devel readline-devel libedit-devel
cd /usr/local/src
wget -c https://www.php.net/distributions/php-7.0.33.tar.gz # 7.0 最後版本
tar zxvf php-7.0.33.tar.gz
cd php-7.0.33
./configure \
--prefix=/usr/local/php7 \
--with-config-file-path=/usr/local/php7/etc \
--enable-fpm \
--with-fpm-user=www \
--with-fpm-group=www \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-iconv-dir \
--with-freetype-dir=/usr/local/freetype \
--with-jpeg-dir \
--with-png-dir \
--with-zlib \
--with-libxml-dir=/usr \
--enable-xml \
--disable-rpath \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--with-curl \
--enable-mbregex \
--enable-mbstring \
--with-mcrypt \
--enable-ftp \
--with-gd \
--enable-gd-native-ttf \
--with-openssl \
--with-mhash \
--enable-pcntl \
--enable-sockets \
--with-xmlrpc \
--enable-zip \
--enable-soap \
--with-gettext \
--disable-fileinfo \
--enable-opcache \
--with-webp-dir \
--with-xpm-dir \
--enable-gd-jis-conv
# 編譯(利用多核加速)
make -j$(nproc) ZEND_EXTRA_LIBS='-liconv'
make install
# 配置文件
cp php.ini-production /usr/local/php7/etc/php.ini
cp /usr/local/php7/etc/php-fpm.conf.default /usr/local/php7/etc/php-fpm.conf
cp /usr/local/php7/etc/php-fpm.d/www.conf.default /usr/local/php7/etc/php-fpm.d/www.conf
# 系統服務(systemd)
cp sapi/fpm/php-fpm.service /usr/lib/systemd/system/php7-fpm.service
systemctl daemon-reload
systemctl enable php7-fpm
systemctl start php7-fpm
cd /usr/local/src # PHP 7 兼容分支 wget -c https://github.com/phpredis/phpredis/archive/5.3.4.tar.gz -O phpredis-5.3.4.tar.gz tar zxvf phpredis-5.3.4.tar.gz cd phpredis-5.3.4 /usr/local/php7/bin/phpize ./configure --with-php-config=/usr/local/php7/bin/php-config make -j$(nproc) make install # 啟用擴展 echo "extension=redis.so" >> /usr/local/php7/etc/php.ini # 或建立獨立 ini:echo "extension=redis.so" > /usr/local/php7/etc/php.d/redis.ini
cd /usr/local/src
wget -c https://github.com/swoole/swoole-src/archive/v4.8.12.tar.gz -O swoole-4.8.12.tar.gz # 最後支援 PHP 7.0 的版本
tar zxvf swoole-4.8.12.tar.gz
cd swoole-src-4.8.12
/usr/local/php7/bin/phpize
./configure --with-php-config=/usr/local/php7/bin/php-config \
--enable-openssl \
--enable-http2 \
--enable-mysqlnd \
--enable-sockets \
--enable-coroutine
make -j$(nproc)
make install
echo "extension=swoole.so" >> /usr/local/php7/etc/php.ini
/usr/local/php7/bin/php -v
# PHP 7.0.33 (cli) (built: ...)
/usr/local/php7/bin/php -m | grep -E 'redis|swoole'
# redis
# swoole
# 測試 Redis 連接
/usr/local/php7/bin/php -r '$r=new Redis(); $r->connect("127.0.0.1",6379); echo $r->ping();'
# +PONG
# 測試 Swoole 版本
/usr/local/php7/bin/php -r 'echo swoole_version();'
# 4.8.12
; /usr/local/php7/etc/php.ini 關鍵配置 memory_limit = 256M max_execution_time = 300 post_max_size = 50M upload_max_filesize = 50M date.timezone = Asia/Shanghai opcache.enable = 1 opcache.memory_consumption = 256 opcache.interned_strings_buffer = 16 opcache.max_accelerated_files = 20000 opcache.validate_timestamps = 0 ; 生產環境關閉,部署後需重啟 php-fpm 生效 opcache.save_comments = 1 ; /usr/local/php7/etc/php-fpm.d/www.conf pm = dynamic pm.max_children = 100 pm.start_servers = 20 pm.min_spare_servers = 10 pm.max_spare_servers = 30 pm.max_requests = 1000 request_terminate_timeout = 300 rlimit_files = 65535
| 錯誤 | 原因 | 解決 |
|---|---|---|
| configure: error: libxml2 not found | 缺開發包 | yum install libxml2-devel |
| configure: error: Cannot find OpenSSL's <evp.h> | OpenSSL 開發包缺失或路徑 | yum install openssl-devel;或指定 --with-openssl-dir=/usr |
| make: *** [ext/gd/gd.o] Error 1 | freetype/jpeg/png 庫版本不匹配 | 確保安裝 -devel 包;指定 --with-freetype-dir=/usr/local/freetype |
| phpize: command not found | 未安裝 php-devel 或路徑未加入 PATH | 用絕對路徑 /usr/local/php7/bin/phpize |
| Swoole 編譯報錯 openssl/ssl.h not found | 缺 OpenSSL 開發包 | yum install openssl-devel;configure 加 --enable-openssl |
yum install php70-php-fpm),避免手動編譯維護負擔。本文提供了 PHP 7.0 編譯安裝全流程,但**強烈不建議在新項目使用 PHP 7.0** 。請直接部署 PHP 8.2/8.3 + Swoole 5.x,享受 JIT、纖程、原生屬性等現代特性。若維護舊項目,本教程可作參考。