Using PHP 7.x on CentOS 7.4
This article is about a concern I had recently : > How to use PHP 7.0 or 7.1 on CentOS 7 on the default httpd server without having to use external repositories or recompile PHP from source?
The answer lies in the Software Collections. Here we will start from a fresh CentOS installation, and move forward into having a website running PHP 7.0.
First of all, we will install the Software Collection’s repository (it’s an official repo that needs to be enabled).
root @ centos7-vm: ~ # yum -y install centos-release-scl.noarch
This command will deploy a new repository on your system, which is part of the official repositories of CentOS.
Once this is installed, let’s install the PHP 7 package as well as the httpd packages, along with the addons.
root @ centos7-vm: ~ 24 # yum -y install rh-php70 rh-php70-php rh-php70-php-fpm httpd
In order to use php 7.0, we will be using php-fpm, which is the recommended way, in place of mod_php. To do this, we will use the fpm service. This service will run by default on port 9000. If you need to change that port, head to the file :
And change the line :
listen = 127.0.0.1:9000
to the one you want to setup (e.g 9002). Once this is done, we need to change the selinux database to add 9002 to a valid port to run for httpd services :
semanage port -a -t http_port_t -p tcp 9002
After that, start the php-fpm service and enable it at boot :
root @ centos7-vm: ~ 29 # systemctl enable rh-php70-php-fpm.service Created symlink from /etc/systemd/system/multi-user.target.wants/rh-php70-php-fpm.service to /usr/lib/systemd/system/rh-php70-php-fpm.service. root @ centos7-vm: ~ 30 # systemctl start rh-php70-php-fpm.service root @ centos7-vm: ~ 31 # systemctl status rh-php70-php-fpm.service ● rh-php70-php-fpm.service - The PHP FastCGI Process Manager Loaded: loaded (/usr/lib/systemd/system/rh-php70-php-fpm.service; enabled; vendor preset: disabled) Active: active (running) since Tue 2017-10-24 13:35:11 CEST; 2s ago Main PID: 30045 (php-fpm) Status: "Ready to handle connections" CGroup: /system.slice/rh-php70-php-fpm.service ├─30045 php-fpm: master process (/etc/opt/rh/rh-php70/php-fpm.conf) ├─30046 php-fpm: pool www ├─30047 php-fpm: pool www ├─30048 php-fpm: pool www ├─30049 php-fpm: pool www └─30050 php-fpm: pool www
Now, we need to configure our httpd daemon to tell it to use this service to process php pages.
Let’s create a file named <code>fpm.conf</code> in our <code>/etc/httpd/conf.d/</code> folder and give it the folder and give it the following content :
# PHP scripts setup ProxyPassMatch ^/(.*.php)$ fcgi://127.0.0.1:9000/var/www/html Alias / /var/www/html/
When doing this, apache will send all files that end with .php to our php-fpm service and display the result.
To confirm it’s working, we can create a small page with a phpinfo request in /var/www/html
root @ centos7-vm: ~ 1 # cat /var/www/html/index.php <?php phpinfo() ?> root @ centos7-vm: ~ 1 #
Please don't forget that this is a basic setup to confirm your installation is working. If you need to use this in a real life application, you might want to tune your php settings as described in this article for example.
Don’t forget to also start, enable and add your httpd service to your firewall :
root @ centos7-vm: ~ 43 # systemctl enable httpd ; systemctl start httpd root @ centos7-vm: ~ 46 # firewall-cmd --add-service=http --permanent success root @ centos7-vm: ~ 47 # firewall-cmd --reload success
Now if we connect to the website, it will display the php correctly handled :