how-to-deploy-a-secure-laravel-application-on-aws-with-docker.html

How to Deploy a Secure Laravel Application on AWS with Docker

Deploying a secure Laravel application on Amazon Web Services (AWS) using Docker is an efficient way to ensure scalability and reliability. In this guide, we'll walk you through the entire process step by step, from setting up your environment to deploying your application securely. Whether you are a seasoned developer or just starting with Laravel and Docker, this comprehensive tutorial will equip you with the knowledge and tools you need.

Understanding the Basics

What is Laravel?

Laravel is a powerful PHP framework designed for building web applications following the MVC (Model-View-Controller) architectural pattern. It simplifies common tasks such as routing, authentication, and caching, making it a popular choice among developers.

What is Docker?

Docker is a platform that enables developers to automate the deployment of applications inside lightweight, portable containers. These containers encapsulate everything needed to run an application, ensuring consistency across different environments.

Why Use AWS?

AWS is a leading cloud service provider that offers a wide array of resources, including computing power, storage, and networking capabilities. Deploying your Laravel application on AWS allows you to leverage its scalability, reliability, and robust security features.

Prerequisites

Before we dive into the deployment process, ensure you have the following:

  • An AWS account
  • Docker and Docker Compose installed on your local machine
  • Basic knowledge of Laravel and command-line interfaces

Step-by-Step Guide to Deploying Laravel on AWS with Docker

Step 1: Prepare Your Laravel Application

If you haven’t already created a Laravel application, you can do so by running the following command:

composer create-project --prefer-dist laravel/laravel my-laravel-app

Navigate to your application directory:

cd my-laravel-app

Step 2: Create a Dockerfile

In your Laravel project root, create a file named Dockerfile. This file will define how your application will run inside the Docker container.

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

# Install dependencies
RUN docker-php-ext-install pdo pdo_mysql

# Copy the existing application directory contents
COPY . /var/www/html

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

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

Step 3: Create a Docker Compose File

Next, create a docker-compose.yml file to define the services needed for your application, including the web server and database.

version: '3.8'

services:
  app:
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "8000:80"
    volumes:
      - .:/var/www/html
    environment:
      - DB_CONNECTION=mysql
      - DB_HOST=db
      - DB_PORT=3306
      - DB_DATABASE=laravel
      - DB_USERNAME=root
      - DB_PASSWORD=root

  db:
    image: mysql:5.7
    environment:
      MYSQL_DATABASE: laravel
      MYSQL_ROOT_PASSWORD: root
    volumes:
      - db_data:/var/lib/mysql

volumes:
  db_data:

Step 4: Build and Run Your Docker Containers

You can now build and run your Docker containers using Docker Compose. In your terminal, execute:

docker-compose up -d

This command will build the application container and the database container in detached mode.

Step 5: Migrate Your Database

Once your containers are up and running, you will need to run migrations to set up your database schema. Execute the following command:

docker-compose exec app php artisan migrate

Step 6: Configure AWS

  1. Set Up an EC2 Instance: Log in to your AWS Management Console, and navigate to the EC2 dashboard. Launch a new instance and select an Amazon Linux 2 AMI.

  2. Security Groups: Configure the security group to allow HTTP (port 80) and SSH (port 22) traffic.

  3. Connect to Your Instance: Once your instance is running, connect to it using SSH.

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

Step 7: Install Docker on EC2

Run the following commands to install Docker on your EC2 instance:

sudo amazon-linux-extras install docker
sudo service docker start
sudo usermod -a -G docker ec2-user

Log out and back in for the group changes to take effect.

Step 8: Deploy Your Application

  1. Clone Your Repository: If you have your Laravel application on a Git repository, clone it to your EC2 instance.
git clone your-repo-url
cd your-laravel-app
  1. Run Docker Compose: Execute the following command to start your containers:
docker-compose up -d

Step 9: Set Up HTTPS with Let's Encrypt

For added security, it’s essential to secure your application using HTTPS. Follow these steps:

  1. Install Certbot:
sudo yum install -y certbot
  1. Obtain an SSL certificate:
sudo certbot certonly --standalone -d your-domain.com
  1. Configure your web server to use the SSL certificates.

Step 10: Troubleshooting Common Issues

  • Database Connection Issues: Ensure that your database credentials in the .env file match those in your docker-compose.yml.
  • Permission Errors: Check the permissions of your storage and bootstrap/cache directories.

Conclusion

Deploying a secure Laravel application on AWS with Docker not only enhances your app's performance but also provides a robust framework for future scalability and security. By following these steps, you can effectively manage your application deployment process, ensuring a seamless transition from local development to a live environment.

With AWS's powerful infrastructure and Docker's containerization capabilities, you are well on your way to building a secure, efficient, and easily maintainable Laravel application. Happy coding!

SR
Syed
Rizwan

About the Author

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