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

Top 5 Beaded Jewelry Trends in Africa (2025)

Beaded jewelry continues to thrive in Africa, blending traditional...

Crocheting for Beginners: Step-by-Step African Patterns

Crocheting is a rewarding craft that allows you to...

DIY African Black Soap: Homemade Recipes & Benefits

African black soap is renowned for its natural ingredients...

Where to Buy Candle-Making Supplies in Nigeria

If you're interested in starting candle-making in Nigeria, here...

How to Make Tie-Dye at Home (African Fabric Edition)

Creating tie-dye patterns on African fabrics is a fun...

Topics

Top 5 Beaded Jewelry Trends in Africa (2025)

Beaded jewelry continues to thrive in Africa, blending traditional...

Crocheting for Beginners: Step-by-Step African Patterns

Crocheting is a rewarding craft that allows you to...

DIY African Black Soap: Homemade Recipes & Benefits

African black soap is renowned for its natural ingredients...

Where to Buy Candle-Making Supplies in Nigeria

If you're interested in starting candle-making in Nigeria, here...

How to Make Tie-Dye at Home (African Fabric Edition)

Creating tie-dye patterns on African fabrics is a fun...

5 Best Sewing Machines for Beginners in Africa

Starting your sewing journey can be exciting, and choosing...

DIY Wall Décor Ideas Using African Prints

Transform your space with vibrant African prints! Here are...

How to Start a Bead-Making Business in Africa: Profitable Skills

Starting a bead-making business can be a rewarding venture...
spot_img

Related Articles

Popular Categories

spot_imgspot_img