Install Latest Nginx 1.10.1, MariaDB 10 and PHP 5.5/5.6 on RHEL/CentOS 7/6 & Fedora 20-24

In this article we will explain how to install a LEMP stack (Linux, Nginx, MariaDB, PHP) along with PHP-FPM on RHEL/CentOS 7/6 and Fedora 20-24 servers using yum and dnf package manager. During the...

In this article we will explain how to install a LEMP stack (Linux, Nginx, MariaDB, PHP) along with PHP-FPM on RHEL/CentOS 7/6 and Fedora 20-24 servers using yum and dnf package manager.

Install Nginx, MariaDB, PHP and PHP-FPM in RHEL/CentOS and Fedora

Install Nginx, MariaDB, PHP and PHP-FPM in RHEL/CentOS and Fedora

During the process we will enable the Remi and Nginx repositories in order to be able to install the latest versions of these packages.

Read Also: Install Latest Apache 2.4, MySQL 5.5/MariaDB 10 and PHP 5.5/5.6 on RHEL/CentOS 7/6 & Fedora 20-24

What is Nginx?

Nginx (Engine X) is open source, robust, lightweight and high performance Web server, reverse proxy sever and also mail proxy server for HTTP, SMTP, POP3 and IMAP protocols. For further details, visit http://wiki.nginx.org/Overview

What is PHP-FPM?

PHP-FPM stands for FastCGI Process Manager. It is an alternative PHP FastCGI implementation with some additional useful features for heavily loaded websites. For more information visit http://php-fpm.org/.

Step 1: Installing Remi Repository


Remi is a repository where you can find the latest versions of the PHP stack (full featured) for installation in the Fedora and Enterprise Linux distributions.

On RHEL/CentOS 7

# yum update && yum install epel-release # rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm --------- For RHEL Only ---------
# subscription-manager repos --enable=rhel-7-server-optional-rpms 

On RHEL/CentOS 6

# yum update && yum install epel-release # rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm --------- For RHEL Only ---------
# subscription-manager repos --enable=rhel-6-server-optional-rpms 

On Fedora 24

# wget http://rpms.remirepo.net/fedora/remi-release-24.rpm # dnf install remi-release-24.rpm 

On Fedora 23

# wget http://rpms.remirepo.net/fedora/remi-release-23.rpm # dnf install remi-release-23.rpm 

On Fedora 22

# wget http://rpms.remirepo.net/fedora/remi-release-22.rpm # dnf install remi-release-22.rpm 

Other Fedora versions (21 and below) are no longer being maintained or supported. You can, however, enable the repository by changing the version number in the command above.

For example, to enable the Remi repository in a Fedora 20 server, do:

On Fedora 20

# wget http://rpms.remirepo.net/fedora/remi-release-20.rpm # yum install remi-release-20.rpm 

Step 2: Installing Nginx and MariaDB Repositories

The Nginx repository is only needed in RHEL and CentOS distributions. Create a file called /etc/yum.repos.d/nginx.repo and add the following lines to it.

For RHEL 7/6:

[nginx] name=nginx repo baseurl=http://nginx.org/packages/rhel/$releasever/$basearch/ gpgcheck=0 enabled=1 

For CentOS 7/6:

[nginx] name=nginx repo baseurl=http://nginx.org/packages/centos/$releasever/$basearch/ gpgcheck=0 enabled=1 

To enable the MariaDB repository, create a file named /etc/yum.repos.d/mariadb.repo with the following contents:

[mariadb] name = MariaDB baseurl = http://yum.mariadb.org/10.1/centos7-amd64 gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB gpgcheck=1 

Step 3: Enabling the Remi Repository

The default Remi repository provides PHP 5.4 version, if you want to install PHP 5.5 or 5.6, you need to enable by adding the line enabled=1 to /etc/yum.repos.d/remi.repo file for the section [remi-php55] and [remi-php56].

To Install PHP 5.5 on CentOS/RHEL 7 and Fedora

[remi-php55]
name=Remi's PHP 5.5 RPM repository for Enterprise Linux 6 - $basearch
#baseurl=http://rpms.remirepo.net/enterprise/6/php55/$basearch/
mirrorlist=http://rpms.remirepo.net/enterprise/6/php55/mirror
# NOTICE: common dependencies are in "remi-safe"
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-remi

To Install PHP 5.6 on CentOS/RHEL 7 and Fedora

[remi-php56]
name=Remi's PHP 5.6 RPM repository for Enterprise Linux 6 - $basearch
#baseurl=http://rpms.remirepo.net/enterprise/6/php56/$basearch/
mirrorlist=http://rpms.remirepo.net/enterprise/6/php56/mirror
# NOTICE: common dependencies are in "remi-safe"
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-remi

Step 4: Installing Ngnix, MariaDB, PHP & PHP-FPM

----------- Installing on RHEL/CentOS 7/6 ----------- # yum --enablerepo=remi install nginx MariaDB-client MariaDB-server php php-common php-fpm ----------- Installing on Fedora 22-24 ----------- # dnf --enablerepo=remi install nginx MariaDB-client MariaDB-server php php-common php-fpm ----------- Installing on Fedora 20-21 ----------- # yum --enablerepo=remi install nginx MariaDB-client MariaDB-server php php-common php-fpm 

Step 5: Installing PHP Modules

----------- Installing on RHEL/CentOS 7/6 ----------- # yum --enablerepo=remi install php-mysql php-pgsql php-pecl-mongo php-sqlite php-pecl-memcache php-pecl-memcached php-gd php-mbstring php-mcrypt php-xml php-pecl-apc php-cli php-pear php-pdo ----------- Installing on Fedora 22-24 ----------- # dnf --enablerepo=remi install php-mysql php-pgsql php-pecl-mongo php-sqlite php-pecl-memcache php-pecl-memcached php-gd php-mbstring php-mcrypt php-xml php-pecl-apc php-cli php-pear php-pdo ----------- Installing on Fedora 20-21 ----------- # yum --enablerepo=remi install php-mysql php-pgsql php-pecl-mongo php-sqlite php-pecl-memcache php-pecl-memcached php-gd php-mbstring php-mcrypt php-xml php-pecl-apc php-cli php-pear php-pdo 

Step 6: Stopping and Disabling Apache Service

By default, Apache and Nginx listen in same port (TCP 80). For that reason, if Apache is installed in your server, you need to stop it and disable / mask it (a stronger version of disable that links the service to /dev/null) in order to use Nginx, or you can remove it if you’re not planning on using it anymore.

# systemctl stop httpd # systemctl disable httpd or # systemctl mask httpd 

Step 7: Starting/Stopping Nginx, MariaDB and PHP-FPM

----------- Enable Nginx, MariaDB and PHP-FPM on Boot ----------- # systemctl enable nginx # systemctl enable mariadb # systemctl enable php-fpm ----------- Start Nginx, MariaDB and PHP-FPM ----------- # systemctl start nginx # systemctl start mariadb # systemctl start php-fpm 

Step 8: Configuring Nginx and PHP-FPM

Let us now create a directory structure for your website (a virtual host, or server block as it is called in Nginx) under /srv/www/. In this example we will use www.tecmint.com, but feel free to choose another domain and main directory if you want.

# mkdir -p /srv/www/tecmint/public_html # mkdir /srv/www/tecmint/logs # chown -R nginx:nginx /srv/www/tecmint 

Step 9: Configuring Nginx Virtual Host Directories

As you know, the ability of running several sites from the same machine is one of the distinguishing features of major web servers. Let us now proceed to create the directories to store our server blocks (known as virtual hosts in Apache) under /etc/nginx.

# mkdir /etc/nginx/sites-available # mkdir /etc/nginx/sites-enabled 

The following line of code, which must be inserted before closing the http block in /etc/nginx/nginx.conf, will ensure that configuration files inside the /etc/nginx/sites-enabled directory will be taken into account when Nginx is running:

## Load virtual host conf files. ## include /etc/nginx/sites-enabled/*; 
Configuring Nginx VirtualHost

Configuring Nginx VirtualHost

To create the server block for tecmint.com, add the following lines of code to /etc/nginx/sites-available/tecmint (this file will be created when you enter the full path to start your preferred text editor). This is a basic virtual host config file.

server { listen 80 default; server_name tecmint; access_log /srv/www/tecmint/logs/access.log; error_log /srv/www/tecmint/logs/error.log; root /srv/www/tecmint/public_html; location ~* \.php$ { fastcgi_index index.php; fastcgi_pass 127.0.0.1:9000; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param SCRIPT_NAME $fastcgi_script_name; } } 

The process of “activating” a virtual host consists of creating a symbolic link from the definition of the tecmint virtual host to /etc/nginx/sites-enabled.

# ln -s /etc/nginx/sites-available/tecmint /etc/nginx/sites-enabled/tecmint 

In order to actually apply the changes we have been doing, we now need to restart Nginx. It is sometimes useful to check the configuration files for syntax errors before doing so:

# nginx -t # systemctl restart nginx # systemctl status nginx 
Restart Nginx and Verify Status

Restart Nginx and Verify Status

To access your newly created virtual host, you need to add the following line to /etc/hosts as a basic method of domain name resolution.

192.168.0.18 www.tecmint.com tecmint.com 

Step 10: Testing Nginx, MySQL, PHP and PHP-FPM

Let’s stick with the classic way of testing PHP. Create a file called test.php under /srv/www/tecmint/public_html/ and add the following lines of code to it.

The phpinfo() function shows a great deal of information about the current PHP installation:

<?php phpinfo(); ?> 

Now point your web browser to http://tecmint/test.php and check the presence of the installed modules and additional software:

Congratulations! You now have a working installation of a LEMP stack. If something did not go as expected, feel free to contact us using the form below. Questions and suggestions are also welcome.

Posted by wiredgorilla