How to Install PHP 7, NGINX & MySQL 5.6 on CentOS/RHEL 7.4 & 6.9
Few days back PHP version 7.2 has been released. Which has a number of changes and improvements over PHP version 7.X. This article will help you to install PHP 7, NGINX and MySQL 5.6 on CentOS / RHEL 7.4 & 6.9 operating systems. This tutorial has been tested with CentOS 7.4, so all the services command are used with systemctl, For CentOS 6 users change all systemctl command correspondence service command.
Step 1 – Setup Yum Repository
In the first step install all the required yum repositories in your system used in the remaining tutorial for various installations. You are adding REMI, EPEL, Webtatic & MySQL community server repositories in your system.
CentOS / RHEL 7
1 | yum install epel-release |
CentOS / RHEL 6
1 | yum install epel-release |
Step 2 – Install PHP 7
Now install PHP 7 packages from remi-php72 rpm repository using the following command.
1 | # yum --enablerepo=remi-php72 install php |
Now install required php modules. Use following command to list available modules in yum repositories.
1 | # yum --enablerepo=remi-php72 search php |
Now check all listed modules in above command and install required modules like below.
1 | # yum --enablerepo=remi-php72 install php-mysql php-xml \ |
Step 3 – Install NGINX
NGINX is the popular web server used on Linux systems. Let’s install Nginx web server using the following command on your system.
1 | # yum install nginx |
Now start nginx service and enable to start on boot using below commands.
1 | # systemctl enable nginx.service |
Step 4 – Install MySQL 5.6
In step 1 we already have installed required yum repository in your system. Lets use following command to install MySQL server on your system.
1 | # yum install mysql-server |
You need to execute mysql_secure_installation once after installation of MySQL server using following command. First it will prompt to set a password for root account, after that ask few questions, I suggest to say yes ( y ) for all.
1 | # systemctl start mysqld.service |
Now restart MySQL service and enable to start on system boot.
1 | # systemctl restart mysqld.service |
Step 5 – Setup PHP-FPM
Now use following command to install php7 fpm package using following command.
1 | # yum --enablerepo=remi-php72 install php-fpm |
Step 6 – Create Nginx VirtualHost
Finally do the configuration of Nginx VirtualHost. For this example we are editing default configuration file.
1 | # sudo vi /etc/nginx/conf.d/example.conf |
and make changes as below.
1 | server { |
You have to do the same changes in all Virtual Hosts configured.
Step 7 – Restart Services
After installing all services on your system, start all required services.
1 | # systemctl restart nginx.service |
Step 8. Open Port in Firewall
Finally open firewall ports for HTTP (80) and https (443) services using the following command.
1 | # firewall-cmd --permanent --zone=public --add-service=http |
Step 9. Verify Setup
Let’s check the installed versions of packages on system using following commands one by one.
1 | # php -v |
Finally, verify installation of PHP 7 with NGINX. Let’s create a file index.php on website document root using following content.
1 | <?php |
Now browse this file in web browser. It will so all the details about versions and installation.
You have successfully configured LEMP Stack setup on your CentOS / RHEL 7.4 & 6.9 system.