Installing LEMP Stack (Nginx, MySQL, PHP) On CentOS 7

Introduction

A LEMP programming stack is a gathering of open source programming that is commonly introduced together to empower a server to have dynamic sites and web applications. This term is really an acronym which speaks to the Linux working framework, with the Nginx web server (which replaces the Apache part of a LAMP stack). The site information is put away in a MySQL database (utilizing MariaDB), and dynamic substance is prepared by PHP.

In this guide, we’ll get a LEMP stack introduced on a CentOS 7 VPS. CentOS will satisfy our first prerequisite: a Linux working framework.

Requirements

First you need a VPS running CentOS 7 with root privileges.  Connection to your VPS via SSH or similar protocol. You can download SSH client PuTTY or BitVise, I personally recommend BitVise.

Step 1: Updating

First thing we are going to do is updating the vps. To do that insert bellow command.

# yum update

Step 2: Installing Nginx

To install nginx first we need to install the CentOS 7 EPEL repository.

# yum install epel-release

After that we will install nginx.

# yum install nginx

Once it is installed, you can start Nginx on your VPS

# systemctl start nginx

Enable nginx on boot

# systemctl enable nginx

Step 3: Installing MySQL (MariaDB)

# yum install mariadb-server mariadb

After installation completes, we will start mariadb

# systemctl start mariadb

then we install mysql by

# mysql_secure_installation

Then we will enable mysql

# systemctl enable mariadb

Step 4: Installing PHP

# yum install php php-mysql php-fpm

After installation edit the config file by

# nano /etc/php.ini

If your vps doesn’t have nano install it by

# yum install nano

Find the text cgi.fix_pathinfo and uncomment it and set it like bellow

cgi.fix_pathinfo=0

Next, open the php-fpm configuration file www.conf:

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

Discover the line that determines the listen parameter, and change it so it would appear that the accompanying:

Change it to

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

Next, discover the lines that set the listen.owner and listen.group and uncomment them. They should resemble this:

listen.owner = nobody

listen.group = nobody

In conclusion, discover the lines that set the client and gathering and change their qualities from “apache” to “nginx”:

user = nginx

group = nginx

At that point save and quit.

Now we will start and enable php

# systemctl start php-fpm

# systemctl enable php-fpm

Step 5: Configuring PHP for Nginx

Open the file /etc/nginx/conf.d/default.conf if  it doesn’t exists create new file by:

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

Change accordingly your needs

server {
    listen       80;
    server_name  server_domain_name_or_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;
    }
}
Restart nginx
 # systemctl restart nginx

Step 6: Testing PHP

Create a file

# nano /usr/share/nginx/html/info.php

and put bellow contents

<?php phpinfo(); ?>

after than navigate to http://your_domain_or_ip/info.php , you should see a php info page.

Thank you!

Leave a Reply

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