How to Install Nginx 1.15, MariaDB 10 and PHP 7 on CentOS 7

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 26-29 servers using yum and dnf package manager.

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

Read Also: Install Apache, MySQL 8 or MariaDB 10 and PHP 7 on CentOS 7

Step 1: Installing EPEL and Remi Repository

EPEL (Extra Packages for Enterprise Linux) is a community based repository offers add-on software packages for RHEL-based Linux distributions.

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 7 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 6 Only ------
# subscription-manager repos --enable=rhel-6-server-optional-rpms

On Fedora 24-29

# rpm -Uvh http://rpms.remirepo.net/fedora/remi-release-29.rpm  [On Fedora 29]
# rpm -Uvh http://rpms.remirepo.net/fedora/remi-release-28.rpm  [On Fedora 28]
# rpm -Uvh http://rpms.remirepo.net/fedora/remi-release-27.rpm  [On Fedora 27]
# rpm -Uvh http://rpms.remirepo.net/fedora/remi-release-26.rpm  [On Fedora 26]
# rpm -Uvh http://rpms.remirepo.net/fedora/remi-release-25.rpm  [On Fedora 25]
# rpm -Uvh http://rpms.remirepo.net/fedora/remi-release-24.rpm  [On Fedora 24]

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 4: Installing Ngnix and MariaDB

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.

MariaDB is a fork of the well-known MySQL, one of the world’s most popular Relational Database Management System (RDBMS). It is entirely developed by the community and as such it is intended to remain FOSS and compatible with the GPL.

To install Ngnix and MariaDB, run the following commands.

----------- Installing on RHEL/CentOS 7/6 ----------- 
# yum --enablerepo=remi install nginx MariaDB-client MariaDB-server php php-common php-fpm 

----------- Installing on Fedora ----------- 
# dnf --enablerepo=remi install nginx MariaDB-client MariaDB-server php php-common php-fpm 

Step 3: Installing PHP Using Remi Repository

PHP (Hypertext Preprocessor) is a Free and Open Source server-side scripting language that is best suited for web development. It can be used to produce dynamic web pages for a website and is most frequently found in *nix servers. One of the advantages of PHP is that it is easily extensible through the use of a wide variety of modules.

To install PHP, first you need to enable Remi repository by installing yum-utils, a collection of useful programs for managing yum repositories and packages.

# yum install yum-utils

Once installed, you can use yum-config-manager provided by yum-utils to enable Remi repository as the default repository for installing different PHP versions as shown.

For example, to install PHP 7.x version, use the following command.

------------- On CentOS & RHEL ------------- 
# yum-config-manager --enable remi-php70 && yum install php       [Install PHP 7.0]
# yum-config-manager --enable remi-php71 && yum install php       [Install PHP 7.1]
# yum-config-manager --enable remi-php72 && yum install php       [Install PHP 7.2]
# yum-config-manager --enable remi-php73 && yum install php       [Install PHP 7.3]

------------- On Fedora ------------- 
# dnf --enablerepo=remi install php70      [Install PHP 7.0]
# dnf --enablerepo=remi install php71      [Install PHP 7.1]
# dnf --enablerepo=remi install php72      [Install PHP 7.2]
# dnf --enablerepo=remi install php73      [Install PHP 7.3]

Next, we are going to install all these following PHP modules.

------ On RHEL/CentOS 7/6 ------
# yum --enablerepo=remi install php-mysqlnd php-pgsql php-fpm php-pecl-mongo php-pdo php-pecl-memcache php-pecl-memcached php-gd php-xml php-mbstring php-mcrypt php-pecl-apcu php-cli php-pear

------ On Fedora ------
# dnf --enablerepo=remi install php-mysqlnd php-pgsql php-fpm php-pecl-mongo php-pdo php-pecl-memcache php-pecl-memcached php-gd php-xml php-mbstring php-mcrypt php-pecl-apcu php-cli php-pear

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.

If you liked this article, then do subscribe to email alerts for Linux tutorials. If you have any questions or doubts? do ask for help in the comments section.

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Support Us

We are thankful for your never ending support.

46 thoughts on “How to Install Nginx 1.15, MariaDB 10 and PHP 7 on CentOS 7”

  1. Hello,

    Nginx is perfectly working in my server with your help…thanks for that…..

    I think the nginx is working is based on the reverse proxy concept…. So it needs two or more machines to setup and understand the actual working of nginx…,…..

    1. So, is this configuration is for proxy server or proxy client machine…………? Or Do we have to install nginx on both machines..?

    2. In which configuration file, will we mention the proxy client details…… Is it in Virtual host file?

    Thank you

    Reply
  2. Hi Ravi,
    I installed LEMP with this document.
    But I can’t Install MariaDB.
    ( https://www.tecmint.com/install-mariadb-in-linux/ )

    How can I solve this?
    Thank you.

    CentOS 6.3 64bit.
    Error Summary is below.

    Loaded plugins: fastestmirror
    Loading mirror speeds from cached hostfile
    * base: data.nicehosting.co.kr
    * epel: http://ftp.neowiz.com
    * extras: data.nicehosting.co.kr
    * remi: remi.kazukioishi.net
    * remi-test: remi.kazukioishi.net
    * updates: data.nicehosting.co.kr
    Setting up Install Process
    Package nginx-1.4.4-1.el6.ngx.x86_64 already installed and latest version
    Package mysql is obsoleted by MariaDB-client, trying to install MariaDB-client-5.5.34-1.x86_64 instead
    Package mysql-server is obsoleted by MariaDB-server, trying to install MariaDB-server-5.5.34-1.x86_64 instead
    Package php-5.4.23-1.el6.remi.x86_64 already installed and latest version
    Package php-common-5.4.23-1.el6.remi.x86_64 already installed and latest version
    Package php-fpm-5.4.23-1.el6.remi.x86_64 already installed and latest version
    Resolving Dependencies
    –> Running transaction check
    —> Package MariaDB-client.x86_64 0:5.5.34-1 will be installed
    –> Processing Dependency: MariaDB-common for package: MariaDB-client-5.5.34-1.x86_64
    —> Package MariaDB-server.x86_64 0:5.5.34-1 will be installed
    –> Running transaction check
    —> Package MariaDB-common.x86_64 0:5.5.34-1 will be installed
    –> Processing Dependency: MariaDB-compat for package: MariaDB-common-5.5.34-1.x86_64
    –> Running transaction check
    —> Package MariaDB-compat.x86_64 0:5.5.34-1 will be installed
    –> Finished Dependency Resolution

    Dependencies Resolved

    ===================================================================================================================================================================
    Package Arch Version Repository Size
    ===================================================================================================================================================================
    Installing:
    MariaDB-client x86_64 5.5.34-1 mariadb 10 M
    MariaDB-server x86_64 5.5.34-1 mariadb 34 M
    Installing for dependencies:
    MariaDB-common x86_64 5.5.34-1 mariadb 23 k
    MariaDB-compat x86_64 5.5.34-1 mariadb 2.7 M

    Transaction Summary
    ===================================================================================================================================================================
    Install 4 Package(s)

    Total size: 47 M
    Installed size: 195 M
    Is this ok [y/N]: y
    Downloading Packages:
    Running rpm_check_debug
    Running Transaction Test

    Transaction Check Error:
    file /usr/share/mysql/czech/errmsg.sys from install of MariaDB-server-5.5.34-1.x86_64 conflicts with file from package mysql-libs-5.5.35-1.el6.remi.x86_64
    file /usr/share/mysql/danish/errmsg.sys from install of MariaDB-server-5.5.34-1.x86_64 conflicts with file from package mysql-libs-5.5.35-1.el6.remi.x86_64
    file /usr/share/mysql/dutch/errmsg.sys from install of MariaDB-server-5.5.34-1.x86_64 conflicts with file from package mysql-libs-5.5.35-1.el6.remi.x86_64
    file /usr/share/mysql/english/errmsg.sys from install of MariaDB-server-5.5.34-1.x86_64 conflicts with file from package mysql-libs-5.5.35-1.el6.remi.x86_64
    file /usr/share/mysql/estonian/errmsg.sys from install of MariaDB-server-5.5.34-1.x86_64 conflicts with file from package mysql-libs-5.5.35-1.el6.remi.x86_64
    file /usr/share/mysql/french/errmsg.sys from install of MariaDB-server-5.5.34-1.x86_64 conflicts with file from package mysql-libs-5.5.35-1.el6.remi.x86_64
    file /usr/share/mysql/german/errmsg.sys from install of MariaDB-server-5.5.34-1.x86_64 conflicts with file from package mysql-libs-5.5.35-1.el6.remi.x86_64
    file /usr/share/mysql/greek/errmsg.sys from install of MariaDB-server-5.5.34-1.x86_64 conflicts with file from package mysql-libs-5.5.35-1.el6.remi.x86_64
    file /usr/share/mysql/hungarian/errmsg.sys from install of MariaDB-server-5.5.34-1.x86_64 conflicts with file from package mysql-libs-5.5.35-1.el6.remi.x86_64
    file /usr/share/mysql/italian/errmsg.sys from install of MariaDB-server-5.5.34-1.x86_64 conflicts with file from package mysql-libs-5.5.35-1.el6.remi.x86_64
    file /usr/share/mysql/japanese/errmsg.sys from install of MariaDB-server-5.5.34-1.x86_64 conflicts with file from package mysql-libs-5.5.35-1.el6.remi.x86_64
    file /usr/share/mysql/korean/errmsg.sys from install of MariaDB-server-5.5.34-1.x86_64 conflicts with file from package mysql-libs-5.5.35-1.el6.remi.x86_64
    file /usr/share/mysql/norwegian-ny/errmsg.sys from install of MariaDB-server-5.5.34-1.x86_64 conflicts with file from package mysql-libs-5.5.35-1.el6.remi.x86_64
    file /usr/share/mysql/norwegian/errmsg.sys from install of MariaDB-server-5.5.34-1.x86_64 conflicts with file from package mysql-libs-5.5.35-1.el6.remi.x86_64
    file /usr/share/mysql/polish/errmsg.sys from install of MariaDB-server-5.5.34-1.x86_64 conflicts with file from package mysql-libs-5.5.35-1.el6.remi.x86_64
    file /usr/share/mysql/portuguese/errmsg.sys from install of MariaDB-server-5.5.34-1.x86_64 conflicts with file from package mysql-libs-5.5.35-1.el6.remi.x86_64
    file /usr/share/mysql/romanian/errmsg.sys from install of MariaDB-server-5.5.34-1.x86_64 conflicts with file from package mysql-libs-5.5.35-1.el6.remi.x86_64
    file /usr/share/mysql/russian/errmsg.sys from install of MariaDB-server-5.5.34-1.x86_64 conflicts with file from package mysql-libs-5.5.35-1.el6.remi.x86_64
    file /usr/share/mysql/serbian/errmsg.sys from install of MariaDB-server-5.5.34-1.x86_64 conflicts with file from package mysql-libs-5.5.35-1.el6.remi.x86_64
    file /usr/share/mysql/slovak/errmsg.sys from install of MariaDB-server-5.5.34-1.x86_64 conflicts with file from package mysql-libs-5.5.35-1.el6.remi.x86_64
    file /usr/share/mysql/spanish/errmsg.sys from install of MariaDB-server-5.5.34-1.x86_64 conflicts with file from package mysql-libs-5.5.35-1.el6.remi.x86_64
    file /usr/share/mysql/swedish/errmsg.sys from install of MariaDB-server-5.5.34-1.x86_64 conflicts with file from package mysql-libs-5.5.35-1.el6.remi.x86_64
    file /usr/share/mysql/ukrainian/errmsg.sys from install of MariaDB-server-5.5.34-1.x86_64 conflicts with file from package mysql-libs-5.5.35-1.el6.remi.x86_64
    file /etc/my.cnf from install of MariaDB-common-5.5.34-1.x86_64 conflicts with file from package mysql-libs-5.5.35-1.el6.remi.x86_64
    file /usr/share/mysql/charsets/Index.xml from install of MariaDB-common-5.5.34-1.x86_64 conflicts with file from package mysql-libs-5.5.35-1.el6.remi.x86_64

    Error Summary
    ————-

    Reply
  3. Hi Ravi,
    Thank you for great work. I am a beginner with linux and planning to host a website from my homeserver on CentOS. I Have followed both of your tutorial on Nginx + PHP & Phpmyadmin. However I am not able to get the phpmyadmin screen. I would also like to install drupal on it. I have created 2 vhost, drupal & phpmyadmin. But only able to get the drupal installation screen. phpmyadmin vhost gives 404 error. Would you please be kind to make tutorial for CentOS+ Nginx+PHP+ MysQL+ Phpmyadmin+ drupal on a combined vhost file please? I would be very grateful to you. I am sure it will be useful for many others like me…. Thanks again

    Reply
      • This is exactly the problem im having too. A tutorial for that would be great. I’m hoping this will be for 6.4? Thanks for the tutorials though they are great.

        Reply
  4. Sir can you tell me the default web directory…….
    Were I will just copy and paste my html and php file that can browsable from other clients to view that web page.

    Reply
  5. hello, I have the following problem in php-fpm. I set up nginx + php-fpm + mysql and it occurs, the processes of php-fpm are always running on Linux, even after the client close the connection, the processes remain active, the only way to solve it by restarting the service php -fpm. Did this happen to you?

    Reply

Leave a Reply to Ravi Saive Cancel reply

Have a question or suggestion? Please leave a comment to start the discussion. Please keep in mind that all comments are moderated and your email address will NOT be published.