最近开发一个新项目,我们团队交流决定采用PHP7作为我们的开发语言。转眼间已经过去了许多年了,PHP的版本也已发生巨大变化,历史的工单系统还有很多其它系统都跑在5.6版本上,要升级估计近似重构了,不管如何稳定才是硬道理。
这是我在2013年写的centos搭建lnmp环境笔记,回到现在依然可用,如果你有兴趣可用移步到这里https://www.markdream.com/technologies/server/centos-install-nmp.shtml
好了,回到正文,今天我们来使用CentOS7.3 x64 系统来源码安装PHP7.4.0,官方下载地址:https://www.php.net/distributions/php-7.4.0.tar.gz (sha256: 004a1a8176176ee1b5c112e73d705977507803f425f9e48cb4a84f42b22abf22)如果你觉得慢可用使用我的CDN来高速下载:http://cdn.markdream.com/ref/sources/lnmp/php-7.4.0.tar.gz (流量省着点用哈)
为了简便起见在接下来的shell命令中我默认以root用户(一般不建议)来执行,如果你使用非root用户请记得加sudo
前期依赖安装
yum install -y openssl-devel libxml2-devel bzip2-devel libcurl-devel libjpeg-devel libpng-devel freetype-devel libmcrypt-devel recode-devel libicu-devel libzip-devel sqlite-devel oniguruma-devel
yum -y install http://mirror.centos.org/centos-7/7.7.1908/cloud/x86_64/openstack-queens/oniguruma-6.7.0-1.el7.x86_64.rpm
yum -y install http://mirror.centos.org/centos-7/7.7.1908/cloud/x86_64/openstack-queens/oniguruma-devel-6.7.0-1.el7.x86_64.rpm
cd /opt/src
wget https://www.php.net/distributions/php-7.4.0.tar.gz
tar zxvf php-7.4.0.tar.gz
cd php-7.4.0
./configure --prefix=/opt/php7 --with-config-file-scan-dir=/opt/php7/etc/conf.d --enable-mbstring --enable-zip --enable-bcmath --enable-pcntl --enable-ftp --enable-xml --enable-shmop --enable-soap --enable-intl --with-openssl --enable-exif --enable-calendar --enable-sysvmsg --enable-sysvsem --enable-sysvshm --enable-opcache --enable-fpm --enable-session --enable-sockets --enable-mbregex --with-curl --with-iconv --with-gd --with-png --with-jpeg --with-freetype --with-zlib-dir=/usr --enable-gd-jis-conv --with-openssl --with-pdo-mysql=mysqlnd --with-gettext=/usr --with-zlib=/usr --with-bz2=/usr --with-recode=/usr --with-xmlrpc --with-mysqli=mysqlnd
make
make install clean
cp /opt/php7/etc/php-fpm.conf.default /opt/php7/etc/php-fpm.conf
cp /opt/php7/etc/php-fpm.d/www.conf.default /opt/php7/etc/php-fpm.d/www.conf
cp php.ini-production /opt/php7/etc/php.ini
cd /opt/src/php-7.4.0/sapi/fpm/
cp init.d.php-fpm /etc/init.d/php7fpm
chmod +x /etc/init.d/php7fpm
service php7fpm start
chkconfig --add php7fpm
#加入开机启动
chkconfig php7fpm on
PHP多版本运行
如果你要和我一样进行多版本运行(主要是没钱…),你需要配置一下这个文件
vi /opt/php7/etc/php-fpm.d/www.conf
#修改用户(大约在23~24行)
...
user = www
group = www
...
#修改运行端口,因为php5.6版本已经用了9000端口,不然你无法运行php7的fpm(大约在36行)
...
listen = 127.0.0.1:9088
...
修改好了就保存退出吧
接下来我们配置nginx让它能够根据我们项目需求来选择使用php的版本,如果你对nginx虚拟目录(多域名/项目)不太了解,可以参见https://www.markdream.com/technologies/server/nginx-config-null-host.shtml
nginx配置文件(PHP5.6)
server { listen 80; server_name p5.gxxyh.cn; error_page 404 /404.html; access_log logs/access.log; error_log logs/error.log; root /home/web/php5site; index index.html index.php; location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name; include fastcgi_params; try_files $uri =404; } location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|mp3|woff|js|css)$ { expires 1y; } }
nginx配置文件(PHP7.4)
server { listen 80; server_name p7.gxxyh.cn; error_page 404 /404.html; access_log logs/access.log; error_log logs/error.log; root /home/web/php7site; index index.html index.php; location ~ \.php$ { fastcgi_pass 127.0.0.1:9088; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name; include fastcgi_params; try_files $uri =404; } location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|mp3|woff|js|css)$ { expires 1y; } }
好了然后重启nginx试一下看看是否可以根据域名对应不同的PHP版本解析了。
安装可能遇到的一些问题
安装zip扩展
//安装libzip
wget http://packages.psychotic.ninja/7/plus/x86_64/RPMS//libzip-0.11.2-6.el7.psychotic.x86_64.rpm
yum localinstall libzip-0.11.2-6.el7.psychotic.x86_64.rpm
//安装libzip-devel
wget http://packages.psychotic.ninja/7/plus/x86_64/RPMS//libzip-devel-0.11.2-6.el7.psychotic.x86_64.rpm
yum localinstall libzip-devel-0.11.2-6.el7.psychotic.x86_64.rpm
《 “CentOS7源码安装PHP7.4.0笔记” 》 有 3 条评论
老师现在还码代码吗?
是的
生命不息,代码不止