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.
- SSH into the Linux terminal AWS EC2 to update the packages and install nginx webserver.
sudo apt update
sudo apt install nginx
- Verify ngnix is successfully running.
sudo systemctl status nginx
- 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
- In the browser (use the public Ip address of the vm)
curl http://44.203.157.197/
STEP TWO
Installation of the database - MySql.
- Install MySql
sudo apt install mysql-server
- Login into MySql
sudo mysql
- 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';
- 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;
- Test MySql Admin/root login and exit.
sudo mysql -p
STEP THREE
PHP installation
- 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
STEP FOUR
Configuring Nginx to use the Php processor.
- 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
- Create a new configuration file for projectLEMP directory.
sudo nano /etc/nginx/sites-available/projectLEMP
- 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/
- Test configuration for error
sudo nginx -t
- 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
- 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
- View in browser (using public ip address).
- View in browser (using dns address).
STEP FIVE
Test to make sure Nginx can handle .php files.
- Create a info.php.
sudo nano /var/www/projectLEMp/info.php
- Test in the browser with the vm dns or ip.
- For security reasons, delete the info.php file. It can also be generated we needed.
sudo rm /var/www/projectLEMp/info.php
STEP SIX
Retrieving data from MySQL using Php.
- Login into mysql (Admin/root account)
sudo mysql -p
- Create a database
create database project2db
- Create user and password.
CREATE USER 'project2_user'@'%' IDENTIFIED WITH mysql_native_password BY 'PassWord.2';
- Grant full privileges and exit.
- Login with the new user credentials.
mysql -u example_user -p
- View the databases in the MySQL database sever.
show databases;
- Create a table
CREATE TABLE project2db.todo_list ( item_id INT AUTO_INCREMENT, content VARCHAR(255), PRIMARY KEY(item_id));
- Insert some records into the table.
CREATE TABLE project2db.todo_list ( item_id INT AUTO_INCREMENT, content VARCHAR(255), PRIMARY KEY(item_id));
- Confirm the data is saved into the table and exit.
SELECT * FROM project2db.todo_list;
- Create a php page to fetch the data from the database.
nano /var/www/projectLEMP/todo_list.php
save and close.
- Use your public Ip to view the sample todo_list.php page in the browser.
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!
Top comments (0)