How to Setup Linux, Nginx, MariaDB and PHP (LEMP) Stack on CentOS 7

LEMP stack is a collection of four open-source applications that are installed together on a server to run dynamic websites. These include Linux, Nginx, MariaDB, and PHP.

When setting up a VPS (Virtual Private Server) account, you will be prompted to select a Linux image (e.g. CentOS 7) when deploying the server. Linux is an operating system based on the Linux Kernel. It is very powerful, secure and runs most of the world’s servers.

Nginx is an event-driven web server popularly used for serving dynamic content. It has a very predictable performance, especially for high trafficked websites.

MariaDB is a fork of MySQL database and is compatible with all SQL commands. It is considered as a drop-in replacement for MySQL. The Relational Database Management System is fast, highly scalable and secure.

PHP is a scripting language that is popularly used as the middleware between the database server and web server.

In this guide, we will show you how to install Nginx, MariaDB, and PHP on a VPS plan running CentOS 7 as the operating system.

Prerequisites

Before you begin make sure you have the following:

Step 1: Installing Nginx on CentOS 7

Nginx packages are available in the EPEL repositories. If you don’t have EPEL repository already installed you can do it by typing:

Nginx packages can be pulled from the EPEL repositories, so you can first install it by typing the command below:

$ sudo yum install epel-release

Once installed, run the command below to install Nginx

$ sudo yum install nginx

Press Y and hit Enter when prompted to confirm the installation. Next, enable the Nginx server by typing the command below:

$ sudo systemctl start nginx

In order to start the web server when the system is rebooted, run the command below:

$ sudo systemctl enable nginx

The default CentOS 7 inbuilt firewall is set to block Nginx traffic. In order to allow inbound traffic to the Nginx server, run the commands below:

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

To check if Nginx was successfully installed on your server, enter the IP address associated with your VPS instance on a web browser like Google Chrome:

$ 192.0.0.1

You should see the default Nginx home page as shown below:

Alternatively, if you wish to check the status of the web server on the command line tool, enter the command below:

$ sudo systemctl status nginx

Output:

nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset                                             : disabled)

Active: active (running) since Wed 2019-02-13 07:52:14 UTC; 3min 17s ago

Main PID: 12740 (nginx)

CGroup: /system.slice/nginx.service

├─12740 nginx: master process /usr/sbin/nginx

└─12741 nginx: worker process



Feb 13 07:52:14 lemp-server-1 systemd[1]: Starting The nginx HTTP and revers....

Feb 13 07:52:14 lemp-server-1 nginx[12735]: nginx: the configuration file /e...k

Feb 13 07:52:14 lemp-server-1 nginx[12735]: nginx: configuration file /etc/n...l

Feb 13 07:52:14 lemp-server-1 systemd[1]: Failed to read PID from file /run/...t

Feb 13 07:52:14 lemp-server-1 systemd[1]: Started The nginx HTTP and reverse....

Hint: Some lines were ellipsized, use -l to show in full.

Step 2: Installing MariaDB on CentOS 7

Next, we are going to install the MariaDB database server on the CentOS 7 machine. We are going to use the yum package manager

$ sudo yum install mariadb-server

Press Y and hit Enter when prompted to confirm the installation. After a while, your MariaDB server installation should be completed.

To start the database server, run the command below:

$ sudo systemctl start mariadb

Just like we did for the Nginx server, we want to make sure that the database server is started when the system boots. To do this, run the command below

$ sudo systemctl enable mariadb

You can check if the MariaDB server is running by typing the command below

$ sudo systemctl status mariadb

Output:

mariadb.service - MariaDB database server

Loaded: loaded (/usr/lib/systemd/system/mariadb.service; enabled; vendor preset: disabled)

Active: active (running) since Wed 2019-02-13 08:02:03 UTC; 56s ago

Main PID: 12913 (mysqld_safe)

CGroup: /system.slice/mariadb.service

├─12913 /bin/sh /usr/bin/mysqld_safe --basedir=/usr

└─13075 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --log-...



Feb 13 08:02:01 lemp-server-1 mariadb-prepare-db-dir[12835]: MySQL manual for more instructions.

Feb 13 08:02:01 lemp-server-1 mariadb-prepare-db-dir[12835]: Please report any problems at http://mariadb.org/jira

Feb 13 08:02:01 lemp-server-1 mariadb-prepare-db-dir[12835]: The latest information about MariaDB is available at http...rg/.

Feb 13 08:02:01 lemp-server-1 mariadb-prepare-db-dir[12835]: You can find additional information about the MySQL part at:

Feb 13 08:02:01 lemp-server-1 mariadb-prepare-db-dir[12835]: http://dev.mysql.com

Feb 13 08:02:01 lemp-server-1 mariadb-prepare-db-dir[12835]: Consider joining MariaDB's strong and vibrant community:

Feb 13 08:02:01 lemp-server-1 mariadb-prepare-db-dir[12835]: https://mariadb.org/get-involved/

Feb 13 08:02:01 lemp-server-1 mysqld_safe[12913]: 190213 08:02:01 mysqld_safe Logging to '/var/log/mariadb/mariadb.log'.

Feb 13 08:02:01 lemp-server-1 mysqld_safe[12913]: 190213 08:02:01 mysqld_safe Starting mysqld daemon with databases f...mysql

Feb 13 08:02:03 lemp-server-1 systemd[1]: Started MariaDB database server.

The default MariaDB installation is not secure, so we are going to run the command below to set a root password, remove anonymous users and disable remote access

The default MariaDB installation is not secure, so we are going to run the command below to set a root password, remove anonymous users and disable remote access

$ sudo mysql_secure_installation
Enter current password for root (enter for none):
Set root password: Y
New password: STRONGPASSWORDHERE
Re-enter new password: REPEATSTRONGPASSWORDHERE
Remove anonymous users? [Y/n] Y
Disallow root login remotely? [Y/n]: Y
Remove test database and access to it? [Y/n]: Y
Reload privilege tables now? [Y/n]: Y

Output:

Cleaning up...
All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.
Thanks for using MariaDB!

Once the MariaDB  installation is complete, you can log in to the database server using the command below:

$ sudo mysql -uroot -p

Enter the root password of your MariaDB server and press Enter to continue.

You should see the MariaDB command line interface

Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 12
Server version: 5.5.60-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]>

Once logged in, you can run any SQL command e.g., to list databases, run the command below:

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)

Step 3: Installing PHP on CentOS 7

Next, we are going to install PHP(Hypertext Preprocessor).

This is a general-purpose scripting language that is highly suitable for web applications. In fact, most Content Management Systems (CMS) are coded in PHP.

To install the PHP software package, run the command below:

$ sudo yum install php php-mysql php-fpm

Again, press Y and hit Enter when prompted to confirm the PHP installation on CentOS 7 server.

Once PHP is installed, we will make a few changes to the configuration file. Open the file using nano text editor

$ sudo nano /etc/php.ini

Then, look for the line below:

;cgi.fix_pathinfo=1

And change it to:

cgi.fix_pathinfo=0

Press CTRL+X, Y and hit Enter to continue.

Then, we are going to edit the PHP-fpm configuration file:

$ sudo nano /etc/php-fpm.d/www.conf

Make the following changes to the file:

listen = /var/run/php-fpm/php-fpm.sock
listen.owner = nginx
listen.group = nginx
user = nginx
group = nginx

We can now start the PHP package by running the command below:

$ sudo systemctl start php-fpm
$ sudo systemctl enable php-fpm

To enable Nginx to process PHP pages, we are going to create a configuration file using nano text editor

$ sudo nano /etc/nginx/conf.d/default.conf

Then, enter the following details:

server {
    listen   80;
    server_name  192.0.0.1;

    # note that these lines are originally from the "location /" block
    root   /usr/share/nginx/html;
    index index.php index.html index.htm;

    location / {
        try_files $uri $uri/ =404;
    }
    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

Save the file by pressing CTRL+X, Y, and Enter. Then, restart the Nginx web server:

$ sudo systemctl restart nginx

To test if PHP is working, create PHP info file:

$ sudo nano sudo nano /usr/share/nginx/html/info.php

Then enter the information below:

<?php
 phpinfo();
?>

Save and close the file. Then, on a web browser visit the page below and remember to replace 192.0.0.1 with your IP address:

http://192.0.0.1/info.php

You should see a page similar to the one shown below with lots of information about PHP

Conclusion

That’s all when it comes to installing the LEMP stack on your VPS machine. Once the setup is complete, you can import your website files and have a fully functioning web server.

Remember to point your domain name DNS records the public IP address associated with your VPS machine.

New to VPS hosting and cloud computing. Sign up with Digitial Ocean today and enjoy up to $100 worth of free trial credit.

Ads by Google

Leave a Reply

Your email address will not be published. Required fields are marked *