php安装与配置详解
CentOS 7.9
php-fpm 5.6.40
安装php
[root@ecs ~]# cd /usr/local/src && tar -jxvf freetype-2.4.0.tar.bz2 && cd freetype-2.4.0 [root@ecs freetype-2.4.0]# ./configure --prefix=/usr/local/freetype [root@ecs freetype-2.4.0]# make && make install [root@ecs freetype-2.4.0]# yum install -y bzip2 gcc libxml2-devel bzip2-devel curl-devel libjpeg-devel libpng-devel libmcrypt-devel php-gd [root@ecs freetype-2.4.0]# cd /usr/local/src && tar -jxvf php-5.6.40.tar.bz2 && cd php-5.6.40 [root@ecs php-5.6.40]# ./configure --prefix=/usr/local/php \ --enable-fpm \ --with-mcrypt \ --enable-mbstring \ --disable-pdo \ --with-curl \ --disable-debug \ --disable-rpath \ --enable-inline-optimization \ --with-bz2 \ --with-zlib \ --enable-sockets \ --enable-sysvsem \ --enable-sysvshm \ --enable-pcntl \ --enable-mbregex \ --with-mhash \ --enable-zip \ --with-pcre-regex \ --with-mysql \ --with-mysqli \ --with-gd \ --with-jpeg-dir \ --with-freetype-dir=/usr/local/freetype [root@ecs php-5.6.40]# make && make install配置php
[root@ecs ~]# cd /usr/local/php/etc/ [root@ecs etc]# cp php-fpm.conf.default php-fpm.conf [root@ecs etc]# sed -i -e '/^;pid =/c\pid = /usr/local/php/var/run/php-fpm.pid' \ -e '/^;daemonize =/c\daemonize = yes' \ -e '/^user =/c\user = www' \ -e '/^group =/c\group = www' \ -e '/^pm.max_children =/c\pm.max_children = 100' php-fpm.conf开机自启动
[root@ecs ~]# cat > /usr/lib/systemd/system/php-fpm.service <<EOF [Unit] Description=The PHP FastCGI Process Manager After=syslog.target network.target [Service] Type=simple PIDFile=/usr/local/php/var/run/php-fpm.pid EnvironmentFile=/usr/local/php/etc/php-fpm.conf ExecStart=/usr/local/php/sbin/php-fpm --daemonize ExecReload=/bin/kill -USR2 $MAINPID PrivateTmp=true [Install] WantedBy=multi-user.target EOF [root@ecs ~]# systemctl daemon-reload [root@ecs ~]# systemctl enable --now php-fpm [root@ecs ~]# ps aux | grep php-fpm添加到PATH
[root@ecs ~]# echo "export PATH=\$PATH:/usr/local/php/bin" >> /etc/profile [root@ecs ~]# source /etc/profile [root@ecs ~]# php -m
Ubuntu
ubuntu 24.04 php-fpm 5.6.40
缺少依赖的报错提示
依赖 报错 libxml2-dev configure: error: xml2-config not found. Please check your libxml2 installation libbz2-dev configure: error: Please reinstall the BZip2 distribution zlib1g-dev configure: error: Cannot find libz libcurl4-gnutls-dev configure: error: Please reinstall the libcurl distribution libcurl4-gnutls-dev与libcurl4-openssl-dev二选一,安装任意一个则会自动卸载另外一个。
GnuTLS是OpenSSL的一个分支,设计更现代化、更注重安全性和性能libjpeg-dev configure: error: jpeglib.h not found libpng-dev configure: error: png.h not found libmcrypt-dev configure: error: mcrypt.h not found. Please reinstall libmcrypt libfreetype-dev configure: error: freetype-config not found
/usr/local/src/php-5.6.40/ext/gd/gd.c:83:12: fatal error: ft2build.h: No such file or directoryopenssl-1.0.2u configure: error: Cannot find OpenSSL's <evp.h> libltdl-dev /usr/bin/ld: cannot find -lltdl: No such file or directory xutils-dev ../util/domd: 23: makedepend: not found 安装OpenSSL
OpenSSL从1.1.0开始,将EVP_PKEY设为opaque(不透明结构体),不再支持通过
pkey->pkey.dh等方式直接访问其成员。使用高版本的OpenSSL(如Ubuntu 24.04使用apt-get install libssl-dev安装的libssl-dev:amd64 (3.0.13-0ubuntu3.15))则会报错:error: invalid use of incomplete typedef ‘EVP_PKEY’ {aka ‘struct evp_pkey_st’}。root@ecs:~# apt install zlib1g-dev make xutils-dev gcc root@ecs:~# cd /usr/local/src && tar -zxvf openssl-1.0.2u.tar.gz && cd openssl-1.0.2u root@ecs:/usr/local/src/openssl-1.0.2u# ./config --prefix=/usr/local/openssl-1.0.2u -fPIC no-gost shared zlib root@ecs:/usr/local/src/openssl-1.0.2u# make depend && make install root@ecs:~# export LD_LIBRARY_PATH=/usr/local/openssl-1.0.2u/lib:$LD_LIBRARY_PATHmake depend依赖xutils-dev和gcc。shared:生成静态库(libssl.a等)的同时生成动态库(libssl.so等)-fPIC:生成位置无关(Position-Independent Code)的libssl.so。不同程序在调用libssl.so时,会将libssl.so加载到不同的内存地址才可以正常运行。如果不加-fPIC,libssl.so会因加载到相同的内存地址而报错。zlib:启用zlib压缩支持,依赖zlib1g-dev开发包。- 必须在LD_LIBRARY_PATH中指定openssl的lib路径,否则会报错:
error while loading shared libraries: libcrypto.so.1.0.0: cannot open shared object file: No such file or directory。
也可以下载libssl-dev_1.0.2g-1ubuntu4_amd64.deb(依赖libssl)进行安装:
root@ecs:~# apt-get install zlib1g-dev root@ecs:~# wget https://launchpad.net/ubuntu/+archive/primary/+files/libssl1.0.0_1.0.2g-1ubuntu4_amd64.deb root@ecs:~# dpkg -i libssl1.0.0_1.0.2g-1ubuntu4_amd64.deb root@ecs:~# wget https://launchpad.net/ubuntu/+archive/primary/+files/libssl-dev_1.0.2g-1ubuntu4_amd64.deb root@ecs:~# dpkg -i libssl-dev_1.0.2g-1ubuntu4_amd64.deb安装libssl-dev_1.0.2g-1ubuntu4_amd64.deb有时会报错:
dpkg: dependency problems prevent configuration of libssl-dev:amd64: libssl-dev:amd64 depends on zlib1g-dev; however: Package zlib1g-dev is not installed.使用
apt-get install zlib1g-dev安装zlib1g-dev,又会报错:The following packages have unmet dependencies: zlib1g-dev : Depends: zlib1g (= 1:1.3.dfsg-3.1ubuntu2.2) but 1:1.3.dfsg-3.1ubuntu2.1 is to be installed E: Unmet dependencies. Try 'apt --fix-broken install' with no packages (or specify a solution).再使用
apt-get install zlib1g=1:1.3.dfsg-3.1ubuntu2.2升级zlib1g,又会报错:The following packages have unmet dependencies: libssl-dev : Depends: zlib1g-dev but it is not going to be installed Recommends: libssl-doc but it is not going to be installed E: Unmet dependencies. Try 'apt --fix-broken install' with no packages (or specify a solution).升级
zlib1g要依赖libssl-dev,libssl-dev又依赖zlib1g-dev,而安装zlib1g-dev又必须升级zlib1g,死循环了。其实只需要卸载libssl-dev,然后再安装zlib1g-dev,依赖问题交由apt-get解决。root@ecs:~# dpkg -l | grep libssl | awk '{print $2,$3}' libssl-dev:amd64 1.0.2g-1ubuntu4 libssl1.0.0:amd64 1.0.2g-1ubuntu4 libssl3t64:amd64 3.0.13-0ubuntu3.15 root@ecs:~# dpkg -r libssl-dev:amd64安装完libssl1.0.0_1.0.2g-1ubuntu4_amd64.deb之后,
/usr/lib/x86_64-linux-gnu/目录会安装libssl.so(软链到libssl.so.1.0.0)、libssl.so.1.0.0、libcrypto.so(软链到libcrypto.so.1.0.0)和libcrypto.so.1.0.0。使用deb安装libssl-dev,一定要在最后安装,否则在安装libcurl4-gnutls-dev时,会自动升级libssl-dev到3.0.13-0ubuntu3.15。
root@ecs:~# apt-get install binutils root@ecs:~# strings /usr/lib/x86_64-linux-gnu/libssl.so | grep -ioE '[0-9]+\.[0-9]+\.[0-9]+' | sort -V | tail -1 3.0.3 root@ecs:~# dpkg -l | grep libssl | awk '{print $2 $3}' libssl-dev:amd643.0.13-0ubuntu3.15 libssl1.0.0:amd641.0.2g-1ubuntu4 libssl3t64:amd643.0.13-0ubuntu3.15 root@ecs:~# file /usr/lib/x86_64-linux-gnu/libssl.so /usr/lib/x86_64-linux-gnu/libssl.so: symbolic link to libssl.so.3此时编译php 5.6.40,虽然可以找到
evp.h和libssl.so,但libssl.so.3与php 5.6.40是不兼容的,依旧会报错:error: invalid use of incomplete typedef ‘EVP_PKEY’ {aka ‘struct evp_pkey_st’}。其它安装包下载地址:https://launchpad.net/ubuntu/+source/openssl/
查看历史版本:https://changelogs.ubuntu.com/changelogs/pool/main/o/openssl/openssl_3.0.13-0ubuntu3.4/changelog
查看已安装libssl-dev
root@ecs:~# apt-get install apt-show-versions root@ecs:~# apt-show-versions libssl-dev卸载libssl
root@ecs:~# dpkg -l | grep libssl root@ecs:~# dpkg -r libssl-dev:amd64 root@ecs:~# dpkg --purge libssl1.0.0:amd64安装libfreetype-dev
apt-get安装的libfreetype-dev版本为2.13.2+dfsg-1ubuntu0.1,而freetype从2.10.1开始不再安装freetype-config,改用pkg-config来管理CFLAGS和库。
http://changelogs.ubuntu.com/changelogs/pool/main/f/freetype/freetype_2.10.1-2/changelog
* New upstream release (Closes: #901052):
- Avoid dereferencing a NULL pointer (CVE-2018-6942) (Closes: #890450).
- The `freetype-config' script is no longer installed by default
(Closes: #871470, #886461). All packages depending on libfreetype6-dev
should use pkg-config to find the relevant CFLAGS and libraries.使用
pkg-config freetype2 --cflags,php会自动查找freetype2的头文件位置(/usr/include/freetype2)。root@ecs:~# apt-get install libfreetype-dev pkg-config root@ecs:~# cat > /usr/local/bin/freetype-config << EOF #!/bin/bash pkg-config freetype2 "\$@" EOF root@ecs:~# chmod a+x /usr/local/bin/freetype-config root@ecs:~# freetype-config --cflags -I/usr/include/freetype2 -I/usr/include/libpng16 root@ecs:~# freetype-config --libs -lfreetype安装php(完整脚本)
root@ecs:~# apt-get update root@ecs:~# apt install zlib1g-dev make xutils-dev gcc root@ecs:~# cd /usr/local/src && tar -zxvf openssl-1.0.2u.tar.gz && cd openssl-1.0.2u root@ecs:/usr/local/src/openssl-1.0.2u# ./config --prefix=/usr/local/openssl-1.0.2u -fPIC no-gost shared zlib root@ecs:/usr/local/src/openssl-1.0.2u# make depend && make install root@ecs:~# export LD_LIBRARY_PATH=/usr/local/openssl-1.0.2u/lib:$LD_LIBRARY_PATH root@ecs:~# apt-get install pkg-config libfreetype-dev root@ecs:~# cat > /usr/local/bin/freetype-config << EOF #!/bin/bash pkg-config freetype2 "\$@" EOF root@ecs:~# chmod a+x /usr/local/bin/freetype-config root@ecs:~# apt-get install libxml2-dev libbz2-dev libcurl4-gnutls-dev libjpeg-dev libpng-dev libmcrypt-dev libltdl-dev bzip2 root@ecs:~# ln -s /usr/include/x86_64-linux-gnu/curl/ /usr/local/include/ root@ecs:~# cd /usr/local/src && tar -jxvf php-5.6.40.tar.bz2 && cd php-5.6.40 root@ecs:/usr/local/src/php-5.6.40# ./configure --prefix=/usr/local/php \ --enable-fpm \ --with-mcrypt \ --enable-mbstring \ --disable-pdo \ --with-curl \ --disable-debug \ --disable-rpath \ --enable-inline-optimization \ --with-bz2 \ --with-zlib \ --enable-sockets \ --enable-sysvsem \ --enable-sysvshm \ --enable-pcntl \ --enable-mbregex \ --with-mhash \ --enable-zip \ --with-pcre-regex \ --with-mysql \ --with-mysqli \ --with-gd \ --with-jpeg-dir \ --with-freetype-dir \ --with-iconv \ --with-openssl=/usr/local/openssl-1.0.2u root@ecs:/usr/local/src/php-5.6.40# make && make installphp-fpm是独立的,可以搭配nginx使用。--with-iconv使用glibc内置的iconv。--with-openssl如果不指定openssl的头文件路径,则必须安装pkg-config来自动获取include和lib路径。
编译安装openssl时,既可以在--with-openssl后面指定openssl的安装路径,又可以使用export PKG_CONFIG_PATH=/usr/local/openssl-1.0.2u/lib64/pkgconfig:$PKG_CONFIG_PATH,pkg-config会到PKG_CONFIG_PATH配置的目录读取openssl.pc并获取openssl的头文件和库文件路径。
ubuntu 26.04 php-fpm 5.6.40
Ubuntu 26.04的libxml2-dev版本是2.15.2+dfsg-0.1ubuntu0.1,新版libxml2部分函数不再兼容php 5.6.40,因此必须安装低版本的libxml2-dev。
root@ecs:~# wget https://archive.ubuntu.com/ubuntu/pool/main/i/icu/libicu74_74.2-1ubuntu3.1_amd64.deb
root@ecs:~# wget https://archive.ubuntu.com/ubuntu/pool/main/i/icu/icu-devtools_74.2-1ubuntu3.1_amd64.deb
root@ecs:~# wget https://archive.ubuntu.com/ubuntu/pool/main/i/icu/libicu-dev_74.2-1ubuntu3.1_amd64.deb
root@ecs:~# wget https://archive.ubuntu.com/ubuntu/pool/main/libx/libxml2/libxml2_2.9.14+dfsg-1.3ubuntu3.8_amd64.deb
root@ecs:~# wget http://archive.ubuntu.com/ubuntu/pool/main/libx/libxml2/libxml2-dev_2.9.14+dfsg-1.3ubuntu3.8_amd64.deb
root@ecs:~# dpkg -i libicu74_74.2-1ubuntu3.1_amd64.deb
root@ecs:~# dpkg -i icu-devtools_74.2-1ubuntu3.1_amd64.deb
root@ecs:~# dpkg -i libicu-dev_74.2-1ubuntu3.1_amd64.deb
root@ecs:~# dpkg -i libxml2_2.9.14+dfsg-1.3ubuntu3.8_amd64.deb
root@ecs:~# dpkg -i libxml2-dev_2.9.14+dfsg-1.3ubuntu3.8_amd64.deb- libxml2:amd64 depends on libicu74 (>= 74.1-1~);
- libicu-dev:amd64 depends on icu-devtools (>= 74.2-1ubuntu3.1);
其余步骤和ubuntu 24.04 php-fpm 5.6.40完全一致。
php 5.6.40
安装依赖
root@ecs:~# apt-get install libxml2-dev zlib1g-dev libcurl4-gnutls-dev libjpeg-dev libpng-dev libfreetype-dev root@ecs:~# ln -s /usr/include/x86_64-linux-gnu/curl /usr/include/curl root@ecs:~# cd /usr/local/src && tar -zxvf libmcrypt-2.5.8.tar.gz && cd libmcrypt-2.5.8 root@ecs:/usr/local/src/libmcrypt-2.5.8# ./configure --prefix=/usr/local/libmcrypt root@ecs:/usr/local/src/libmcrypt-2.5.8# make && make install root@ecs:/usr/local/src/libmcrypt-2.5.8# cd /usr/local/src && tar -zxvf libiconv-1.14.tar.gz && cd libiconv-1.14 root@ecs:/usr/local/src/libiconv-1.14# ./configure --prefix=/usr/local/libiconv root@ecs:/usr/local/src/libiconv-1.14# make && make install安装php
root@ecs:~# cd /usr/local/src && tar -jxvf php-5.6.40.tar.bz2 && cd php-5.6.40 root@ecs:/usr/local/src/php-5.6.40# apt-get install pkg-config autoconf root@ecs:/usr/local/src/php-5.6.40# sed -i "s/freetype-config/pkg-config/g" ./ext/gd/config.m4 root@ecs:/usr/local/src/php-5.6.40# sed -i "s/FREETYPE2_CONFIG --cflags/FREETYPE2_CONFIG freetype2 --cflags/g" ./ext/gd/config.m4 root@ecs:/usr/local/src/php-5.6.40# sed -i "s/FREETYPE2_CONFIG --libs/FREETYPE2_CONFIG freetype2 --libs/g" ./ext/gd/config.m4 root@ecs:/usr/local/src/php-5.6.40# ./buildconf --force root@ecs:/usr/local/src/php-5.6.40# ./configure --prefix=/usr/local/php \ --with-apxs2=/usr/local/apache/bin/apxs \ --with-mysql \ --with-mysqli \ --with-pdo-mysql \ --enable-calendar \ --enable-exif \ --enable-ftp \ --with-gd \ --with-jpeg-dir \ --with-png-dir \ --with-zlib-dir \ --with-freetype-dir \ --enable-gd-native-ttf \ --enable-mbstring \ --enable-bcmath \ --with-curl \ --with-openssl=/usr/local/openssl \ --enable-soap \ --enable-zip \ --with-config-file-path=/usr/local/php/lib \ --with-mcrypt=/usr/local/libmcrypt \ --with-iconv-dir=/usr/local/libiconv root@ecs:/usr/local/src/php-5.6.40# make && make install注:
- 安装apache时已经安装了openssl,
--with-openssl指定openssl的安装路径/usr/local/openssl。 --with-iconv-dir=指定GNU libiconv的安装路径。- freetype 2.10.1开始不再安装freetype-config,而是使用pkg-config来管理CFLAGS和库。需要修改
./ext/gd/config.m4中freetype-config为pkg-config,否则会报错:configure: error: freetype-config not found.。
apache添加php扩展
root@ecs:~# echo "AddType application/x-httpd-php .php" >> /usr/local/apache/conf/httpd.conf root@ecs:~# systemctl restart httpd root@ecs:~# cat >> /usr/local/apache/htdocs/index.php << EOF <?php phpinfo(); EOF- 安装apache时已经安装了openssl,
配置php
root@ecs:~# cp -f php.ini-development /usr/local/php/lib/php.ini root@ecs:~# sed -i '/^;date.timezone =/cdate.timezone = "Asia/Shanghai"' /usr/local/php/lib/php.ini root@ecs:~# sed -i '/^expose_php = On/cexpose_php = Off' /usr/local/php/lib/php.ini root@ecs:~# sed -i "s/^;session.save_path/session.save_path/" /usr/local/php/lib/php.ini root@ecs:~# chmod -R 777 /tmp
Docker
php 5.6.40
部署容器
[root@ecs ~]# docker run --name php-fpm \ -e TZ=Asia/Shanghai \ -v /data/wwwroot:/data/wwwroot \ -v /tmp:/tmp \ -p 9000:9000 \ --privileged=true --restart=always \ -d php:5.6.40-fpm统一用户
[root@ecs ~]# cat /etc/passwd | grep www [root@ecs ~]# docker exec -it php-fpm /bin/sh # groupadd www # useradd -g www -s /sbin/nologin www # usermod -u 1000 www && groupmod -g 1000 www # sed -i -e '/user =/a \user = www' \ -e '/group =/a \group = www' \ /usr/local/etc/php-fpm.d/www.conf通过ID让宿主机的www用户和php-fpm容器内的www用户识别为同一个用户。
php-fpm进程的默认用户为www-data,而宿主机的网站用户为www,这2个用户不一致则要求给文件或文件夹设置权限时必须设置其他用户(ugo中的o)权限。宿主机的www用户id为1000,容器内www-data用户id为33,如果直接修改www-data的用户id为1000则会报错:
usermod: user www-data is currently used by process 7,而如果关闭php-fpm进程则会自动退出容器,因此可以在容器内新建www组和www用户,并设置php-fpm进程的用户和组为www。安装mysqli扩展
[root@ecs ~]# docker exec -it php-fpm /bin/sh # ln -snf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \ && echo "Asia/Shanghai" > /etc/timezone # cat > /etc/apt/sources.list << EOF deb [trusted=yes] http://mirrors.aliyun.com/debian-archive/debian/ stretch main contrib non-free deb [trusted=yes] http://mirrors.aliyun.com/debian-archive/debian-security/ stretch/updates main contrib non-free EOF # apt-get update \ && apt-get -y --no-install-recommends install \ zlib1g-dev libfreetype6-dev libjpeg62-turbo-dev libpng-dev \ && docker-php-ext-configure gd \ --with-freetype-dir=/usr --with-jpeg-dir=/usr --with-png-dir=/usr \ && docker-php-ext-install -j$(nproc) bcmath gd pcntl opcache \ mysqli pdo pdo_mysqlphp 5.6.40-fpm基于debian stretch(debian 9),目前已经EOL,
/etc/apt/sources.list需要使用Debian过期源地址。
php 7.4
部署容器
[root@ecs ~]# docker run --name php-fpm \ -e TZ=Asia/Shanghai \ -v /data/wwwroot:/data/wwwroot \ -v /tmp:/tmp \ -v /var/run/docker.sock:/var/run/docker.sock \ -v /usr/bin/docker:/usr/bin/docker \ -p 9000:9000 \ --privileged=true --restart=always \ -d maisi/php:7.4-fpm/usr/bin/docker:/usr/bin/docker非必须,但如果php需要调用docker程序,则必须设置docker映射。yum install docker方式安装的docker最高版本为:1.13.1,使用-v /usr/bin/docker:/usr/bin/docker,在容器中执行docker命令会报错:Can't open /etc/sysconfig/docker,必须完全卸载后再安装高版本的docker-ce。
安装mysqli扩展
[root@ecs ~]# docker exec -it php-fpm /bin/sh # ln -snf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \ && echo "Asia/Shanghai" > /etc/timezone # sed -i 's/deb.debian.org/mirrors.aliyun.com/;s/security.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list # apt-get update \ && apt-get -y --no-install-recommends install \ cron rsyslog supervisor \ zlib1g-dev libpng-dev libzip-dev \ && docker-php-ext-configure gd \ && docker-php-ext-install -j$(nproc) bcmath gd pcntl opcache zip \ mysqli pdo pdo_mysql