下载编译PHP

安装依赖

1
apt-get install gcc g++ libxml2-dev libssl-dev libbz2-dev libjpeg-dev libpng-dev libxpm-dev libfreetype6-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 libmagickcore-dev bison re2c libzip-dev apache2 apache2-dev libzstd-dev

Apache支持

source /etc/apache2/envvars 在apache.conf添加一行支持php AddType application/x-httpd-php .php


下载源码

http://php.net/downloads.php, 解压

1
2
3
wget http://php.net/distributions/php-7.3.13.tar.gz
tar zxf php-7.3.13.tar.gz
cd php-7.3.13/

配置

1
./configure --prefix=/usr/local/php --with-config-file-path=/etc --enable-fpm --with-fpm-user=www-data  --with-fpm-group=www-data --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-jpeg-dir --with-png-dir --with-zlib-dir  --with-freetype-dir --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 --with-pear --enable-opcache --with-apxs2=/usr/bin/apxs --enable-maintainer-zts --enable-intl --with-libzip

编译常见问题

virtual memory exhausted: Cannot allocate memory Makefile:993: recipe for target ‘ext/fileinfo/libmagic/apprentice.lo’ failed make: *** [ext/fileinfo/libmagic/apprentice.lo] Error 增加swap

缺少icu-config wget http://download.icu-project.org/files/icu4c/64.2/icu4c-64_2-src.tgz tar zxf icu4c-64_2-src.tgz cd icu/source ./configure make && make install

configure: error: Cannot find OpenSSL’s libraries 安装libssl-dev find / -name libssl.so ln -s /usr/lib/x86_64-linux-gnu/libssl.so /usr/lib

编译警告:

Config variable ${APACHE_RUN_DIR} is not defined source /etc/apache2/envvars

编译警告:

apache2:DefaultRuntimeDir must be a valid directory, absolute or relative to ServerR

1
2
3
4
5
6
7
8
9
10
source /etc/apache2/envvars

# *** WARNING ***
#
# You have built PHP for Apache's current non-threaded MPM.
# If you change Apache to use a threaded MPM you must reconfigure
# PHP with --enable-maintainer-zts

#在配置时加入 --enable-maintainer-zts 支持线程安全
#https://www.cnblogs.com/sucld/p/4594113.html

编译警告:

configure: error: Please reinstall the libcurl distribution - easy.h should be in /include/curl/

1
2
apt install libcurl4-gnutls-dev
ln -s x86_64-linux-gnu/curl /usr/include/curl

配置没问题后编译安装

1
2
make
make install

根据提示运行

1
/root/php-7.3.13/build/shtool install -c ext/phar/phar.phar /usr/local/php/bin

添加到环境变量

1
2
3
4
5
6
vi /etc/profile
#在末尾追加
PATH=$PATH:/usr/local/php/bin:/usr/local/php/sbin
export PATH
#执行命令使得改动立即生效
source /etc/profile
1
2
3
4
5
#ln -s /usr/local/php/bin/php /usr/local/bin/php
#ln -s /usr/local/php/bin/phar.phar /usr/local/bin/phar
#ln -s /usr/local/php/bin/phpize /usr/local/bin/phpize
#ln -s /usr/local/php/bin/php-config /usr/local/bin/php-config
#ln -s /usr/local/php/sbin/php-fpm /usr/sbin/php-fpm

配置文件和服务

1
2
3
4
5
cp php.ini-production /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
cp sapi/fpm/php-fpm.service /etc/systemd/system/php-fpm.service
systemctl enable php-fpm

php相关模块安装

7.2后的版本自带phar,不需要再另外安装

安装phar

wget http://pear.php.net/go-pear.phar php -d detect_unicode=0 go-pear.phar ln -s /usr/local/php/bin/pear /usr/local/bin/pear ln -s /usr/local/php/bin/pecl /usr/local/bin/pecl 更新协议

1
pecl channel-update pecl.php.net

使用:

1
pecl install XXX

php 安装 imagick模块(nextcloud程序需要)

1
2
3
4
5
6
7
wget ftp://ftp.imagemagick.org/pub/ImageMagick/ImageMagick.tar.gz
tar zxf ImageMagick.tar.gz
cd ImageMagick-*
./configure --prefix=/usr/local/imagemagick --with-php-config=/usr/local/php/bin/php-config
make
make install
pecl install imagick

其他模块

pecl install igbinary pecl install lzf pecl install redis


redis-server

apt install redis-server redis警告

WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128. echo “net.core.somaxconn= 4096” >> /etc/sysctl.conf sysctl -p

mysql数据库相关支持

在php.ini修改 mysqli.default_socket = /var/run/mysqld/mysqld.sock pdo_mysql.default_socket=/var/run/mysqld/mysqld.sock

php配置

php.ini

一般需要扩展

1
2
3
4
5
extension=imagick.so
zend_extension=opcache.so
extension=redis.so
extension=igbinary.so
extension=lzf.so

OPcache 支持

1
2
3
4
5
6
7
opcache.enable=1
opcache.enable_cli=1
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=10000
opcache.memory_consumption=128
opcache.save_comments=1
opcache.revalidate_freq=1

设置getenv(“PATH”)函数

/usr/local/php/etc/php-fpm.d/www.conf

1
2
3
4
5
env[HOSTNAME] = $HOSTNAME
env[PATH] = /usr/local/bin:/usr/bin:/bin
env[TMP] = /tmp
env[TMPDIR] = /tmp
env[TEMP] = /tmp

其他

nextcloud 文件操作显示错位 设置语言为中文