Deploying Node JS or Meteor app on cPanel, WHM based Dedicated Server or VPS

Deploy Meteor or Node JS app on cPanel/WHM

If you are having a dedicated server or VPS, chances are you are not using it to its full capacity and has room for other applications. But, installing non PHP based applications is not easy and straight forward. In this post I will cover how you can install a nodejs application on a cPanel based dedicated server or VPS servers usually available from GoDaddy, Hostgator, BlueHost Etc.. . I will be using our Meteor app NYBR for this tutorial.

Installing Nginx

We will use Nginx as a reverse proxy for apache and our nodejs application.

There were three ways to install Nginx and integrate it with cPanel.

  1. Engintron, Free. This is the one we will be using. Very easy to use, no nonsense. Integrates well with cPanel.
  2. CPNginx, Paid. Did not consider or explore this paid option.
  3. NginxCP, Free. This the one I tried first, it just did not work for me. After trying Engintron, I would never use this.

Before moving any further, lets make sure we have a clean slate and clean any earlier installations on Nginx.

How to uninstall Nginx in CentOS 7 / RHEL 7

1. Stop Nginx service and remove Nginx auto start script

sudo systemctl stop nginx.service
sudo systemctl disable nginx.service

2. Remove Nginx user and it related directory

sudo userdel -r nginx

3. Delete and related Nginx installation directory

sudo rm -rf /etc/nginx
sudo rm -rf /var/log/nginx
sudo rm -rf /var/cache/nginx/

4. Remove the created nginx.service script under systemd

sudo rm -rf /usr/lib/systemd/system/nginx.service

How to install Nginx with Engintron

cd / 
rm -f engintron.sh 
wget --no-check-certificate https://raw.githubusercontent.com/engintron/engintron/master/engintron.sh 
bash engintron.sh install

Thats it. Your are done installing Engintron.

You can access Engintron admin page on your WHM under Plugins in side menu.

Engintron admin screen on cPanel

Installing MongoDB on cPanel Centos 7/ RHEL 7

Since we need MongoDB for our Meter app, we will install it now.

1. Add MongoDB repo

sudo vi /etc/yum.repos.d/mongodb-org-3.4.repo

and enter

[mongodb-org-3.4]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.4/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.4.asc

2. Install MongoDB

sudo yum install -y mongodb-org
 3. Configure SELinux
semanage port -a -t mongod_port_t -p tcp 27017

More Info

4. Start and Enable MongoDB

sudo systemctl start mongod
sudo systemctl enable mongod

Install NodeJS

sudo yum install -y epel-release curl
$ curl --fail -sSL -o setup-nodejs https://rpm.nodesource.com/setup_4.x
$ sudo bash setup-nodejs
$ sudo yum install -y nodejs gcc-c++ make

Installing Passenger

Enable EPEL Repo

sudo yum install -y epel-release yum-utils
sudo yum-config-manager --enable epel
sudo yum clean all && sudo yum update -y

Make sure everything is set properly

sudo yum update -y 
date # if the output of date is wrong, please follow these instructions to install ntp
sudo yum install -y ntp 
sudo chkconfig ntpd on 
sudo ntpdate pool.ntp.org 
sudo service ntpd start

Installing Passenger Packages

sudo yum install -y pygpgme curl
# Add our el7 YUM repository
sudo curl --fail -sSLo /etc/yum.repos.d/passenger.repo https://oss-binaries.phusionpassenger.com/yum/definitions/el-passenger.repo

# Install Passenger Nginx
sudo yum install -y nginx passenger || sudo yum-config-manager --enable cr && sudo yum install -y nginx passenger

Install Passenger Nginx Module

passenger_root /some_dir/locations.ini; # Replace with output of passenger-config --root
passenger_ruby /usr/bin/ruby;
passenger_instance_registry_dir /var/run/passenger-instreg;
passenger_log_file /var/log/myapp-passenger.log;

Validate the installation 

sudo /usr/bin/passenger-config validate-install

Installing the MeteorJS Application on cPanel Hosting

Build Meteor App

Change into your meteor directory on your local system and run

meteor build ../build-ngbr/ --verbose --server-only --server=https://my-app-server.com  --mobile-settings=settings.json

Upload myapp.tar.gz to remote host. You can use scp

scp /path/to/myapp.tar.gz root@remotehost.com:/root

Now back to the remote system…

It’s always a good idea to run each app under it’s own user name. Since we are using cPanel, lets create a separate account on WHM. Let it be, myappuser.

Extract the application we uploaded before.

sudo mkdir -p /var/www/myapp
cd /var/www/myapp
tar xvzf /root/myapp.tar.gz
chown -R myappuser: .
rm /root/myapp.tar.gz

Install application dependencies

The application dependencies must be install as the app user

sudo -u myappuser -H bash -l
cd /var/www/myapp/bundle/programs/server
npm install --production

Exit from application user

exit

Create Nginx configuration file

sudo nano /etc/nginx/conf.d/myapp.conf

Add following configuration to the file.

server {

    listen 80;
    server_name my-app-server.com;
    
    # If you are using ssl, uncomment below three lines and change listen above to 443
    # ssl on;
    # ssl_certificate /etc/letsencrypt/live/my-app-server.com/fullchain.pem;
    # ssl_certificate_key /etc/letsencrypt/live/my-app-server.com/privkey.pem;

    # Tell Nginx and Passenger where your app's 'public' directory is
    root /var/www/myapp/bundle;

    # Turn on Passenger
    passenger_enabled on;
    # Tell Passenger that your app is a Meteor app
    passenger_app_type node;
    #Even though the root was /var/www/myapp/bundle it would look a directory below
    # at /var/www/myapp/, so had to add bundle before main.js
    passenger_startup_file bundle/main.js; 

    # Tell your app where MongoDB is
    passenger_env_var MONGO_URL mongodb://localhost:27017/myapp;
    # Tell your app what its root URL is
    passenger_env_var ROOT_URL https://yourappurl.com;
passenger_env_var METEOR_SETIINGS '{"facebook":{"AppID":"156714******","AppSecret":"*********"},"public":{"facebook":{"permissions":["public_profile","email"],"profileFields":["id","name","gender","location","email","first_name","last_name","link","username","locale","age_range"]}}}';
}

Make sure there are no errors in Nginx configuration and restart it

nginx -t
systemctl restart Nginx

Making Sure Everything is Working

Open your browser and access your app. It should work. If you face any issues, check the error log at

/var/log/myapp-passenger.log;

Debugging

  1. Make sure your mongoldb is up and running. If it has a password, be sure to set it.
  2. Check if your Nginx root and passenger_startup_file are correct.
  3. Be sure to set any environment variables your application need in the /etc/nginx/conf.d/myapp.conf file. We have set METEOR_SETIINGS as an example.

Leave a Reply

Your email address will not be published. Required fields are marked *