VPS

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

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

Introduction

Nginx is a growing open-source web server software and PHP v7 is the latest version of PHP engine. In this tutorial, we will use it to build a LEMP (Linux, ENginx, MySQL, PHP) stack server. Nginx replaces the popular Apache package found in LAMP stack.

What you’ll need

Before you begin this guide you’ll need the following:

  • root access to the VPS

Step 1 — Installing Nginx on CentOS 7

Since Nginx is not available in default CentOS repositories, we will install EPEL repository by running this command:

Next, we will install Nginx itself:

After installation completes, enable Nginx start on boot and run it:

In order to check if Nginx is running, you can visit your IP address via the browser. Firstly, locate your IP:

Then, just copy-paste it into the browser and you should see similar page:

Nginx Default Page

Step 2 — Installing MySQL (MariaDB)

Once the web server is installed, we can proceed further towards MySQL installation. MariaDB is a community fork of the old and well known MySQL service. Since MariaDB comes with default CentOS repositories we can run Yum to install it:

After finishing the installation, enable and start the service:

Lastly, run initial setup script which will remove some of the default settings:

MariaDB will ask you for the root password, however, since this is initial installation, you don’t have any, so just press enter. Next prompt will ask if you want to set a root password, enter Y and follow the instructions:

You can safely click ENTER key and accept default settings for all other questions. After you complete the setup, proceed further to PHP installation.

Step 3 — Installing PHP v7.1.0

The first thing that we will do is install additional CentOS repo which contains required packages for PHP v7.1:

Enable php71 repository which is disabled by default:

Secondly, install PHP package:

Install common modules:

Step 4 — Configuring Nginx to work with PHP 7

Create a new Nginx configuration file by running vim or nano text editor:

Input this code:

IMPORTANT! Replace your_server_ip with your actual server IP.

Save the file by hitting CTRL + X (CMD + X for Mac users). Restart Nginx for change to take effect:

Now, open PHP-FPM configuration:

Find and replace 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

And, lastly, under ;listen = 127.0.0.1:9000 add this line:

listen = /var/run/php-fpm/php-fpm.sock

Once again, save the file by hitting CTRL + X. And finally, start php-fpm and enable it on boot:

Conclusion

Maybe LEMP stack installation requires more configuration than the very well known LAMP setup, but you can be assured that you will be using the latest technology from both worlds – fast PHP-FPM v7 processor package with modern Nginx web service. That is all for now, feel free to check other VPS tutorials on our VPS tutorial page.

6 Comments

Click here to post a comment

  • 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.

  • 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

  • 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!

  • 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.

Join thousands of subscribers worldwide

and get awesome deals & the newest tutorials to your email

Please wait...

Thank you for sign up!

More in VPS
How to Make a CS:GO Server On Linux VPS
How to Make a CS:GO Server On Linux VPS

Close