Deploying static websites to VPS using git

git is a distributed open source version control system. If you’re not already familiar with git then read some free tutorials listed here and come back after you have some basic understanding to follow through the tutorial. Whether it’s a static website or a fun project, it’s usually a good idea to use a version control system such as git. Additionally, git can also help you in deployment and make your development workflow much easier and simpler.

git deployment

Step #1. Server Setup

If you’ve not already selected a VPS then get one there on Digital Ocean or Linode. Make sure you select Ubuntu (preferably : 14.04 LTS) for the server OS. Otherwise, you may need to adjust few commands depending on the Linux distribution you’re using.

Step #2. Configure git on the server

SSH into the remote server and configure git for deployment.

ssh example_user@IP_ADDRESS
mkdir example.git
cd example.git
git init --bare

Next, create a git hook – a shell script that will be executed on git push (to update code on server).

nano ~/example.git/hooks/post-receive

Here is a sample post-receive script you can use : (use Ctrl+X to save and exit)

And make it executable.

chmod +x ~/example.git/hooks/post-receive

Step #3. Install and configure git on dev machine

First, install git, move into your project directory and commit it under git.

sudo apt-get -y install git-core
cd example.com
git init
git add .
git commit -am "MVP is ready for the wild"

Add remote repository for deployment server.

git remote add production ssh://example_user@IP_ADDRESS/~/example.git

And now, you’re ready to deploy. Just type (to push recent changes to live server) :

git push production master

Step #4. Configure nginx

ssh into the server and create a nginx config for the domain (example.com).

sudo nano /etc/nginx/sites-available/example.conf

Here is a sample config you could use :

sudo ln -s /etc/nginx/sites-available/example.conf /etc/nginx/sites-enabled/example.com
sudo service nginx reload

Now, update the DNS settings at the registrar and create two records – one for naked domain (e.g example.com) and other for www-version of the website (e.g www.example.com) – both pointing to the server IP. And, wait for the DNS to be propagated (few minutes).

Join the Conversation

1 Comment

  1. Sounds like a good way to deploy jekyll blogs to linode. I’ll file this away! Thx.

Leave a comment

Your email address will not be published. Required fields are marked *