How to deploy PHP application to AWS using AWS CodeDeploy and Github actions for beginners

Ed
4 min readJul 17, 2022

--

Why?

AWS provides a huge list of instruments to run your application in the cloud. One of the easiest way to deploy PHP application is to use EC2 instance and CodeDeploy service.

I am going to describe the way we chose when we started to develop our startup application docase.io. Docase.io will be an instrument to create BIO link pages with creator monetization features. Backend part of the app uses Symfony framework.

AWS: launch EC2 instance

First of all you need to launch a EC2 instance. EC2 type t3.micro and Ubuntu OS will be enough. Add a tag “CodeDeploy” with value “Backend” in the instance settings, we will use this tag in CodeDeploy configuration to filter instances where the new version of the application will be deployed.

You need to install NGINX:

sudo apt install nginx

Then PHP FPM

sudo apt update sudo apt install php php-cli php-fpm php-json php-mysql php-zip php-gd php-mbstring php-curl php-xml php-pear php-bcmath

Configure NGINX configuration. (we use configuration from official Symfony documentation )

AWS CodeDeploy

Create a new application in CodeDeploy with name Backend-PHP

You will need a AWS service role with CodeDeploy permissions. Create it in AWS IAM management page. Add the permission AWSCodeDeployRole to the new service role.

Then you have to create a Deployment group with name Backend-PHP-DepGroup inside the application. Choose the new service role that we created recently, choose In-place deployment type, choose Amazon EC2 instances as a target and add a tag group to filter EC2 instances where the application will be deployed

Disable Load balancer configuration:

Github actions

To deploy our application automatically each time the main branch is updated we use Github actions. This is a configuration of the action:

name: CI
on:
push:
branches: [ main ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: us-east-1
- name: Create CodeDeploy Deployment
id: deploy
run: |
aws deploy create-deployment \
--application-name Backend-PHP \
--deployment-group-name Backend-PHP-DepGroup \
--deployment-config-name CodeDeployDefault.AllAtOnce \
--github-location repository=${{ github.repository }},commitId=${{ github.sha }}

It connects to AWS and creates a deployment by using the last commit in the branch main.

You need to add two Action secrets in the Github repository settings AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY. We need a user credentials from AWS that will be used in Github actions. You can create a new user in AWS IAM setting that have one permission AWSCodeDeployDeployerAccess

Then go to Security credentials tab and create Access key, use credentials of the key as values of the new Github action secrets (AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY).

PHP Application

You need to add a appspec.yml file to the root folder of your project. It contains a configuration that CodeDeploy will use during deployment process. Here you need to choose which folders and files you want to deploy to EC2 instance, set permissions to the root folder and which hooks to run at the end.

version: 0.0
os: linux
files:
- source: bin
destination: /var/www/backend/bin
- source: config
destination: /var/www/backend/config
- source: public
destination: /var/www/backend/public
- source: src
destination: /var/www/backend/src
- source: templates
destination: /var/www/backend/templates
- source: .env
destination: /var/www/backend
- source: composer.json
destination: /var/www/backend
- source: composer.lock
destination: /var/www/backend

permissions:
- object: /var/www/backend
owner: ubuntu
group: ubuntu
mode: 755
type:
- directory
- object: /var/www/backend
owner: ubuntu
group: ubuntu
mode: 755
type:
- file
hooks:
AfterInstall:
- location: deployment/hook/install_composer
runas: ubuntu

If your application uses composer as our you need to add a bash script that will run composer commands. Create a file deployment/hook/install_composer and add there:

#!/bin/bash
composer install -d /var/www/backend --no-dev --no-progress --no-interaction --optimize-autoloader

Final

Push a new commit to main branch and see results in AWS CodeDeploy deployment history page.

and in Github actions “Workflow runs” page.

We use this workflow to deploy updates of backend service of Docase.io because it is easy and fast. I hope it will help you too.

--

--