Deploying Laravel Applications on AWS with Docker and CI/CD
In today's fast-paced development environment, deploying applications efficiently is paramount. Laravel, a popular PHP framework, combined with Docker for containerization and AWS for cloud hosting, offers a powerful solution for developers. This article will guide you through deploying Laravel applications on AWS using Docker and implementing Continuous Integration/Continuous Deployment (CI/CD) pipelines.
Understanding the Components
What is Laravel?
Laravel is an open-source PHP framework designed for building web applications following the MVC (Model-View-Controller) architectural pattern. Its elegant syntax and robust features make it a favorite among developers.
What is Docker?
Docker is a platform that uses containerization to package applications and their dependencies into a standardized unit called a container. This ensures that your application runs consistently across different environments.
What is AWS?
Amazon Web Services (AWS) is a comprehensive cloud computing platform provided by Amazon. It offers a wide range of services, including computing power, storage options, and networking capabilities, making it ideal for hosting applications.
What is CI/CD?
Continuous Integration (CI) and Continuous Deployment (CD) are practices that automate the integration and deployment processes. CI ensures that code changes are automatically tested and merged, while CD automates the deployment of these changes to production.
Use Cases for Deploying Laravel on AWS with Docker and CI/CD
- Scalability: With AWS, you can scale your application up or down based on demand.
- Consistency: Docker ensures that your application runs the same way in development, testing, and production environments.
- Efficiency: CI/CD pipelines streamline the deployment process, reducing the time and effort needed to release updates.
Prerequisites
Before diving into deployment, ensure you have the following:
- An AWS account
- Docker installed on your local machine
- Laravel installed on your local machine
- Basic knowledge of PHP and Laravel
Step-by-Step Guide to Deploying Laravel on AWS with Docker and CI/CD
Step 1: Setting Up Your Laravel Application
First, create a new Laravel project. If you haven't installed Laravel yet, you can do so by running:
composer create-project --prefer-dist laravel/laravel my-laravel-app
Navigate to the project directory:
cd my-laravel-app
Step 2: Creating a Dockerfile
Create a Dockerfile
in your project root. This file defines the environment for your Laravel application:
# Use the official PHP image
FROM php:8.1-fpm
# Install 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
# Set working directory
WORKDIR /var/www
# Copy composer.lock and composer.json
COPY composer.lock composer.json ./
# Install Composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
# Install Laravel dependencies
RUN composer install
# Copy the application code
COPY . .
# Expose the port
EXPOSE 9000
CMD ["php-fpm"]
Step 3: Creating a Docker Compose File
Next, create a docker-compose.yml
file to define services:
version: '3.8'
services:
app:
build:
context: .
dockerfile: Dockerfile
volumes:
- .:/var/www
ports:
- "9000:9000"
Step 4: Building and Running Your Docker Container
Run the following command to build and start your containers:
docker-compose up -d
Your Laravel application should now be running on http://localhost:9000
. You can access it through your browser.
Step 5: Setting Up AWS
- Create an EC2 Instance: Log into your AWS Management Console and launch an EC2 instance with a suitable Amazon Machine Image (AMI).
- Install Docker on EC2: SSH into your instance and install Docker:
bash
sudo apt-get update
sudo apt-get install -y docker.io
sudo systemctl start docker
sudo systemctl enable docker
- Deploy Your Docker Container: Transfer your application to the EC2 instance using
scp
or Git, then run:
bash
docker-compose up -d
Step 6: Setting Up CI/CD with GitHub Actions
CI/CD helps automate the deployment process. Create a .github/workflows/deploy.yml
file in your project:
name: Deploy to AWS
on:
push:
branches:
- main
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Check out code
uses: actions/checkout@v2
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
- name: Build and push Docker image
uses: docker/build-push-action@v2
with:
context: .
push: true
tags: your-docker-repo/my-laravel-app:latest
- name: Deploy to AWS
run: |
ssh -o StrictHostKeyChecking=no ec2-user@your-ec2-public-dns "docker pull your-docker-repo/my-laravel-app:latest && docker-compose up -d"
Troubleshooting Common Issues
- Docker Build Errors: Ensure all dependencies are correctly defined in your
Dockerfile
. - AWS Permissions: Make sure your EC2 instance has the necessary permissions to pull from your Docker repository.
- Application Not Running: Check the container logs using
docker logs <container_id>
to identify any issues.
Conclusion
Deploying Laravel applications on AWS using Docker and CI/CD can significantly enhance your development workflow. By leveraging these tools, you can ensure your applications are scalable, consistent, and efficiently maintained.
As you implement this guide, remember that the key to successful deployment lies in understanding each component's role and how they interact. Embrace the power of automation through CI/CD practices, and watch your deployment process transform into a seamless experience. Happy coding!