Deploying Nodejs - newgeekorder/TechWiki GitHub Wiki

Back Home

Application Monitoring

To ensure the node server keeps running one options is to use Monit to restart it if it goes down.

sudo apt-get install monit

Monit takes an 'rc' file in /etc/monit/monitrc a sample file is below:

#!monit
set logfile /var/log/monit.log
 
check process nodejs with pidfile "/var/www/NODEAPP/YOUR_APP.pid"
    start program = "/etc/init.d/node_app start"
    stop program  = "/etc/init.d/node_app stop"
    if failed port 8080 protocol HTTP
        request /
        with timeout 10 seconds
        then restart

To get monit started, you'll also need to change /etc/default/monit so that it contains the line 'startup=1'.

PM2

Another useful option is PM2, which is a process manager for Node.js applications. PM2 provides an easy way to manage and daemonize applications (run them as a service).

sudo npm install pm2 -g

Then to start the application in the background:

pm2 start hello.js

Security

One option but probably not the best way is to route data via the IPTable configuration

As we don't want to run our node proxy as root, we can use an IPTables rule which will send all of the port 80 traffic over to our proxy. To do so, enter this into your terminal:

iptables -t nat -I PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 800

Running with Apache Web Server

In apache2 modify the /etc/sysconfig/apache2 installing

  • proxy

  • proxy_http

  • restart the apache2 server with sudo /etc/init.d/apache2 reload

  • You can check which modules are loaded with sudo /usr/sbin/apache2ctl -M

  • then one can modify and add to the bottom of sudo vim default-server.conf

something like the following

<VirtualHost 109.74.199.47:80>
    ServerAdmin [email protected]
    ServerName thatextramile.be
    ServerAlias www.thatextramile.be
 
    ProxyRequests off
 
    <Proxy *>
        Order deny,allow
        Allow from all
    </Proxy>
 
    <Location />
        ProxyPass http://localhost:3000/
        ProxyPassReverse http://localhost:3000/
    </Location>
</VirtualHost>

Set Up Reverse Proxy Server with Nginx

Installing Nginx (use the appropriate package manager)

sudo apt-get install nginx

Now open the default server block configuration file for editing:

sudo vi /etc/nginx/sites-available/default

Delete everything in the file and insert the following configuration. Be sure to substitute your own domain name for the server_name directive (or IP address if you don't have a domain set up), and the app server private IP address for the APP_PRIVATE_IP_ADDRESS. Additionally, change the port (8080) if your application is set to listen on a different port:

server {
    listen 80;

    server_name example.com;

    location / {
        proxy_pass http://APP_PRIVATE_IP_ADDRESS:8080;
        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;
    }
}

This configures the web server to respond to requests at its root. Assuming our server is available at example.com, accessing http://example.com/ via a web browser would send the request to the application server's private IP address on port 8080, which would be received and replied to by the Node.js application.

You can add additional location blocks to the same server block to provide access to other applications on the same web server. For example, if you were also running another Node.js application on the app server on port 8081, you could add this location block to allow access to it via http://example.com/app2:

    location /app2 {
        proxy_pass http://APP_PRIVATE_IP_ADDRESS:8081;
        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;
    }

Once you are done adding the location blocks for your applications, save and exit.

On the web server, restart Nginx:

sudo service nginx restart

Assuming that your Node.js application is running, and your application and Nginx configurations are correct, you should be able to access your application via the reverse proxy of the web server. Try it out by accessing your web server's URL (its public IP address or domain name).

⚠️ **GitHub.com Fallback** ⚠️