How do I setup the mean stack for production

September 15, 2015 1.7k views
MEAN Nginx Ubuntu

Hi guys, I'm new to this.
It's a two part question.

Question 1:
I currently have used the terminal to ssh into the droplet containing the MEAN stack.
I went into /opt/mean/ and ran "grunt".
However once I close the terminal on my machine the website is no longer up.
How do I leave running even when I exit the server?

Question 2:
I have purchased a namespace and sorted out the DNS etc.
However when I go to my domain it is on port 80, the MEAN stack is on port 3000.
How should I fix this, should I change the MEAN port to 3000 or should I use nginx to reverse proxy.

Thank you for all help :)

1 Answer


While this was asked a while ago, I believe the following information will be able to help future visitors who might be experiencing the same issue:

In order to bind a program to port 80 or any port under 1024, it needs to have root privileges. Running an app as root is most of the time a bad idea, so, what is usually done is running a reverse proxy such as nginx or HAProxy on port 80 that forwards all incoming requests to the MEAN app on port 3000.

First, install nginx:

sudo apt-get update
sudo apt-get install nginx

Then, replace the default server block in /etc/nginx/sites-available/default with the following:

server {
    listen 80;


    location / {
        proxy_pass http://localhost: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;

Finally, restart nginx:

sudo service nginx restart

nginx should now be listening on port 80 and forwarding all requests to the MEAN app that is running on port 3000.

If you're running the app in production, you will need to set it up so that it automatically starts on boot and restarts when it crashes. Take a look at the Install PM2 and Manage Application with PM2 sections in the following tutorial:

How To Set Up a Node.js Application for Production on Ubuntu 14.04

One other option, which is true only if you're running Ubuntu 15.04 or later, or different OSes that use systemd as the init service, would be creating a systemd service as described in this tutorial.

by Felix Saparelli
When deploying a web application to a Droplet, it might be tempting to simply use the same kind of setup as is used in development, i.e. starting the server by running "ruby app.rb" or "node server.js" in a terminal. This is simple and easy, while providing visible logs.This is dangerous: what happens if the server crashes and no one is around to restart it? This tutorial uses a simple Node.js application, but is applicable to most, if not all, others as well (be they Ruby, Python, etc.)
Have another answer? Share your knowledge.