Plesk

Plesk 12.5 and Node.js

Plesk Node Js setup

During the last few years, Node.js has been steadily growing in popularity. On top of that, according to StackOverflow, JavaScript is today’s most popular programming language. If you are a Node.js developer, you may be curious whether it is possible to host Node.js apps on Plesk. The short answer is “yes, it’s possible”. In this article, I’ll explain how to set up the environment step by step using an Ubuntu 14.04 server with Plesk 12.5 for my demonstration. If you want, you can use other OSes or Plesk versions, but there can be the minor differences in commands and instructions.

I assume that you’ve already tried installing Node.js on the server. If not, let’s do it right now using the official instructions provided at nodejs.org:

curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -
sudo apt-get install -y nodejs

Let’s verify the installation by checking the version of Node.js:

node -v

Output should look like this:

v6.2.0

The next thing to do is to install an application server.  Plesk 12.5 comes with the Phusion Passenger component that you can install with just a few clicks. If you’re unsure what do we need the application server for, just take my word for it – the task will be mush simpler with it.

Ok, let’s go ahead. Log in to Plesk as the admin , go to “Tools & Settings” and click the “Updates & Upgrades” link:

 

Click “Add/Remove Components” on the next screen. Expand the “Web hosting” section and mark the “Phusion Passenger server” component for installation:

 

Phusion Passenger will run as a web server module, so after the installation, it will have to be enabled. Return to “Tools & Settings” and click the “Apache Web Server” link (found under “General Settings”). Here we need to enable the “passenger” module:

 

The next step is to prepare a domain for hosting our Node.js app. We need to set up a proper document root for the domain. By default, Phusion Passenger looks for the app.js file that should be located in the parent directory of the document root. We will deploy our app to the “my-app” directory, so we need to set up the document root as “my-app/public”. Go to “Websites & Domains” for your domain and click “Hosting Settings”. Here, we need to change the document root, so it looks like this:

 

The final step is to deploy our Node.js app. We will use a basic “Hello World” app as for this example. You can use the File Manager to create the following directory structure in the root of the webspace:

 

Alternatively, log in via SSH with the webspace system user credentials (don’t forget to enable SSH access on the “Web Hosting Access” screen) and run the following commands

mkdir my-app
mkdir my-app/public
mkdir my-app/tmp

Then, create the my-app/app.js file with the following contents:

const http = require('http');

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World\n');
});

server.listen(0, '0.0.0.0', () => {
  console.log('Node.js app is running...');
});

Let’s check the results by browsing your domain. You should see the “Hello World” text on the page. The /var/log/apache2/error.log log file should contain the “Node.js app is running…” message if everything is ok, or some error messages otherwise.

The last thing I’d like to mention is how to restart the app. Use the standard Phusion Passenger method – namely, create or “touch” a file named restart.txt in the tmp directory:

touch tmp/restart.txt

That’s it, congratulations! In no time at all, you’ve got your very own Node.js app running on a Plesk server.