php7安装(多个php版本共存)

  • Post author:
  • Post category:php


有两个项目需要的php环境不同,以前是5.5的,现在需要一个7以上的,记录一下过程。

安装依赖包

yum install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel ncurses curl gdbm-devel db4-devel libXpm-devel libX11-devel gd-devel gmp-devel expat-devel xmlrpc-c xmlrpc-c-devel libicu-devel libmcrypt-devel libmemcached-devel

下载源码:


https://www.php.net/distributions/php-7.4.22.tar.gz

解压:

tar -zxvf php-7.4.22.tar.gz
cd php-7.4.22

编译安装:(./configure –help 查看编译参数)

./configure \
--prefix=/usr/local/php7.4 \
--with-config-file-path=/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-mcrypt \
--with-mhash \
--with-pcre-regex \
--with-sqlite3 \
--with-zlib \
--enable-bcmath \
--with-iconv \
--with-bz2 \
--enable-calendar \
--with-curl \
--with-cdb \
--enable-dom \
--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-native-ttf \
--enable-gd-jis-conv \
--with-gettext \
--with-gmp \
--with-mhash \
--enable-json \
--enable-mbstring \
--enable-mbregex \
--enable-mbregex-backtrack \
--with-libmbfl \
--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

编译报错:


configure: error: Package requirements (oniguruma) were not …..

安装oniguruma:

yum install oniguruma oniguruma-devel

重新编译./configure ……

编译完成后,安装:

make && make install

添加环境变量(如果一台机器有多个php版本就可以不用加,用的时候手动启动感觉更方便)

vim /etc/profile

#加入下面两行
PATH=$PATH:/usr/local/php7.4/bin
export PATH

#然后更新环境变量
source /etc/profile

查看php版本:

php -v
#没配环境变量
/usr/local/php7.4/bin/php -v

创建php-fpm配置文件

#多个版本的php保留以前的.ini就行   可以不同版本用不同的php.ini,启动时 加-c ini路径  -y php-fpm.conf路径
cp php.ini-production /etc/php.ini
cp /usr/local/php7.4/etc/php-fpm.conf.default /usr/local/php7.4/etc/php-fpm.conf
cp /usr/local/php7.4/etc/php-fpm.d/www.conf.default /usr/local/php7.4/etc/php-fpm.d/www.conf

复制完配置文件后,如果服务器有多个php版本,需要改一下端口

vim /usr/local/php7.4/etc/php-fpm.d/www.conf
#默认9000改掉
listen = 127.0.0.1:9001

启动php-fpm报错

/usr/local/php7.4/sbin/php-fpm

不同php.ini启动
/usr/local/php7.4/sbin/php-fpm -c /etc/php7.4.ini

报错:

[05-Aug-2021 15:57:37] ERROR: [pool www] cannot get uid for user ‘www’

[05-Aug-2021 15:57:37] ERROR: FPM initialization failed

说明缺少用户,需要添加用户www

groupadd www
useradd -g www www

重新启动php-fpm

/usr/local/php7.4/sbin/php-fpm

查看php-fpm进程

两个php的版本都存在了。php安装到此结束。


在web服务器端要使用哪个版本就在配置文件中指定,比如nginx:

nginx.conf中修改(默认是9000,9001:端口就是刚才7.4版本改的端口):

 location ~ .*\.(php|php7)?$
  {
      root            /www/projectAll/csphp7/;
      fastcgi_pass 127.0.0.1:9001;
      fastcgi_read_timeout 150;
      fastcgi_index index.php;
      include fastcgi.conf;
   }



版权声明:本文为u010533511原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。