How to Install LEMP on Ubuntu 16.04

r00t July 24, 2017

Install LEMP on Ubuntu 16.04

In this tutorial we’ll learn how to install LEMP on Ubuntu 16.04. We will also install and configure its prerequisites. LEMP (a server stack) stands for Linux, Nginx, MariaDB, and PHP. Nginx is a free, open-source, high-performance HTTP web server; known for its stability, rich feature set, simple configuration, and low resource consumption.

Install LEMP on Ubuntu 16.04

Step 1. First, ensure your system and apt package lists are fully up-to-date by running the following:

apt-get update -y && apt-get upgrade -y

Step 2. Installing and configuring Nginx.

Enter this command to install Nginx Web server:

sudo apt install nginx

Start Nginx service using the following command:

systemctl start Nginx

Enable Nginx to start upon reboot:

systemctl enable nginx

Step 3. Installing and configuring MySQL.

Now that we have a web server, we need to install MySQL, a database management system, to store and manage the data for our site. You can install this easily by running the following:

apt-get install mysql-server

Start MySQL service using the following command:

systemctl start mysql

The output of the command above should be similar to this:

● nginx.service - A high performance web server and a reverse proxy server
 Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
 Active: active (running)
 Main PID: 5926 (nginx)
 CGroup: /system.slice/nginx.service
 ├─5924 nginx: master process /usr/sbin/nginx -g daemon on; master_process on
 ├─5925 nginx: worker process
 └─5926 nginx: worker process

Auto-start the Mysql service during the every boot, type the following on terminal and hit enter:

systemctl enable mysql

Now run the post installation security script:

mysql_secure_installation

Follow the prompts as below:

  • Would you like to setup VALIDATE PASSWORD plugin? press Enter here for No
  • Please set the password for root here. New password: Type a secure password here then press Enter
  • Re-enter new password: Re-type the secure password here then press Enter
  • Remove anonymous users? (Press y|Y for Yes, any other key for No) : press y and then Enter here
  • Disallow root login remotely? (Press y|Y for Yes, any other key for No) : press y and then Enter here
  • Remove test database and access to it? (Press y|Y for Yes, any other key for No) : press y and then Enter here
  • Reload privilege tables now? (Press y|Y for Yes, any other key for No) : press y and then Enter here

Step 4. Installing and configuring PHP 7.

Install PHP through PHP-FPM (PHP-FastCGI Process Manager), an alternative PHP FastCGI implementation. It provides additional features useful for sites of any size, especially busier sites. Install php-fpm by issuing the following command:

apt-get install php-fpm php-mysql php-cli

Now start php7.0-fpm:

systemctl start php7.0-fpm

Set php7.0-fpm to start on reboot:

systemctl enable php7.0-fpm

Next, Create a test index.php file:

nano /var/www/html/index.php

Paste in the following code, then save and exit the file:

<?php
echo "Hello world, I'm PHP!";
?>

Now edit the deafult Nginx config file:

nano /etc/nginx/sites-available/default

Find the following lines:

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# include snippets/fastcgi-php.conf;
#
# # With php7.0-cgi alone:
# fastcgi_pass 127.0.0.1:9000;
# # With php7.0-fpm:
# fastcgi_pass unix:/run/php/php7.0-fpm.sock;
#}

Replace the lines above with the lines below:

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
 include snippets/fastcgi-php.conf;

# With php7.0-cgi alone:
 #fastcgi_pass 127.0.0.1:9000;
 # With php7.0-fpm:
 fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}

Now restart php7.0-fpm and nginx:

systemctl restart php7.0-fpm
systemctl restart nginx

In your browser, you can go to your server’s IP address in your browser once again and you should see the text “Hello world, I’m PHP!” instead of the default Nginx page.

Install LEMP on Ubuntu 16.04

Congratulation’s! You have successfully configured LEMP Stack on your Ubuntu 16.04 LTS server.

Save

Save

Save

Save

The Tags:

Leave a Comment

Comments are closed.