Install PHP 7.0 (CentOS 7)

To totally unlock this section you need to Log-in


Login

This article explains how to install PHP 7.0 on CentOS 7. You will need to have a http server installed prior to following this tutorial; we will provide instructions on configuring PHP 7.0 with Apache 2.4 and Nginx.

Preliminary steps

First, ensure your system is fully up-to-date by running the following:

yum update -y

The -y flag tells yum to go ahead and update the system without prompting you.

You will use a text editor throughout this tutorial and may use whichever one you’re most comfortable with. For the sake of simplicity, we’ll use nano here and most likely will need to be installed on a fresh system. Do that with:

yum install nano -y

Now you’re ready to move on with installing PHP 7.

Installing PHP 7

First, you need to install and enable the EPEL repository, which may already be installed if you used it to install Nginx:

yum install epel-release -y

Next, install and enable the IUS CentOS 7 repository:

rpm -Uvh https://centos7.iuscommunity.org/ius-release.rpm

Just to see what’s now available, search yum for php7:

yum search php7

Note the PHP 7 packages are prefixed with php70u.

The packages you install will depend upon whether you’re using Apache or Nginx as your http server.

Installing PHP 7 to use with Apache

If you’re using Apache, run the following command to install PHP 7:

yum install php70u -y

Enter y if you’re prompted to install a GPG key (“Importing GPG key…”).

Installing PHP 7 to use with Nginx

You will need to install the FPM (FastCGI Process Manager) version of PHP 7.0 to use with Nginx. Do so with the following command:

yum install php70u-fpm php70u-fpm-nginx -y

Enter y if you’re prompted to install a GPG key (“Importing GPG key…”).

Start the php-fpm service:

systemctl start php-fpm.service

Enable php-fpm to start on reboot:

systemctl enable php-fpm.service

Configuring PHP 7.0 to work with an http server

Configuring PHP 7.0 to work with Apache

The Apache configuration file needs one modification, so edit it with:

nano /etc/httpd/conf/httpd.conf

Find:

<ifmodule dir_module>
DirectoryIndex index.html
</ifmodule>

Replace with (add index.php):

<ifmodule dir_module>
DirectoryIndex index.php index.html
</ifmodule>

Note: the order of index.php and index.html determines which one will be prefered by Apache to load first.

Save and exit the file.

Restart Apache:

systemctl restart httpd.service

Create a test index.php file in the web root:

nano /var/www/html/index.php

Paste the following into index.php:

<?php
echo “Hello world, I’m PHP!”;
?>

Save and exit the file.

Now, navigate to your server’s IP in your browser and you should see the “Hello world, I’m PHP!” text you pasted into /var/www/html/index.php.

Configuring PHP 7.0 to work with Nginx

We highly recommend using the most recent stable version of Nginx from the Nginx repository instead of the EPEL repository, which will install a very old version. This section of the tutorial assumes you’ve installed from the Nginx repository.

Edit the default Nginx configuration file:

nano /etc/nginx/conf.d/default.conf

Replace the contents of the file with the following:

server {
listen 80;
server_name localhost;

#charset koi8-r;
#access_log /var/log/nginx/log/host.access.log main;

root /var/www/html;
index index.php index.html index.htm;

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}

location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

# deny access to .htaccess files, if Apache’s document root
# concurs with nginx’s one
#
#location ~ /\.ht {
# deny all;
#}
}

Save and exit the file.

Restart Nginx:

systemctl restart nginx.service

Create a test index.php file in the web root:

nano /var/www/html/index.php

Paste the following into index.php:

<?php
echo “Hello world, I’m PHP!”;
?>

Save and exit the file.

Now, navigate to your server’s IP in your browser and you should see the “Hello world, I’m PHP!” text you pasted into /var/www/html/index.php.

That concludes our tutorial on installing PHP 7.0 on CentOS 7 and configuring it with either Apache or Nginx.