Laravel Deployment in Elastic beanstalk with Bitbucket Pipelines

Laravel deployment in elastic beanstalk

Deploying Laravel applications into elastic beanstalk to the cloud has become a common method  for the developers who want scalability, high availability, and hassle-free infrastructure management. Laravel deployment in Elastic Beanstalk is one of the most efficient ways to achieve this, as AWS handles server provisioning, load balancing, and auto-scaling for you.

In this guide, we’ll explain step-by-step how to deploy a Laravel project on AWS, configure Bitbucket Pipelines for Laravel backend deployment, and ensure smooth delivery to staging and production environments.

Why Choose Elastic Beanstalk for Laravel Deployment?

AWS Elastic Beanstalk is a managed platform that allows developers to focus on writing code rather than worrying about servers. Unlike manually configuring AWS EC2 instances or setting up EC2 Apache environments, Elastic Beanstalk abstracts most of the infrastructure complexity and offers:

  • Automated scaling based on traffic.
  • Easy rollbacks and version control.
  • Simple integration with services like AWS S3, RDS, and AWS Laravel SDK.

Seamless CI/CD pipelines with Bitbucket, GitHub, or AWS CodePipeline for Laravel projects.

Step 1: Prepare Your Laravel Application

Before deploying, make sure your Laravel application is production-ready:

  1. Configure .env to use environment variables for production.
  2. Set up Laravel AWS S3 storage if you want to store files in the cloud.
  3. Run composer install –no-dev to optimize dependencies.

Generate an application key with php artisan key:generate.

Step 2: Configure Bitbucket Pipelines

Create a bitbucket-pipelines.yml file in your repository with the following configuration:

image: php:8.1-fpm

pipelines:
  branches:
    master:
      - step:
          name: Build and Package
          caches:
            - composer
          script:
            - apt-get update && apt-get install -y zip curl libpng-dev libjpeg-dev libfreetype6-dev libzip-dev
            - docker-php-ext-configure gd --with-freetype --with-jpeg
            - docker-php-ext-install -j$(nproc) gd zip
            - curl -sS https://getcomposer.org/installer -o /tmp/composer-setup.php
            - php /tmp/composer-setup.php --install-dir=/usr/local/bin --filename=composer
            - composer install --no-dev --optimize-autoloader
            - zip -r application.zip . -x ".git/*" "vendor/*" "node_modules/*" ".env"
          artifacts:
            - application.zip
      - step:
          name: Deploy to Staging
          deployment: staging
          script:
            - pipe: atlassian/aws-elasticbeanstalk-deploy:1.1.0
              variables:
                AWS_ACCESS_KEY_ID: $AWS_ACCESS_KEY_ID
                AWS_SECRET_ACCESS_KEY: $AWS_SECRET_ACCESS_KEY
                AWS_DEFAULT_REGION: $AWS_DEFAULT_REGION
                APPLICATION_NAME: $APPLICATION_NAME
                ENVIRONMENT_NAME: $STAGING_ENVIRONMENT_NAME
                S3_BUCKET: $S3_BUCKET
                ZIP_FILE: application.zip
      - step:
          name: Deploy to Production
          deployment: production
          trigger: manual
          script:
            - pipe: atlassian/aws-elasticbeanstalk-deploy:1.1.0
              variables:
                AWS_ACCESS_KEY_ID: $AWS_ACCESS_KEY_ID
                AWS_SECRET_ACCESS_KEY: $AWS_SECRET_ACCESS_KEY
                AWS_DEFAULT_REGION: $AWS_DEFAULT_REGION
                APPLICATION_NAME: $APPLICATION_NAME
                ENVIRONMENT_NAME: $PRODUCTION_ENVIRONMENT_NAME
                S3_BUCKET: $S3_BUCKET
                ZIP_FILE: application.zip
                

Step 3: Add Environment Variables in Bitbucket Repository Variables

In Bitbucket Repository Settings → Repository Variables, add the following keys:

  • AWS_ACCESS_KEY_ID
  • AWS_SECRET_ACCESS_KEY
  • AWS_DEFAULT_REGION
  • APPLICATION_NAME
  • STAGING_ENVIRONMENT_NAME
  • PRODUCTION_ENVIRONMENT_NAME
  • S3_BUCKET

These variables allow Bitbucket Pipelines to authenticate with AWS and deploy to the correct environment.

Step 4: Trigger Your First Deployment

Push your code to the master branch, and Bitbucket Pipelines will automatically:

  1. Build your Laravel application.
  2. Upload it to the S3 bucket.
  3. Deploy it to your Elastic Beanstalk environment.

You can monitor the deployment logs in both Bitbucket Pipelines and the Elastic Beanstalk dashboard.

Most Common Issues

Even with a solid pipeline, you may encounter deployment errors such as:

  • 502 Bad Gateway – Often caused by missing PHP extensions or incorrect .htaccess configuration.
  • Failed to Deploy Application Version – Check that your application.zip contains all necessary Laravel files.
  • Database connection issues – Make sure environment variables for your AWS RDS instance are correctly set.

If errors persist, you can also consider using AWS CodePipeline for Laravel as an alternative deployment solution.

MrLoom Final Thoughts

Setting up Laravel deployment in Elastic Beanstalk with Bitbucket Pipelines is one of the best ways to achieve a fully automated CI/CD workflow. It saves time, reduces manual errors, and allows seamless scaling as your application grows.

Whether you are using AWS Laravel SDK, Laravel AWS S3, or integrating with AWS CodePipeline, this approach provides a production-grade deployment strategy for modern Laravel applications.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top