GoDaddy Hjälp

Set up Node.js application for production - Ubuntu

Node.js is an open-source, cross-platform JavaScript runtime environment for developing a wide variety of tools and applications. Node.js runs on most operating systems, including Linux, OSX and Windows. This guide will show you how run Node.js as a service, so your apps will automatically restart when the server is rebooted or if there is a failure.

Requirements:

Your application server will run Node.js applications managed by PM2, while your web server will provide users with access to the application via an NGINX reverse proxy to the application server.

This guide uses two Ubuntu servers. We will refer to these servers by the following names:

App: The server where we will install your Node.js application, Node.js runtime, and PM2.

Web: This is where we will install the NGINX web server, which will act as a reverse proxy to your application. Users will use the web server's public IP address or domain to get to your Node.js application.

Install Node.js

This section is completed on your App server.

Update your apt packages:

sudo apt-get update

Install git.

sudo apt-get install git

Copy the download link for Node.js from the Node.js Downloads page. Locate the 64-bit Linux Binaries (.tar.xz) download link. Right-click the link and copy it to your clipboard.

Change to your home directory.

cd ~

Type in wget and paste the download link in place of the highlighted URL below:

Note: If the version is displayed, Node.js's runtime is now installed and ready to run an application.

Create a Node.js Application

We will create a simple application that returns "Hello World" to any incoming HTTP requests. This is merely a sample application that's going to help you get Node.js set up. You can replace it with your own application, and modify the application to listen on the correct IP addresses and ports.

Create and edit your Node.js application. In our example, we will edit a sample application called hello.js.

cd ~
vim hello.js

Insert the code below into your the hello.js file. Make sure to replace the app server's Public IP address for both of highlighted APP_SERVER_PRIVATE_IP:8080 items. You may also need to replace the highlighted port, 8080, in both locations if you're using different ports, but make sure to use a non-admin port like 1024 or greater:

This Node.js application will listen on the specified port and IP addresses and return "Hello World" with a 200 HTTP success code.

Test the Application (Optional)

Test your application on your app server:

node hello.js

Note: Running Node.js in this way will block additional commands until the application is terminated by pressing CTRL+C.

Test your application by opening http://YOUR_APP_SERVER_IP:8080 in a browser window, using your own public IP.

If you receive the "Hello World" output, your application is working properly.

If you received an incorrect output, make sure your Node.js application is both running and also configured to listen on the correct IP address and port.

Note: Kill the application by pressing CTRL+C on your app server.

Set your Node.js application to use your Private IP address.

Now that we have confirmed the application is working properly, we need to switch the hello.js IPs from the app server's public IP to the private IP. Your App server's Private IP address can be found in Cloud Servers under the IP & DNS tab. Find a server's private IP address.

Navigate back to your home directory and re-open your hello.js file with vim.

cd ~
vim hello.js

Replace the app server's Public IP address for both of highlighted APP_SERVER_PRIVATE_IP:8080 items with your app server's Private IP address.

Manage Application with PM2

Your application will be added to PM2's process list, which will be displayed each time you start an application:

Here's an example output:

As you probably noticed, PM2 automatically assigns an App name that's based on the filename and PM2 id. PM2 also maintains information such as the PID of the process, memory usage and the current status.

Note: If an application that is running under PM2 crashes or is killed, it will be automatically restarted. However, additional steps are necessary to get the application to launch on system startup (boot or reboot). Fortunately, PM2 makes it easy with the startup subcommand.

The startup subcommand creates and configures a startup script that launches PM2 and its managed processes when the server boots. It's also important to specify the init system you're running. Since we are on a Ubuntu server, it's systemd.

Run Pm2 startup.

sudo pm2 startup systemd

You should see output like the following, which indicates that the pm2 service has been installed.

Other PM2 Usage (Optional)

PM2 provides quite a few subcommands that allow you to look up or manage information about your Node.js applications. If you run PM2 without any arguments, it will display a help page. The help page provides usage examples that covers PM2 usage in greater detail if needed.

If you wish to stop an application, you can use the stop command. Make sure to specify the PM2 Id or App name.

$ pm2 stop App or ID

Restart the application. Be sure to indicate the app name or ID.

pm2 restart App or ID

YList the applications currently managed by PM2:

$ pm2 list

By using the info subcommand, you can find more information about a specific application. Make sure you specify the PM2 id or App name in place of the highlighted portion:

pm2 info App or ID

You can pull up the PM2 process monitor by using the monit subcommand. This will show you metrics such as memory usage, CPU usage and application status:

sudo apt-get update
pm2 monit

Set up the Reverse Proxy Server with NGINX

Now that we have established that your Node.js application is running and listening on the Private address, we now need a way for users to access it on the web. To do this, we will set up an NGINX web server as a reverse proxy, which will allow access for our users. This section will be done on the Web server.

Update your packages:

sudo apt-get update

Install NGINX:

sudo apt-get install nginx

Open the NGINX server block configuration file:

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

Add the server's Private IP address for the APP_SERVER_PRIVATE_IP portion. Additionally, change the port (8080) if you set up your application is set to listen on a different port. Locate the line where / is defined starting with the default server block. It should look something like this:

Note: Using this configuration allows the Web Server to respond to requests at its root. As long as our server is available at example.com, accessing http://example.com/ through a web browser will send the requests to the application server's Public IP address on port 8080. Once this request is received, Node.js will reply to the request.

Restart NGINX on your Web server:

sudo service nginx restart

As long as your application and NGINX configurations are correct, and your Node.js application is running, you should be able to access your application through the reverse proxy of the web server. You can test it by accessing your web server's URL. This will either be its public IP, or a domain if a domain has been setup.

If you see the Hello World message in your browser, your installation and configuration were successful.

Conclusion

Your Node.js application is now running behind an NGINX reverse proxy on your Ubuntu servers.
By using this reverse proxy method, you have the flexibility to provide users with access to other applications or even static web content.