Introduction

Getting a site running on a MEAN (MongoDB, Node.js, Express, and AngularJS) server can be a challenge for beginners. In this article we'll guide you through all the steps necessary for setting your MEAN site properly.

This article is written for the latest Ubuntu 16.04 but the steps are similar for other Linux distributions too. Once you grasp the main concept, you should be able to set a MEAN site on another Ubuntu version or even a different Linux distribution.

Prerequisites

Before following this tutorial, you will need an Ubuntu 16.04 (x64) server with at least 1 GB of RAM because MEAN can be quite resource demanding at times. It's recommended also to have some (more than 2GB) swap enabled in case you doubt your RAM will be sufficient.

Except otherwise noted, all of the commands that require root privileges in this tutorial should be run as a non-root user with sudo privileges. Check this article about more info on sudo users in Ubuntu.

Step One - Installing MongoDB

For installing all the necessary software in this article, including MongoDB, we'll use the official Ubuntu repository. This is preferable because it ensures easy setup, maintenance and compatibility of well-tested software. The only drawback is that the available versions may not always be the latest ones.

Before installing the MEAN stack there are some prerequisites which we have to install first. Let's start with MongoDB which is the database used in MEAN. To install it run the apt-get command like this:

sudo apt-get install mongodb

The above command will give you a fresh MongoDB installation with no data in it.

Step Two - Installing NodeJS and the required packages for MEAN

Now, let's install the next component of the MEAN stack - NodeJS and its package manager - npm. We'll do it with the command:

sudo apt-get install nodejs npm

The latter command will also install any additional requirements and dependencies that are needed for NodeJS. There is one small compatibility issue because some applications, such as npm have the nodejs binary hardcoded as /usr/bin/node, while it's actually in /usr/bin/nodejs. To resolve this, the easiest way is to create a symbolic link like this:

sudo ln -s /usr/bin/nodejs /usr/bin/node

Once we have the npm tool we should install the additional packages gulp (build tool) and bower (frontend packages manager). We'll use the npm command like this:

sudo npm install -g gulp bower 

The npm tool will install the above packages globally and they will be available in the directory /usr/local/lib/node_modules/. To confirm their proper installation, go to this directory with the command:

cd /usr/local/lib/node_modules/

Then run the npm command with the list argument in order to get the available packages and their versions:

npm list

In a three view, you'll get a list of all the installed packages and their sub-packages like this:

├── [email protected]
├─┬ [email protected]
...

You should see in the above output bower, gulp, and their corresponding versions.

Now you are ready to continue with installing MEAN.

Step Three - Installing MEAN's command line interface

The most essential part of MEAN is its command line interface. It allows you to manage applications, including to isntall them. To download and install this tool, we'll use again npm like this:

sudo npm install -g mean-cli

Step Four - Creating a new MEAN application

For simplicity, we'll create a new example MEAN application in our home directory and call it example. To get the files for the default MEAN web application, we'll use MEAN's cli like this:

mean init example

This command will start an installation process, during which you will have to answer a couple of questions:

  • What would you name your mean app? - there answer example or any other name of your choice.
  • Do you want to set up an admin user? - leave the default no (n) answer if you are not certain.

This command will create a new directory called example in your home directory. Right after that we can go inside the example directory to start our application with the command:

cd example
gulp

You will see a long list of debug output. The most important message is:

...
Standard linter results: ✔ All OK!
...

So far so good. Your new MEAN application has started successfully and you can access it via a browser at your server IP on port 3000 (http://server_ip:3000).

Step Five - Installing nginx in front of MEAN

It is recommended to install Nginx in front of MEAN because it will help you protect better your website, on one hand. On the other hand, it will allow your visitors to access the web service at the standard http and https ports in contrast to the default MEAN 3000 port.

To get started with nginx, first follow the article on how to install nginx on Ubuntu 16.04.

Once you have an nginx server up and running, you have to configure it to proxy the connections to the backend MEAN server. To do this, open the default server block with the command:

sudo nano /etc/nginx/sites-enabled/default

Then, go to the location stanza, delete everything and as a minimal configuration add only the line proxy_pass http://localhost:3000;. At the end location configuration should look like this

[label  /etc/nginx/sites-enabled/default]
...
        location / {
                proxy_pass       http://localhost:3000;
        }
...

Save the file and restart nginx for the change to take effect with the command:

sudo service nginx restart

Now you can open your MEAN site at the default http port with a browser at the URL http://server_ip, which is much better than before. For additional security, you can also install SSL following the article on installing Let's encrypt.

Step Six - Starting MEAN automatically

Starting gulp manually from the console is very useful for debugging and development. Though, it has drawbacks when it comes to moving your site into production mode. For one, the MEAN service will stop once you close your terminal session.

To ensure that your MEAN site is available even after closing the terminal, instead of gulp you can run:

nohup node server &

This command will start the production-ready nodejs server to run your MEAN site. Just make sure that, similarly to when running gulp, you are inside your MEAN site directory.

Probably, you would also like to have this script started automatically with the server. For the beginning you can just add the start command to the /etc/rc.local file and the MEAN service will be automatically started during boot time. First, though, you will have to make sure that /etc/rc.local is executed during the server startup. In Ubuntu 16.04 it requires to be additionally enabled with the command:

sudo systemctl enable rc-local.service

Then open the file /etc/rc.local for editing:

sudo nano /etc/rc.local

There add the start command before the exit line like this:

[label  /etc/rc.local]
...
sudo -H -u YOUR_USER bash -c 'cd /home/YOUR_USER/example/ && nohup node server &'
exit 0

Please make sure to replace YOUR_USER with your user in the above command. What this command does is to switch to your user, then to change to your MEAN site directory, and lastly to start the application server.

After completing the this step you don't have to worry about starting your MEAN server manually after reboots.

Conclusion

As we have seen in this article, launching a MEAN site takes quite a few steps and could be challenging. The default installation does not always work as expected but with the help of this article you should be able to overcome most obstacles, and get your MEAN site online running smoothly.