Step-by-Step Guide: Deploying a Laravel Application to AWS Lightsail Ubuntu Instance (EC2) Using GitHub Actions CI/CD

Posted by & filed under AWS, GitHub, Laravel.

In this blog post topic focuses on providing a comprehensive step-by-step guide to deploying a Laravel application to an AWS Lightsail Ubtuntu instance using Github Actions for continuous integration and continuous deployment (CI/CD).

Step-by-Step Guide: Deploying a Laravel Application to AWS Lightsail Ubuntu Instance (EC2) Using GitHub Actions CI/CD

Technical Requirements

  1. AWS Lightsail Instance
  2. Github (your code must be github repo)
  3. Laravel App

I am using following Github repo.

For this tutorial I am going assume you have already have Github repository for your Laravel application.

Configuring AWS Access Key & Secret

To establish communication with AWS via command line or programmatically, we need Access Key & Secret access key.

First login into your AWS console using your username and password. Then go to IAM -> Users section.

AWS IAM User List

Next Click on Add users button on the top right to create user.

AWS Create User – Step 1

Now enter user name you want create and click Next button to continue.

Set Permission to User

Now don’t provide any permission and click on Next button to continue.

AWS Review Create User

Now click Create user button to create new user.

AWS User list with New User

Now click on newly created user ‘depoy-laravel-app’ to create Access key & Secret.

Create AWS Access Key & Secret

Now click Create access key button to create access key & secret.

AWS Create Access Key Recomendation

Now check check the radio button ‘I understand the above recommendation and want to proceed to create an access key‘. And click on ‘Next‘ continue.

Set description tag

Finally click ‘Create access key’ button to create a key.

AWS Retrieve Access key.

Now note down the Access key & Secret access key. (Click on show link to get Secret access key). Or Click on the Download .csv file to get download the access key and secret key in CSV file format.

Configuring GitHub Secrets:

Now login into Github.com account and access your repository settings page to configure all the secrets that are required for CI & CD deployment. I have added following secrets by clicking on ‘New repository secret‘ button. (AWS ACCESS KEY, ACCESS SECRET, PEM FILE, AWS HOSTNAME & USERNAME).

Github Action Secret Variables

Defining the Deployment Workflow:

Now create ‘.github‘ folder in the project root directory, and add ‘workflows‘ folder in it. Now add deploy-aws.yml file in it.

├── .github
│   └── workflows
│       └── deploy-aws.yml
├── .gitignore
├── README.md
├── app
```

Now following content to it.

name: Quiz

on:
  push:
    branches:
    - main

jobs:
  deploy:
    runs-on: ubuntu-latest

    steps:
    - name: Checkout
      uses: actions/checkout@v2

    - name: Install Build dependencies
      run: |
        sudo apt-get update
        sudo apt-get install -y git php composer
      
    - name: Set up Node.js 18.12.0
      uses: actions/setup-node@v2
      with:
        node-version: '18.12.0'
    
    - 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_ACCESS_SECRET_KEY }}
        aws-region: eu-west-2

    - name: Install Frontend dependencies
      run: |
        npm install
    
    - name: Build Frontend Code
      run: npm run build

    - name: Build Backend Code
      run: composer install --prefer-dist

    - name: Deploy Laravel code
      env:
          PRIVATE_KEY: ${{ secrets.AWS_PEM_KEY  }}
          HOSTNAME : ${{ secrets.HOSTNAME  }}
          USER_NAME : ${{ secrets.USER_NAME  }}
          ENV : ${{ secrets.ENV  }}
      run: |
        echo "$ENV" > .env
        echo "$PRIVATE_KEY" > private_key && chmod 600 private_key
        rsync -r --exclude 'node_modules' --exclude 'storage' --exclude '.git/' --delete-after -e "ssh -o StrictHostKeyChecking=no -i private_key" . ${USER_NAME}@${HOSTNAME}:/var/www/nxgeninventors.com/quiz/

        ssh -o StrictHostKeyChecking=no -i private_key ${USER_NAME}@${HOSTNAME} '
            cd /var/www/nxgeninventors.com/simba/ && 
            sudo chmod -R 777 storage && 
            php artisan storage:link 
        '
        
        
```

When ever code pushed to main branch, it trigger job.

Step1: Checkout codebase from main branch

Step 2: Install build dependencies that are required for this project (git, php, composer).

Step 3: Setting up Node version 18.12.0.

Step 4: Configure AWS Credentials (Access key & Secret).

Step 5: Install Frontend dependencies.

Step 6: Build Frontend Code

Step 7: Finally Deploy Laravel build code to the server, and set appropriate permission.

Download Premium Only Scripts & 80+ Demo scripts Instantly at just 1.95 USD per month + 10% discount to all Exclusive Scripts

If you want any of my script need to be customized according to your business requirement,

Please feel free to contact me [at] muni2explore[at]gmail.com

Note: But it will be charged based on your customization requirement

Get Updates, Scripts & Other Useful Resources to your Email

Join 10,000+ Happy Subscribers on feedburner. Click to Subscribe (We don't send spam)
Every Email Subsciber could have access to download 100+ demo scripts & all future scripts.

Get Instant Script Download Access!