Setting Up a Production Node.js Environment on Ubuntu 16.04

Setting Up a Production Node.js Environment on Ubuntu 16.04

Introduction

Node.js is an open-source JavaScript runtime environment for server-side and web applications. It can run on various platforms, including Linux, OS X, FreeBSD, and Windows. In this article, we will cover how to set up a production-ready Node.js environment on a single server running Ubuntu 16.04. The server will run and provide secure access to applications managed by Nginx reverse proxy, using free certificates provided by Let’s Encrypt.

Prerequisites

To complete this tutorial, you need to have already set up a non-root account on an Ubuntu server with a firewall turned on. Students without a server can purchase one, but we recommend using a free Tencent Cloud Developer Lab to test, install, and learn to buy servers.

Assumptions

This tutorial assumes that you have the following:

  • An Ubuntu 16.04 server with non-root user privileges.
  • A host name that points to the server’s public IP or domain name (e.g., example.com).
  • Nginx installed on the server.
  • A Let’s Encrypt certificate installed on the server.

Setting Up the Server

  1. Install Nginx: Follow the instructions in our article on “How to Install Nginx on Ubuntu 16.04” to install Nginx on your server.
  2. Install Let’s Encrypt Certificate: Use Nginx configuration to install a Let’s Encrypt certificate, providing HTTPS access to your server.
  3. Configure Nginx: Complete the preparation by setting up a server at https://example.com/ with Nginx’s default placeholder page.

Installing Node.js

  1. Install NodeSource PPA: Use the following command to install the latest LTS version of Node.js from the NodeSource package archive:
    cd ~
    curl -sL https://deb.nodesource.com/setup_6.x -o nodesource_setup.sh
    sudo nano nodesource_setup.sh
    sudo bash nodesource_setup.sh
    
    This will add the NodeSource PPA to your configuration and update your local package cache.
  2. Install Node.js: Run the following command to install Node.js:
    sudo apt-get install nodejs
    
    The Node.js package contains binaries and npm, so you don’t need to install them separately. However, you need to install the build-essential package to make certain npm packages work properly:
    sudo apt-get install build-essential
    

Creating a Node.js Application

  1. Write a Hello World Application: Create a Node.js application that returns “Hello World” when any HTTP request is made. This is a simple application that can help you set up Node.js, and you can replace it with your own application by modifying the application to listen on the correct IP address and port.
  2. Create and Open the Node.js Application: Create a new file called hello.js and open it for editing:
    cd ~
    nano hello.js
    
  3. Insert the Hello World Code: Insert the following code into the hello.js file:
    #!/usr/bin/env nodejs
    var http = require('http');
    http.createServer(function (req, res) {
      res.writeHead(200, {'Content-Type': 'text/plain'});
      res.end('Hello World\n');
    }).listen(8080, 'localhost');
    console.log('Server running at http://localhost:8080/');
    
  4. Save and Exit: Save and exit the hello.js file.

Testing the Application

  1. Mark the Hello World Application as Executable: Use the following command to mark the hello.js file as executable:
    chmod +x ./hello.js
    
  2. Run the Hello World Application: Run the hello.js application:
    ./hello.js
    
  3. Test the Application: Open another terminal session on your server and use the curl command to test the application:
    curl http://localhost:8080
    
    If you see the following output, the application works and is listening on the correct address and port:
    Hello World
    

Installing PM2

  1. Install PM2: Use the following command to install PM2, a process manager for Node.js applications:
    sudo npm install -g pm2
    
    The -g option tells npm to install the module globally, so it can be used system-wide.

Using PM2 to Manage the Application

  1. Start the Application with PM2: Use the following command to start the hello.js application with PM2:
    pm2 start hello.js
    
  2. List the PM2 Processes: Use the following command to list the PM2 processes:
    pm2 list
    
  3. Restart the Application: Use the following command to restart the hello.js application:
    pm2 restart hello.js
    
  4. Stop the Application: Use the following command to stop the hello.js application:
    pm2 stop hello.js
    

Setting Up a Reverse Proxy with Nginx

  1. Configure Nginx: Open the Nginx configuration file for editing:
    sudo nano /etc/nginx/sites-available/default
    
  2. Replace the Existing Location Block: Replace the existing location / block with the following configuration:
    location / {
      proxy_pass http://localhost: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;
    }
    
  3. Save and Exit: Save and exit the Nginx configuration file.
  4. Test the Nginx Configuration: Use the following command to test the Nginx configuration:
    sudo nginx -t
    
  5. Restart Nginx: Use the following command to restart Nginx:
    sudo systemctl restart nginx
    

Conclusion

Congratulations! You have now set up a production-ready Node.js environment on your Ubuntu 16.04 server, using Nginx as a reverse proxy and PM2 to manage the application. This reverse proxy setup is flexible enough to allow your users to access other applications that you want to share or static Web content. Good luck with your Node.js development!