Why Use Node.js
Now, you might be wondering why you’d use something like Node.js instead of PHP or Ruby on Rails. Over on the Plesk blog, we have several articles comparing Node.js to each of them, but here are some of the highlights:
- But PHP is quite powerful and a great connector if you’re doing a lot of server-side work. It’s reliable and has been around a long time. It worked really well with MySQL, and powers popular web applications like WordPress.
- Ruby on Rails is more of a website framework with multiple languages and a complex architecture. It’s built for quick site development and has its own integrated web server. But debugging is difficult and it can be inflexible at times.
What you choose is really up to you and what’s best for the task at hand. But if you’re working on the web, chances are you’ll need to use Node.js at some point, if you haven’t already.
If you are considering providing Node.js application hosting as a service to your customers, or if you would like to use Plesk to automate hosting Node.js applications you are developing, we’ve got good news for you.
Plesk comes with a Node.js extension that allows you to install and manage apps quickly and easily. Other key functions include:
• Using the latest releases of Node.js (LTS and Current),
• Choice of package manager: NPM or Yarn,
• Viewing application logs in Plesk web interface using Log Browser,
• Easy to use web interface for other related management functions
In this episode, we’ll describe in detail how to work with Node.js apps in the context of Plesk Obsidian, the latest version of Plesk.
One more note: we have a step-by-step tutorial with screenshots that we’ll link up in the show notes. This episode will give you a high-level overview of what you need to do, and what to consider.
Get Node.js Extension
The first thing you need to do is install Plesk Obsidian. You can use either a Linux distro or Windows; the tutorial on our site used Debian. As you work with Node.js and Plesk, just note that there could be some minor differences if you run with a different OS than the tutorial you’re using.
Assuming Plesk is already installed, we can now install the Node.js extension.
After you log in to Plesk, you’ll want to go to “Tools and Settings”, and click “Updates and Upgrades”.
Next, go to “Add/Remove Components”. Find the “NodeJS support” component. Select it, and continue with the Node.js installation.
In a few minutes, all the necessary components will be installed and you’ll be ready to go!
Hosting a ‘Hello World’ App
To make sure everything works, you can try to host a sample Node.js app, such as a “Hello, World” app. You can find one from Github in the notes of this episode.
Copy/pasting content from the App.js file manually is an option. However there’s another Plesk extension instead worth exploring — the Git extension.
Now, to use the Git extension, you have to create a domain with hosting, which is called a subscription. Then, you need to simply create a target directory where the app will be stored. From there, you’ll be able to install the Git extension. Then you need to set up a local repo to pull the code from. You’ll want the local repo to mirror the remote one so the code can be deployed correctly.
Again, see our tutorial for step-by-step actions with screenshots.
Differences Between ‘Document Root’ and ‘Application Root’
As you set up your application, you’ll see references to both “Document root” and “Application root.” It is important to note that they are not always the same thing.
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…this is likely your HTML, CSS, and image files.
The “Application root” is the root directory where the app itself is located. This, for the purpose of this episode, is where the executable Node.js code lives.
In most cases, the “Document root” is a directory inside the “Application root” directory.
You can change the Document root in Plesk by going to “Domains”, selecting your website and going to “Hosting Settings.”
From there you can change the existing Document root to match the repo name, and adding “slash public” to the end — but again, note that your milage may vary depending on your own server environment and code repo.
Enable Node.js Support
The last thing to do before you run an app is to enable Node.js support on the domain. Go to “Domains”, choose the website and click “Node.js”. Here you will see some information about your app with a row of buttons on top. Click “Enable Node.js”, and in a few seconds, you’ll see a confirmation screen.
You will then want to visit the “Application URL” to make sure that the app is running. If you see the expected results (in the example mentioned, “Hello World” will show on the screen), you’ve successfully installed and enabled Node.js.
App with Dependencies
Real-world Node.js apps always have external dependencies defined in the package.json file and installed in the node_modules directory.
You will need to install dependencies separately for them to work.
You do this by initializing the app first. Luckily, in Plesk, you can install dependencies by clicking the “NPM install” button. This will install all app dependencies according to the package.json file.
Note: You can also use Yarn package manager. The extension will attempt to determine the package manager you need based on the application files. Or you can specify it explicitly in the extension interface.
Then, you need to tune the app startup file. Plesk uses the Phusion Passenger application server to serve Node.js apps. You can find the sample code in the show notes!
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.
Next up, let’s talk about debugging. Knowing how to debug Node.js apps in case any issues appear is essential for any Node.js developer. Here are a few tips that can save you a lot of headaches:
Viewing Hosted Application Logs
The first thing to do when troubleshooting a Node.js app is to check the log files. To find the log records including console output for your application, open “Log Browser” for your website. You can focus the listed records using the dropdown at the right.
Bring Error Messages to the Browser
When developing an app, it’s convenient to see the error messages directly displayed on the site – they are still getting logged. Go to the Node.js app settings screen and change the Application Mode to “development”. Next time an issue pops up, the error will be shown in the browser window.
Node.js and Windows
Node.js support is available in both Plesk for Linux and Plesk for Windows. But hosting Node.js apps on Windows has a few caveats you need to take into account.
First, Node.js support in Plesk Obsidian is implemented with the help of the iisnode module (if you would like to learn more about the iisnode project, you can find the documentation in the show notes).
Second, while in Plesk for Linux, you can select a different Node.js version for every domain. In Plesk for Windows, a single Node.js version is used server-wide – so you can select the Node.js version during the installation.
There are a few other small differences. For example, In Plesk for Linux, it must be located in the parent directory of the domain’s “Document root”. In Plesk for Windows, it must be placed directly in the “Document root” directory. But it is possible to run Node.js on Windows!
So there you go. A high–level overview of:
- What Node.js is
- How it compares to PHP and Ruby on Rails
- How to install it using Plesk Obsidian
- Working with dependencies
- Installing it on Windows
Do you have more questions about using Node.js on Plesk? Let us know!
The Official Plesk Podcast: Next Level Ops Featuring
Joe is a college-accredited course developer and podcast coach. You can find him here.
Did you know we’re also on Spotify and Apple Podcasts? In fact, you can find us pretty much anywhere you get your daily dose of podcasts. As always, remember to update your daily podcast playlist with Next Level Ops. And stay on the lookout for our next episode!
Great podcast. Subscribed!