Deploying a Laravel app to a cloud server with CI/CD using GitHub Actions or GitLab CI involves several steps. Below is a hands-on example and step-by-step guide for deploying a Laravel app to a cloud server like AWS, Google Cloud, Azure, or DigitalOcean using CI/CD pipelines.
Prerequisites:
- A Laravel project hosted on GitHub or GitLab.
- A cloud server (AWS EC2, Google Cloud Compute Engine, Azure VM, or DigitalOcean Droplet).
- SSH access to your cloud server.
- Installed Docker on the cloud server (optional but recommended for consistency across environments).
High-level steps:
- Set up the cloud server (AWS, Google Cloud, Azure, or DigitalOcean).
- Configure web server (Apache/Nginx) on your cloud instance.
- Set up CI/CD pipelines using GitHub Actions or GitLab CI for automatic deployment.
- Test the deployed application.
Step-by-step Guide:
1. Set Up Cloud Server
-
AWS (EC2) Setup:
- Go to AWS Console → EC2 → Launch Instance.
- Select an Amazon Linux/Ubuntu instance.
- Configure instance (memory, storage, etc.).
- Set up a security group allowing traffic on HTTP (80), HTTPS (443), and SSH (22).
- Launch the instance and SSH into the server.
ssh -i your-key.pem ec2-user@your-server-ip
-
Google Cloud (Compute Engine) Setup:
- Go to Google Cloud Console → Compute Engine → Create Instance.
- Choose your machine type, boot disk (Linux), and configure firewall rules (allow HTTP, HTTPS).
- SSH into your instance.
gcloud compute ssh instance-name --zone zone-name
-
Azure VM Setup:
- Go to Azure Portal → Create Virtual Machine.
- Choose Ubuntu as the operating system.
- Configure networking, allowing HTTP and SSH.
- SSH into your VM.
ssh username@your-vm-ip
-
DigitalOcean Droplet Setup:
- Create a Droplet from the DigitalOcean dashboard.
- Select Ubuntu and SSH into the droplet.
ssh root@your-droplet-ip
2. Configure Web Server (Apache/Nginx)
-
Install Nginx or Apache:
- On Ubuntu, use:
sudo apt update
sudo apt install nginx # or apache2 for Apache
-
Install PHP, PHP extensions, and Composer:
- For Laravel, you’ll need PHP 7.4+:
sudo apt install php php-fpm php-mysql php-mbstring php-xml php-curl
sudo apt install composer
- Configure Nginx for Laravel: Create an Nginx config file for your Laravel app:
sudo nano /etc/nginx/sites-available/laravel
Add the following configuration:
server {
listen 80;
server_name your-domain.com;
root /var/www/laravel/public;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
Enable the site and restart Nginx:
sudo ln -s /etc/nginx/sites-available/laravel /etc/nginx/sites-enabled/
sudo systemctl restart nginx
3. Set Up CI/CD Pipelines
Option 1: GitHub Actions
-
Create
.github/workflows/deploy.yml
in your Laravel repository:
name: Deploy Laravel App
on:
push:
branches:
- main # Trigger on pushes to the main branch
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Set up PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.0' # Adjust based on your PHP version
- name: Install dependencies
run: composer install --no-dev --prefer-dist --no-progress --no-suggest
- name: Copy files to server
uses: appleboy/scp-action@master
with:
host: ${{ secrets.SERVER_IP }}
username: ${{ secrets.SSH_USER }}
key: ${{ secrets.SSH_KEY }}
source: "./"
target: "/var/www/laravel"
- name: Execute remote commands
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.SERVER_IP }}
username: ${{ secrets.SSH_USER }}
key: ${{ secrets.SSH_KEY }}
script: |
cd /var/www/laravel
php artisan migrate --force
php artisan config:cache
-
Add Secrets in GitHub:
- Go to the repo settings → Secrets.
- Add the following secrets:
-
SERVER_IP
: Cloud server IP. -
SSH_USER
: SSH username. -
SSH_KEY
: Your private SSH key.
-
Option 2: GitLab CI
-
Create
.gitlab-ci.yml
in your Laravel repository:
stages:
- deploy
deploy_to_production:
stage: deploy
script:
- apt-get update -y
- apt-get install -y sshpass
- sshpass -p "${SSH_PASSWORD}" scp -o StrictHostKeyChecking=no -r ./* ${SSH_USER}@${SERVER_IP}:/var/www/laravel
- sshpass -p "${SSH_PASSWORD}" ssh ${SSH_USER}@${SERVER_IP} 'cd /var/www/laravel && composer install --no-dev && php artisan migrate --force && php artisan config:cache'
-
Add Variables in GitLab:
- Go to CI/CD Settings → Variables.
- Add the following variables:
-
SERVER_IP
: Cloud server IP. -
SSH_USER
: SSH username. -
SSH_PASSWORD
: SSH password (or use SSH keys).
-
4. Test the Deployed Application
- Visit your cloud server's IP or domain in a browser to verify the Laravel app is working.
- Check logs and ensure that the deployment pipeline is running correctly.
Summary:
- Set up your cloud server (AWS, Google Cloud, Azure, or DigitalOcean).
- Configure a web server (Nginx or Apache) to serve your Laravel app.
- Set up CI/CD pipelines using GitHub Actions or GitLab CI to automate deployment.
- Test your application and monitor deployments.
This setup allows for seamless and automated deployment whenever code is pushed to the repository.
Top comments (0)