How to Install LEMP Stack (Nginx, MySQL, PHP v7) on CentOS 7

How to Install LEMP Stack (Nginx, MySQL, PHP v7) on CentOS 7

In this tutorial, you will learn how to install LEMP stack on a CentOS 7 server. It is one of the most popular groups of software that you can use to build your website. Let’s get right into it.

What Is LEMP

LEMP is a stack of programs that work collectively to serve dynamic websites or web applications. It consists of Linux (the server’s operating system), ENginX/Nginx (web server application), MySQL (database management system), and PHP (scripting language).

LEMP is a popular alternative to LAMP, which uses Apache instead of Nginx and focuses on serving static web pages. There are also WAMP and MAMP that replace Linux with Windows and macOS.

Before we begin the tutorial on how to install LEMP, make sure that your server or VPS is running on CentOS 7. If you’re using a Hostinger Linux VPS hosting plan, install CentOS from the Servers tab of hPanel.

How to Install LEMP Stack

Now that Linux is installed, we are going to guide you through the installations of Nginx, MySQL, and PHP.

This tutorial requires you to have root access and use an SSH client like PuTTY (Windows) or terminal shell (Linux, macOS) to connect to your VPS.

1. Install Nginx on CentOS 7

  1. Since Nginx is not available in default CentOS repositories, we need to install EPEL repository first by running this command:
    yum install epel-release -y
  2. Next, we will install Nginx itself:
    yum install nginx -y
  3. After the installation is completed, type the following lines one by one to activate Nginx:
    systemctl start nginx
    systemctl enable nginx
  4. Check whether Nginx is running or not by visiting your server’s public IP address. Your page should look like this:
    Nginx welcome page once Nginx is installed succesfully

Pro Tip

Use the sudo command if you don’t have root access.

2. Install MySQL (MariaDB)

We’re going to install MySQL using MariaDB service, a community fork of MySQL.

  1. MariaDB is included in the default CentOS repositories, so we can simply run the yum command to install it:
    yum install mariadb-server mariadb -y
  2. After the installation is completed, enable and start MariaDB service:
    systemctl start mariadb
    systemctl enable mariadb
  3. Secure the installation by running the following command:
    mysql_secure_installation

    MariaDB will ask you for the root password, which you do not have because you’ve just installed MySQL. For that reason, simply press enter.

  4. The next prompt will ask if you want to set a root password. Enter Y and follow the instructions:
    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 authorization.
    
    New password:
    Re-enter new password:
    Password updated successfully!
    Reloading privilege tables..
    ... Success!
  5. The script will return a few security questions. We recommend you to press Y for all of them.
    Remove anonymous users? [Y/n]
    Disallow root login remotely? [Y/n]
    Remove test database and access to it? [Y/n]
    Reload privilege tables now? [Y/n]

    After you complete the setup, proceed to PHP installation.

3. Install PHP v7.3

  1. We need to download and install an additional CentOS repository that contains the required packages for PHP v7.3. Run these commands one after another:
    wget http://rpms.remirepo.net/enterprise/remi-release-7.rpm
    rpm -Uvh remi-release-7.rpm
  2. Enable php73 repository, which is disabled by default:
    yum install yum-utils -y
    yum-config-manager --enable remi-php73
  3. After that, install the PHP package:
    yum --enablerepo=remi,remi-php73 install php-fpm php-common

    When asked for installation permission, simply press Y.

  4. Now, install common PHP modules to make sure the service is working properly:
    yum --enablerepo=remi,remi-php73 install php-opcache php-pecl-apcu php-cli php-pear php-pdo php-mysqlnd php-pgsql php-pecl-mongodb php-pecl-redis php-pecl-memcache php-pecl-memcached php-gd php-mbstring php-mcrypt php-xml

4. Configure Nginx to Work with PHP 7

The last thing we need to do is configure Nginx to work with PHP 7. Here’s how you do it:

  1. Create a new Nginx configuration file by running nano text editor:
    nano /etc/nginx/conf.d/default.conf

    Then, insert this code:

    server {
        listen   80;
        server_name  your_server_ip;
    
        # 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;
        }
    }

    IMPORTANT! Replace your_server_ip with your actual server IP.

  2. Save the file by hitting CTRL + X (or CMD + X for Mac users). Next, restart Nginx so the changes can take effect:
    systemctl restart nginx
  3. Open PHP-FPM configuration:
    nano /etc/php-fpm.d/www.conf

    Find and change these lines:

    • user = apache to user = nginx
    • group = apache to group = nginx
    • listen.owner = nobody to listen.owner = nginx
    • listen.group = nobody to listen.group = nginx
  4. After it’s done, add the following line under ;listen = 127.0.0.1:9000
    listen = /var/run/php-fpm/php-fpm.sock
  5. Save the file by hitting CTRL + X. Lastly, start PHP-FPM and enable it on boot:
    systemctl start php-fpm.service
    systemctl enable php-fpm.service

Conclusion

LEMP (Linux, Nginx, MySQL, PHP) stack is the perfect choice to serve dynamic web pages or web applications. Fortunately, the installation process is simple. With basic knowledge of the command-line interface, you can easily set up all the required programs on your VPS.

In this article, you have learned how to install LEMP on CentOS 7. To conclude, let’s take a look at all the steps once again:

  1. Get EPEL repository and install Nginx on your server.
  2. Install and configure MariaDB
  3. Install PHP and all of its common packages.
  4. Configure Nginx to work with PHP.

Good luck and feel free to leave a comment if you have any questions!

Suggested Reading

Check out our guide on how to install SSL certificate on Linux running CentOS 7.

Author
The author

Edgaras G.

Edgaras is a veteran server administrator at Hostinger. He makes sure that every server runs at full throttle and has all the latest technological advancements. When he's not working, Edgaras enjoys skiing and exploring the world.