In this post, I’ll try to cover the following things in brief.
- Getting familiar with Digital Ocean and creating your first droplet
- Setting up SSH and connecting to your server from your local machine
- Setting up a simple Express.js web-server in local machine
- Using Git to deploy your code to server
- Installing Node.js and NPM on the server
- Deploying a simple Hello world app
In the upcoming posts, we’ll cover
- Attaching a domain name to the server
- Setting Up NGINX
- Using PM2 for your node application in Production
- Reverse proxying using NGINX
Step 1. Creating a DigitalOcean account with free credits
DigitalOcean with no doubt have made cloud easier and much accessible to people with any skill level. The simple interface and outstanding customer support makes DigitalOcean favorite for hobbyist developers and large enterprises. Digitalocean provides a welcome credit of $100 for every signups with this unique link. Follow the link and click on Sign Up, You should be able to see a message mentioning your welcome credit of $100.
You might need to add your credit card information to continue the sign-up procedure and get the $100 reward. Don’t worry, DigitalOcean won’t deduct any money from your account without prior notice and a $100 is good enough to run a free server for almost a year.
Once your sign up process is complete, and your credit card details has been successfully added, your account will have $100 credit to use in DigitalOcean. You’ll be able to see your credits in the billing section of your account, you can find the “billing” menu under the “account” section in the left navigation ar in your dashboard. Now, Let’s head on to creating a new server!
Step 2. Creating a new Droplet
Droplets are individual computing units in DigitalOcean, You can imagine them as individual servers or computers (after all cloud is actually someone else’s computer). We need to host our server in one of the droplets we create. Click on the “Create” button and choose droplet to proceed to create a new droplet.
Next, in the choose an image option, you can choose “ubuntu”, which will be selected by default. This is the image of the operating system that DigitalOcean will install in our server.
Next, on the choose a size option, you can choose a server configuration that fits your needs. Here, I’ll be choosing a $40 per month configuration which has quite a good configuration, to begin with. You may choose a $5 server if all you need is a 1gb ram server.
You can leave the Add backup option to “No” and Add block storage option as is for now. Next, on the Choose a data center option you need to choose a data center location that is closer to the location of your target audience. The larger the audience from a Data Center near to them, the faster your site will be accessible to them. Since I am from India, the Bangalore data center is closer to me, and since most of my reader are from India, this is the best choice. If you are living elsewhere feel free to chose the one nearest to you.
Next, we need to set up SSH keys for secure access to our server. Click on the add SSH Key button, a popup will open requesting you to paste the public key and name.
To create a new SSH key pair, if you are in a Linux machine use the following command in your terminal. (without the $)
You can specify the name of the key and provide a passphrase while generating the RSA keys. For example, if you have given the name as “mydroplet”, your private key and public key will be saved to the current working directory with the names mydroplet and mydroplet.pub. Once the keys have been generated, move the files named as you specified and name.pub to your
.ssh directory in the home folder. Once you are done with this you can copy the contents of the public key, ie. mydroplet.pub and paste it in the above-mentioned field in DigitalOcean. If you have NPM installed in your system, u can simply use the
cli-cp library to copy the contents of the file directly. To install this package, simply execute the command
npm i -g cli-cp, this is completely optional, and you can do it manually too. We’ll walk you through the process in the below screencast so you can follow the steps easily.
In the end, paste the contents into the text field in DigitalOcean to add the SSH key with a name and choose the name to select the keys to be used.
Next, click on the big green button saying create to create your droplet.
Step 3. Assigning a floating IP to your droplet
A floating IP is a static IP address that points to one of your Droplets. It allows you
to redirect network traffic to any of your Droplets in the same datacenter. So that you can have a backup server and you can change the current floating IP to point to the backup server in case the main server goes down (or u need to shut down for maintenance, or want to point the domain to a different droplet in later time). To create a new floating IP head on to the Networking menu in the side navigation and choose the tab Floating IPs. Click on the Search for droplet field and choose the droplet you just created and click on Assign Floating IP.
Step 4. Logging into the Server/droplet using SSH
Once you have assigned the floating IP, you can now log in to the server from your local machine using SSH. Copy the floating IP of your droplet and execute the following command in your Terminal. Windows users can either use Windows Subsystem for Linux or manually install OpenSSH client in your PowerShell to use SSH.
$ ssh root@YOUR-FLOATING-IP
If it gives a warning, type yes on the prompt to add your server to known host. You’ll be logged into your server and will be able to execute commands in your droplet now.
Step 5. Installing Node.js in your server
Make sure you are logged in to your server using SSH, We’ll use the Node Version Manager (NVM) to install Node.js and NPM in our server. Execute the following shell script provided in the nvm documentation to get started with the installation.
$ wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.34.0/install.sh | bash
Once the Shell Script is successfully run, you need load nvm. Execute the following command to make the changes made by the shell script take effect in the current terminal.
$ source .bashrc
Once it’s done, you can check whether nvm is installed, by simply executing the command
nvm. To install the latest version of Node.js and NPM execute the following command.
$ nvm install node
In order for some npm packages to work (those that require compiling code from source, for example), you will need to install the build-essential package:
$ sudo apt install build-essential
Step 6. Testing everything with a simple Node.js app
Once we are done setting up all the necessary things we need to run a simple Node.js application, let’s put it into the test. Let’s use a simple Node HelloWorld application. We have already created a simple node app for this, and we’ll use git to clone this project and run it. Make sure you have git installed in the server, you can use the command
git --version to check the version of git in the server. Git will be installed by default in the server, if not, you can install git by simply executing
sudo apt install git.
Execute the following command to clone the repository from GitHub to your server. This will be the common workflow we’ll be using to bring our code into the server later.
$ git clone https://github.com/zocada/node-hello-world.git
Let’s run the app
$ cd node-hello-world.git $ node index.js
Now you should be able to check the output of the node web app we ran from a browser by navigating to:
http://192.168.1.1:3000 and you will be able to see the HelloWorld message.
In the upcoming post, we’ll discuss how to attach a domain to your server, setup Nginx for reverse proxy and PM2 for running the node applications in production.