Deploying a Laravel application to AWS Elastic Beanstalk


Deploying a Laravel application to AWS Elastic Beanstalk involves several steps to set up the environment, configure the application, and deploy it. Here's a step-by-step guide:

Prerequisites:

  1. AWS Account: You need an AWS account.
  2. AWS CLI: Install the AWS Command Line Interface (CLI) on your local machine.
  3. Elastic Beanstalk CLI (EB CLI): Install the Elastic Beanstalk Command Line Interface.
  4. Git: Make sure Git is installed for version control.
  5. Composer: Laravel uses Composer for dependency management, so ensure it’s installed.
  6. Laravel Project: A working Laravel project on your local machine.

Step 1: Prepare Your Laravel Application

  1. Set up your .env file:

    • Ensure your .env file is configured for production. In particular, update your database and cache configurations to use Amazon RDS, S3, or any other AWS service you plan to use.
  2. Install Dependencies:

    • Run composer install to install your project dependencies.
    composer install --optimize-autoloader --no-dev
  3. Generate Application Key:

    • If you haven’t already, generate the Laravel application key:
    php artisan key:generate
  4. Set Up Storage Links:

    • Ensure your storage is correctly linked to the public directory:
    php artisan storage:link
  5. Migrate Database:

    • Run migrations to set up your database schema. Make sure your production database is set in the .env file.
    php artisan migrate --force
  6. Optimize Laravel for Production:

    • Run the following commands to optimize your Laravel application for production:
    php artisan config:cache
    php artisan route:cache
    php artisan view:cache

Step 2: Create Elastic Beanstalk Environment

  1. Install AWS CLI: If you haven't installed AWS CLI, you can install it by following the instructions here: AWS CLI Installation.

    After installation, configure your AWS CLI:

    aws configure

    Provide your AWS Access Key, Secret Key, region, and output format.

  2. Install Elastic Beanstalk CLI: The EB CLI is used to interact with AWS Elastic Beanstalk. Install it by following the instructions in the official EB CLI documentation.

  3. Create a new Elastic Beanstalk application: Navigate to your Laravel project directory in the terminal and initialize Elastic Beanstalk:

    eb init

    This command will prompt you to:

    • Choose your region (e.g., us-east-1).
    • Select the application platform. Choose PHP since Laravel is a PHP application.
    • Set up an Elastic Beanstalk environment (you can choose to create a new one).
  4. Configure Environment: Elastic Beanstalk will automatically detect your PHP environment, but you may need to adjust the configuration, especially if you're using an RDS database or specific storage options (e.g., Amazon S3).

Step 3: Configure Elastic Beanstalk for Laravel

  1. Add a .ebextensions directory: Elastic Beanstalk allows you to customize your environment using configuration files stored in the .ebextensions directory.

    Create a directory called .ebextensions in your Laravel project if it doesn’t exist, and add a configuration file named nginx.conf for handling file uploads, etc.

    Example nginx.conf:

    files:
      "/etc/nginx/conf.d/00_application.conf":
        content: |
          client_max_body_size 100M;

    This file sets the maximum upload size for files.

  2. Create Procfile for Elastic Beanstalk: Create a Procfile at the root of your Laravel project. The Procfile tells Elastic Beanstalk how to run your application.

    Add the following line to the Procfile:

    web: vendor/bin/heroku-php-apache2 public/

    This line configures Elastic Beanstalk to serve your application using Apache.

Step 4: Deploy Your Application

  1. Create an Elastic Beanstalk Environment: After configuring your project, create a new environment using the following command:

    eb create

    Choose an environment name (e.g., laravel-env) and select the environment type (typically Load balanced for production or Single instance for development).

  2. Deploy Your Application: Once your environment is set up, deploy your Laravel application with:

    eb deploy

    This command will package your application, upload it to Elastic Beanstalk, and start the application.

  3. Open Your Application: After deployment, you can open your application in the browser with:

    eb open

    This will open your Laravel application hosted on Elastic Beanstalk.

Step 5: Configure RDS and S3 (Optional)

If you're using Amazon RDS for your database or Amazon S3 for file storage, you need to configure these services.

  1. RDS Database:

    • In your .env file, set the following RDS variables:
    DB_CONNECTION=mysql
    DB_HOST=your-rds-endpoint
    DB_PORT=3306
    DB_DATABASE=your-database-name
    DB_USERNAME=your-username
    DB_PASSWORD=your-password
  2. S3 Storage:

    • In your .env file, configure S3 storage:
    FILESYSTEM_DRIVER=s3
    AWS_ACCESS_KEY_ID=your-access-key
    AWS_SECRET_ACCESS_KEY=your-secret-key
    AWS_DEFAULT_REGION=your-region
    AWS_BUCKET=your-bucket-name

    Ensure your Laravel application is configured to use S3 for file uploads by updating config/filesystems.php.

Step 6: Monitor and Manage Your Application

  • You can manage your Elastic Beanstalk application using the AWS Management Console or the EB CLI.

  • Use the following command to view logs:

    eb logs
  • To update the application, push changes to your Git repository, then deploy again with:

    eb deploy

Conclusion

You’ve successfully deployed your Laravel application to AWS Elastic Beanstalk! Elastic Beanstalk provides a scalable, easy-to-manage environment for hosting web applications, while Laravel allows you to quickly build powerful, feature-rich applications.

Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Java Stream API - How to convert List of objects to another List of objects using Java streams?

Registration and Login with Spring Boot + Spring Security + Thymeleaf

Java, Spring Boot Mini Project - Library Management System - Download

ReactJS, Spring Boot JWT Authentication Example

Top 5 Java ORM tools - 2024

Java - Blowfish Encryption and decryption Example

Spring boot video streaming example-HTML5

Google Cloud Storage + Spring Boot - File Upload, Download, and Delete