Hey there! Deploying a Laravel app used to feel like a chore for me—manually uploading files, running commands, and hoping nothing broke. Then I discovered how to automate the process using GitHub Actions with AWS EC2, and it’s been a game-changer.
In this article, I’ll share how I set up a CI/CD pipeline to deploy my Laravel apps to an AWS EC2 instance using GitHub Actions.
By the end, you’ll have your Laravel app automatically deployed to AWS whenever you push code to GitHub
Here’s how I set up a CI/CD pipeline to deploy my Laravel app to an AWS EC2 instance using GitHub Actions. This guide assumes you have a Laravel app and some familiarity with GitHub and AWS.
First, ensure your Laravel app is ready and hosted on a GitHub repository. If you don’t have one, create a new Laravel project:
composer create-project laravel/laravel my-laravel-app
cd my-laravel-app
git init
git add .
git commit -m "Initial commit"
git remote add origin <your-github-repo-url>
git push -u origin main
Make sure you have some tests (e.g., created with php artisan make:test
) to run in the CI pipeline. This ensures your code is solid before deployment.
To host your Laravel app, you need an AWS EC2 instance. Here’s what I do:
t2.micro
for testing, free-tier eligible)..pem
file securely) for SSH access.SSH into your EC2 instance to set it up:
ssh -i your-key.pem ubuntu@<your-ec2-public-ip>
Install PHP, Composer, a web server (I use Nginx), and MySQL:
sudo apt update && sudo apt upgrade -y
sudo apt install -y php php-cli php-fpm php-mysql php-mbstring php-xml php-zip nginx mysql-server git
sudo curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
Set up your Laravel project on the server:
sudo mkdir -p /var/www/laravel
sudo git clone <your-github-repo-url> /var/www/laravel
sudo chown -R ubuntu:ubuntu /var/www/laravel
cd /var/www/laravel
sudo composer install --no-dev
sudo cp .env.example .env
sudo php artisan key:generate
sudo chmod -R 775 storage bootstrap/cache
Configure Nginx (/etc/nginx/sites-available/laravel
):
server {
listen 80;
server_name <your-ec2-public-ip>;
root /var/www/laravel/public;
index index.php;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
Enable the site and restart Nginx:
sudo ln -s /etc/nginx/sites-available/laravel /etc/nginx/sites-enabled/
sudo systemctl restart nginx
Set up your MySQL database and update .env
with the credentials.
Now, let’s automate the deployment with GitHub Actions. In your Laravel project’s root, create a file at .github/workflows/deploy-to-ec2.yml
:
name: Deploy Laravel to AWS EC2
on:
push:
branches:
- main
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Set up PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.1'
extensions: mbstring, xml, ctype, json, tokenizer, pdo_mysql
- name: Install Composer dependencies
run: composer install --prefer-dist --no-dev --no-progress
- name: Deploy to EC2
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.SERVER_IP }}
username: ${{ secrets.SSH_USER }}
key: ${{ secrets.SSH_KEY }}
script: |
cd /var/www/laravel
git pull origin main
composer install --no-dev --no-interaction
php artisan migrate --force
php artisan config:cache
php artisan route:cache
sudo systemctl restart nginx
To securely connect to your EC2 instance, add these secrets in your GitHub repository (Settings > Secrets and variables > Actions > New repository secret):
ubuntu
for Ubuntu instances).your-key.pem
).Generate an SSH key pair on your server if needed:
ssh-keygen -t rsa -b 4096 -f ~/.ssh/deploy_key -N ""
cat ~/.ssh/deploy_key.pub >> ~/.ssh/authorized_keys
Copy the private key (deploy_key
) to the SSH_KEY
secret.
Push a change to your main
branch:
git add .
git commit -m "Test deployment"
git push origin main
Go to your GitHub repository > Actions tab to monitor the workflow. It will pull the latest code to your EC2 instance, run migrations, cache configurations, and restart Nginx.
If the deployment fails, check the logs in the Actions tab. Common issues include incorrect SSH keys, firewall rules blocking port 22, or missing PHP extensions. I always ensure my security group allows SSH from GitHub’s IP ranges and double-check my env file.
Setting up a CI/CD pipeline with GitHub Actions to deploy my Laravel app to AWS EC2 has saved me so much time. Now, every push to my main
branch triggers an automated deployment, and I can focus on coding instead of manual server tasks. This setup is reliable, free (within GitHub Actions’ limits), and scalable.
Q: What is the benefit of using GitHub Actions for Laravel deployment?
A: GitHub Actions automates testing and deployment, reducing manual errors and saving time. It integrates seamlessly with GitHub and AWS.
Q: Do I need an AWS account to deploy Laravel with GitHub Actions?
A: Yes, you need an AWS account to create an EC2 instance and manage resources for hosting your Laravel app.
Q: Can I use AWS Elastic Beanstalk instead of EC2?
A: Yes, Elastic Beanstalk simplifies deployment but requires a different GitHub Actions setup. You’d zip your app and use AWS CLI to deploy it.
Q: What if my deployment fails due to SSH issues?
A: Check your security group (port 22 open), SSH key, and GitHub Secrets. Ensure your server’s authorized_keys
includes the public key.
Q: Can I add tests to the pipeline?
A: Absolutely! Add a step like php artisan test
before the deployment step to run your Laravel tests and ensure code quality.
You might also like :