Over a million developers have joined DZone.

Deploy Your Static or Dynamic Websites Using Git in Public Cloud

Do you want to save some pennies or just for a quick demo? Well, that doesn't necessarily mean that you need to rent a VM.

· DevOps Zone

The DevOps zone is brought to you in partnership with Sonatype Nexus. The Nexus suite helps scale your DevOps delivery with continuous component intelligence integrated into development tools, including Eclipse, IntelliJ, Jenkins, Bamboo, SonarQube and more. Schedule a demo today

So, You Don’t Want to Start VMs in Public Cloud

If you want to save some pennies or just for a quick demo, you don’t necessarily need to rent a VM. You can run websites directly in your laptop, Linux, or Mac (pity for Windows users), then export your laptop to the internet.

Allow Git Clone by SSH Key and Run Git Pull Every 10 Minutes

Configure GitHub/BitBucket your SSH key.

I assume you’re familiar with Git clone by SSH. Probably like lots of developers, you will use your own private key. I highly disagree with that. Too dangerous! Your SSH private key can unlock lots of precious treasures, far more than this git repo. Right?

Git deploy key

How to improve? Create a read-only git deploy key in GitHub/BitBucket repo setting. To do that, we need to create a new SSH key pair so that it’s new and isolated. And Git deploy key has a nice limitation: we can’t use the same deploy key for multiple repos. It adds extra security.

For simplicity or reasonable security compromise, some people may still use their private key. I admit, it makes sense to some extent. Then, protect your SSH key with a passphrase. It’s easy and simple.

For simplicity, here, we use root to checkout the code and set up the website. To improve the security, try with an ordinary OS user.

# Update ssh key to below
vim /root/git_deploy_key

# ssh private key can't be widely open
chmod 400 /root/git_deploy_key

Specify the SSH key for the Git clone:

vim /root/.ssh/config

host github.com
     HostName github.com
     StrictHostKeyChecking no
     User git
     IdentityFile /root/git_deploy_key

Run the Git clone to get the repo:

# Run with correct parameters.
cd /var/www/
git clone git@github.com:XXX/denny_www.git

Create a crontab for a periodical Ggit pull. Now, there is no need for manual login and update anymore.

crontab -e

# Change file path
*/10 * * * * cd /var/www/denny_www; git pull

For Static Websites, Create Apache Vhost

For static websites of plain HTML, we don’t need to run  with our own server. It can be done with GitHub Pages. See more discussion here.

Well, GitHub Pages only solves 80% of the problem. If you’re in China, the accessibility of GitHub is not guaranteed. You may want to use your own domain instead of XXX.github.io. Use Apache2 or NGINX to serve the requests.

Apache vhost example (NGINX is pretty similar):

# Install apache
apt-get install -y apache2

# Define vhost
vim /etc/apache2/sites-enabled/my-static.conf

<VirtualHost *:80>
    ServerAdmin webmaster@dummy.test.com
    DocumentRoot /var/www/denny_www
    <Directory '/var/www/denny_www'>
        Options All
        AllowOverride All
        Require all granted
    ErrorLog /var/log/apache2/my-error.log
    CustomLog /var/log/apache2/my.log common

# Reload apache
service apache2 reload

For Dynamic Websites, Use Apache For HTTP Reverse Proxy

Let’s say your dynamic website listens on port 8082 and you want to open by My Test

Configure the DNS setting properly or be sure that /etc/hosts is properly changed. Then, enable Apache modules for:

a2enmod proxy
a2enmod proxy_http

# Restart apache to load modules
service apache2 restart

vim /etc/apache2/sites-enabled/dynamic.conf:

<VirtualHost *:80>
ServerName www.mytest.com
ServerAlias mytest.com
ServerAdmin webmaster@smallco.example.com
ErrorLog /var/log/apache2/dynamic_error_log
TransferLog /var/log/apache2/dynamic_access_log
DocumentRoot /var/www
ProxyPass /
ProxyPassReverse /

If necessary, configure the firewall to allow the traffic:

ufw allow 80/tcp

# Some URI might has port number attached
ufw allow 8082/tcp
curl -I http://www.mytest.com

Add Password Protection to Your Existing Websites

Sometimes, you may only want certain people to visit your websites. With Apache htpasswd, we can create a username-password pair easily. People will need to input the credential first in their web browser before the actual pages load.

Create a user credential:

apt-get install -y apache2-utils

# create password file for a new user
/etc/apache2/.htpasswd $username

Update the vhost setting to enforce the authentication from...

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    <Directory "/var/www/html">


<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    <Directory "/var/www/html">
        AuthType Basic
        AuthName "Restricted Content"
        AuthUserFile /etc/apache2/.htpasswd
        Require valid-user

Verify the setting and open http://$domain_name:$port in your web browser.

Enforce username and password for apache

Monitor Your Websites with Free SaaS Monitoring

Check your URL every five minutes. If the URL doesn’t return HTTP 200 OK or tcp ports down, then we get alerts.

From my experience, I highly recommend uptimerobot. It’s a very reliable and light-weight SaaS monitoring service and it’s totally free. You can even integrate the alert notification with Slack via a Slack email robot.

More Reading: Effectively Technical Writing in GitHub

The DevOps zone is brought to you in partnership with Sonatype Nexus. Use the Nexus Suite to automate your software supply chain and ensure you're using the highest quality open source components at every step of the development lifecycle. Get Nexus today


Published at DZone with permission of Denny Zhang, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}