Docker Deploy Laravel In Elastic Beanstalk

Akshay Krison
10 min readMar 17, 2023

A guide to deploying Laravel web app in AWS Elastic Beanstalk with Dokcer containarization

This article is focused on the deployment of Laravel Application in AWS Elastic Beanstalk using Ubuntu 20.04 LTS Image instance in simple developer-friendly configurations for development purposes.

Prerequisites to Proceed Further

  1. Basic in Larval commands
  2. Basic in Docker Commands
  3. Basic in Linux Commands
  4. Basic in AWS Console to Create EB

Deploy the Laravel APP to AWS EB

Login to the AWS console and search or Navigate to the Elastic Beanstalk console below shown image and click on the create instance for creating a server.

Elastic Beanstalk APP Creation

Next you have to create an EB Application for that click on the Create Application Button as above image show’s

Next you have to give the application information as like the below image shows, change the name of you Application Name as your project name and choose the Platfrom as Docker and user the latest versions and Click on the Create Application as like the below image shown.

Wait until the application get created you will see a log creation of the application as like the below image

After waiting some time you will redirect to the application page as like the below image shows

Next we have to dockerize our PHP Laravel application so go to the root of your project open in your faviorite editor, here i prefer Vscode

and Create a Dockerfile in your root of the project as like the below image shows and add the docker commands in it

FROM php:7.4-fpm

# Install required system packages and PHP extensions
RUN apt-get update && apt-get install -y \
libpng-dev \
libzip-dev \
libonig-dev \
libxml2-dev \
zip \
unzip \
&& docker-php-ext-install pdo_mysql \
&& docker-php-ext-install gd \
&& docker-php-ext-install zip \
&& docker-php-ext-install opcache \
&& docker-php-ext-install bcmath \
&& docker-php-ext-install calendar \
&& docker-php-ext-install exif \
&& docker-php-ext-install gettext \
&& docker-php-ext-install pcntl \
&& docker-php-ext-install shmop \
&& docker-php-ext-install sockets \
&& docker-php-ext-install sysvmsg \
&& docker-php-ext-install sysvsem \
&& docker-php-ext-install sysvshm

# Install Nginx
RUN apt-get update && apt-get install -y nginx

# Configure Nginx to serve PHP files
RUN rm /etc/nginx/sites-enabled/default
COPY nginx.conf /etc/nginx/sites-enabled/default


# Install Composer
RUN php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" \
&& php composer-setup.php --install-dir=/usr/local/bin --filename=composer \
&& php -r "unlink('composer-setup.php');"

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

# Copy the code into the container
COPY . /var/www/html/

# Install dependencies
RUN composer install

# Set the permissions for the storage and bootstrap directories
RUN chgrp -R www-data storage bootstrap/cache && \
chmod -R ug+rwx storage bootstrap/cache

RUN php artisan migrate

RUN php artisan optimize:clear

# Expose port 80
EXPOSE 80

# Start Nginx and PHP-FPM
CMD service nginx start && php-fpm && --restart=on-failure
Dockerfile

Here we use php:7.4-fpm as our base image used to run our application also we use nginx to route the laravel application from the docker container so next we want to create a nginx configuration, for that create a nginx.conf file in the root of your project as like below done

server {
listen 80;
server_name localhost;

root /var/www/html/public;
index index.php;

access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;

location / {
try_files $uri $uri/ /index.php?$query_string;
}

location ~ \.php$ {
fastcgi_pass localhost:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

location ~ /\.ht {
deny all;
}
}

So the Nginx helps to serve the application on port 80 when a container get created

Next we have to create Database for our application here we use AWS RDS as Our database so we create it in AWS

Login to the AWS console and search or Navigate to the RDS console below shown image and click on the create database for creating a server.

RDS Creation

Here we choose Engine Option as MySQL since my application work in MySQL

Part 1

Next below section we have to input the DB Instance Identifier (1) as your db name, then type a Master Username for the DB (2). Also type a Master Password for the DB. Remember the Master Username and Master password we have set, this will considerd as DB_USERNAME & DB_PASSWORD. Then choose the Instance Configuration as your project need here i choose db.t3.micro to reduce the over billing(You can increase the db size later if needed)

Part 2

After completing the above fields and you scroll down you can saw some more field to be filled,

Public access: Opt for Yes since we have to test the db in our local machine netstatwe need to do this after the testing and deployment completed we go back and edit this to No [Strictly for Secruity Purposes]

Then change the DB port as what you want, then give an name to the database want to be created.

After filling all last click the create db button and wait for the DB creation, remember the credentials you have filled we will insert this in our .env file

After successfull creation we will get a db cosole like the below image shows and in that note down the endpoints, this will be provided as DB_HOST for our application

RDS Creation

Next we have to update the security groups rules to set the port for MySQL for that Navigate to the Security Groups, and find the security group for rds

Next you have to update the rules in security group for making the RDS port publicly (FOR TESTING) for that got the RDS and find the Security Group

Security Group of RDS

click on the security group (here i choose the default one advanced users can create your own security group and do) you will redriect to a page as like the below image shows

Security Groups

and click on the security group and add a new inbound rule as like the below image shows and save it

Rule Updation

After all the steps we completed next we have to insert the db fields in the env file in our application

Go to the .env file of the root of your project and add the values as like the below image shows

.env setup

Next we have to do a trial run to build the docker image for building the docker image open the terminal in vscode and type below command to build the docker image. You can change the name projectx with any name

docker build -t projectx .
docker build

it will build the project and wait until the build complete

Building the image

After the completion of build you will get a success alert with out any errors

Build Sucess

Next check the images you have build by running the below command

docker images

Next we have to test the docker images working properly for that you have to run the docker image using the below command, Note your image ID and paste it in the <image id> section

docker run -dp 80:80 <image id>

After that it will run successfully and you have to check the running container by running the below commands

docker ps

If you have not seen any container here run the below command

docker ps -a

this will list the exited container and you can check the logs of the container why its in exited state by running

docker logs <container id>

if all working perfectl on the first command docker ps you are good to go

next we want to get the docker container IP for that run the below command

docker inspect <container id>
inspect the conatiner

Note down the IP address and paste it in your browser to see the output

After successfully dockerized and tested in our local now we need to push this in to the elastic beanstalk environment for that you need to install AWS CLI in your system

CLICK THIS TO INSTALL AWS CLI

After that go the security credentials from the top right menu of you AWS account as like the below image shows

Main settings menu of AWS

After that go to the Access Key and create a new access key of yours and note down the Access Key ID and Secret Key ID

Creating Access Keys

Type the below command in your terminal as like the below image shows and input the details they have asked

Next go the Project Directory and open terminal and initiate the Elastic Beanstalk [EB] by running the below command and choose the region you where you created the eb, for my project ap-south-1 (Mumbai) region so i choose 6 and click enter

eb init
chooose the region where you want to deploy eb

Next you will ask the Project Name choose the project name we previously created eb or You can create a new eb from here, here i have already created in starting of the doc so choose my project

choose the project

Next they will ask we want to upload the code via code commit, since in this doc am showing the code will directly push from our local machine and docker hub so i choose n and click enter

choose the code deploy over code commit

Then all set!, you can see a new folder for eb configurations and file created like the below image shows

eb configurations

Next we have to create a Dockerrun.aws.json file as like the below image shows, . The file defines the location of the Docker image, ports to expose, environment variables, and other details that are required to run the Docker container on Elastic Beanstalk

Dockerrun.aws.json

Note Image name is given as your project name you have build

For Advanced Users who can specify the env along with this by creating secrets

Next we can deploy our code to eb directly by running the below command from the root of our project directory and wait until it get deployed

eb deploy
eb deploy
EB deploying in AWS Beanstalk

After waiting you will get an event log in the terminal like Environment update completed successfully. and you can check the eb link to view the application

Next we have to secure our db and its port since we want to test it in local we open the db ports so we want to make it private and access allowed only from the eb so that go the RDS and find the VPC security group and change the inbound rules as like the below images shows

RDS Security Groups
Edit Inbound rules

Search the security group of your Elastic Beanstalk and choose as like the belo image shows instead of 0.0.0.0/0

Security Group Inbound is Set to EB Security group

All done!!!

--

--