Upgrade PHP 7.0 to 7.2 on Centos
Solution 1:
This is solution for CentOS 6.x and 7.x:
yum install epel-release
then install Remi repo, for Centos 6.x:
rpm -Uvh http://rpms.remirepo.net/enterprise/remi-release-6.rpm
and for Centos 7.x:
rpm -Uvh http://rpms.remirepo.net/enterprise/remi-release-7.rpm
check which PHP packages installed:
yum list installed php*
remove current PHP:
yum remove php*
install same packages as were installed for 7.0 using PHP 7.2, for example:
yum install --enablerepo=remi-php72 php php-mcrypt php-cli php-gd php-curl php-mysql php-ldap php-zip php-fileinfo
check version of PHP installed:
php -v
You don't need to PHP 7.0 and 7.2 coexist on Your server. If You got a problem using PHP 7.2 You can always reinstall older PHP 7.0. If not sure it will work for You on production server, try it on virtual machine first. But it worked for me on many production servers. PHP is not critical OS component, so server will never fail if PHP changed. It's just Your web app which may fail when change to 7.2, but as I wrote it's unlikely and You may reinstall older version if it happens.
I forgot about php.ini. If You want to keep Your php.ini customizations in another PHP version, make a copy of php.ini before running yum remove php*. After PHP reinstall diff saved_php.ini php.ini and look what is need to be merged.
Solution 2:
Yes, you can install multiple versions of PHP simultaneously, without having to build anything, only using RPM packages.
This is exactly why Software Collections were designed.
See: PHP Configuration Tips (about switching to FPM and using software collections for recent and/or multiple php versions)
For PHP 7.2 collection, also see the Configuration wizard (and choose Multiple versions)
But, indeed, solution from @NoAngel also works if you think a single version is enough, especially if you have a test environment.
Solution 3:
Yes, you can keep multiple version at the same time. Now you have 7.0 on server so you can download source from php official website and build it to another location.
Also you can use mod_fcgid or php-fpm to execute your PHP web applications with new php version. You can refer below steps to build php from source.
- Download php source archive from php official mirror.
- Extract archive in your server and then change directory to extracted directory.
- Install dependencies required for build
yum install libxml2-devel libcurl-devel libjpeg-devel libpng-devel freetype-devel libicu-devel gcc-c++ libxslt-devel net-snmp-devel readline-devel aspell-devel unixODBC-devel libc-client-devel freetype-devel libvpx-devel enchant-devel libmcrypt-devel krb5-devel libtidy-devel bzip2-devel
- Use below commands to configure and build PHP.
./configure --prefix=/usr/local/php --enable-mbstring --with-curl --with-openssl --with-xmlrpc --enable-soap --enable-zip --with-gd --with-jpeg-dir --with-png-dir --with-mysqli --with-freetype-dir --enable-intl --with-xsl --with-mcrypt --with-zlib --enable-bcmath --enable-dba --enable-calendar --enable-exif --enable-ftp --enable-pcntl --enable-shmop --enable-sockets --enable-sysvmsg --enable-sysvsem --enable-sysvshm --enable-wddx --enable-opcache --with-gettext --with-bz2 --with-mhash --with-readline --with-tidy --with-libdir=lib64
note : you can change prefix if want to change installation directory.
make && make install
It will install PHP binaries and libraries to the prefix directory you mentioned in configure command.
- Now install mod_fcgid module in apache to use new version for php scripts executions
yum install mod_fcgid
create a file with name php-fcgi in cgi-bin directory(possible path is /var/www/cgi-bin), and add below content.
#!/bin/sh
PHPRC=/etc/
export PHPRC
export PHP_FCGI_MAX_REQUESTS=5000
export PHP_FCGI_CHILDREN=0
exec /usr/local/php/bin/php-cgi
Assign execute permission to the cgi file.
AddHandler php7-fastcgi .php
Action php7-fastcgi /cgi-bin/php-fcgi
Options -Indexes +FollowSymLinks +ExecCGI
DirectoryIndex index.php
<IfModule mod_fcgid.c>
FcgidIdleTimeout 3600
FcgidProcessLifeTime 3600
FcgidBusyTimeout 3600
FcgidIOTimeout 3699
FcgidConnectTimeout 20
</IfModule>
Now you can use this handler to any virtualhost where you want to use latest PHP version.
Before doing this on production you must have to try it on local/dev system to avoid any unexpected errors. Also ./configure --help
can help you to choose php modules you currently using on production server.
Solution 4:
The Remi repo instructions are linked below. For example if you have CentOS version 7.4 then at the command line:
wget https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
wget http://rpms.remirepo.net/enterprise/remi-release-7.rpm
rpm -Uvh remi-release-7.rpm epel-release-latest-7.noarch.rpm
yum install yum-utils
yum-config-manager --enable remi-php72
yum update
All future updates for PHP will be for version 7.2.
https://blog.remirepo.net/post/2017/12/04/Install-PHP-7.2-on-CentOS-RHEL-or-Fedora