How to Host a MEAN Stack App on Digital Ocean

How to Host a MEAN Stack App on Digital Ocean

How to Host a MEAN Stack App on Digital Ocean ... Helping millions of developers easily build, test, manage, and scale applications of any size – faster than ever before.🌟🌟🌟🌟🌟

Software development involves certain steps to be repeated overtime for different projects.

One of those steps is setting up the infrastructure for your web applications.

Since humans are easily annoyed with performing the same steps over and over, cloud computing is a giant leap towards saving humans the aforementioned annoyance.

Table of Contents

  • A Word on Digital Ocean
  • Creating an SSH Key
  • Registering your Account
  • Creating your Droplet
  • Logging in to your Droplet
  • MEAN.JS
  • Hosting Node Applications
  • Keeping your Application Alive
  • Deploying your Application
  • Curtains

There are many services available that provide you with a point and click interface to set up your application's infrastructure within seconds, one of them is Digital Ocean.

In this tutorial, you will learn.

  1. Digital Ocean's main features.
  2. How to create a droplet using Digital Ocean's one-click install apps.
  3. Logging in to your droplet.
  4. Using git to deploy your application.

A Word on Digital Ocean

So what really is Digital Ocean? From Digital Ocean's official website.

DigitalOcean is a cloud infrastructure provider focused on simplifying web infrastructure for software developers.

Building software is complex, laying out the infrastructure to run software is even more complex.

Digital Ocean provides you with options that allow you to lay out your application's infrastructure without a sweat.

Some of it's key features are.

  1. Servers use high performance solid state drives. Their speed greatly enhances your application's performance and setting up instances take less of your time.
  2. They have a very intuitive control panel that helps you perform a range of tasks.
  3. They have a very active community and their help pages are frequently updated and added to which means there is a great chance of help being readily available in case you get stuck.
  4. One-click installations allow you to spin up your preferred stack with a single click(as you will see later in this tutorial).

And the best among the lot is their prices are pretty easy on your wallet, starting at $5 per month.

Creating an SSH Key

When we create our droplet, we are going to need to specify an SSH key in order to access it.

So let us have it ready before creating the droplet. Fire up your terminal and execute the following command.

$ ssh-keygen -t rsa

You will be prompted to enter the name of the file (with the full path) in which to save the key. This was shown to you when you generated the key and chose a place to save it. Usually the default is in your User folder and the /.ssh folder in there.

Once you have done that, skip the passphrase section by hitting enter twice.

Your private and public key will be created and all the essential details will be prompted on the screen.

The file that we are interested in, which will be used later when creating the droplet is the .pub file which is the public key that will be used for authentication. We will be using the contents of this file during the droplet creation process later.

Also, note down your computer's name using the hostname command.

Registering your Account

Before we get started, you should have a fully functional Digital Ocean account.

Registering an account is pretty simple.

Here is a walkthrough of the registration process.

  1. Adding your personal details along with your email address.
  2. Verifying your account using the email sent at your provided email address in step one.
  3. Adding your credit card details.

At the time of this writing, Digital Ocean only accepts credit cards. Debit cards and prepaid cards such as Payoneer will not work so be sure to have a credit card before getting started.

Creating your Droplet

Once you have registered your account, move to the Droplets section at the dashboard and click the Create Droplet button.

In my case, I have not yet created a single droplet so your Droplet section may differ slightly considering you have created other droplets before.

Nonetheless, in either case, you should have the Create Droplet button.

The Create Droplets section further gives you the option to setup your application from scratch using the Distributions tab or use ready to use stacks under the One-click apps section.

We are going to use the MEAN 0.5.0 on 16.04 stack.

One-click apps provide a ready to use infrastructure configuration where our application's environment is set up for us and we only need to focus on application deployment and stability.

Once you have clicked MEAN 0.5.0 on 16.04, scroll down to move to the Choose a size section.

Choosing a size depends on the type of application you intend to deploy so you will need to use a bit of your imagination to get through this part.

For example, if you are deploying a high traffic image service that provides cropping and resizing features to it's users, you will need a high end configuration with lots of CPUs and disk space.

Similarly, a REST API can do with a medium configuration machine.

For our case, we are going to specify the very first $5/mo configuration with 1 CPU, 512MB RAM, 20GB SSD disk, and 1000GB data transfer.

Skip the Add block storage section.

The Choose a datacenter region section allows you to specify the physical location of your application.

In our case, it is really upto your personal preference as we are deploying a single instance of our application. However, remember that this option is used in case you wish to deploy multiple instances of your application.

It helps reduce network latency as your users can be served from the nearest located instance of the application.

It can also help to have your application in multiple physical locations in case of a natural disaster such as an earthquake.

For some strange reason, I have always loved Germany hence I have chosen the Frankfurtdatacenter.

You can skip the Select additional options section as well but if you are curious, you can hover over each option to view it's description.

Moving to the Add your SSH keys section, we need to create an SSH key for our droplet in order to initiate remote shell sessions.

It is a recommended security practice and whenever dealing with server communication, you should opt for it.

Click the New SSH Key button.

In the New SSH Key form's SSH key content field, paste in the contents of the .pub file that we created in the Creating an SSH Key section.

Also paste in the output of the hostname command in the Name field.

Click the Add SSH Key button to create your SSH key.

Finally, we need to specify the number of droplets and the hostname(for our droplet) in the Finalize and create section.

You can also add tags to easily identify your droplet such as the application's name.

Click the Create button.

You will be taken back to the Droplets section where you will be shown the progress of the droplet creation process.

Logging in to your Droplet

Once your droplet is up and running, you can log in to it via SSH.

Secure Socket Shell, also called Secure Shell is just a fancy name for logging into your server's terminal.

It allows server administrators to execute commands on target machines remotely.

Execute the following command to create a remote terminal session to your droplet.

$ ssh -i <path-to-your-key-file> [email protected]<your-droplet-ip>

You can recall the path of the key file from the Creating an SSH Key section. Generally, it is the path of your public key file without the .pub extension.

You droplet IP address is visible next to your droplet in the Droplets section.

You will be prompted with a welcome message, followed by a blinking cursor ready to accept your commands.

Let us execute a few commands to make sure everything was setup correctly.

# view Node version
[email protected]:~# node -v
v6.9.2
# view NPM version
[email protected]:~# npm -v
3.10.9

Node and NPM seems to be working correctly.

Our droplet has a sample application that we can run to test things out.

# sample application is located at /opt/mean
[email protected]:~# cd /opt/mean
# start sample application
[email protected]:~# npm start

> [email protected] start /opt/mean
> gulp

[11:59:47] Using gulpfile /opt/mean/gulpfile.js
[11:59:47] Starting 'default'...
[11:59:47] Starting 'env:dev'...
.
.
.
MEAN.JS - Development Environment

Environment: development
Server: http://0.0.0.0:3000
Database: mongodb://localhost/mean-dev
App version: 0.5.0
MEAN.JS version: 0.5.0

Notice that our application runs on port 3000.

Point your browser to http://<your-droplet-ip>:3000 and you will see the sample application's welcome page.

The sample application is pretty functional with a signin and signup page.

Go ahead and have a play!


MEAN.JS

MEAN is an acronym for MongoDB, ExpressJS, AngularJS, and Node.

They are separate modules that need to be set up if you wish to create an application that runs on this stack.

Just like Digital Ocean's one-click MEAN stack installation installs the necessary software to host a MEAN stack application. Similarly, MEAN.JS sets up all the boilerplate code you need to get started with a MEAN stack application.

If you are interested in the concept, MEAN.JS's Github page has all the details you need to get started.


Hosting Node Applications

It's nice to be alive in a time when mostly all major web programming languages provide a built-in server that can be used to quickly start your application during development and testing.

However, it is a bit risky and impractical to run your applications on the language's built in server.

A simple case in point.

Web server softwares are built to function as full fledged web servers and allow complete control to the publisher through configuration files and runtime options.

For example, what if you need to configure the maximum POST data size? It is inefficient to handle it using your application's file upload code and to let a request go through if it does not fulfill this configuration requirement.

Generally, in this case, we set up a reverse proxy to our application using a web server software.

We are going to see how we can do this using the Nginx web server.

Execute the following command to install Nginx.

[email protected]:~# sudo apt-get install nginx

Next, configure Nginx using the /etc/nginx/sites-available/default file.

server {
listen 80;

server_name &lt;your-droplet-ip&gt;;

location / {
    proxy_pass http://&lt;your-droplet-ip&gt;:3000;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
}

}

Restart Nginx service by executing the command sudo service nginx restart.

Point your browser to http://<your-droplet-ip> and you will see the sample application's welcome page again.

Specifying the IP address only without the need to add port 3000 shows Nginx was setup correctly.

In other words, web browsers forward requests to port 80 by default and since we have Nginx running on port 80 ready to forward requests to the Node application, everything works as expected.

Keeping your Application Alive

In the previous section, we saw why it is not a good idea to depend on Node's built-in server. There is one more caveat to add to the equation.

Generally, a Node server does not restart itself.

Why would you need to restart a Node server, here is a little background.

When you write applications, there is always a chance you may miss a few error scenarios to handle in your code. For example, division by zero or a 404 response by a third party API.

In such cases, when exceptions are raised, a Node application breaks down and execution is halted.

Unless you plan to watch your application logs twenty four hours a day, you need to setup an environment where your application keeps running or is restarted in case of errors.

PM2 is a Node process manager that allows you to run your applications as a service in the background which is restarted whenever it goes down.

Execute the following command in your droplet to install PM2 globally.

[email protected]:~# npm install -g pm2

Then on, simply pass your application's root file to the pm2 start command.

[email protected]:~# pm2 start app.js
Deploying your Application

The last part we are going to look at is how to deploy your application to the droplet.

The sample application I am going to deploy has been taught to build in one of the previous tutorials by Ahmed Haque.

We can securely copy our application to the droplet using the scp command.

However, that is a bit inefficient so we are going to use git.

First, fork the sample application using Github.

Next, we are going to pull a copy of the code to our droplet at /opt/mean using the repository URL.

Be sure to remove the sample application first.

# remove sample application
$ rm -rf /opt/mean

move to /opt

$ cd /opt

cloning repository at /opt/mean

$ git clone https://github.com/<your-git-username>/MeanMapAppV2.0.git "mean"

Move to the project root.

$ cd /opt/mean

Install NPM modules.

$ npm install

Finally, execute the following command to start your application using PM2.

$ pm2 start server.js

Point your browser to http://<your-droplet-ip> and you should see the application's home page.


Curtains

In this tutorial, you saw how easy it is to launch a MEAN stack application using Digital Ocean.

I encourage you to explore Digital Ocean further and dive in deep with the sharks (see what I did there?).

I hope you found this tutorial interesting and knowledgeable. Until my next piece, happy coding!

================================================================

Thanks for reading :heart: If you liked this post, share it with all of your programming buddies! Follow me on Facebook | Twitter

Learn More

☞ Learning MEAN Stack by Building Real world Application

☞ MERN Stack Front To Back: Full Stack React, Redux & Node.js

☞ Angular & NodeJS - The MEAN Stack Guide

☞ Master the MEAN Stack - Learn By Example

☞ NodeJs, MEAN Stack & MongoDB: JavaScript, AngularJS, Web Dev

☞ The Complete Node.js Developer Course (3rd Edition)

☞ NodeJS - The Complete Guide (incl. MVC, REST APIs, GraphQL)

☞ Node.js, Express & MongoDB Dev to Deployment




Mobile App Development Company India | Ecommerce Web Development Company India

Mobile App Development Company India | Ecommerce Web Development Company India

Best Mobile App Development Company India, WebClues Global is one of the leading web and mobile app development company. Our team offers complete IT solutions including Cross-Platform App Development, CMS & E-Commerce, and UI/UX Design.

We are custom eCommerce Development Company working with all types of industry verticals and providing them end-to-end solutions for their eCommerce store development.

Know more about Top E-Commerce Web Development Company

Hire PHP Developer and Web Developer for your Online Business

Hire PHP Developer and Web Developer for your Online Business

PHP is widely used open-source scripting language it helps in making dynamically easy your websites and web application. Mobiweb Technology is your best technical partner and offering you solution for any kind of website and application...

PHP is widely used open-source scripting language it helps in making dynamically easy your websites and web application. Mobiweb Technology is your best technical partner and offering you solution for any kind of website and application development. To hire PHP developer and web developer at affordable prices contact Mobiweb Technology via [email protected]

Website Development - Top Reasons Why Your Business Need A Creative Website?

Website Development - Top Reasons Why Your Business Need A Creative Website?

Currently, most business owners want a website and expand their business to increase the selling on local to international markets. We give you the top reasons why you need a website.

Most of the entrepreneurs with innovative ideas about start-up businesses are unaware of all the opportunities the internet has for their business.

A major percentage of our population uses the internet to look for local businesses, and if the business does not have a website then most of their potential consumers won’t get to know about the website only. It gets difficult for beginners who have little knowledge about web development this is where the role of web development services comes into the picture. Having a website connects our business to all the parts of the world effortlessly and opens up various new platforms which were not available previously.

The power of a web page is simply incomparable to other expensive methods of advertising like print media. In the era of smartphones, tablets and PCs where everybody is constantly exposed to the internet at every point of life, the competition is brewing like coffee.

Your web page needs to stand out and connect with the consumers, all the web development services can help you to make your website look just as you want it to.

The purpose of your webpage is not only to serve your customer base, but also to attract new customers, so our website needs to be easily accessible and attractive and some of the ways in which a web development company would help you are by.

“The page should be navigable smoothly and effortlessly, and whatever information the customer needs he/she should be able to retrieve it easily through a search box, etc.”

Professional web developers are able to provide these services.

1. Search Engine Optimization
All the websites are competing to be on top of the list of search engine result pages, but only web developers know how to work on this with the help of proper keywords, image optimization, linking, etc.

2. Appropriate Visual Content
It is the need of the hour to provide the customers with relevant visuals to get a proper idea of your business. Hence, we need professional help to balance the text and visuals for your website so that the customer doesn’t get bored beforehand and leave the webpage

3. Online Transaction
Most of the consumers these days prefer cashless and online mode of payment. It becomes more convenient from the sales point of view too to keep a track of online sales records. Web development services ensure that your website has all required mediums for a transaction and also whether all of them are up to date or not.

4. Global Recognition
Once the brand name is out there, it is very important for the website to have a proper connection with the international client base as well as local customers, the format and design of the website helps is doing so.

5. Responsiveness
Anyone could set up an informative website, but to hold the customer’s attention the website also needs to responsive and interactive, Services are provided through which feedback of the customers can be collected and problems can be addressed as well.

6. Reducing Downtime
While the page is being formatted it is unavailable to the customers, this could result in a loss of attention span from many customers. Hence, webmasters make sure that they take as little time as possible to put up new blog posts or advertisements on the website and don’t make the customers wait.

7. Profit-Making Schemes
One of the ways in which our website might earn small profits is by allowing ads. However, it is a tedious job to also block inappropriate content. Web developers allow and block ads accordingly so that the ads don’t take up much space, but also harmlessly add up to the profits.

Read also: PHP Trends: 5 Best Framework For Web Development

The article gives an idea about how it is necessary for a business in the current world to have a webpage, but adding up all the features which would attract customers is not as easy as it looks and which is why web development services to help us.

Source by