DEV Community

Cover image for How to implement a LEMP Stack on a ubuntu server in AWS EC2
Israel .O. Ayanda
Israel .O. Ayanda

Posted on • Updated on

How to implement a LEMP Stack on a ubuntu server in AWS EC2

In this post, I will demonstrate steps on how you can implement a LEMP stack on ubuntu server in AWS EC2 virtual machine.

LEMP is an acronym that stands for
Linux - Operating System
Nginx (pronounced as engine-x, hence the E in the acronym). web server.
MySql - Database.
Php - Scripting Language.

LEMP is a variation of LAMP (Linux, Apache, MySql and Php) and a common stack used in web development and deployment.
In this post, I will show you how to deploy a simple php app(todo_list.php) to test a our configuration and deployment.

Requirements:
Basic Linux skills
Basic MySql skills
AWS Free Account Here

STEP ONE

Login into your AWS account, Lunch Ec2 instance with your prefered settings.

  1. SSH into the Linux terminal AWS EC2 to update the packages and install nginx webserver.

sudo apt update

Image description

sudo apt install nginx

Image description

  1. Verify ngnix is successfully running.

sudo systemctl status nginx

Image description

  1. To access the nginx web server locally and in the browser - make sure the default (port 80) is opened in your security group settings.
  • Locally curl http://localhost

Image description

  • In the browser (use the public Ip address of the vm) curl http://44.203.157.197/

Image description

STEP TWO

Installation of the database - MySql.

  1. Install MySql

sudo apt install mysql-server

Image description

  1. Login into MySql

sudo mysql

Image description

  1. Currently, we are login as the Admin (sudo mysql). Set a new password for the Admin/root by using mysql_native_password as default authentication method and the exit.

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'PassWord.1';

Image description

  1. For security reasons, we need to remove all insecure default settings (e.g, password strength, test database e.t.c) by a pre-installed script.

sudo mysql_secure_installation;

Image description

  1. Test MySql Admin/root login and exit.

sudo mysql -p

Image description

STEP THREE

PHP installation

  1. We would need to install two packages.
    • php fastCGI protocol for interfacing interactive programs with a web serve and minimize the memory consumption and rise the performance for the web server with heavy traffic.
  • Php-MySQL - php module that allows communication between php and mysql database.

sudo apt install php-fpm php-mysql

Image description

STEP FOUR

Configuring Nginx to use the Php processor.

  1. Create a web directory to host our application which is different from the default at /var/www/html. Additional, assign ownership of the current system user to the newly create directory.

sudo mkdir /var/www/projectLEMP

sudo chown -R $USER:$USER /var/www/projectLEMP

Image description

  1. Create a new configuration file for projectLEMP directory.

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

Image description

Image description

  1. Activate your configuration by linking to the config file from Nginx’s sites-enabled directory and test for error in the configuration file.

sudo ln -s /etc/nginx/sites-available/projectLEMP /etc/nginx/sites-enabled/

Image description

  1. Test configuration for error

sudo nginx -t

Image description

  1. Disable the default ngix host/directory defualt which listen on port 80 and reload ngnix.

sudo unlink /etc/nginx/sites-enabled/default

sudo systemctl reload nginx

Image description

  1. Create an index.html to test the custom directory proectLEMP.

sudo echo 'Hello LEMP from hostname' $(curl -s http://169.254.169.254/latest/meta-data/public-hostname) 'with public IP' $(curl -s http://169.254.169.254/latest/meta-data/public-ipv4) > /var/www/projectLEMP/index.html

Image description

  • View in browser (using public ip address).

Image description

  • View in browser (using dns address).

Image description

STEP FIVE

Test to make sure Nginx can handle .php files.

  1. Create a info.php.

sudo nano /var/www/projectLEMp/info.php

Image description

Image description

  1. Test in the browser with the vm dns or ip.

Image description

  1. For security reasons, delete the info.php file. It can also be generated we needed.

sudo rm /var/www/projectLEMp/info.php

Image description

STEP SIX

Retrieving data from MySQL using Php.

  1. Login into mysql (Admin/root account) sudo mysql -p

Image description

  1. Create a database

create database project2db

Image description

  1. Create user and password.

CREATE USER 'project2_user'@'%' IDENTIFIED WITH mysql_native_password BY 'PassWord.2';

Image description

  • Grant full privileges and exit.

Image description

  1. Login with the new user credentials.

mysql -u example_user -p

Image description

  1. View the databases in the MySQL database sever.

show databases;

Image description

  1. Create a table

CREATE TABLE project2db.todo_list ( item_id INT AUTO_INCREMENT, content VARCHAR(255), PRIMARY KEY(item_id));

Image description

  1. Insert some records into the table.

CREATE TABLE project2db.todo_list ( item_id INT AUTO_INCREMENT, content VARCHAR(255), PRIMARY KEY(item_id));

Image description

  1. Confirm the data is saved into the table and exit.

SELECT * FROM project2db.todo_list;

Image description

  1. Create a php page to fetch the data from the database.

nano /var/www/projectLEMP/todo_list.php

Image description

Image description

save and close.

  1. Use your public Ip to view the sample todo_list.php page in the browser.

Image description

Congratulations!!! You have successfully deploy a LEMP stack on AWS EC2. Similarly, you can deploy your LEMP application and additionally, configure the DNS with the public IP to you use your domain name.

I hope this helps someone.

Please feel free to share your tips, questions, corrections in the comments!

Thank you for reading!

Top comments (0)