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:

yum install epel-release -y

Next, we will install Nginx itself:

yum install nginx -y

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

systemctl start nginx
systemctl enable nginx

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

dig +short myip.opendns.com @resolver1.opendns.com

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:

yum install mariadb-server mariadb -y

After finishing the installation, enable and start the service:

systemctl start mariadb
systemctl enable mariadb

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

mysql_secure_installation

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:

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: password
Re-enter new password: password
Password updated successfully!
Reloading privilege tables..
 ... Success!

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:

wget http://rpms.remirepo.net/enterprise/remi-release-7.rpm
rpm -Uvh remi-release-7.rpm

Enable php71 repository which is disabled by default:

yum install yum-utils -y
yum-config-manager --enable remi-php71

Secondly, install PHP package:

Looking for a better way to host your website?

Transfer your website to the fastest web hosting platform with 24/7 dedicated support.

yum --enablerepo=remi,remi-php71 install php-fpm php-common

Install common modules:

yum --enablerepo=remi,remi-php71 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

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

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

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

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

systemctl restart nginx

Now, open PHP-FPM configuration:

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

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:

systemctl start php-fpm.service
systemctl enable php-fpm.service

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.

14 Comments

Click here to post a comment

Leave a Reply to Garry Cancel reply

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

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

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

  • 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

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

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

Close