Here is a step by step guide to deploy a Laravel application on AWS EC2 using Laravel Sail with Docker. Laravel Sail is a light-weight command-line interface for interacting with Laravel's default Docker development environment. It is designed to provide a minimal configuration for Laravel apps that are using Docker.
- Create an EC2 Instance: Log in to your AWS Management Console, navigate to the EC2 Dashboard, and launch a new instance. Choose an Ubuntu Server as the Amazon Machine Image (AMI) because Laravel Sail runs in a Docker container, which is compatible with Linux environments.
- Configure Instance Details: Select the instance type and configure instance details according to your needs.
- Configure Security Group: Add rules to allow HTTP (port 80), HTTPS (port 443), and SSH (port 22) traffic.
- Launch Instance: Review and launch your instance. Make sure to select a key pair for SSH access.
- Connect to the EC2 Instance: Use the SSH key pair to connect to the EC2 instance using the terminal or an SSH client.
ssh -i /path/to/your-key-pair.pem ubuntu@your-ec2-instance-ip
- Update and Upgrade Packages: Run the following commands to update and upgrade the package list on the EC2 instance.
sudo apt update
sudo apt upgrade
- Install Docker: Run the following commands to install Docker on the EC2 instance.
sudo apt-get install -y docker.io
sudo apt-get install docker-ce docker-ce-cli containerd.io
sudo systemctl start docker
sudo systemctl enable docker
sudo usermod -aG docker ${USER}
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
- Clone the Laravel Application: Clone your Laravel application from the repository to the EC2 instance using Git.
git clone https://github.com/HMZElidrissi/Deploy-Laravel-on-EC2.git
- if you need to clone a certain branch, you can use the following command:
git clone --branch <your-branch-name> https://github.com/HMZElidrissi/Deploy-Laravel-on-EC2.git
- Install Composer: Run the following commands to install Composer on the EC2 instance.
sudo apt-get install composer
- Install Dependencies: Navigate to the root directory of your Laravel application and run the following command to install the dependencies.
cd path/to/your/laravel-app
composer install
if you need to install php8.3, you can use the following commands:
sudo apt -y install software-properties-common
sudo add-apt-repository ppa:ondrej/php
sudo apt-get update
sudo apt-get install php8.3
sudo apt-get install php8.3-cli php8.3-fpm php8.3-mysql php8.3-xml php8.3-mbstring php8.3-curl php8.3-zip php8.3-intl php8.3-gd
sudo update-alternatives --set php /usr/bin/php8.3
- Install Sail: Run the following command to install Laravel Sail.
composer require laravel/sail --dev
php artisan sail:install
- If you need to use an alias for the
sail
command, you can add the following line to your.bashrc
file:
alias sail='bash vendor/bin/sail'
- Run the Laravel Application: Run the following command to start the Laravel application using Laravel Sail.
./vendor/bin/sail up -d
- Access Your Application: Open a web browser and enter the public IP address of your EC2 instance to access your Laravel application.
- You may run into this error:
- To fix this error, you need just to grant write permissions to the storage directory:
sudo chmod -R 777 storage
sudo chmod o+w ./storage/ -R
Don't forget to change the
APP_DEBUG
tofalse
in the.env
file 😉
This guide assumes a basic setup. Depending on your specific requirements (like using a database, queues, etc.), you might need to perform additional configuration steps.