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

------ 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

------ For RHEL 6 Only ------
# subscription-manager repos --enable=rhel-6-server-optional-rpms

On Fedora 24-29

# rpm -Uvh  [On Fedora 29]
# rpm -Uvh  [On Fedora 28]
# rpm -Uvh  [On Fedora 27]
# rpm -Uvh  [On Fedora 26]
# rpm -Uvh  [On Fedora 25]
# rpm -Uvh  [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:

name=nginx repo 

For CentOS 7/6:

name=nginx repo 

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

name = MariaDB 
baseurl = 

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

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 
# 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, 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, 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; 
	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. 

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:


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 read this far, tweet to the author to show them you care. Tweet a thanks
Ravi Saive
I am an experienced GNU/Linux expert and a full-stack software developer with over a decade in the field of Linux and Open Source technologies

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

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

  1. What is the problem, Ravi? How can I fix it?

    2017/03/18 13:06:48 [error] 5956#0: *8012409 FastCGI sent in stderr: “Primary script unknown” while reading response header from upstream, client:, server:, request: “GET /movies.php?genre=horror HTTP/1.1”, upstream: “fastcgi://unix:/tmp/php_fpm.sock:”, host: “”

  2. Hi team,

    I was followed this tutorial, everything works fine except an error in error log, many logs was generated

    2016/10/22 21:39:27 [error] 3044#0: *413880 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, 
    client:, server:, request: "GET /wp-login.php HTTP/1.1", 
    upstream: "fastcgi://unix:/tmp/php_fpm.sock:", host: ""

    What kind of this error, team? And how can I fix it?

    • The website is work normally?

      You should check your error log carefully, “GET /wp-login.php HTTP/1.1”

      I think it is wrong path

  3. Pingback: PhpMyAdmin 4.2.2 Released - Install for Apache or Nginx on RHEL/CentOS 6.5/5.9 Fedora 20-12
  4. ———– Installing on RHEL/CentOS 7/6 ———–
    # yum –enablerepo=remi install nginx MariaDB-server (depend: MariaDB-client) php php-common php-fpm

  5. Pingback: Install Latest Apache 2.4, MySQL/MariaDB 5.5 and PHP 5.5/5.6 on RHEL/CentOS 7/6 & Fedora 23-18
  6. Pingback: apache에서 nginx로 갈아타기 - just quit your job
  7. I did exactly what it says several times and I do not phphinfo displays. Only The webpage cannot be found.
    My system is VPS, CentOS 6, nginx, mysql, php.

  8. Hi,
    I installed nginx and php follow above tips, the nginx was ok and I can see the “Welcome to nginx!” site. But when I visit the php site, the browser download the php file. How can I fix this issue?


Leave a Reply to Rojo Cancel reply

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.