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 a Professional Website Can Double Your Business Leads in 90 Days

Why a Professional Website Matters A business website isn’t just...

How to Move Your School Online with Tremhost’s Education Plans

Learn how Zimbabwean schools can transition online with Tremhost’s...

How School Websites Help Increase Enrollments in Zimbabwe

Learn how having a professional school website in Zimbabwe...

Why Your Business Needs a Website in 2025 – Especially in Zimbabwe

Discover why Zimbabwean businesses need a professional website in...

How to Choose the Right Web Design Company in Zimbabwe

Why Choosing the Right Web Design Company Matters Your website...

Topics

How a Professional Website Can Double Your Business Leads in 90 Days

Why a Professional Website Matters A business website isn’t just...

How to Move Your School Online with Tremhost’s Education Plans

Learn how Zimbabwean schools can transition online with Tremhost’s...

How School Websites Help Increase Enrollments in Zimbabwe

Learn how having a professional school website in Zimbabwe...

Why Your Business Needs a Website in 2025 – Especially in Zimbabwe

Discover why Zimbabwean businesses need a professional website in...

How to Choose the Right Web Design Company in Zimbabwe

Why Choosing the Right Web Design Company Matters Your website...

Websites Built to Grow Your Business in Zimbabwe

Discover how Tremhost builds custom websites that grow businesses...

How to Make Your School Visible on Google in Zimbabwe

Learn how Zimbabwean schools can rank higher on Google...

Why Professional School Emails Build Parent Trust

Discover how professional school email addresses boost parent trust...
spot_img

Related Articles

Popular Categories

spot_imgspot_img