|
This topic will teach you how to set up Node.js™ on a SmartOS SmartMachine from Joyent. Since updates to Node.js occur at such a rapid pace, your best bet is to install Node.js from source. Once you have Node.js installed, you can follow the below steps for creating a simple Hello World! web server. You can then apply the same techniques you use when deploying Hello World! to any node application you develop. |
At A Glance
This page shows you how to install Node.js on your SmartMachine and provides a simple demonstration for writing a web server in node. |
Prepare Your SmartMachine
| For more information on setting up a SmartMachine, see Setting Up Your SmartMachine. |
Visit Joyent and provision a new SmartMachine.
| You can set up a variable in a bash session with your SmartMachine public IP to minimize typing. For example:
|
When your SmartMachine is generated, you will receive an email with several passwords. You can change them by following the instructions here.
You can also secure copy your public keys so you do not have to enter the password every time:
- Login to your SmartMachine:
- Change the root password:
- Change the admin password:
Installing Node.js and npm
Follow these instructions to install the latest stable version of Node.js (v0.6.8) and npm on a Joyent SmartMachine.
| These instructions install node in the ~/local directory avoiding the need for root privileges when installing things with npm, which is bad. |
- Create a ~src/ directory. This is where you will download the latest version of node.
- Decompress the downloaded tarball:
- Create the ~/local directory if it does not already exist:
- Add it to your PATH:
- Configure, build, and install node:

This step may take a while to complete.
| These steps use gtar instead of tar and use gmake instead of make due to differences between Solaris and GNU. |
Cleanup
You will install your Node.js web server on port 80. If your SmartMachine has Apache or Nginx pre-installed, you will need to disable them to prevent them from interfering with the install. You can disable those server by running these commands:
The Hello, world! program
Below is a simple demonstration of deploying a small web application to a SmartMacine web server.
First, create a folder where your program is going to live:
You can grab the file itself from the github repo, or probably just crank it out from memory if you’ve been messing around with Node.js long enough:
(Feel free to get a little more creative with it.)
Save that to ~/hello-world/server.js.
Pause for Reflection
Let’s make sure everything is kosher at this point.
Then hit your SmartMachine URL and make sure it says Hello, world!
If anything is broken, now’s the time to fix it, because we’re about to dive into Solaris service land.
Define the Service
“Services” in Solaris are first-class citizens in the OS. Each service has an XML manifest file that defines it. The svccfg command is used to add one of these
config files to the system, and svcadm is used to administrate
services once they’re defined.
I went ahead and created a node-hello-world-service-manifest.xml file for this purpose. So, download that, and add it to the system configuration:
If you’ve cloned the git repo, then you can of course svccfg it from there, instead.
It’s outside the scope of this article to go through all the different settings and what they do. Most are pretty self-explanatory (folder paths and such). For more info than you ever wanted to know about smf template files, see man 5 smf_template.
Start the Service
Starting a service is a simple one-liner:
To stop the service is also predictably simple:
Once you get tired of stopping and starting the service, start it one last time, and then load up your SmartMachine URL in a web browser.
If it says Hello, world!, then congratulations, you’re done! You can quit while
you’re ahead, or read on to learn a few tricks you can use when it breaks.
Troubleshooting
The services in Solaris each have a log file based on the name that they’re defined with. For ours, the log file lives at /var/svc/log/site-node-hello-world-service:default.log. If something doesn’t work right, you can view the last few log messages like this:
Solaris will give it a reasonable try to restart your service if it crashes, which is a really nice feature. But, if it keeps crashing, it’ll give up, and put the service into “maintenance” mode.
For instance, if the server can’t be started, you might see a bunch of log messages that look like this:
If this happens, first of all, make sure that the site will start up if you do it manually. This will uncover most problems.
The sudo is required because we’re listening on port 80.
You can also get information about a service by using the svcs command. For example, if our site thrashes itself into maintenance mode, you might see this:
Once you’ve fixed whatever was preventing the site from starting properly, you can restart it by doing the following:
Note that once you go into maintenance mode, your service must be disabled before it can be re-enabled.
At this point, the logs should look like this:
And hitting the site in a web browser or curl http://127.0.0.1/ should show it working.
Written by Isaac Schlueter
1 Comment
comments.show.hideMar 04, 2011
Anonymous
This seems to get us node.js v0.2.1. Any way to get the latest version ? Build fails on my SmartMachine: