Electronics Engineer Fixing Cables on Server

How to Point Domain and Host Laravel Project using Github on Apache Web Server VPS Hosting

Hosting a Laravel project using Github on an Apache web server VPS involves several steps including configuring your domain, setting up Apache, MySQL, PHP, Composer, and Git, as well as deploying your Laravel project and automating the deployment process using Github Actions.

Step 1: Configure Domain Settings

  1. Login to Your Domain Provider Website:
  • Access your domain provider’s website and log in to your account.
  1. Navigate to Manage DNS:
  • Find and navigate to the DNS management section.
  1. Add DNS Records:
  • Add the following DNS records to point your domain to your VPS:
    • A Record:
    • Host/Name: @
    • Value: Your Remote Server IP
    • A Record:
    • Host/Name: www
    • Value: Your Remote Server IP
    • AAAA Record (for IPv6):
    • Host/Name: @
    • Value: Your Remote Server IPv6
    • AAAA Record (for IPv6):
    • Host/Name: www
    • Value: Your Remote Server IPv6

Step 2: Push Project to Github and Set Up SSH

  1. Push Project to Github:
  • Push your Laravel project to your Github account as a private repository.
  1. Access Remote Server via SSH:
  • SSH into your remote server:
  • Example:
bash ssh -p 22 root@
  1. Verify Software Installation:
  • Check if all necessary software is installed:
bash apache2 -v mysql php -v composer -v git --version
  1. Verify Apache2 is Active:
  • Check the status of Apache:
bash service apache2 status
  1. Verify Web Server Ports and Firewall:
  • Check the status of ports and firewall rules:
bash ufw status verbose
  1. Generate SSH Keys and Add to Github:
  • Generate SSH keys on your remote server:
bash ssh-keygen -t ed25519 -C "your_email@example.com"
  • Add the generated SSH public key to your Github repository’s deploy keys.

Step 3: Clone and Deploy Project

  1. Clone Project from Github:
  • Clone your Laravel project from Github:
    • Using HTTPS:
bash git clone https_repo_path
  • Using SSH:
bash git clone ssh_repo_path
  1. Move Project Folder and Configure Virtual Host:
  • Move your project folder to the web server’s public directory:
bash sudo mv project_folder_name /var/www
  • Create a virtual host configuration file for your domain and enable it.
  1. Enable Mod Rewrite and Restart Apache:
  • Enable the Apache mod_rewrite module:
bash sudo a2enmod rewrite
  • Restart Apache to apply changes:
bash sudo service apache2 restart

Step 4: Set Up Laravel Environment and Permissions

  1. Copy .env File and Install Dependencies:
  • Copy .env.example to .env and configure environment variables.
  • Install Composer dependencies:
bash composer install --optimize-autoloader --no-dev
  1. Generate Application Key:
  • Generate a unique application key for Laravel:
bash php artisan key:generate
  1. Set Permissions for Folders:
  • Set appropriate permissions for storage and bootstrap/cache directories:
bash sudo chown -R www-data:www-data storage bootstrap/cache
  1. Create Database and Migrate Tables:
  • Create the necessary database and configure database settings in .env.
  • Run database migrations:
bash php artisan migrate
  1. Create Symbolic Link for Storage:
  • Create a symbolic link at public/storage pointing to storage/app/public:
bash php artisan storage:link
  1. Clear Cache:
  • Clear cache if needed:
bash php artisan cache:clear php artisan config:clear

Step 5: Automate Deployment using Github Actions

  1. Create Deployment Script:
  • Create a deployment script in your project’s .scripts folder.
  1. Set Up Github Actions:
  • Create a Github Actions workflow YAML file in .github/workflows folder.
  1. Add Secrets:
  • Add necessary secrets such as HOST, PORT, USERNAME, and SSHKEY in Github repository settings.
  1. Commit and Push Changes:
  • Commit and push the changes to your Github repository.
  1. Verify Deployment:
  • Verify that your Laravel project is deployed automatically on Github Actions triggers.

By following these steps, you can successfully host and deploy your Laravel project using Github on an Apache web server VPS hosting environment. Automating the deployment process using Github Actions ensures seamless deployment of your application.