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.

## Basic VPS Setup for deploying static sites
## Server OS : Ubuntu 14.04 LTS 64 bit
## Author: Ramesh Jha (ramesh[at]rameshjha.com),(http://blog.sudobits.com)
## License: MIT
#### SSH into the server `ssh root@IP_ADDRESS`
### 1. Update system packages
apt-get -y update
apt-get -y upgrade
### 2. For apt-add
apt-get install python-software-properties
### 3. Create a deployment user
adduser example_user --ingroup sudo
### 4. Install server (nginx)
apt-add-repository -y ppa:nginx/stable
apt-get -y update
apt-get -y install nginx git-core
#### Logout from the remote server (exit)
#### and setup ssh authentication
### Upload ssh key [dev machine]
### run `ssh-keygen`command to generate a ssh key if you don't already have one
ssh-copy-id example_user@IP_ADDRESS

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)

#!/bin/sh
echo 'Deploying to the server............'
GIT_WORK_TREE=/home/example_user/example.com git checkout -f
echo '..............................Done.'

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 :

server {
listen 80;
server_name www.example.com;
rewrite ^(.*) http://example.com$1 permanent;
}
server {
listen 80;
server_name example.com;
root /home/example_user/example.com/public;
# Cache static content
location ~* \.(?:ico|css|js|gif|jpe?g|png)$ {
expires 90d;
add_header Vary Accept-Encoding;
access_log off;
}
}

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 *