How to Install Nginx with PHP and MySQL (LEMP Stack) on CentOS 7.2

Nginx (pronounced "engine x") is a free, open-source, high-performance HTTP server. Nginx is known for its stability, rich feature set, simple configuration, and low resource consumption. This tutorial shows how you can install Nginx on a CentOS 7.2 server with PHP support (through PHP-FPM) and MySQL (MariaDB) support.


1 Preliminary Note

In this tutorial, I will use the hostname with the IP address These settings might differ for you, so you have to replace them where appropriate.

I will use the nano editor in this tutorial to edit configuration files. Nano can be installed like this.

yum -y install nano

I recommend having a firewall installed. If you do not have firewalld installed yet and want to use a firewall, then install it with these commands:

yum -y install firewalld

start the firewall and enable it to be started at boot time.

systemctl start firewalld.service
systemctl enable firewalld.service

Next, open your SSH port to ensure that you will be able to connect to the server by SSH.

firewall-cmd --permanent --zone=public --add-service=ssh
firewall-cmd --reload


2 Enabling Additional CentOS Repositories

The latest Nginx is not available from the official CentOS repositories, so we include the repository of the Nginx project to install it:

nano /etc/yum.repos.d/nginx.repo
name=nginx repo


3 Installing MySQL (MariaDB)

First, we install MariaDB as MySQL replacement. MariaDB is a free fork of MySQL. Run this command on the shell to install the MariaDB database server:

yum -y install mariadb mariadb-server net-tools

Then we create the system startup links for MariaDB (so that it starts automatically whenever the system boots) and start the MariaDB server:

systemctl enable mariadb.service
systemctl start mariadb.service

Now check that networking is enabled. Please note that the MraiDB service is named mysql as it is a compatble database server. Run

netstat -tap | grep mysql

It should show something like this:

[[email protected] ~]# netstat -tap | grep mysql
tcp 0 0* LISTEN 19842/mysqld 



to set a password for the user root (otherwise, anybody can access your MySQL database!):

[[email protected] ~]# mysql_secure_installation
/usr/bin/mysql_secure_installation: line 379: find_mysql_client: command not found
In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none):
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.
Set root password? [Y/n] <-- ENTER
New password: <-- yourrootsqlpassword
Re-enter new password: <-- yourrootsqlpassword
Password updated successfully!
Reloading privilege tables..
... Success!
By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.
Remove anonymous users? [Y/n] <-- ENTER
... Success!
Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] <-- ENTER
... Success!
By default, MariaDB comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n] <-- ENTER
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n] <-- ENTER
... Success!
Cleaning up...
All done! If you've completed all of the above steps, your MariaDB
installation should now be secure.
Thanks for using MariaDB!
[[email protected] ~]#

[[email protected] ~]# mysql_secure_installation

4 Installing Nginx

Nginx is available as a package from which we can install like this:

yum -y install nginx

Then we create the system startup links for nginx and start it:

systemctl enable nginx.service
systemctl start nginx.service

There are chances that you get an error that port 80 is already in use, the error message will be like this:

[[email protected] ~]# service nginx start
Starting nginx: nginx: [emerg] bind() to failed (98: Address already in use)
nginx: [emerg] bind() to failed (98: Address already in use)
nginx: [emerg] bind() to failed (98: Address already in use)
nginx: [emerg] bind() to failed (98: Address already in use)
nginx: [emerg] bind() to failed (98: Address already in use)
nginx: [emerg] still could not bind()
[[email protected] ~]#

This means that another web server (probably Apache) is already running on this server. Stop the Apache service and then start the service for NGINX:

systemctl stop httpd.service
yum remove httpd
systemctl disable httpd.service

Then try to start Nginx again.

systemctl start nginx.service

Open the HTTP and HTTPS ports in the firewall

firewall-cmd --permanent --zone=public --add-service=http
firewall-cmd --permanent --zone=public --add-service=https
firewall-cmd --reload

The resulting output on the shell will look like this:

[[email protected] ~]# firewall-cmd --permanent --zone=public --add-service=http
[[email protected] ~]# firewall-cmd --permanent --zone=public --add-service=https
[[email protected] ~]# firewall-cmd --reload
[[email protected] ~]#

Type in your web server's IP address or hostname into a browser (e.g., and you should see the Nginx welcome page:

The Nginx Welcome page.


Share this page:

Suggested articles

7 Comment(s)

Add comment


By: Azaz Qadir

This is a really tedious task. You have to write so many commands just to install Nginx. This is the reason why I prefer managed hosting platform where Nginx, Apache and everything else are already installed. This saves a lot of time for me.

By: brody

what version of nginx does this install?

By: luis medina

This is a great post, nginx it's a great option even to an small server. The low footprint makes it more suitable for sites with small resources and heavy trafic.

By: Chris

This is a horrible post, as this simply does not say how to install PHP :/

By: till

It explains in detail how to install PHP off course, see chapter "5 Installing PHP" on the second page. I guess you missed to read page 2 :)

By: Eugene

Very good article. I tried to use the digital ocean article and for some reason ran into issues. I did run into one issue when following your article but it was due to my own typeo!

Thank you

By: Leszek

 Thanks for a nice tutorial, however it didn't say how to enable SSL. You open the firewall for SSL, but then nothing.