If you’ve been keeping up with some of my previous posts, you’ll know that I’ve been making an effort to move away from PHP ZendFramework (or PHP in general) to Node.js. Up until mid December 2014, my personal profile website www.nraboy.com has been using PHP ZendFramework 2, and for whatever reason it would crash my Apache instance constantly.
Since mid December 2014, I’ve made the switch to Express.js framework on top of Node.js. I did a previous post on the topic of Express.js and why I like it, but it is beyond the point for this particular article.
I have a few sites still running PHP and I didn’t want to pay for a new server strictly for Node.js applications. Instead I chose to run LAMP (Linux, Apache, MySQL, PHP) side-by-side with Node.js on the same server. The following explains how I did this.
At this point, I’m going to assume you already have a functional LAMP stack. I’m going to focus strictly on explaining adding Node.js to your server. Here are the list of assumptions I’m going to make:
The first thing you want to do is install the Node Package Manager (NPM) if you haven’t already. On Ubuntu or Debian, this can be done by running the following:
curl -sL https://deb.nodesource.com/setup | bash - apt-get install nodejs
If you get complaints about
curl not being installed, install it with the following command:
apt-get install curl
With NPM installed, it is time to transfer your web application to the server. For production Node.js applications, we need a way to run them on a background process so you can safely exit the Terminal without shutting down your instance. I found a nifty package called Forever, which is currently running great on my server.
If you’d like to follow in my footsteps, install Forever by doing the following:
npm install -g forever
If your site is uploaded and NPM and Forever are configured correctly, it is time to start the Node.js instance. If you’re using Express.js like I am, run the following command to start a Forever instance:
forever start ./bin/www
In the above command you’ll notice I am feeding the ./bin/www script because that is what
npm start launches for Express.js. Be sure to change the script to whatever your launch script is.
By default, my website is now running at http://localhost:3000 which isn’t ideal for remote visitors. We want to be able to access our site from a domain name processed by Apache. In your Apache VirtualHost file, you might have something that looks like the following:
<virtualhost *:80> ServerName www.nraboy.com ProxyPreserveHost on ProxyPass / http://localhost:3000/ ProxyPassReverse / http://localhost:3000/ </virtualhost>
We are telling Apache to create a proxy that will get our Node.js http://localhost:3000 site every time the www.nraboy.com domain name is hit. All assets and pages will use the www.nraboy.com path instead of http://localhost:3000 leading everyone to believe the website is being served no differently than any other.
However, by default, the Apache proxy modules are not enabled. You must run the following two commands if the modules are not already enabled:
a2enmod proxy a2enmod proxy_http
You may be required to restart Apache after enabling these modules.
By setting up this proxy in Apache we can have Node.js and PHP run on the same server rather than spending money on a new one.
Unsubscribe at any time without hassle.