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
- Install Nginx: Follow the instructions in our article on “How to Install Nginx on Ubuntu 16.04” to install Nginx on your server.
- Install Let’s Encrypt Certificate: Use Nginx configuration to install a Let’s Encrypt certificate, providing HTTPS access to your server.
- Configure Nginx: Complete the preparation by setting up a server at https://example.com/ with Nginx’s default placeholder page.
Installing Node.js
- Install NodeSource PPA: Use the following command to install the latest LTS version of Node.js from the NodeSource package archive:
This will add the NodeSource PPA to your configuration and update your local package cache.cd ~ curl -sL https://deb.nodesource.com/setup_6.x -o nodesource_setup.sh sudo nano nodesource_setup.sh sudo bash nodesource_setup.sh - Install Node.js: Run the following command to install Node.js:
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 nodejssudo apt-get install build-essential
Creating a Node.js Application
- 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.
- Create and Open the Node.js Application: Create a new file called
hello.jsand open it for editing:cd ~ nano hello.js - Insert the Hello World Code: Insert the following code into the
hello.jsfile:#!/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/'); - Save and Exit: Save and exit the
hello.jsfile.
Testing the Application
- Mark the Hello World Application as Executable: Use the following command to mark the
hello.jsfile as executable:chmod +x ./hello.js - Run the Hello World Application: Run the
hello.jsapplication:./hello.js - Test the Application: Open another terminal session on your server and use the
curlcommand to test the application:
If you see the following output, the application works and is listening on the correct address and port:curl http://localhost:8080Hello World
Installing PM2
- Install PM2: Use the following command to install PM2, a process manager for Node.js applications:
Thesudo npm install -g pm2-goption tells npm to install the module globally, so it can be used system-wide.
Using PM2 to Manage the Application
- Start the Application with PM2: Use the following command to start the
hello.jsapplication with PM2:pm2 start hello.js - List the PM2 Processes: Use the following command to list the PM2 processes:
pm2 list - Restart the Application: Use the following command to restart the
hello.jsapplication:pm2 restart hello.js - Stop the Application: Use the following command to stop the
hello.jsapplication:pm2 stop hello.js
Setting Up a Reverse Proxy with Nginx
- Configure Nginx: Open the Nginx configuration file for editing:
sudo nano /etc/nginx/sites-available/default - 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; } - Save and Exit: Save and exit the Nginx configuration file.
- Test the Nginx Configuration: Use the following command to test the Nginx configuration:
sudo nginx -t - 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!