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

From $200 to $199: How Tremhost Beats Cloudflare’s Own Pricing Model

Cloudflare’s Business Plan is legendary. It includes enterprise-grade features...

Cheaper Than Cloudflare Itself? How Tremhost Bundles World-Class Security for Less

When it comes to website performance and protection, Cloudflare...

The World’s Cheapest Fully Managed Cloudflare Security—And Why Competitors Don’t Want You to Know

Let’s be real: big hosting providers make their money...

Africa’s Best-Kept Secret: Tremhost + Cloudflare = World-Class Security at Local Prices

Across Africa, businesses face the same cyber threats as...

From Downtime to Peace of Mind: Affordable Cloudflare DDoS Protection with Tremhost

Every minute your website is down costs money. Whether...

Topics

From $200 to $199: How Tremhost Beats Cloudflare’s Own Pricing Model

Cloudflare’s Business Plan is legendary. It includes enterprise-grade features...

Cheaper Than Cloudflare Itself? How Tremhost Bundles World-Class Security for Less

When it comes to website performance and protection, Cloudflare...

From Downtime to Peace of Mind: Affordable Cloudflare DDoS Protection with Tremhost

Every minute your website is down costs money. Whether...

The World’s Cheapest Managed Cloudflare Hosting? Tremhost Just Did It

Cloudflare is the name everyone trusts for DDoS protection,...

Cloudflare Protection Without the Global Price Tag: Tremhost Shows How

Cloudflare is known worldwide for delivering enterprise-grade website security...

How Tremhost Makes Enterprise-Grade Cloudflare Protection Affordable for Startups

Every startup has the same dream—scale fast, win customers,...
spot_img

Related Articles

Popular Categories

spot_imgspot_img