---

How to install Nginx, MongoDB, NodeJS and host MeteorJS applications on an Ubuntu VPS

In this tutorial we are going to show you the steps required to bootstrap an environment to host your Meteor JS applications. More specifically, the tutorial explains how to install Nginx, MongoDB, NodeJS and host MeteorJS applications on an Ubuntu VPS. We are using one of our Ubuntu 16.04 managed virtual servers, though it should work on any other Ubuntu or Debian based system.

Let’s say a word or two for each of the components being used, shall we?

What is Nginx?
Nginx is the world’s most popular open source web server and load balancer for high-traffic sites, powering millions if not billions of web applications world-wide.

What is MongoDB?
MongoDB is an open source, document-oriented database designed with both scalability and developer agility in mind. Instead of storing your data in tables and rows as you would with a relational database, in MongoDB you store JSON-like documents with dynamic schemas.

What is NodeJS?
Node.js is an open-source, cross-platform runtime environment for developing server-side Web applications. It is much faster than Ruby, Python, PHP or Perl and can handle thousands of concurrent connections with minimal overhead on a single process.

What is MeteorJS?
Meteor, or MeteorJS, is a free and open-source JavaScript web framework written using Node.js. Meteor allows for rapid prototyping and web-development and can also produce cross-platform (Web, Android, iOS) code.

0. SSH TO YOUR UBUNTU LINUX VPS
First thing to do is to login to your virtual server via SSH and optionally fire up a screen/tmux session. For example:

ssh YOUR_VPS_IP -p YOUR_VPS_SSH_PORT
screen -U -S meteorjs-screen
1. UPDATE YOUR UBUNTU LINUX VPS
Next, it’s recommended to fully update your ubuntu virtual server using the distribution package manager apt:

apt-get update
apt-get upgrade
2. INSTALL REQUIRED PACKAGES
Install some required packages using Ubuntu’s package manager apt:

apt-get install build-essential curl wget dialog rsync
3. INSTALL MONGO DB ON UBUNTU LINUX VPS
Next, proceed with installing Mongo DB on your Ubuntu VPS using apt. You do this as root user:

apt-key adv –keyserver hkp://keyserver.ubuntu.com:80 –recv EA312927
echo “deb http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.2 multiverse” | sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list
apt-get update
apt-get install -y –allow-unauthenticated mongodb-org
In order to manage the Mongo DB service, you need to create a systemd unit configuration file for Mongo DB in /etc/systemd/system/mongodb.service:

vim /etc/systemd/system/mongodb.service
and paste the following:

[Unit]
Description=High-performance, schema-free document-oriented database
After=network.target

[Service]
User=mongodb
ExecStart=/usr/bin/mongod –quiet –config /etc/mongod.conf

[Install]
WantedBy=multi-user.target
Now, you can manage your Mongo DB service using systemctl, for example:

systemctl daemon-reload
systemctl start mongodb
systemctl status mongodb
systemctl enable mongodb
4. CREATE UNPRIVILEGED SYSTEM USER
Proceed with creating an unprivileged system user for running your NodeJS / MeteorJS applications. This is also recommended for security reasons. The home directory path would be set to /opt/meteor . Run these commands with root user.

useradd -d /opt/meteor -m meteor
usermod -L meteor
5. INSTALL NODEJS AND NPM ON UBUNTU VPS USING NVM
Ok, so far so good. Now, switch to the newly created meteor account and install NodeJS and NPM on the Ubuntu Linux Virtual Server using NVM. Keep in mind though to get the latest install script from https://github.com/creationix/nvm

su – meteor
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.31.1/install.sh | bash
once the installation is completed, either logout from your current session and re-login as meteor user or simply source your .bashrc to export the needed variables/paths etc..

. ~/.bashrc
once the .bashrc is sourced, you can verify your NVM installation by running the following command:

command -v nvm
next, list the available NodeJS versions using:

nvm ls-remote
and proceed with the installation of your desired NodeJS version using the following set of commands:

nvm install 6.2.2
nvm use 6.2.2
nvm alias default 6.2.2
nvm ls default
6. INSTALL METEOR JS ON UBUNTU LINUX VPS
It’s time to install Meteor JS on your Ubuntu Linux VPS. Switch to the unprivileged meteor user if you’re not already switched by running:

su – meteor
and proceed with the installation of Meteor JS using:

curl https://install.meteor.com/ | sh
Once the installation is completed, switch back to the root account and copy/link the launch-meteor script to /usr/bin, for example:

su – root
cp -v “/opt/meteor/.meteor/packages/meteor-tool/1.3.4_1/mt-os.linux.x86_64/scripts/admin/launch-meteor” /usr/bin/meteor
note that the path may change so be sure to tune the path
now, switch back to your meteor account and verify your meteor installation by executing:

su – meteor
meteor –version
7. BUILD YOUR METEOR APPLICATION
Ok, so it’s time to build your Meteor JS application on your development machine (your laptop, desktop, workstation etc) using the following command:

cd /path/to/your/meteor/application/dir
meteor build .
Once the application is built, transfer the archive to your linux vps using your preferred method (rsync, ssh, scp, ftp etc.). For example, a simply rsync would look like this:

rsync -Wav -e ssh my_meteor_app.tar.gz root@YOUR_LINUX_VPS_IP:/home/
8. DEPLOY YOUR METEOR APPLICATION
Now, create your deployment meteor directory using your root account

mkdir -p /opt/meteor/webapps/my_meteor_app
Next, extract the transferred meteor archive to the deployment directory using:

tar zxf /home/my_meteor_app.tar.gz -C /opt/meteor/webapps/my_meteor_app
and set the correct ownership by executing:

chown meteor: -R /opt/meteor/webapps/my_meteor_app
switch to your unprivileged meteor user account and navigate to bundle/programs/server directory:

su – meteor
cd /opt/meteor/webapps/my_meteor_app/bundle/programs/server
once there, execute the following npm command in order to install the required NodeJS modules:

npm install
ok, now we are going to install the pm2 module which is an advanced Node.js process manager. Install it globally using:

npm install -g pm2
now go back to the bundle directory by running:

cd /opt/meteor/webapps/my_meteor_app/bundle
and create the following pm2 configuration file i.e pm2-config.json

vim pm2-config.json

{
“apps”: [
{
“name”: “MyMeteorApp”,
“script”: “./bundle/main.js”,
“log_date_format”: “YYYY-MM-DD”,
“exec_mode”: “fork_mode”,
“env”: {
“PORT”: 3000,
“MONGO_URL”: “mongodb://127.0.0.1:27017/my_meteor_app”,
“ROOT_URL”: “https://mymeteor.app/”,
“BIND_IP”: “127.0.0.1”
}
}
]
}
with the pm2 configuration in place, start the Meteor application by executing:

cd /opt/meteor/webapps/my_meteor_app
pm2 start bundle/pm2-config.json -i max –watch
if everything is OK, add pm2 to your system’s startup

pm2 startup
and save your current pm2 process using:

pm2 save
you can now check pm2 status and logs using the following commands:

pm2 list
pm2 logs
9. INSTALL NGINX ON UBUNTU VPS
The final step is to install and configure Nginx in your Ubuntu VPS. You can install Nginx via apt as in:

apt-get install nginx
Once the installation is finished, create the following Nginx server block/virtual host:

vim /etc/nginx/sites-available/my_meteor.app.conf
and paste the contents below. Make sure to tune the configuration according to your needs:

server_tokens off;
set_real_ip_from ;
set_real_ip_from 127.0.0.1;

map $http_upgrade $connection_upgrade {
default upgrade;
” close;
}

upstream my_meteor_app {
server 127.0.0.1:3000;
}

server {
listen 80;
server_name my_meteor.app;
return 301 https://my_meteor.app$request_uri; # enforce https
}

server {
listen 0.0.0.0:443 ssl;
server_name my_meteor.app;

access_log /var/log/nginx/my_meteor.app.log;
error_log /var/log/nginx/my_meteor.app.error.log error;

# ssl_stapling on;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 5m;

ssl_prefer_server_ciphers On;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:ECDH+3DES:DH+3DES:RSA+AESGCM:RSA+AES:RSA+3DES:!aNULL:!MD5:!DSS;

ssl_certificate /etc/nginx/ssl/my_meteor.app.crt;
ssl_certificate_key /etc/nginx/ssl/my_meteor.app.key;

location / {
proxy_pass http://my_meteor_app;
# proxy_http_version 1.1;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
proxy_set_header Upgrade $http_upgrade; # allow websockets
proxy_set_header Connection $connection_upgrade;
proxy_redirect off;
proxy_buffering off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;

# this setting allows the browser to cache the application in a way compatible with Meteor
# on every applicaiton update the name of CSS and JS file is different, so they can be cache infinitely (here: 30 days)
# the root path (/) MUST NOT be cached
if ($uri != ‘/’) {
expires 30d;
}
}
location ~ /.well-known {
allow all;
}
}
with all that in place, enable the newly created nginx server block using the following commands:

cd /etc/nginx/sites-enabled
ln -s /etc/nginx/sites-available/my_meteor.app.conf
and test your Nginx configuration by running:

nginx -t

if everything is OK, restart your Nginx server and add it to your virtual server startup using systemctl:

systemctl restart nginx
systemctl enable nginx

That’s it. Happy Coding!

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends, & analysis