编译安装 PHP-FPM 7.2
现在大多数php环境都推荐使用php7版本,本文主要介绍如何安装php-fpm7.2
安装环境准备
1 2 |
sudo yum install epel-release -y sudo yum install autoconf libtool re2c bison libxml2-devel bzip2-devel libcurl-devel libpng-devel libicu-devel gcc-c++ libmcrypt-devel libwebp-devel libjpeg-devel openssl-devel -y |
如果安装过程报错,见问题排查1,执行完成后,重新执行安装命令。
下载并加压源码
1 2 3 |
curl -O -L https://github.com/php/php-src/archive/php-7.2.3.tar.gz tar -zxvf php-7.2.3.tar.gz cd php-src-php-7.2.3 |
编译PHP
首先生成configure文件
1 |
./buildconf --force |
配置PHP安装选项,选择要开启的PHP扩展,例如PDO, FPM, OPCache, GD 库等,可以通过 ./configure --help 检查支持哪些扩展。
这里我们使用最常用的扩展选项。
1 |
./configure --prefix=/usr/local/php --enable-fpm --disable-short-tags --with-openssl --with-pcre-regex --with-pcre-jit --with-zlib --enable-bcmath --with-bz2 --enable-calendar --with-curl --enable-exif --with-gd --enable-intl --enable-mbstring --with-mysqli --enable-pcntl --with-pdo-mysql --enable-soap --enable-sockets --with-xmlrpc --enable-zip --with-webp-dir --with-jpeg-dir --with-png-dir |
除了需要打开的扩展模块外,上面的命令还通过设 --prefix 置了php的安装路径。本地php安装在 /usr/local/php ,可以根据自己的需求修改路径。
编译并安装PHP
1 2 3 |
make clean make sudo make install |
如果make过程报错virtual memory exhausted: Cannot allocate memory make: *** [ext/fileinfo/libmagic/apprentice.lo] Error 1,解决办法见问题2。
配置php-fpm.conf和php.ini
生成php.ini配置文件
1 2 3 |
cp php.ini-production /usr/local/php/lib/php.ini sed -i 's/;date.timezone =/date.timezone = "Asia\/Shanghai"/g' /usr/local/php/lib/php.ini sed -i 's/;cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/g' /usr/local/php/lib/php.ini |
生成php-fpm配置文件
1 2 3 4 |
cd /usr/local/php/etc/ cp php-fpm.conf.default php-fpm.conf cp php-fpm.d/www.conf.default php-fpm.d/www.conf sed -i 's/;pid = run\/php-fpm.pid/pid = run\/php-fpm.pid/g' /usr/local/php/etc/php-fpm.conf |
配置centos7下php-fpm启动脚本
创建启动单元文件 vi /lib/systemd/system/php-fpm.service,加入如下内容
1 2 3 4 5 6 7 8 9 10 11 12 |
[Unit] Description=The PHP 7.2 FastCGI Process Manager After=network.target [Service] Type=simple 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 [Install] WantedBy=multi-user.target |
启用php-fpm服务,设置php-fpm开机自启
1 2 |
systemctl enable php-fpm.service systemctl daemon-reload |
启动php-fpm服务
1 |
systemctl start php-fpm |
查看启动成功
1 2 3 4 5 |
[root@www sbin]# ps -ef|grep php root 9336 1 0 03:11 ? 00:00:00 php-fpm: master process (/usr/local/php/etc/php-fpm.conf) nobody 9337 9336 0 03:11 ? 00:00:00 php-fpm: pool www nobody 9338 9336 0 03:11 ? 00:00:00 php-fpm: pool www root 9349 24287 0 03:11 pts/1 00:00:00 grep --color=auto php |
错误排查
1. Downloading packages:
Delta RPMs disabled because /usr/bin/applydeltarpm not installed.
解决办法:
1 2 |
yum provides '*/applydeltarpm' #查看依赖包的位置 yum -y install deltarpm #安装命令 |
2.php编译过程报错
virtual memory exhausted: Cannot allocate memory make: *** [ext/fileinfo/libmagic/apprentice.lo] Error 1
解决办法:
将 –disable-fileinfo 添加到 ./configure可解决问题。
或者可以扩大swap空间
[root@www php-src-php-7.2.3]# cat /proc/swaps
Filename Type Size Used Priority
/swap file 135164 33632 -1
[root@www php-src-php-7.2.3]# mkdir /opt/images/
[root@www php-src-php-7.2.3]# dd if=/dev/zero of=/opt/images/swap bs=1024 count=2048000
2048000+0 records in
2048000+0 records out
2097152000 bytes (2.1 GB) copied, 4.68408 s, 448 MB/s
[root@www php-src-php-7.2.3]# mkswap /opt/images/swap
Setting up swapspace version 1, size = 2047996 KiB
no label, UUID=e9e677af-b6ce-40d3-aef9-bbea981af4e0
[root@www php-src-php-7.2.3]# swapon /opt/images/swap
swapon: /opt/images/swap: insecure permissions 0644, 0600 suggested.
[root@www php-src-php-7.2.3]# free -h
total used free shared buff/cache available
Mem: 503M 50M 14M 15M 438M 411M
Swap: 2.1G 32M 2.0G
[root@www php-src-php-7.2.3]# cat /proc/swaps
Filename Type Size Used Priority
/swap file 135164 33620 -1
/opt/images/swap file 2047996 0 -2
编译完成可以关闭swap 释放空间,
zm_startup_openssl':
/root/php-src-php-7.2.3/ext/openssl/openssl.c:1434: undefined reference to
OPENSSL_init_ssl'ext/openssl/.libs/openssl.o: In function
zm_info_openssl':
/root/php-src-php-7.2.3/ext/openssl/openssl.c:1588: undefined reference to
OpenSSL_version'ext/openssl/.libs/openssl.o: In function
php_openssl_asn1_time_to_time_t':
...
/root/php-src-php-7.2.3/ext/phar/util.c:1860: undefined reference to
EVP_MD_CTX_new'/root/php-src-php-7.2.3/ext/phar/util.c:1892: undefined reference to `EVP_MD_CTX_free'
collect2: error: ld returned 1 exit status
make: *** [sapi/cli/php] Error 1
mv /usr/lib64/openssl /usr/lib64/openssl.old
mv /usr/lib64/libssl.so /usr/lib64/libssl.so.old
ln -s /usr/local/bin/openssl /usr/bin/openssl
ln -s /usr/local/include/openssl /usr/include/openssl
ln -s /usr/local/lib64/libssl.so /usr/lib64/libssl.so
ln -s /usr/local/lib64/libcrypto.so /usr/lib64/libcrypto.so
echo "/usr/local/lib64" >> /etc/ld.so.conf
ldconfig -v // 建立动态链接