StarDomain

Deploying Your Website with Git

Deploying Your Website with Git

Git deployment provides a modern, reliable way to push code changes from your local development environment directly to your web hosting server. Instead of manually uploading files via FTP, you can use Git to deploy updates with a single command. This guide covers the different methods for deploying your website using Git on your {{COMPANY_NAME}} hosting account.

Why Use Git for Deployment?

Git-based deployment offers significant advantages over traditional FTP:

  1. Version control: Every change is tracked and can be rolled back
  2. Atomic deployments: All files update together, preventing partial updates
  3. Speed: Only changed files are transferred, not the entire site
  4. Automation: Deploy with a single git push command
  5. Collaboration: Multiple developers can work on the same project safely
  6. History: Full audit trail of who changed what and when

Tip: All {{COMPANY_NAME}} shared hosting plans include SSH access and Git support. You can start using Git deployment immediately.

Prerequisites

Before setting up Git deployment:

  1. Git installed locally on your development machine
  2. SSH access to your hosting account (enabled by default on our plans)
  3. SSH key authentication configured (recommended over password authentication)
  4. Basic Git knowledge (commits, branches, push/pull)

Method 1: Git Push to a Bare Repository

This is the most common and reliable method for shared hosting deployment.

Step 1: Set Up SSH Key Authentication

  1. Generate an SSH key pair on your local machine (if you do not have one):
bash
ssh-keygen -t ed25519 -C "[email protected]"
  1. Copy the public key to your hosting account:

- In cPanel: Go to Security > SSH Access > Manage SSH Keys > Import Key

- In DirectAdmin: Go to Account Manager > SSH Keys

- Paste your public key content and authorize it

Step 2: Create a Bare Repository on the Server

SSH into your hosting account and create a bare Git repository:

bash
ssh [email protected]
mkdir -p ~/repos/mysite.git
cd ~/repos/mysite.git
git init --bare

Step 3: Create a Post-Receive Hook

This hook automatically copies files to your web directory after each push:

bash
cat > ~/repos/mysite.git/hooks/post-receive << 'HOOK'
#!/bin/bash
GIT_WORK_TREE=$HOME/public_html git checkout -f main
HOOK
chmod +x ~/repos/mysite.git/hooks/post-receive

Replace public_html with your actual web root if different, and main with your branch name.

Step 4: Add the Remote to Your Local Repository

On your local machine:

bash
cd /path/to/your/project
git remote add production ssh://[email protected]/home/user/repos/mysite.git

Step 5: Deploy

Push your code to deploy:

bash
git push production main

Your website files will be automatically checked out to the web root.

Method 2: Using cPanel Git Version Control

cPanel includes a built-in Git deployment feature:

  1. Log in to cPanel
  2. Go to Files > Git Version Control
  3. Click Create to set up a new repository
  4. Enter the repository details:

- Clone URL: Your remote Git repository URL (e.g., GitHub, GitLab, Bitbucket)

- Repository Path: Where to store the repo on the server (e.g., /home/user/repositories/mysite)

- Repository Name: A descriptive name

  1. Click Create

Pulling Updates

  1. Go to Git Version Control in cPanel
  2. Click Manage next to your repository
  3. Click Pull or Deploy tab
  4. Click Update from Remote to pull the latest changes
  5. Click Deploy HEAD Commit to deploy the changes

Setting Up a .cpanel.yml Deploy File

Create a .cpanel.yml file in your repository root to define deployment tasks:

yaml
---
deployment:
  tasks:
    - export DEPLOYPATH=/home/user/public_html/
    - /bin/cp -R * $DEPLOYPATH

This file tells cPanel how to deploy your files.

Method 3: GitHub/GitLab Webhooks

Automate deployment when you push to GitHub or GitLab:

Step 1: Create a Deployment Script

Create a PHP file on your server (e.g., deploy.php):

php
<?php
$secret = 'your-webhook-secret';
$signature = $_SERVER['HTTP_X_HUB_SIGNATURE_256'] ?? '';
$payload = file_get_contents('php://input');

if (!hash_equals('sha256=' . hash_hmac('sha256', $payload, $secret), $signature)) {
    http_response_code(403);
    die('Unauthorized');
}

$output = shell_exec('cd /home/user/repos/mysite && git pull origin main 2>&1');
file_put_contents('/home/user/logs/deploy.log', date('Y-m-d H:i:s') . "\n" . $output . "\n", FILE_APPEND);
echo "Deployed successfully";

Step 2: Set Up the Webhook

  1. Go to your GitHub repository > Settings > Webhooks
  2. Add a new webhook with:

- Payload URL: https://example.com/deploy.php

- Content type: application/json

- Secret: Your secret key (matching the script)

- Events: Select "Just the push event"

  1. Save the webhook

Now, every push to your GitHub repository will trigger a deployment.

Handling Build Steps

If your project requires a build step (e.g., npm install, composer install), add it to your post-receive hook:

bash
#!/bin/bash
GIT_WORK_TREE=$HOME/public_html git checkout -f main

cd $HOME/public_html

# Install PHP dependencies
if [ -f composer.json ]; then
    composer install --no-dev --optimize-autoloader
fi

# Install Node.js dependencies and build
if [ -f package.json ]; then
    npm install --production
    npm run build
fi

Best Practices

  1. Use .gitignore properly: Exclude sensitive files (.env, config files with passwords), node_modules, vendor directories, and compiled assets from version control
  2. Never commit secrets: Keep API keys, database passwords, and other secrets out of Git. Use environment variables or .env files (excluded from Git)
  3. Test before deploying: Always test changes locally before pushing to production
  4. Use branches: Deploy from a specific branch (e.g., main or production) and do development on feature branches
  5. Keep the repository clean: Avoid committing large binary files (images, videos, databases)
  6. Monitor deployments: Check your site after each deployment to catch issues early
  7. Document your setup: Keep notes on your deployment configuration for team members

Troubleshooting

Permission Denied When Pushing

  • Verify your SSH key is added and authorized in your hosting account
  • Check the repository path in your remote URL is correct
  • Ensure the repository directory permissions allow your user to write

Files Not Updating After Push

  • Check the post-receive hook has execute permission (chmod +x)
  • Verify the GIT_WORK_TREE path matches your actual web root
  • Check the branch name matches (main vs. master)
  • Review the hook output for errors

Merge Conflicts on the Server

  • Avoid making direct changes to files on the server
  • If conflicts occur, SSH into the server and resolve them manually
  • Consider resetting the server copy: git checkout -f main
  • Setting Up Git Version Control for Your Website
  • How to Access Your Account via SSH
  • How to Generate and Use SSH Keys

Need help setting up Git deployment? Contact our support team at {{SUPPORT_EMAIL}} or visit {{SUPPORT_URL}}.