There is a new version of this tutorial available for CentOS 8.

How To Install Nginx With PHP And MySQL (LEMP Stack) On CentOS 7

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 server with PHP support (through PHP-FPM) and MySQL (Mariadb) support.

 

1 Preliminary Note

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

 

2 Enabling Additional Repositories

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

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

 

3 Installing MySQL

First we install Mariadb. Mariadb is a free fork of MySQL. Run this command on the shell:

yum install mariadb mariadb-server net-tools

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

systemctl enable mariadb.service
systemctl start mariadb.service

Now check that networking is enabled. Run

netstat -tap | grep mysql

It should show something like this:

[root@example ~]# netstat -tap | grep mysql
tcp 0 0 0.0.0.0:mysql 0.0.0.0:* LISTEN 10623/mysqld 

Run

mysql_secure_installation

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

[root@example ~]# mysql_secure_installation
/usr/bin/mysql_secure_installation: line 379: find_mysql_client: command not found
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
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!
[root@example ~]#

[root@server1 ~]# mysql_secure_installation

4 Installing Nginx

Nginx is available as a package from nginx.org which we can install as follows:

yum 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 like port 80 already in use, error message will be like this

[root@server1 ~]# service nginx start
Starting nginx: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()
                                                           [FAILED]
[root@server1 ~]#

Then it means that there are chances of apache service running there. Stop the service & further start the service for NGINX as follows

systemctl stop httpd.service
yum remove httpd
systemctl disable httpd.service
systemctl enable nginx.service
systemctl start nginx.service

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

[root@example ~]# firewall-cmd --permanent --zone=public --add-service=http
success
[root@example ~]# firewall-cmd --permanent --zone=public --add-service=https
success
[root@example ~]# firewall-cmd --reload
success
[root@example ~]#

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

 

Share this page:

2 Comment(s)