If you’re interested in hosting Node.js apps, we’ve got good news for you. Plesk Onyx comes with a new Node.js extension that allows you to manage apps easily. In this article, we’ll describe in detail how to work with Node.js apps in the context of Plesk Onyx.
Node.js on Plesk Onyx
PrerequisitesThe first thing you need to do is install Plesk Onyx, the latest version of Plesk. I chose Debian 8 as the server OS for this article. You can use either a Linux distro or Windows – just note that there could be some minor differences if you go with a different OS.Assuming Plesk Onyx is already installed, let’s install the Node.js extension. Log in to Plesk, go to Tools & Settings, and click “Updates and Upgrades”. Next, go to “Add/Remove Components”. Find the “NodeJS support” component, select it, and continue with the installation. In a few minutes, all the necessary components will be installed and you’ll be ready to go!
Hello World AppTo make sure everything works, let’s try to host a sample Node.js app, such as a “Hello, World” app. You can download one here: https://github.com/plesk/node-hello-world. While copy/pasting the contents of the app.js file manually is an option, I suggest trying out another Plesk Onyx extension instead – namely, the Git extension.You need to create a subscription (a domain with hosting). Then, you need to create a target directory where the app will be stored. Select your subscription, go to the “Files” tab, and create a directory named “hello-world”. Next, go to the “Websites & Domains” tab and click the “Git” button. I will now teach you how to set up a local repo as a mirror of a remote one and deploy the code to the previously created directory. Set https://github.com/plesk/node-hello-world.git as the remote Git repo URL and change the target to the “hello-world” directory. The settings should look like this:The next step is to change the “Document root” for the domain. Notice that I’ll be talking about “Document root” and “Application root”: they are not one and the same. When we’re talking about web apps (e.g. Node.js based apps or similar), the “Document root” is the location where the static assets can be found. Meanwhile, the “Application root” is the root directory where the app itself is located. In most cases, the “Document root” is a directory inside the “Application root” directory (e.g. “public”). You need to change the “Document root” now. To do so, go to “Websites & Domains”, click “Hosting Settings”, change “Document root” to “hello-world/public” (without quotes), and click the “Ok” button to save the changes.The last thing before you run the app is to enable Node.js support on the domain. Go to “Websites & Domains” and click “Node.js”. Here you will see some information about your app and a row of buttons on top. Click “Enable Node.js”, and in a few seconds your screen will look like this:First, let’s click on “Application URL” link to make sure that the app is running. If you see a “Hello, World!” message, congratulations! Your Node.js app is up and running.
App with DependenciesReal-world Node.js apps always have external dependencies defined in the package.json file and installed in the node_modules directory. Let’s take a look at an Express-based app (you can download it from the repository here: https://github.com/plesk/node-express.git) Either repeat the steps above to set up a repository in Plesk, or download/upload the source code manually. Once you’re done, the Node.js settings screen should look like this (assuming you uploaded the code to the “express-demo” directory):Before you try accessing the app, you need to initialize it first. Let’s begin by installing dependencies – luckily, in Plesk it is as easy as clicking the “NPM install” button. This will install all app dependencies according to the package.json file. Then, you need to tune the app startup file. Plesk uses the Phusion Passenger application server to serve Node.js apps. Our demo app repository contains a file named “server.js”. This file is not a part of a typical Express-based app, and contains just a few lines of code, but is nonetheless necessary for the app to run:
You can use the idea behind this file when hosting other Node.js apps (e.g. based on a different framework). The last thing you need to do is to change the Application Startup File setting to “server.js”. Now, try browsing the domain on which you installed the app – you should see the Express welcome message:By default, the app is running in the “production” mode. If you modify the app, don’t forget to restart it by clicking the “Restart” button.
const app = require('./app');
const http = require('http');