Guide to Migrate From EC2 to Droplets

Written by s0ftvar | Published 2017/01/27
Tech Story Tags: digitalocean | software-deployment | python-flask | server-configuration | server-migration

TLDRvia the TL;DR App

Note: This article isn’t about comparing Amazon EC2 and DigitalOcean Droplet. It’s just why I decided to shift and how easy it was.

Hey, guys! I daydream myself as a Software Engineer, who loves software designing and development to build something useful for humans. Developing a software not only involves programming but also to deploy it(assuming it to be a web-app in this article) to be easily accessible anytime anywhere over the internet. This deployment stuff obviously requires a server to host the site and configuring it properly in order to serve the user-made calls. Behind the human-computer interaction, there’s a lot of fuzz going on the servers. Static sites, which are simply built for the purpose of displaying information like portfolio sites, are easy to host and generally don’t require a dedicated server. There are lots of good options out there in the market like Github’s free hosting which is handy to operate and very popular these days.The server is essential when there’s a need to process user input, persist data and retrieve it from a database, or do some computational work. Following is the minimalistic software lifecycle chart.

This post happens to be a lengthy one as it actually explains a step-by-step procedure to shift from one server to another. Why don’t you grab up a cup of coffee to enjoy the show?

Sometime back in time, I developed two web-apps back to back, officially named as Json2Html and Translatr. [ Click names to view live demo ]

Json2Html helps in converting JSON structure into a well defined tabular structure whereas Translatr tries to help the (multilingual) audience to translate a text or a paragraph into more than one language at once. Please give both the apps a shot and provide some feedback. Thank You! 🙏

I choose Python as the programming language for both the apps just because I prefer it’s ease of use. Once the apps were programmed and ready for the general availability, came the time to deploy them. As it was my first time deploying some real stuff, I chose Heroku which provides a rich ecosystem of pre-integrated extensions and services to deploy great apps at scale with little configuration. But I wasn’t happy at all. I observed Heroukuapp to be slow plus I always like things to be managed my own way. Moreover, I consider trying things over getting things done with ready-made solutions to be a great self-learning experience in any field. I usually learn technologies by actually building stuff myself. Hands-on experience is the power bank of knowledge.Soon after some time, I went with Amazon Ec2 for it’s most obvious reasons.

Amazon Elastic Compute Cloud (Amazon EC2) is a web service that provides resizable compute capacity in the cloud. It is designed to make web-scale cloud computing easier for developers. Amazon EC2’s simple web service interface allows you to obtain and configure capacity with minimal friction.

It’s a very popular cloud hosting provider in the market, a reliable, secure, completely controlled, and easy to start with provider. Moreover, it offers one year long free-tier plan for the beginners to get hands-on experience with AWS Cloud Services. Who wouldn’t like to play with such liberty? 😎

So, I went through the relevant AWS Amazon Docs Online and must admit that the quality of docs is very very impressive. They are written in a well-organized manner, covering exactly what the user actually wants. I think Amazon has used Machine Learning here too. 😜

My EC2 t2.micro instance was ready to rule after managing a lot of struggle configuring it like a new born baby. The server was quite a noob and obeyed my commands well and was up to serve the audience. It was a moment of gratification.

Ah! The time hits the door exactly after a year and reminded me of the free tier plan’s expiry. I started getting emails to pay off the bill for the extra graceful month. The cost, as it reads, was something around $22/month. I couldn’t believe that my personal portfolio cum blogging site, running just two more python apps could generate such a handsome amount of money. I was totally confused but as soon as the next email arrived in my inbox clearly indicating me to pay the another next month bill which was projected to be somewhat around $25 otherwise the instance would forcefully be shut down; I decided to look out for other options.

For a moment, I couldn’t understand the dilemma I was in; Whether I should start making the real value from the organic traffic which could save me a couple of dollars from my pants or I should first prepare myself to switch to some lower-range option available and then think of monetization.

This hit me hard to think wisely. Finally, after researching and discussing stuff with my colleagues, I considered DigitalOcean to be best suitable for my overall personal requirements.

DigitalOcean Setup

DigitalOcean is a simple cloud hosting provider built for developers. Every droplet which spins up is a new VPS for our personal use. It’s very easy to deploy a blazing fast SSD cloud server in just 55 seconds within an easy-to-use control panel. Plans start at $5 per month and include 512MB RAM, 20GB SSD Disk, and 1TB Transfer.

A damn good deal, indeed.

I signed up instantly with a new account. Check out these promo codes before entering billing info to get some good discount. Get flat $10 discount using this link. Once the account was created, verified and billing info was provided, I proceeded to create a new droplet.

STEP 1: Choose an image

I considered Ubuntu 14.04 as I’m much more familiar with this OS. Also, most of the DigitalOcean blogs are written considering this OS only.

STEP 2: Choose the droplet size

I went with $5 plan for its obvious benefits in lesser cost.

STEP 3: Choose a datacenter

I choose New York 3 based on my website traffic history. I went through my Google Analytics account associated with my website and realized most of the visitors are coming from the US. So, it’s better to choose the datacenter located nearest to the majority of visitors’ geographical location to enjoy the benefits of reduced latency.

STEP 4: Other Information

I created a new ssh key for the droplet.

cd ~/.sshssh-keygen -t rsa -f ubuntu_512mb_nyc3_01 -C “[email protected]”ssh-add ~/.ssh/ubuntu_512mb_nyc3_01cat ubuntu_512mb_nyc3_01.pub

I Copied and pasted the content in the box shown below. The SSH key is very private, so please don’t share it or post it publicly.

STEP 5: And it’s done!

I hit the Create button and Voila! I have my own server up and running with my preferred choice of OS installed in it.

Add a domain

Now, point server’s IP to the domain name. I entered my personal website URL to start managing my DNS within my newly created DigitalOcean account.

To make a new hostname, I only need to fill in the A record. If you are using an IPv6 address, you should enter it into the AAAA record.

A Records: I entered in my server’s IP address and the hostname itself. The hostname will be prepended to the domain name. My screen looked like this:

Read more on the DigitalOcean blog in detail.

Point to DigitalOcean nameservers from common domain registrars

I use Namecheap as my default registrar. It’ simply awesome. I bound my domain name to one of the droplets by changing the domain name servers with the domain’s registrar.Read more on the DigitalOcean blog in detail.

Install apache and get the static site up and running

I usually prefer Apache over Nginx as I have been using it for long and I’m less accustomed to Nginx. Apache is a free open source software which runs over 50% of the world’s web servers. The Apache web server is the most popular way of serving web content on the internet.

To install apache, I ssh to my server.

ssh [email protected]

And installed apache via the following commands.

sudo apt-get updatesudo apt-get install apache2

That’s it. To check if Apache is installed, direct your browser to your server’s IP address (eg. http://12.34.56.789). The page should display the words “It works!” like this.

How to Find your Server’s IP address

The following command helps revealing server’s IP address.

ifconfig eth0 | grep inet | awk '{ print $2 }'

Securely transfer files from server to remote and vice-versa

I used sftp for transferring files between a remote server and a local server.

SFTP, which stands for SSH File Transfer Protocol, or Secure File Transfer Protocol, is a separate protocol packaged with SSH that works in a similar way over a secure connection. The advantage is the ability to leverage a secure connection to transfer files and traverse the filesystem on both the local and remote system.

Read more on the DigitalOcean blog in detail which is an ultimate guide on how to transfer files from one server(eg: A_mazon EC2_) to local server(eg: personal system) and then to the another server(eg: DO droplet).

This way of transferring is obviously very verbose in nature. One can opt for easy to use interface like thatof Cyberduck. I have used it and is quite useful sometimes.

Deploying Python Applications

My two web apps: Json2Html and Translatr, which I mentioned earlier, both uses Flask, a micro-framework for Python based on Werkzeug.

My next step was to deploy them successfully on the server to be accessible over the internet to the audience.

STEP 1: Install mod_wsgi

sudo apt-get install libapache2-mod-wsgi python-dev

STEP 2: Enable mod_wsgi

sudo a2enmod wsgi

STEP 3: Creating Flask app

Check out the following apps with their source code to build up a basic Flask app.1. Json2Html — View Source Code 2. Translatr — View Source Code

Always have all your requirements in a file. Let’s name it: requirements.txt. Its content can be manually written or run pip freeze which will list down all the installed modules so far in the virtual environment. Eg:

Flask==0.10.1requests

STEP 4: Setting up Python environment using virtualenv

Using pip to install virtualenv and Flask. Installed pip through apt-get.

sudo apt-get install python-pip

Use pip to install virtualenv using the following command:

sudo pip install virtualenv

I created a virtual Python environment(VENV is the name I gave to my temporary environment).

sudo virtualenv VENV

Now, I installed Flask in that environment by activating the virtual environment with the following command:

source VENV/bin/activate

Now, I installed all the dependencies of my app as listed in requirements.txt above.

pip install -r requirements.txt

I verified if everything’s working fine by running:

python __init__.py

I got a message that my server is running on 127.0.0.1:5000. It guaranteed that everything was working fine.

I created Virtual Hosts for different apps with different domains to point at. Creating Virtual Hosts has been covered up in next section.

Read more on the DigitalOcean blog in detail.

Set Up Apache Virtual Hosts

Virtual Host is the basic unit which allows the administrator to use one server to host multiple domains or sites off of a single interface or IP by using a matching mechanism. This is relevant to anyone looking to host more than one site off of a single VPS.

STEP 1: Change directory as follows:

cd /etc/apache2/sites-enabled

STEP 2: Sample Virtual Host file, let’s call it example.conf

vim example.conf

# Content of example.conf

<VirtualHost *:80>ServerAdmin [email protected] example.comServerAlias www.example.comDocumentRoot /var/www/example.com/public_htmlErrorLog ${APACHE_LOG_DIR}/error.logCustomLog ${APACHE_LOG_DIR}/access.log combined</VirtualHost>

STEP 3: Enable the Virtual Host

sudo a2ensite example.conf

STEP 4: Restart the apache server

sudo service apache2 restart

Read more on the DigitalOcean blog in detail.

Hope you might have learned something new in this post.For any queries, drop me words at [email protected].Please recommend, tweet and share to boost my confidence for more write-ups. Thanks a lot!

I’m a social person. Would love to be connected on the following:Website | Github | Twitter | LinkedIn | StackOverflow


Published by HackerNoon on 2017/01/27