DEV Community

Cover image for Deploying Laravel Application On AWS ElasticBeanstalk from Git using CodePipeline
olorunda olaoluwa
olorunda olaoluwa

Posted on

Deploying Laravel Application On AWS ElasticBeanstalk from Git using CodePipeline

So you have built your awesome laravel application, and now you want to deploy it on AWS Elasticbeanstalk .

Here are some steps that could help you to successfully deploy your laravel application on elastic beanstalk from GIT using CodePipeline.

This article is for someone who is familiar with the aws environment but struggling deploying Laravel application on elastickbeanstalk from Git using CodePiepline

Step 1 :
Create a “.ebextensions” folder in you Laravel Application Root directory as shown in the image below

Step 1

Inside the “.ebextensions” folder , create an “environment.config” file , it could be any name of your choice , copy and paste the content below in the config file you created.

option_settings:
  aws:elasticbeanstalk:container:php:phpini:
    document_root: /public
  aws:elasticbeanstalk:application:environment:
    APP_NAME: Laravel
    APP_ENV: local
    APP_KEY: “your_app_key”
    APP_DEBUG: true
    APP_URL: http://localhost
    LOG_CHANNEL: stack
    LOG_DEPRECATIONS_CHANNEL: null
    LOG_LEVEL: debug
    DB_CONNECTION: mysql
    DB_HOST: "${RDS_HOSTNAME}"
    DB_PORT: "${RDS_HOSTNAME}"
    DB_DATABASE: "${RDS_DB_NAME}"
    DB_USERNAME: "${RDS_USERNAME}"
    DB_PASSWORD: "${RDS_PASSWORD}"
    BROADCAST_DRIVER: log
    CACHE_DRIVER: file
    FILESYSTEM_DRIVER: local
    QUEUE_CONNECTION: sync
    SESSION_DRIVER: file
    SESSION_LIFETIME: 120
    MEMCACHED_HOST: 127.0.0.1
    REDIS_HOST: 127.0.0.1
    REDIS_PASSWORD: null
    REDIS_PORT: 6379
    MAIL_MAILER: smtp
    MAIL_HOST: mailhog
    MAIL_PORT: 1025
    MAIL_USERNAME: null
    MAIL_PASSWORD: null
    MAIL_ENCRYPTION: null
    MAIL_FROM_ADDRESS: null
    MAIL_FROM_NAME="${APP_NAME}"
    AWS_ACCESS_KEY_ID: ''
    AWS_SECRET_ACCESS_KEY: ''
    AWS_DEFAULT_REGION: us-east-1
    AWS_BUCKET: ''
    AWS_USE_PATH_STYLE_ENDPOINT: false
    PUSHER_APP_ID: ''
    PUSHER_APP_KEY: ''
    PUSHER_APP_SECRET: ''
    PUSHER_APP_CLUSTER: mt1
    MIX_PUSHER_APP_KEY: "${PUSHER_APP_KEY}"
    MIX_PUSHER_APP_CLUSTER: "${PUSHER_APP_CLUSTER}"
container_commands:
  01-no_dev:
    command: "composer.phar install --optimize-autoloader"
  02-artisan_optmize:
    command: "php artisan optimize"
files:
    "/etc/cron.d/schedule_run":
        mode: "000644"
        owner: root
        group: root
        content: |
            * * * * * webapp php /var/www/html/artisan schedule:run --env=production >> /dev/null 2>&1
    "/etc/php.d/99max_size.ini" :
        mode: "000755"
        owner: root
        group: root
        content: |
          upload_max_filesize = 128M
          post_max_size = 128M 
Enter fullscreen mode Exit fullscreen mode

Create a second config file , could be anyname different from the name of the first config file. Copy and paste the content below in the newly created config file.

commands:
    setvars:
        command: /opt/elasticbeanstalk/bin/get-config optionsettings | \jq '."aws:elasticbeanstalk:application:environment"' | \jq -r 'to_entries | .[] | "export \(.key)=\"\(.value)\""' > /etc/profile.d/local.sh

Enter fullscreen mode Exit fullscreen mode

The command in this config file exposes the ElasticBeanstalk environment variables to the EC2 instance created by the Elasticbeanstalk instance so that your laravel application can have access to the environment variables

Step 2:
Create a .platform folder in the root of your laravel application , following this structure in the image below

Image 2
Copy and paste the content below in the laravel.conf file

location / {
         try_files $uri $uri/ /index.php?$query_string;
         gzip_static on;
     }
 client_max_body_size 128M;

Enter fullscreen mode Exit fullscreen mode

Step 3 :
Head over to https://console.aws.amazon.com, create an elastic beanstalk instance , select “Web server enviroment” as show in the picture below.

Image 3

Image 4

Step 4 : After you are done creating your elasticbeanstalk instance , go here to see how to attach an rds database to your elasticbeanstalk instance . This will expose the following environment variables to your laravel application

    RDS_HOSTNAME
    RDS_HOSTNAME
    RDS_DB_NAME
    RDS_USERNAME
    RDS_PASSWORD
Enter fullscreen mode Exit fullscreen mode

You can either change your laravel config to read this environment variable name directly in your “config/database.php” or you reference it as shown in the environment.config file you created in step 1.

Step 4 :

• Create a code pipeline on AWS.
• Choose Source Provider as “Github (Version 2)” , you can skip the build stage.
• On the Deploy stage choose “AWS Elasticbeanstalk” as your deploy provider, you would then select the earlier created application from the list of application names available
• Once you are done filling all the necessary information, review and release changes

Image 5

Step 5: Your Pipeline would run through all the stages and automatically deploy your laravel application to your elasticbeanstalk environment

Top comments (0)