下面以 Debian / Ubuntu 为例,介绍从源码编译 PHP 的基本流程(以 PHP 8.x 为例,其他版本类似)。
sudo apt update
sudo apt install -y \
build-essential \
libxml2-dev \
libsqlite3-dev \
libcurl4-openssl-dev \
libssl-dev \
libonig-dev \
libzip-dev \
pkg-config \
autoconf \
bison \
re2c \
libreadline-dev
根据你的需求,可能还需要:
libpng-dev、libjpeg-dev(GD)libpq-dev(PostgreSQL)libmongoc-dev(MongoDB)cd /usr/src
sudo wget https://www.php.net/distributions/php-8.3.11.tar.gz
sudo tar -xzf php-8.3.11.tar.gz
cd php-8.3.11
(版本号可替换)
./configure \
--prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/etc \
--enable-cli \
--enable-fpm \
--with-fpm-user=www-data \
--with-fpm-group=www-data \
--with-openssl \
--with-curl \
--with-zip \
--enable-mbstring \
--with-readline \
--with-zlib
--enable-fpm:PHP-FPM--enable-cli:命令行--with-mysqli / --with-pdo-mysql:MySQL--with-pear:PEAR可用:
./configure --help
查看所有参数。
make -j$(nproc)
sudo make install
sudo cp php.ini-production /usr/local/php/etc/php.ini
sudo cp sapi/fpm/php-fpm.conf /usr/local/php/etc/
sudo cp sapi/fpm/www.conf /usr/local/php/etc/php-fpm.d/
/usr/local/php/bin/php -v
sudo ln -s /usr/local/php/bin/php /usr/bin/php
创建 /etc/systemd/system/php-fpm.service:
[Unit]
Description=PHP FastCGI Process Manager
After=network.target
[Service]
ExecStart=/usr/local/php/sbin/php-fpm --nodaemonize
User=www-data
Group=www-data
[Install]
WantedBy=multi-user.target
sudo systemctl enable php-fpm
sudo systemctl start php-fpm
如果你有 特定版本 / 模块需求(如 Swoole、xdebug、静态编译),可以告诉我,我可以给你定制编译方案。