Publish static websites with git
seba@r7:~/snarvaez.poweredbygnulinux.com$ git remote add live ubuntu@u22small:~/snarvaez.poweredbygnulinux.com.git
git push live master:master
==================================================
how to use git to publish a static website to a remove hosting server? Provided that you have root ssh access to the remote server
To publish a static website to a remote hosting server using Git, you can automate the deployment process with a few steps. Here's a general workflow you can follow, assuming you have root SSH access to the remote server:
Steps:
Initialize a Git repository for your static website on your local machine (if you haven’t already):
git init
git add .
git commit -m "Initial commit"
Set up SSH keys (if not already configured) to allow password-less SSH login to your remote server:
On your local machine:
ssh-keygen -t rsa -b 4096
Copy the public key to the remote server:
ssh-copy-id root@your-server-ip
Test if the SSH login works:
ssh root@your-server-ip
Set up a bare Git repository on the remote server. A bare repository is a special type of Git repository without a working directory, used as a central repository for pushing and pulling changes.
On the remote server, create a directory for the bare repository:
ssh root@your-server-ip
Once logged in, create the Git repository directory:
mkdir -p /var/www/html/my-static-site.git
cd /var/www/html/my-static-site.git
git init --bare
Set up a post-receive hook to deploy the static files when changes are pushed to the bare repository:
Navigate to the hooks directory inside the bare repository and create a post-receive hook:
cd /var/www/html/my-static-site.git/hooks
touch post-receive
chmod +x post-receive
Edit the post-receive file to automatically deploy the static files when changes are pushed:
nano post-receive
Add the following script (replace /var/www/html/live-site with the directory where you want the website files to be deployed):
#!/bin/bash
GIT_WORK_TREE=/var/www/html/live-site git checkout -f
This will deploy the latest files to the /var/www/html/live-site directory whenever you push changes to the bare repository.
Add the remote repository to your local Git repository:
On your local machine, add the remote repository to your Git project:
git remote add live ssh://root@your-server-ip:/var/www/html/my-static-site.git
Deploy your static website:
Now, whenever you want to deploy your static website, just push to the live repository:
git push live master
The post-receive hook will automatically deploy the static files to the /var/www/html/live-site directory on the remote server.
Optional Improvements:
Use a non-root user for security: Instead of using root, it’s better to create a dedicated user for deploying the website, with limited privileges.
Use branches for staging/production environments: You can use different branches (staging, production, etc.) and have the post-receive hook deploy different branches to different directories.
Automate build processes: If your static site uses a build process (e.g., Jekyll, Hugo, etc.), you can modify the post-receive hook to run the build commands before deploying.
Author: Sebastian Emilio Narvaez
Created: 2024-09-08 dom 00:58
Validate