4-deploying-a-laravel-application-on-aws-with-docker-and-cicd.html

Deploying a Laravel Application on AWS with Docker and CI/CD

Deploying a Laravel application can be a complex task, especially when you want to ensure scalability, reliability, and ease of maintenance. By leveraging AWS (Amazon Web Services), Docker, and a CI/CD (Continuous Integration/Continuous Deployment) pipeline, you can streamline this process significantly. This article will guide you through the steps necessary for deploying your Laravel application in a robust and efficient manner.

What is Laravel?

Laravel is a popular PHP framework designed to make web development easier and more enjoyable. It provides elegant syntax, robust features, and a plethora of built-in functionalities that simplify tasks like routing, authentication, and caching. With Laravel, developers can create everything from small web applications to large-scale enterprise solutions.

Why Use Docker?

Docker is a containerization platform that allows you to package applications and their dependencies into containers. This ensures that your application runs consistently across different environments, whether on your local machine or in production.

Benefits of Using Docker for Laravel

  • Isolation: Each app runs in its own container, preventing conflicts.
  • Portability: Use the same container across various environments.
  • Scalability: Easily manage application scaling with Docker.
  • Efficiency: Quick deployment and resource utilization.

Setting Up Your AWS Environment

Before we dive into the Docker configuration, you need to set up your AWS environment.

Create an EC2 Instance

  1. Log in to AWS Management Console.
  2. Navigate to the EC2 Dashboard.
  3. Click on “Launch Instance”.
  4. Choose an Amazon Machine Image (AMI). For Laravel, a basic Amazon Linux 2 AMI works well.
  5. Select an instance type; for testing, the t2.micro is sufficient.
  6. Configure instance details, add storage, and configure security groups (allow traffic on ports 80 and 22).
  7. Launch the instance and create a key pair for SSH access.

Connect to Your EC2 Instance

Using SSH, connect to your EC2 instance:

ssh -i "your-key.pem" ec2-user@your-ec2-public-ip

Dockerizing Your Laravel Application

Step 1: Create a Dockerfile

In your Laravel project root, create a file named Dockerfile:

# Use the official PHP image with Apache
FROM php:8.1-apache

# 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

# Set the working directory
WORKDIR /var/www/html

# Copy the current directory contents into the container
COPY . .

# Install Composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

# Install Laravel dependencies
RUN composer install

# Set permissions
RUN chown -R www-data:www-data /var/www/html/storage

Step 2: Create a Docker Compose File

Next, create a docker-compose.yml file to define services and link them together:

version: '3.8'

services:
  app:
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "80:80"
    volumes:
      - .:/var/www/html
    depends_on:
      - db

  db:
    image: mysql:5.7
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: laravel
    ports:
      - "3306:3306"

Step 3: Build and Run Docker Containers

Run the following commands to build and start your Docker containers:

docker-compose up --build

Your Laravel application should now be running on http://your-ec2-public-ip.

Setting Up CI/CD with GitHub Actions

To automate the deployment process, you can use GitHub Actions. This will allow your application to be deployed automatically when changes are pushed to your repository.

Step 1: Create a GitHub Actions Workflow

In your Laravel project, create a directory .github/workflows/ and add a file named ci-cd.yml:

name: CI/CD Pipeline

on:
  push:
    branches:
      - main

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout 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: .
          file: ./Dockerfile
          push: true
          tags: your-dockerhub-username/your-app:latest

      - name: Deploy to AWS
        run: |
          ssh -o StrictHostKeyChecking=no ec2-user@your-ec2-public-ip 'docker pull your-dockerhub-username/your-app:latest && docker-compose up -d'

Step 2: Configure Secrets

In your GitHub repository, navigate to Settings > Secrets and add the following:

  • DOCKER_USERNAME
  • DOCKER_PASSWORD
  • EC2_IP
  • EC2_PRIVATE_KEY (your EC2 key pair)

Conclusion

Deploying a Laravel application on AWS using Docker and a CI/CD pipeline not only simplifies the deployment process but also enhances your application's scalability and reliability. By following the steps outlined in this article, you can ensure a smooth deployment and continuous integration workflow for your Laravel applications.

With the power of Docker and AWS, you're well-equipped to tackle the challenges of modern web development. Embrace these tools, and watch your deployment processes become more efficient, allowing you to focus on what really matters—building great applications!

SR
Syed
Rizwan

About the Author

Syed Rizwan is a Machine Learning Engineer with 5 years of experience in AI, IoT, and Industrial Automation.