<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:media="http://search.yahoo.com/mrss/"><channel><title><![CDATA[NodeJS - Bacon Applications]]></title><description><![CDATA[Follow me while I share with the world useful things I have learned and projects I am working on.]]></description><link>http://www.baconapplications.com/</link><image><url>http://www.baconapplications.com/favicon.png</url><title>NodeJS - Bacon Applications</title><link>http://www.baconapplications.com/</link></image><generator>Ghost 3.7</generator><lastBuildDate>Sun, 23 Mar 2025 15:11:53 GMT</lastBuildDate><atom:link href="http://www.baconapplications.com/tag/nodejs/rss/" rel="self" type="application/rss+xml"/><ttl>60</ttl><item><title><![CDATA[Moving a NodeJS App to Production on Debian Wheezy]]></title><description><![CDATA[<!--kg-card-begin: markdown--><p>WOW! Moving this application (my new website and blog) to my Debian VPS was a little harder than I wanted and anticipated.  Of course this was my first time publishing a Node app to production so some learning time was to be expected...</p>
<h3 id="gettingreadyforproduction">Getting Ready for Production</h3>
<p>Before moving from</p>]]></description><link>http://www.baconapplications.com/moving-nodejs-app-to-production-on-debian/</link><guid isPermaLink="false">5e535ef4ba89970001e0b059</guid><category><![CDATA[NodeJS]]></category><category><![CDATA[Debian]]></category><dc:creator><![CDATA[Randy Bacon]]></dc:creator><pubDate>Mon, 08 Sep 2014 04:51:49 GMT</pubDate><media:content url="http://www.baconapplications.com/content/images/2020/02/gitCheckin.png" medium="image"/><content:encoded><![CDATA[<!--kg-card-begin: markdown--><img src="http://www.baconapplications.com/content/images/2020/02/gitCheckin.png" alt="Moving a NodeJS App to Production on Debian Wheezy"><p>WOW! Moving this application (my new website and blog) to my Debian VPS was a little harder than I wanted and anticipated.  Of course this was my first time publishing a Node app to production so some learning time was to be expected...</p>
<h3 id="gettingreadyforproduction">Getting Ready for Production</h3>
<p>Before moving from PHP to Node on my server I had read several articles and posts about different set ups.  I decided to move to NGINX from Apache as my web server. I have used Apache since my first FreeBSD server - about 15 plus years ago - but it was time to move forward to something faster and with a fun name.  Another post will follow about this move later...</p>
<p>Since I already had NGINX up and running I just needed to adjust my website config to the following (real ports are not below but would be the port of your apps):</p>
<pre><code>server {
	server_name www.hirerandybacon.com;

	location / {
    	proxy_pass http://localhost:&lt;NODEJSPORT&gt;;
    	proxy_http_version 1.1;
    	proxy_set_header Upgrade $http_upgrade;
    	proxy_set_header Connection 'upgrade';
    	proxy_set_header Host $host;
    	proxy_cache_bypass $http_upgrade;
	}

	location /blog {
    	proxy_pass http://localhost:&lt;NODEJSPORT&gt;;
    	proxy_http_version 1.1;
    	proxy_set_header Upgrade $http_upgrade;
    	proxy_set_header Connection 'upgrade';
    	proxy_set_header Host $host;
    	proxy_cache_bypass $http_upgrade;
	}
}
</code></pre>
<p>Additionally, I went the lazy route and published all my web files as is from the raw project.  I plan on building a Grunt script to help with this but for now it was all manual.</p>
<h3 id="whatdidntwork">What didn't work</h3>
<p>I tried several methods to get my node app up and running.  First I installed <a href="https://github.com/nodejitsu/forever" target="_blank"><i>forever</i></a> figuring it would be the easiest way (and I have heard others talking about it).  Running forever from the command line is as simple as (note: the NODE_ENV=production export is for my <a href="http://docs.ghost.org/" target="_blank">ghost</a> blog):</p>
<pre><code>NODE_ENV=production forever start app.js
</code></pre>
<p>The first issue I had found was a warning about sqlite3.  Because of my recent work with node-webkit I figured I needed to do an actual npm install on the machine to compile SQLITE.  I changed to the web directory of my project and ran:</p>
<pre><code>npm install sqlite3
</code></pre>
<p>Of course this yeilded some random error messages about permissions and also one of interest.  I was missing <i>make</i>.  So installing <i>make</i> was needed:</p>
<pre><code>sudo apt-get install build-essential
</code></pre>
<p>After running this, the base application started but now the ghost module was complaining about sqlite3 (the joys of developing on Windows and publishing to Debian).  I changed to the ghost directory inside of my web/node_modules directory and ran the npm install sqlite3 command again.</p>
<p>Finally this worked without issue and I was excited. Time to set it up to run via the cron.  Being the old school Unix guy I was (or maybe more accurately the lack of recent Unix experience) - I rushed to set up an init.d script.</p>
<p>I tried several init.d scripts from git and Google search results.  It seemed each had an issue and did not work well with my flavor of Debian - Wheezy.  Additionally, more errors were caused by creating the scripts on Windows and coping it to my Debian box.  Turns out carriage returns were the cause.</p>
<h3 id="whatfinallyworkedafterawhile">What finally worked after a while...</h3>
<p>After wasting some good football watching time with the init.d I decided to try something simpler from the <a href="http://support.ghost.org/deploying-ghost/" target="_blank">Ghost docs</a> - <a href="http://supervisord.org/" target="_blank">Supervisor</a>.  The installation and config was pretty easy.</p>
<pre><code>sudo apt-get install supervisor
</code></pre>
<p>On step 2 - <code>service supervisor start</code> I received the following a useless error message:</p>
<pre><code>Error: Another program is already listening on a port that one of our HTTP servers is configured to use.  Shut this program down first before starting supervisord.
</code></pre>
<p>Great!  A quick google search though indicated I just needed to run this:</p>
<pre><code>sudo unlink /var/run/supervisor.sock
</code></pre>
<p>After running the command and trying again, the service was running.  I banged out a quick config for my node application and saved it to the config.d directory - /etc/supervisor/conf.d/ - (of course the real path below is a little different but you get the idea):</p>
<pre><code>[program:hirerandybacon]
command = node /mysecrectpath/app.js
directory = /mysecrectpath
user = www-data
autostart = true
autorestart = true
stdout_logfile = /var/log/supervisor/hirerandybacon.log
stderr_logfile = /var/log/supervisor/hirerandybacon_err.log
environment = NODE_ENV=&quot;production&quot;
</code></pre>
<p>Now I went to fire off the supervisor program via:</p>
<pre><code>sudo supervisorctl start hirerandybacon
</code></pre>
<p>And instead of a running app I received the following error message:</p>
<pre><code>hirerandybacon: ERROR (no such process) 
</code></pre>
<p>&quot;GRRR,&quot; said Randy.  This required more Google searching which brought me to this solution:</p>
<pre><code>supervisorctl reread
supervisorctl reload
</code></pre>
<p>FINALLY starting the program yields success and my site is now running.  Or is it?</p>
<h3 id="moreissues">More Issues</h3>
<p>I guess because I'm bored I decided to log into my blog and finish up my first post.  I tired to log in and received no response until a toast popped up saying a server error had occurred.</p>
<p>I checked the supervisor log and of course there where no relevant log items.  At this point I was tired and wanted to brutte force my way though this thing.  I decided to try to run the Node app again via the command line but this time as the www-data user (you know the user I assigned the app to above).  A quick change of the user via:</p>
<pre><code>sudo su www-data
</code></pre>
<p>Firing up the app via:</p>
<pre><code>NODE_ENV=production node app.js
</code></pre>
<p>And finally a useful error message.  The database was opening in read only mode.  Why?  Because when I deployed my database, although I thought I had chown'd the data directory and db to the :www-data group I hadn't changed permissions...  After changing the permissions on the data directory (775) and database (664) and restarting my app, I was in business.</p>
<p>After a few hours (or maybe an evening) my node app and blog is running in production.  I guess the plus side is I had another topic to blog about.</p>
<!--kg-card-end: markdown-->]]></content:encoded></item></channel></rss>