How To

How To Install Linux, Nginx, MySQL, PHP (LEMP stack) in Ubuntu 16.04

Google+ Pinterest LinkedIn Tumblr

Introduction of LEMP

LEMP is a very popular stack in the web development which can be used to serve the dynamic web pages. LEMP is a combination of the famous open-source operating system Linux, web server NGINX, one of the most popular databases MySQL for the development of mid-range of web application and one of the most used programming language PHP for the web development.

In this tutorial, we will learn how to install LEMP stack in Ubuntu 16.04 by step by step. LEMP stack is a very good option for the beginners to start with.

NOTE: Here, You do not confuse LEMP with the LAMP stack. The LAMP stack is another very famous stack for serving the web applications. We will write it about that in any other tutorial.

Install the Nginx Web Server

Nginx is a very high-performance web server which is used mostly for the reverse proxy, load balancer and for the cache.

First, we update our server by running the below command in the terminal so that all our packaged will be updated.

sudo apt-get update

After updating the packages run the below command to install the nginx

sudo apt-get install nginx

After successfully running the above command, go to your browser and type the address which you receive. You will see the Nginx welcome message or you can see the nginx status by running the below command

systemctl status nginx

Install MySQL Database

Now that we have a web server, we need a database management system to store the data of our website. For this, we are going to install one of the most popular databases for web application development MySQL. Run the below command to install the MySQL in the system

sudo apt-get install mysql-server

Provide the root database password for the mysql when asked and finish the installation. MySQL is now installed but it is not secure yet. To secure the MySQL you need to run the below command in the terminal

mysql_secure_installation

You will ask to enter the root password which you set in the previous step. After entering the root password you will be asked for the VALIDATE PASSWORD PLUGIN. Type y/yes if you want to enable this otherwise press any other key to continue the step. For rest of the process, you just type y/yes and press enter to complete this step.

After completing all of the above steps successfully, your database system is running successfully in your system.

Install PHP

We will install php-fpm which stands for “FastCGI Process Manager” because Nginx does not contain the native PHP processing. To install php-fpm run the below command

sudo apt-get install php-fpm php-mysql

Currently, our php is not secure. To secure this we will edit the php configuration file php.ini which you can find in the directory /etc/php/7.0/fpm/php.ini

Open the php.ini file in your favourite editor and find the cgi.fix_pathinfo line in the terminal and change value from “1” to “0” and uncomment this line.

Now, just restart the php processor by typing below command

sudo systemctl restart php7.0-fpm

Configure Nginx to Use the PHP Processor

Now, we tell our Nginx web server to use PHP processor to serve the dynamic web pages by configuring the Nginx server block. Open the Nginx default server block by typing the below command in the terminal

sudo nano /etc/nginx/sites-available/default

After removing all the comments from the file, our file looks like below

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    
    root /var/www/html;
    
    index index.html index.htm index.nginx-debian.html;
    
    server_name _;

    location / {
        try_files $uri $uri/ =404;
    }
}

In the above, we need to do some changes

  • First, add index.php file so that index.php file can be served
  • Enter the server name in place of _ in the server name tag
  • We will uncomment some line of the default code so that file handle php requests
  • We also uncomment the location dealing with .htaccess

After doing the above changes, now our file looks like below

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www/html;
    index index.php index.html index.htm index.nginx-debian.html;

    server_name server_domain_or_IP;

    location / {
        try_files $uri $uri/ =404;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }

    location ~ /\.ht {
        deny all;
    }
}

Now save the file and check if everything is working fine by running below command

sudo nginx -t

If everything works fine then reload the server by typing below the line in the terminal

sudo systemctl reload nginx

Now our LEMP stack is completely set up and we will create a php file to test the setup. Create a simple php file in the root directory /var/www/html/test_file.php and paste the below line of code or any other code which you want

<?php
phpinfo();

Save and close the file and got to your browser and type the URL with the newly created file. If you see the desired output then your LEMP stack configure properly otherwise follow the above steps again.

Conclusion

Now you have a LEMP stack running on your Ubuntu 16.04 system. You can build awesome web applications using the Stack and your knowledge.

Write A Comment

Pin It