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

How to Install Nginx, MySQL, PHP v7 (LEMP) Stack 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 VPS 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.

Step 1: Installing 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

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

Step 2: Installing 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.

Step 3: Installing 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

Step 4: Configuring 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, ENginx, 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!

The Author

Author

Edgaras G. / @edgaras

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.

Related tutorials

Author

Gary Westergren Reply

April 29, 2017

This tutorial was a lifesaver. I could not get PHP-FPM to work on my newly-setup LEMP with VestaCP. For hours I only had LEM with no P because I could not get PHP-FPM configured. Your instructions got me past a huge learning curve. Everything except phpMyAdmin is working now.

Author

Hassan Paknia Reply

June 17, 2017

Awesome post. Works as expected!

Author

irwan Reply

July 30, 2017

when I create this file 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; } } my nginx can't run, once I check sudo nginx -t, system said nginx: [emerg] unknown directive "my servername" in /etc/nginx/conf.d/default.conf:4 nginx: configuration file /etc/nginx/nginx.conf test failed how to fix it? regards

    Author

    pvsukale

    Replied on August 07, 2017

    Hey try replacing that line with `server_name ;`

Author

Anders Reply

August 03, 2017

Hi great turtorial just one problem i want install phpbb on it but It dont work..... any that have install it and It working .....please share it!

Author

Steve Reply

September 26, 2017

I've gone through this tutorial in setting up a new server, but how can I check to see if PHP is working properly? I tried using a file designed to output phpinfo as a test, but it simply downloads instead of loading in the browser.

    Author

    Matt

    Replied on November 17, 2017

    I have this result also.

      Author

      Josha H.

      Replied on April 15, 2018

      Hey! It's probably because the location directive for .php-files wasn't put in the nginx.conf file. If you input the following code below the other location directives (like the '/'-one) it should work: 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; } Good luck! ;)

    Author

    Kerns Noel

    Replied on April 10, 2019

    --- Put that in a php file, inside the html directory & use a browser to see it.

Author

Garry Reply

January 06, 2018

Your website design sucks. Can't open a site in half on desktop alongside with terminal all commands float too much on left. Had to fix some CSS manually in inspection mode. And somehow those reditt share button is popping up when clicking on comments box. Thank you so much for this tutorial though

    Author

    Garry

    Replied on January 06, 2018

    oh and who puts those commands in table next time use

    or   html tags

      Author

      Domantas G.

      Replied on January 15, 2018

      Hey, Sorry, but where do you see commands in the table?

    Author

    Domantas G.

    Replied on January 15, 2018

    Hey Garry, Thank you for honest feedback. We are in a process of redesigning this our tutorials portal.

Author

Jorge Cordero Reply

April 06, 2018

To test installation create a file /usr/share/nginx/html/info.php with this content "<?php phpinfo();" If you open the page in your browser, and it download the page instead of showing content, then you have to comment in /etc/nginx/nginx.conf the "server" block below the line "include /etc/nginx/conf.d/*.conf;" Save, restart nginx and its solved.

    Author

    Domantas G.

    Replied on April 28, 2018

    Hey Jorge, Nice one, thanks!

Author

Roy Toledo Reply

July 17, 2018

Thanks! Saved me! The best tutorial out there for PHP7!

Author

Waqas Ahmad Khan Reply

August 08, 2018

Thanks, this guide really helped.

Author

Lukman Suprayitno Reply

April 05, 2019

worked perfectly

Author

Bawa Geek Reply

August 12, 2019

Works as expected, Thanks a lot

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Become a part of Hostinger now!

More in VPS
How to Connect to Your Account Using PuTTY SSH Terminal

Close