Step-by-Step Guide to Deploying Laravel Applications on AWS with Docker
Deploying a Laravel application can seem daunting, especially when you factor in containerization with Docker and cloud hosting on AWS. However, with a systematic approach, you can simplify the deployment process and ensure your application runs smoothly. This guide provides you with a comprehensive step-by-step outline for deploying a Laravel application on AWS using Docker.
What is Laravel?
Laravel is a popular PHP framework that simplifies the development of web applications by providing a robust set of tools and features. It follows the MVC (Model-View-Controller) architecture, enabling developers to build scalable and maintainable applications efficiently.
What is Docker?
Docker is a platform that allows developers to automate the deployment of applications inside lightweight, portable containers. Each container encapsulates everything the application needs to run, making it easy to manage dependencies and ensure consistency across environments.
Why Use AWS for Deployment?
AWS (Amazon Web Services) is one of the leading cloud service providers, offering a wide array of services such as storage, computing, and networking, which can help in scaling and managing your Laravel applications effectively. Combining AWS with Docker offers a powerful solution for deploying PHP applications in a microservices architecture.
Step 1: Setting Up Your Laravel Application
Create a New Laravel Project
If you haven't already created your Laravel application, you can do so with Composer. Open your terminal and run:
composer create-project --prefer-dist laravel/laravel my-laravel-app
Set Up Your .env File
Navigate to your project directory:
cd my-laravel-app
Copy the .env.example
to .env
:
cp .env.example .env
Update your .env
file with the necessary configurations, including database settings.
Step 2: Create a Dockerfile
A Dockerfile is a script that contains a series of instructions on how to build a Docker image for your Laravel application.
Create the Dockerfile
In your project root, create a file named Dockerfile
:
# Use the official PHP image
FROM php:8.0-fpm
# Set the working directory
WORKDIR /var/www
# Install system dependencies
RUN apt-get update && apt-get install -y libpng-dev libjpeg-dev libfreetype6-dev \
&& docker-php-ext-configure gd --with-freetype --with-jpeg \
&& docker-php-ext-install gd
# Install Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
# Copy existing application directory contents
COPY . .
# Install Laravel dependencies
RUN composer install
# Expose port 9000
EXPOSE 9000
# Start the PHP FastCGI Process Manager
CMD ["php-fpm"]
Create a Docker Compose File
Create a docker-compose.yml
file to define how your application will interact with other services (like MySQL).
version: '3.8'
services:
app:
build:
context: .
dockerfile: Dockerfile
ports:
- "9000:9000"
volumes:
- .:/var/www
networks:
- my-network
db:
image: mysql:5.7
environment:
MYSQL_DATABASE: laravel_db
MYSQL_ROOT_PASSWORD: secret
volumes:
- db_data:/var/lib/mysql
networks:
- my-network
networks:
my-network:
volumes:
db_data:
Step 3: Build and Run Your Docker Containers
Now that you have your Dockerfile and Docker Compose file ready, you can build and run your containers.
Build the Docker Image
In your terminal, run:
docker-compose build
Start the Docker Containers
To run your application and the database, execute:
docker-compose up -d
This command will start your Docker containers in the background. You can check the logs with:
docker-compose logs -f
Step 4: Set Up AWS
Create an AWS Account
If you don't have an AWS account, go to AWS and create one.
Launch an EC2 Instance
- Go to the EC2 Dashboard and click on "Launch Instance."
- Select an Amazon Machine Image (AMI) – a common choice is Amazon Linux or Ubuntu.
- Choose an instance type (e.g., t2.micro for free tier).
- Configure instance details as needed and proceed to add storage.
- Configure security groups to allow traffic on port 9000.
SSH into Your EC2 Instance
Once your instance is running, connect via SSH:
ssh -i "your-key.pem" ec2-user@your-instance-ip
Install Docker on EC2
On your EC2 instance, install Docker:
sudo yum update -y
sudo amazon-linux-extras install docker
sudo service docker start
sudo usermod -a -G docker ec2-user
Log out and back in to allow the user group changes to take effect.
Transfer Your Application to EC2
Use scp
or rsync
to transfer your Dockerized Laravel application from your local machine to the EC2 instance.
scp -i "your-key.pem" -r my-laravel-app ec2-user@your-instance-ip:/home/ec2-user
Run Your Application on EC2
Navigate to your application directory and run:
cd my-laravel-app
docker-compose up -d
Troubleshooting Common Issues
- Permission Denied Errors: Ensure your folders have the right permissions. Use
chmod -R 755 storage
andchmod -R 755 bootstrap/cache
. - Database Connection Issues: Double-check your
.env
settings and ensure your database service is running in Docker. - Port Access Issues: Make sure security groups in AWS allow traffic on the ports you’re using.
Conclusion
Deploying a Laravel application on AWS using Docker can streamline your workflow and enhance your application's scalability. By following this guide, you can effectively manage your application deployment process, ensuring a robust production environment. With AWS's infrastructure and Docker's containerization, your Laravel application is set to thrive in the cloud! Happy coding!