Plesk 12 comes with a range of pre-built installation wizards for a number of the most popular PHP CMS’ on the market, such as WordPress, Drupal and Joomla, but not for Node.js, or the other Node-based applications.
This is a little unfortunate, but it’s no deal breaker. Given the flexibility of Plesk 12, as administrators, developers and devops, we’re able to do take care of all the necessary steps manually.
In today’s tutorial, we’re going to step through setting up and deploying a basic Node.js application on Plesk 12. By the time we’re done, you’ll see the site as in the screenshot below.
Whilst not too feature-rich, it will have the core Node.js 4 framework installed, allowing you to build on it as suits your needs and desires.
What Is Node.js
Currently at version 0.10.30, Node.js, according to the official website, is:
a platform built on Chrome’s JavaScript runtime for easily building fast, scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices.
In short, if you’d consider yourself adept at JavaScript, even a JavaScript guru, you can now create high-performance, near real-time applications using nothing other than the once maligned client-side language.
Prerequisites
To complete today’s tutorial, you’re only going to need a few prerequisites. These are:
- Curl
- Linux build tools (such as make and configure)
- Root access (or user with sufficient privileges or who’s in the sudoers list)
Getting Setup
Right, so what do we need to do to set it up? Thanks to the modern UI, and intelligent layout available in Plesk 12, it’s a real no brainer, requiring only a few steps to have it live, which are as follows:
- Create a Sub-Domain
- Setup SSH Access
- Install Node.js
- Configure Node.js
- View The Application
Let’s work through the process now.
Create a Sub-Domain
When you first login to your account, click the Domains in the left-hand side navigation bar, under Hosting Services. This will then show you the list of your websites & domains. To keep this simple, we’re going to create a new sub-domain, specifically under huffyscrew.com.
On the far right of that domain’s row in the table, click “Manage Hosting“. This will display the domain and sub-domain entries, which you can see in the screenshot below.
Above the first entry, you’ll see three buttons: “Add New Domain“, “Add New Subdomain” and “Add New Domain Alias“. Click “Add New Subdomain”, and on the form which appears, enter “nodejs” for the subdomain name. The Document root field will be pre-populated, based on the Subdomain name you entered.
It may take a minute or two for the subdomain to be created, based on your server’s specifications. But it shouldn’t take too long. When it’s finished you’ll be back on the domains page you started at, with your new domain available at the bottom of the list.
In the entry, you’ll see all the pertinent details, along with links to more, as well as to make changes.
Setup SSH Access
Now we need to check that SSH access is setup properly, so that we can login and run a few command line scripts. If you’re not too familiar with command line scripts, that’s ok. The one’s I’ve listed here are quite simple and have sufficient documentation, should it be needed.
At the bottom of the subdomain’s entry, click the tab titled “Show More“, then click the first entry in the exposed list, titled “Web Hosting Access“. On the page that appears, you’ll see four sections.
In the second section, titled “System user“, make sure that the last option, “Access to the server over SSH“, is set to /bin/bash or /bin/sh and click OK at the bottom.
The reason for this is because these scripts need that environment to run in. sh may work, but bash works best for this example. Now you’re ready to login and begin installing the application.
Installing Node.js
Node.js isn’t always the easiest of software to install, but gladly Isaac Schlueter from Joyent (sponsor of the Node.js project), compiled this Gist.
It provides a number of ways in which you can install it; the first one, which we’ll be using in this post, is the simplest. I’ve copied the code involved below:
echo 'export PATH=$HOME/local/bin:$PATH' >> ~/.bashrc . ~/.bashrc mkdir ~/local mkdir ~/node-latest-install cd ~/node-latest-install curl https://nodejs.org/dist/node-latest.tar.gz | tar xz --strip-components=1 ./configure --prefix=~/local make install # ok, fine, this step probably takes more than 30 seconds... curl https://www.npmjs.org/install.sh | sh
First it setups up the shell environment then creates a couple of directories which the installer needs. After this it starts the compilation and installation processes for both Node.js and the Node Package Manager (NPM).
If you’re not familiar with the Node Package Manager, it’s a command line tool which both manages dependencies for Node.js projects as well as allows users to install Node.js applications that are available on the NPM registry.
To find out more about it, consult either the NPM website or the respective Wikipedia entry.
Creating a Basic Application
Now that Node.js and NPM are installed, we’re going to create a basic application, one which I’ve borrowed from Nodejs.org:
var http = require('http'); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello Worldn'); }).listen(13370, '0.0.0.0'); console.log('Server running at https://0.0.0.0:13370/');
The code above will listen on port 13370 and executes a simple web server who’s sole job is to print out the string ‘Hello World’. So create a file called simple-server.js in a new directory, /opt/node-services.
Then in the file add the code above and save it. From the command line, run the following command to start Node.js up, using the script as its source:
node simple-server.js
View the Application
With the script running, open up your browser at https://127.0.0.1:13370/. All being well, you’ll see output in your browser like that below.
Serving Node Through Apache
Now that the Node application’s up and running, we now have a few last things to do. These are, firstly, setting up Apache as a reverse proxy. This will have Apache pass all requests to the Node.js sub-domain to our Node.js application. Secondly, we need to create an initialisation shell script, to make sure that our Node.js application is started when the server boots.
Apache as a Reverse Proxy
Thanks to the smooth Plesk 12 interface, configuring Apache as a reverse proxy is quite straight-forward. Under the hosting settings for the sub-domain, click Show More at the bottom.
Then click the second option, in the middle column, labelled Web Server Settings, which you see in the screenshot below.
Then, when the page’s loaded, under Additional directives for HTTP, add the following configuration directives:
<Location /> ProxyPass https://<server_ip_address_>:13370/ ProxyPassReverse https://<server_ip_address>:13370/ </Location>
Linux Initialisation Shell Script
To keep this as simple as possible, I’ve used this Gist which provides most of the work for us already. So save that file in /etc/init.d as node-service. Then you need to make a few changes. Change the user to one which has an active shell.
If you know one already, choose that. Otherwise, run the following command:
egrep --invert-match "nologin|false|shutdown|sync" /etc/passwd | sort | cut -f1 -d:
This will output a list of users which you can choose from. Then set the other configuration options as follows:
Option | Setting |
---|---|
DAEMON | “/usr/local/bin/node” |
ROOT_DIR | “/opt/node-services” |
SERVER | “$ROOT_DIR/simple-server.js” |
LOG_FILE | “$ROOT_DIR/simple-server.js.log” |
LOCK_FILE | “/var/lock/subsys/node-server” |
After all this is done, we need to enable the script to be called at the correct runlevel. To do so, run the following commands:
chkconfig --level 345 node-service on chkconfig --list | grep node-service
This will setup the script as required and do a quick validation that it was done successfully. You should see the following output:
node-service 0:off 1:off 2:off 3:on 4:on 5:on 6:off
Wrapping Up
So there you go. In just a few minutes, we’ve stepped through creating and configuring a sub-domain, to installing Node.js and NPM, creating and running a sample Node.js application.
Now it’d be great if it was simpler, such as having an installer for Drupal, Joomla and WordPress. But it’s a bit hard to compare off the shelf applications, such as these, to frameworks which allow for creating nearly any kind of application.
What I’m hoping for is to have an installer which lays the foundation for this process, which may just be available in the future.
Till then, share your thoughts in the comments. Has this made it simpler for you to start creating Node.js applications on Plesk? I hope so. I’d love to hear how you feel.
References
- https://blog.nodeside.com/nodejs-as-upstart-service-for-ubuntu-centos (UPDATE: 21/08/15 Reference no longer available)
- https://thatextramile.be/blog/2012/01/hosting-a-node-js-site-through-apache
- https://www.conetix.com.au/support/article/plesk-12-running-mod_proxy-to-use-nodejs