### apt 安装 php
参考:http://www.tomato.cm/1195.html
### 环境准备
apt update
apt install build-essential
apt-get install \
libxml2-dev \
libssl-dev \
libbz2-dev \
libjpeg-dev \
libpng-dev \
libxpm-dev \
libgmp-dev \
libgmp3-dev \
libmcrypt-dev \
libpspell-dev \
librecode-dev \
libcurl4-gnutls-dev \
libgmp-dev \
libgmp3-dev \
librecode-dev \
libpspell-dev \
libmcrypt-dev \
libreadline-dev \
libtidy-dev \
libxslt1-dev \
libfreetype6-dev \
-y
### 创建相关用户
命令列出所有用户
cat /etc/passwd |cut -f 1 -d:
创建用户组和用户
groupadd www
useradd -g www www
安装编译工具
apt install gcc make
apt install libxml2 libxml2-dev libjpeg-dev libpng-dev libbz2-dev
### 下载 PHP 源码
wget https://www.php.net/distributions/php-7.3.33.tar.gz
tar -xzf php-7.3.33.tar.gz
cd php-7.3.33
### 编译
./configure \
--prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/etc \
--enable-fpm \
--with-fpm-user=www \
--with-fpm-group=www \
--enable-inline-optimization \
--disable-debug \
--disable-rpath \
--enable-shared \
--enable-soap \
--with-libxml-dir \
--with-xmlrpc \
--with-openssl \
--with-mhash \
--with-pcre-regex \
--with-sqlite3 \
--with-zlib \
--enable-bcmath \
--with-iconv \
--with-bz2 \
--enable-calendar \
--with-curl \
--with-cdb \
--enable-dom \
--enable-pcntl \
--enable-exif \
--enable-fileinfo \
--enable-filter \
--with-pcre-dir \
--enable-ftp \
--with-gd \
--with-openssl-dir \
--with-png-dir \
--with-zlib-dir \
--with-freetype-dir=/usr/include/ \
--with-jpeg-dir=/usr/include/ \
--enable-gd-jis-conv \
--with-gettext \
--with-gmp \
--with-mhash \
--enable-json \
--enable-mbstring \
--enable-mbregex \
--enable-mbregex-backtrack \
--with-onig \
--enable-pdo \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-zlib-dir \
--with-pdo-sqlite \
--with-readline \
--enable-session \
--enable-shmop \
--enable-simplexml \
--enable-sockets \
--enable-sysvmsg \
--enable-sysvsem \
--enable-sysvshm \
--enable-wddx \
--with-libxml-dir \
--with-xsl \
--enable-zip \
--enable-mysqlnd-compression-support \
--enable-opcache
make && make install
### 复制配置文件:
cp /usr/local/src/php-7.3.33/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.service 文件,在 /usr/lib/systemd/system
[Unit]
Description=php-fpm
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/php/sbin/php-fpm
ExecStop=/bin/pkill -9 php-fpm
PrivateTmp=true
[Install]
WantedBy=multi-user.target
### 设置开机启动:
systemctl enable php-fpm
### 添加 PHP 到全局
ln -s /usr/local/php/bin/php /usr/local/bin
### Nginx 虚拟主机支持 PHP
可以把 $realpath_root 换成固定路径
server{
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param PHP_VALUE "open_basedir=/var/www/html/:/tmp/:/proc/";
include fastcgi_params;
}
...
}
### Opcache 开启
需要在 php.ini 下,添加 zend_extension="opcache.so"
### 错误解决
错误提示1:configure: error: freetype-config not found.
由于 php-fpm 镜像使用的 libfreetype6 版本为 2.9.1-3 ,版本过新会导致 freetype-config 无法正常使用。
可以选择自行编译低版本的 freetype 2.8.1:
wget http://download.savannah.gnu.org/releases/freetype/freetype-2.8.1.tar.gz
tar zxvf freetype-2.8.1.tar.gz
cd freetype-2.8.1
./configure --prefix=/usr/include
make && make install
apt install -y libfreetype6-dev libjpeg62-turbo-dev libpng-dev
然后再次执行 php 源码的 ./configure,正常通过。
错误提示2: configure: error: Please reinstall the libzip distribution
wget https://libzip.org/download/libzip-1.3.2.tar.gz
tar xvf libzip-1.3.2.tar.gz
cd libzip-1.3.2
./configure
make && make install
错误提示3: make 的时候报错 error while loading shared libraries: libzip.so.5: cannot open shared object file: No such file or directory
参考:https://blog.csdn.net/wanxuexiang/article/details/84574660
在上一个错误提示中,文件安装到了 /usr/local/lib
1. 将该目录加入到共享库的配置文件中
echo "/usr/local/lib" >> /etc/ld.so.conf
ldconfig
临时解决方案
export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
错误提示4:
Generating phar.phar
chmod: cannot access 'ext/phar/phar.phar': No such file or directory
make: [Makefile:468: ext/phar/phar.phar] Error 1 (ignored)
解决方法,在PHP源码目录执行:
cd ext/phar/
cp ./phar.php ./phar.phar
错误提示5:configure: error: Cannot find OpenSSL's libraries
参考:https://blog.csdn.net/u010415258/article/details/78656441
$ find / -name libssl.so
/usr/lib/x86_64-linux-gnu/libssl.so
$ ln -s /usr/lib/x86_64-linux-gnu/libssl.so /usr/lib
错误提示6: error: Please reinstall the libcurl distribution
$ apt-get install curl
$ apt-get install libcurl4-gnutls-dev
$ find / -name libcurl.so
/usr/lib/x86_64-linux-gnu/libcurl.so
$ cd /usr/include
$ sudo ln -s x86_64-linux-gnu/curl
### 备注
mcrypt 在 PHP7.3 中被废弃,pear 在7.4 被默认废弃。都可以不用安装。
修改时间 2023-07-01