Site logo
Published on

A Quick Setup Guide for LEMP Stack, Laravel and Supervisord with custom AWS EC2 AMI

Authors
  • avatar Nguyen Duc Xinh
    Name
    Nguyen Duc Xinh
    Twitter

Are you looking to streamline the process of setting up a LEMP (Linux, Nginx, MySQL, PHP) stack for your web applications on AWS EC2? You're in the right place! In this guide, I'll show you how to use a custom Amazon Machine Image (AMI) that comes pre-configured with the essential components of the LEMP stack. Whether you're a developer, a DevOps engineer, or just someone looking to save time on server setup, this guide is for you.

About the Custom AWS AMI (ami-0122ba9d1dd0d2f2e)

Before we dive into setting up your LEMP stack using this custom AMI, let's take a moment to introduce the AMI itself and the creator behind it.

AMI Details

AMI ID: ami-0122ba9d1dd0d2f2e
Region: ap-northeast-1 - Asia Pacific (Tokyo)
Author: DucXinhIT

Stack Versions

Ubuntu: 22.04
Nginx: nginx/1.18.0
PHP: 8.2.12
MySQL: 8.0.34
Laravel: 10.29.0
Composer: 2.6.5
Node: v18.18.2
npm: 9.8.1
supervisord: 4.2.1
Docker: N/A

This custom AMI is crafted to include the latest versions of the components that make up the LEMP stack. It's tailored for efficient web application development and hosting, ensuring that you have a reliable foundation for your projects.

The goal of creating this custom AMI

The goal of creating this custom AMI is to simplify and expedite the setup of LEMP stacks, empowering developers and web administrators to focus on their projects rather than infrastructure management.

Now, let's get back to setting up your LEMP stack using this custom AMI.

Prerequisites

Before we begin, make sure you have the following:

  • An AWS account with necessary permissions.
  • An EC2 instance where you want to launch the custom LEMP stack.

Step 1: Launch an EC2 Instance

  1. Log in to your AWS Management Console.
  2. Navigate to the EC2 dashboard and click on "Launch Instance." EC2 dashboard
  3. Under Name and tags, for Name, enter a name to identify your instance. E.g awesome-laravel-web
  4. Under Application and OS Images (Amazon Machine Image), for search input, enter the custom AWS AMI. ami-0122ba9d1dd0d2f2e EC2 dashboard
  5. Press Enter
  6. Click tab Community AMIs and click Select on the first result AMI EC2 dashboard
  7. Under Instance type, for Instance type, select an instance type that meets your web server needs. This tutorial uses t2.micro(Free tier eligible)
  8. Under Key pair (login), for Key pair name, choose your key pair. This tutorial uses awesome-laravel-web EC2 dashboard
  9. Under Network settings, do the following:
    • Allow SSH traffic from: choose My IP
    • Allow HTTPs traffic from the internet: select
    • Allow HTTP traffic from the internet: select EC2 dashboard
  10. In the Summary panel, review your instance configuration and then choose Launch instance. EC2 dashboard
  11. Waiting ultil your instance go into the running state EC2 dashboard EC2 dashboard
  12. You can see your EC2 IP: 18.183.81.105
  13. Open browser and navigate to 18.183.81.105 to see the result EC2 dashboard

Step 2: Connect to the EC2 Instance

Once the instance is running, connect to it via SSH. Use the command provided by AWS with the key pair you selected.

ssh -i /path/to/your-key.pem ubuntu@your-instance-public-ip
# In this tutorial
ssh -i awesome-laravel-web.pem ubuntu@18.183.81.105
# If you have trouble, try to set permission to ssh file by following command:
chmod 400 awesome-laravel-web.pem

You are now connected to your EC2 instance with the custom LEMP stack. EC2 dashboard

Step 3: Verify the LEMP Stack

Check the OS version

cat /etc/lsb-release

Check the Nginx web server:

sudo systemctl status nginx

Verify PHP is installed:

php -v

Confirm MySQL is up and running:

mysql --version

Ensure Composer is installed:

composer --version

Ensure Docker is installed:

sudo docker -v

Ensure Supervisor is up and running

sudo supervisorctl status

EC2 dashboard

Step 4: Start Building Your Web Application

Now that you have a working LEMP stack, you can start developing and deploying your web applications. Simply upload your code, configure your virtual hosts in Nginx, and connect your application to the MySQL database.

  1. Update your code
cd /var/www/apps/laravel
ls -la
git pull origin main

EC2 dashboard 2. You can replace to your git repository and deploy your code.

# Syntax
## With https
git remote set-url origin https://github.com/<owner>/<repo-name>.git
git remote set-url origin https://github.com/ducxinh/laravel-sample.git
## With ssh
git remote set-url origin git@github.com:<owner>/<repo-name>.git
git remote set-url origin git@github.com:ducxinh/laravel-sample.git

git pull origin main

Conclusion

Using a custom AWS EC2 AMI can significantly reduce the time and effort required to set up a LEMP stack for your web projects. This guide has shown you how to launch an instance from a pre-configured AMI, connect to it, and confirm that the LEMP stack is ready for your web development or hosting needs.

Save time, automate your server setup, and focus on what matters most: building and deploying web applications.

Happy coding!