How to deploy a website from Git on your server

How to Deploy a Website from Git on Your Server

Deploying your website directly from a Git repository can streamline updates and version control. Follow these steps to deploy your website on your server:


1. Prepare Your Server

  • Install Git:
    Ensure Git is installed on your server. On Ubuntu/Debian:

    sudo apt update && sudo apt install git -y
    

    On CentOS/AlmaLinux:

    sudo yum install git -y
    
  • Set Up Your Web Directory:
    Decide where your website files will live (e.g., /var/www/yourwebsite). Create the directory if it doesn’t exist:

    sudo mkdir -p /var/www/yourwebsite
    sudo chown -R $USER:$USER /var/www/yourwebsite
    

2. Clone Your Git Repository

  • Navigate to Your Web Directory:
    Change to your website directory:

    cd /var/www/yourwebsite
    
  • Clone the Repository:
    Clone your Git repository into the directory:

    git clone https://github.com/yourusername/yourrepository.git .
    

    Replace the URL with your repository’s URL. The dot (.) at the end clones the contents into the current directory.


3. Set Up Deployment Options

a. Manual Deployment

  • Pull Updates:
    Whenever you update your repository, log in to your server, navigate to your web directory, and run:

    git pull origin main
    

    Adjust the branch name (main or master) as needed.

b. Automated Deployment (Optional)

  • Using Webhooks:
    Set up a webhook in your Git repository (on platforms like GitHub or GitLab) to trigger a deployment script on your server.
  • Create a Deployment Script:
    Create a simple script (e.g., deploy.sh) in your web directory:

    #!/bin/bash
    cd /var/www/yourwebsite || exit
    git pull origin main
    # Add any additional commands, like clearing cache or restarting services
    

    Make it executable:

    chmod +x deploy.sh
    
  • Configure a Listener:
    Use a tool like a simple PHP script or a continuous integration service to trigger the deploy.sh script when a webhook is received.

4. Configure Your Web Server

  • Set Up Virtual Hosts/Server Blocks:
    Ensure your web server (Apache or Nginx) is configured to serve files from /var/www/yourwebsite.
    For Apache, you might create a virtual host file:

    <VirtualHost *:80>
        ServerName yourdomain.com
        DocumentRoot /var/www/yourwebsite
    
        <Directory /var/www/yourwebsite>
            AllowOverride All
            Require all granted
        </Directory>
    
        ErrorLog ${APACHE_LOG_DIR}/yourwebsite_error.log
        CustomLog ${APACHE_LOG_DIR}/yourwebsite_access.log combined
    </VirtualHost>
    

    Enable the site and restart your web server.


5. Secure Your Deployment

  • Set Appropriate Permissions:
    Ensure that your web server user (e.g., www-data for Apache on Ubuntu) can read the files:

    sudo chown -R www-data:www-data /var/www/yourwebsite
    sudo find /var/www/yourwebsite -type d -exec chmod 755 {} \;
    sudo find /var/www/yourwebsite -type f -exec chmod 644 {} \;
    
  • Protect Sensitive Files:
    Make sure your .git directory isn’t accessible from the web. For Apache, you can add this to your .htaccess:

    RedirectMatch 404 /\.git
    

Final Thoughts

Deploying your website from Git on your server not only streamlines your development process but also provides a robust method to manage code versions and updates. Whether you choose manual pulls or automate the process with webhooks, this approach keeps your deployment efficient and organized.

Ready to deploy your website? Set up your server, clone your repository, and configure your web server to enjoy a seamless, Git-driven deployment process!

Hot this week

How to Start a Business with Unlimited Reseller Hosting

Starting a hosting business can be a lucrative venture,...

How to Troubleshoot Slow Sites on Fast cPanel Hosting

Experiencing slow loading times can be frustrating, especially when...

Guide: Fast cPanel Hosting Tips for Beginners

Starting your journey with cPanel hosting can seem daunting,...

How to Choose Fast cPanel Hosting for WordPress Sites

Selecting the right hosting provider for your WordPress site...

Fast cPanel Hosting: How to Optimize for Peak Performance

In the competitive online landscape, optimizing your website for...

Topics

How to Start a Business with Unlimited Reseller Hosting

Starting a hosting business can be a lucrative venture,...

How to Troubleshoot Slow Sites on Fast cPanel Hosting

Experiencing slow loading times can be frustrating, especially when...

Guide: Fast cPanel Hosting Tips for Beginners

Starting your journey with cPanel hosting can seem daunting,...

How to Choose Fast cPanel Hosting for WordPress Sites

Selecting the right hosting provider for your WordPress site...

Fast cPanel Hosting: How to Optimize for Peak Performance

In the competitive online landscape, optimizing your website for...

Fast cPanel Hosting Setup: A Step-by-Step Guide

Setting up your website with fast cPanel hosting is...

How to Speed Up Your Site with Fast cPanel Hosting

In today’s digital landscape, speed is crucial for the...

How to Secure Your Business on Cheap Web Hosting in Zimbabwe

Ensuring your business is secure while using affordable web...
spot_img

Related Articles

Popular Categories

spot_imgspot_img