Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / Node.js

Setting Up a SmartOS Machine With Node.js and an AMP Stack

5.00/5 (1 vote)
25 Jan 2013CPOL3 min read 20.1K  
How to set up a SmartOS machine with Node.js and an AMP stack.

Introduction

Recently I started experimenting with Node.js on Joyent with SmartOS, but I still wanted to be able to fall back on Apache, MySQL, and PHP before I fully migrate over to a Node.js solution, and I found that it was pretty simple to set up.

Background

You may not have heard of SmartOS before, but maybe Solaris will sound a bit more familiar. Basically some of the guys that worked on Solaris ended up forking it into SmartOS with corporate support from Joyent. What you end up with is a cloud service virtual machine that runs very close to the metal and scales up with a click. Exactly what I wanted, but along with all the Node.js and MongoDB goodness, I wanted Apache, MySQL, and PHP.

Turns out that the 'Standard' SmartOS package on Joyent included almost everything I needed - Apache, PHP, and MySQL, as well as Node.js already baked in. What I wanted to add was phpMyAdmin, and this is how to do it.

Adding phpMyAdmin

This assumes that you have already provisioned a machine and already signed in via SSH. There's details on how to do that on Joyent's site.

Install phpmyadmin, type:

pkgin in phpmyadmin

and then add this line to httpd.conf:

Include /opt/local/share/examples/phpmyadmin/apache.conf

by doing:

nano /opt/local/etc/httpd/httpd.conf

and restart apache as root by doing: svcadm restart apache

That's it! You should now be able to launch phpMyAdmin by going to http://YOUR-IP/phpmyadmin/, remember the last forward slash.

File Locations

SmartOS places things into slightly different locations, so HTML files go here: /opt/local/share/httpd/htdocs.

You will need to do this to set up your admin account rights for this folder:

chown -R admin /opt/local/share/httpd/htdocs

Directories in this folder will need the execute permission set to be traversible, so type:

chmod a+x /opt/local/share/httpd/htdocs/

Now you should be able to SFTP into your machine, using the admin account name and password. Remember to set the port to 22 for SFTP to work.

Keep a Node.js Script Running

Something else I discovered is that although it is super simple to test a Node.js script, the moment you close down your terminal, it stops running.

This is fairly easy to solve though. I'll show the longer way first and then the short and easy way, you can choose which one you're more comfortable with, I honestly don't know what the pros and cons of each are at this point. 

To enable a Node script to keep on running, install Manifold to create a manifest:

pkgin in manifold

This is a sample manifest:

<?xml version="1.0"?>
<!DOCTYPE service_bundle SYSTEM "/usr/share/lib/xml/dtd/service_bundle.dtd.1">

<service_bundle type="manifest" name="node-hello-world-service">

  <service name="site/node-hello-world-service" type="service" version="1">
    <create_default_instance enabled="true"/>
    <single_instance/>
    <dependency name="network" grouping="require_all" 
               restart_on="refresh" type="service">
      <service_fmri value="svc:/milestone/network:default"/>
    </dependency>
    <dependency name="filesystem" grouping="require_all" 
              restart_on="refresh" type="service">
      <service_fmri value="svc:/system/filesystem/local"/>
    </dependency>
    <method_context working_directory="/home/admin/hello-world">
      <method_credential user="admin" group="staff" privileges='basic,net_privaddr'  />
      <method_environment>
        <envvar name="PATH" value="/home/admin/local/bin:/usr/local/bin:/usr/bin:/usr/sbin:/bin"/>
        <envvar name="HOME" value="/home/admin"/>
      </method_environment>
    </method_context>
    <exec_method
      type="method"
      name="start"

      exec="/opt/local/bin/node /home/admin/hello-world/server.js"
     timeout_seconds="60"/>
    <exec_method
      type="method"
      name="stop"
      exec=":kill" 
      timeout_seconds="60"/>
    <property_group name="startd" type="framework">
      <propval name="duration" type="astring" value="child"/>
      <propval name="ignore_error" type="astring" value="core,signal"/>
    </property_group> 
    <property_group name="application" type="application">
    </property_group>
    <stability value="Evolving"/>
    <template>
      <common_name>
        <loctext xml:lang="C">node.js hello-world service</loctext>
      </common_name>
    </template>
  </service>
<pre></service_bundle>

To build a manifest, run this command and enter your information:

manifold myservice.xml

To specify the executable, determine where Node.js is running by entering:

which node

Once you generate a manifest, you can import it by running:

svccfg import myservice.xml

After importing, you will need to enable it with:

svcadm enable myservice

This is another way to make your Node script run forever, is to use, well, Forever. Install it like this:

npm install forever -g

Then run your script forever with:

forever start /opt/local/share/httpd/htdocs/server.js

Actually pretty cool way to run Node scripts, since you can get lists of running scripts, start, stop, etc.

That's the end of this little article on setting up a SmartOS machine on Joyent. 

Have fun! Smile | <img src=

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)