首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 綜合知識

Linux 編譯安裝 PHP 7.0 並安裝 Redis、Swoole 擴展完整教程

     編輯:PHP 綜合知識

前言

本文記錄 CentOS 7 下從源碼編譯安裝 PHP 7.0(FPM 模式),並安裝 Redis、Swoole 擴展的完整流程,適合需要特定版本、自定義編譯參數的生產環境。

1. 系統依賴安裝

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

2. 下載並編譯 PHP 7.0

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

3. 安裝 Redis 擴展(phpredis)

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

4. 安裝 Swoole 擴展

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

5. 驗證安裝

/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

6. 生產環境優化配置

; /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

7. 常見編譯錯誤解決

錯誤原因解決
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 1freetype/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

重要提醒

  • PHP 7.0 已於 2018-12-03 終止安全支持,新項目**強烈建議使用 PHP 8.1+**(積極維護、性能更強、語法現代)。
  • Swoole 5.0+ 僅支援 PHP 8.1+,PHP 7.0 只能用 Swoole 4.8.x(已停止維護)。
  • 生產環境請使用官方倉庫或 Remi 源安裝(yum install php70-php-fpm),避免手動編譯維護負擔。

結語

本文提供了 PHP 7.0 編譯安裝全流程,但**強烈不建議在新項目使用 PHP 7.0** 。請直接部署 PHP 8.2/8.3 + Swoole 5.x,享受 JIT、纖程、原生屬性等現代特性。若維護舊項目,本教程可作參考。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved