This README documents the process of deploying a LEMP stack (Linux, Nginx, MySQL, PHP) on an AWS EC2 instance running Ubuntu 24.04 LTS. The LEMP stack is used to serve dynamic web pages and applications and is an alternative to the LAMP stack (where Apache is used instead of Nginx).
- Introduction
- Prerequisites
- Launching an EC2 Instance
- Installing Nginx
- Installing MySQL
- Installing PHP
- Configuring Nginx for PHP Processing
- Setting Up the Database
- Creating a PHP Application
- Testing the Application
- Conclusion
- References
The LEMP stack (Linux, Nginx, MySQL, PHP) is a group of open-source software used to serve dynamic websites and web applications. Nginx, pronounced "Engine-X", is chosen over Apache due to its efficient handling of concurrent connections, especially under heavy load. This guide covers the step-by-step process of deploying LEMP on an AWS EC2 instance.
Related Project: LAMP Stack Implementation
Before starting, ensure you have the following:
- An AWS account and basic knowledge of EC2.
- An understanding of SSH to connect to your instance.
- AWS CLI or PuTTY installed to connect to your instance (depending on your OS).
- Log in to your AWS Console.
- Navigate to EC2 Dashboard and click Launch Instance.
- Choose the Ubuntu 24.04 LTS AMI.
- Select an instance type (e.g.,
t2.micro
for free-tier). - Configure the instance, ensuring you allow HTTP (port 80) and SSH (port 22) in your security group.
- Launch the instance and connect using SSH:
ssh -i "your-key.pem" ubuntu@<your-ec2-public-ip>
Video tutorial: How to Set Up a LAMP Stack on AWS EC2 with Ubuntu | Step-by-Step Tutorial
Nginx is the web server used in the LEMP stack.
sudo apt update
sudo apt install nginx
sudo systemctl status nginx # Check Nginx status
If Nginx is running correctly, you will see
active (running)
as the output.
In your browser, navigate to:
http://<ec2-instance-public-ip>
You should see the Nginx welcome page.
MySQL is the database server in the LEMP stack.
sudo apt install mysql-server
sudo mysql_secure_installation # Follow the prompts to secure MySQL
- Log in to the MySQL shell:
sudo mysql
- Set a password for the root user:
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'Password.1';
- Create a database and user for your application:
CREATE DATABASE test_db; CREATE USER 'testuser'@'localhost' IDENTIFIED BY 'Password.1'; GRANT ALL PRIVILEGES ON test_db.* TO 'testuser'@'localhost'; FLUSH PRIVILEGES; EXIT;
PHP is the scripting language used to generate dynamic content in web applications.
sudo apt install php-fpm php-mysql
php-fpm
is the PHP FastCGI Process Manager, which handles PHP processing for Nginx.
- Create info.php file in the default website root
sudo nano /var/www/info.php
- Add the following content:
<?php
phpinfo();
?>
In your browser, navigate to:
http://<ec2-instance-public-ip>/info.php
Kindly remove the info.php when done as it holds very sensitive information about you web server.
Note: you need to configure the default nginx conf to point to the php by editing the default configuration.
By default, Nginx does not handle PHP files. You need to configure it to pass PHP requests to php-fpm
.
sudo mkdir /var/www/projectLEMP
sudo chown -R $USER:$USER /var/www/projectLEMP
-
Create a new configuration file for the site:
sudo nano /etc/nginx/sites-available/projectLEMP
-
Add the following content:
server { listen 80; server_name projectLEMP www.projectLEMP; root /var/www/projectLEMP; index index.html index.php; location / { try_files $uri $uri/ =404; } location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/var/run/php/php8.3-fpm.sock; } location ~ /\.ht { deny all; } }
-
Enable the configuration:
sudo ln -s /etc/nginx/sites-available/projectLEMP /etc/nginx/sites-enabled/ sudo nginx -t # Test configuration
-
Disable the default configuration:
sudo unlink /etc/nginx/sites-enabled/default
-
Restart Nginx:
sudo systemctl restart nginx
-
create an index.php by running the following command:
PUBLIC_IP=$(curl -s http://169.254.169.254/latest/meta-data/public-ipv4)
echo "Hello LEMP from projectLEMP with public IP $PUBLIC_IP" | sudo tee /var/www/projectLEMP/index.html
"|" is a pipe command that help to pass output of one left command as input to the right command while the "tee" command help to simultaneously write the input into the file and also display in the console In your browser, navigate to:
http://<ec2-instance-public-ip>
Create a table in the MySQL database to store tasks for a todo list:
USE test_db;
CREATE TABLE todo_list (
item_id INT AUTO_INCREMENT,
content VARCHAR(255),
PRIMARY KEY(item_id)
);
INSERT INTO todo_list (content) VALUES ('My first important item');
INSERT INTO todo_list (content) VALUES ('My second important item');
INSERT INTO todo_list (content) VALUES ('My third important item');
Create a simple PHP application to display the contents of the todo list.
sudo nano /var/www/projectLEMP/todo_list.php
<?php
$user = 'testuser';
$password = 'Password.1';
$database = 'test_db';
$table = 'todo_list';
try {
$db = new PDO('mysql:host=localhost;dbname=' . $database, $user, $password);
echo '<h2>Todo List</h2><ol>';
$todo_list = $db->query("SELECT content FROM $table");
foreach($todo_list as $row) {
echo '<li>' . $row['content'] . '</li>';
}
echo '</ol>';
} catch (PDOException $e) {
echo 'Error: ' . $e->getMessage() . '<br/>';
die();
}
?>
Navigate to your EC2 instance's public IP to test the PHP application:
http://<ec2-instance-public-ip>/todo_list.php
You should see the todo list items displayed.
In this guide, you have successfully implemented the LEMP stack on an AWS EC2 instance running Ubuntu 24.04 LTS. You installed Nginx as the web server, MySQL as the database, and PHP for dynamic content processing. You also created a simple PHP application to interact with your MySQL database.
- Nginx Documentation
- MySQL Documentation
- PHP PDO Documentation
- AWS EC2 Documentation
- LEMP vs LAMP Stack
- Video Tutorial
- Blog Post
Happy coding! 🚀