DEV Community

MD ARIFUL HAQUE
MD ARIFUL HAQUE

Posted on

Step-by-Step: Deploy Laravel App to Cloud (AWS, Google, Azure, DigitalOcean) with CI/CD Using GitHub Actions/GitLab CI

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:

  1. Set up the cloud server (AWS, Google Cloud, Azure, or DigitalOcean).
  2. Configure web server (Apache/Nginx) on your cloud instance.
  3. Set up CI/CD pipelines using GitHub Actions or GitLab CI for automatic deployment.
  4. Test the deployed application.

Step-by-step Guide:

1. Set Up Cloud Server

  1. 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
Enter fullscreen mode Exit fullscreen mode
  1. 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
Enter fullscreen mode Exit fullscreen mode
  1. 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
Enter fullscreen mode Exit fullscreen mode
  1. DigitalOcean Droplet Setup:
    • Create a Droplet from the DigitalOcean dashboard.
    • Select Ubuntu and SSH into the droplet.
   ssh root@your-droplet-ip
Enter fullscreen mode Exit fullscreen mode

2. Configure Web Server (Apache/Nginx)

  • Install Nginx or Apache:
    • On Ubuntu, use:
   sudo apt update
   sudo apt install nginx  # or apache2 for Apache
Enter fullscreen mode Exit fullscreen mode
  • 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
Enter fullscreen mode Exit fullscreen mode
  • Configure Nginx for Laravel: Create an Nginx config file for your Laravel app:
   sudo nano /etc/nginx/sites-available/laravel
Enter fullscreen mode Exit fullscreen mode

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;
       }
   }
Enter fullscreen mode Exit fullscreen mode

Enable the site and restart Nginx:

   sudo ln -s /etc/nginx/sites-available/laravel /etc/nginx/sites-enabled/
   sudo systemctl restart nginx
Enter fullscreen mode Exit fullscreen mode

3. Set Up CI/CD Pipelines

Option 1: GitHub Actions

  1. 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
Enter fullscreen mode Exit fullscreen mode
  1. 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

  1. 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'
Enter fullscreen mode Exit fullscreen mode
  1. 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:

  1. Set up your cloud server (AWS, Google Cloud, Azure, or DigitalOcean).
  2. Configure a web server (Nginx or Apache) to serve your Laravel app.
  3. Set up CI/CD pipelines using GitHub Actions or GitLab CI to automate deployment.
  4. Test your application and monitor deployments.

This setup allows for seamless and automated deployment whenever code is pushed to the repository.

Top comments (0)