How to run node.js Web Server on Apache in Centos 6.4

After moving from Wordpress to Ghost which is written using node.js or Server-side JavaScript whereas Wordpress is written in PHP I have to run my node Web Server and figure out the way to let Apache know how to serve my Web Server.

If you are running a node.js server and would like to proxy that with Apache in CentOS 6.4 this is how you can do it:

Assuming you have setup your node.js server and can run it with your IP like 192.241.147.88:2368 and now you want to point to your domain like mine http://alicoding.com. Also, make sure you have httpd installed on your server and if you haven't already then do:

sudo yum install httpd  

1. use your favorite editor to edit this file on your remote server httpd.conf sudo vim /etc/httpd/conf/httpd.conf

2. Uncomment this line #NameVirtualHost *:80 to NameVirtualHost *:80

### Section 3: Virtual Hosts
#
# VirtualHost: If you want to maintain multiple domains/hostnames on your
# machine you can setup VirtualHost containers for them. Most configurations
# use only name-based virtual hosts so the server doesn't need to worry about
# IP addresses. This is indicated by the asterisks in the directives below.
#
# Please see the documentation at
# <URL:http://httpd.apache.org/docs/2.2/vhosts/>
# for further details before you try to setup virtual hosts.
#
# You may use the command line option '-S' to verify your virtual host
# configuration.

#
# Use name-based virtual hosting.
#
NameVirtualHost *:80  
#
# NOTE: NameVirtualHost cannot be used without a port specifier
# (e.g. :80) if mod_ssl is being used, due to the nature of the
# SSL protocol.
#

3. Now find this block <VirtualHost *:80> and edit this

<VirtualHost *:80>  
    ServerAdmin youremail@something.com
    ServerName yourdomain.com
    ErrorLog /path/to/output/errors
    CustomLog /path/to/output/logs common
    ProxyRequests off
    <Proxy *>
            Order deny,allow
            Allow from all
    </Proxy>
    <Location />
            ProxyPass http://localhost:<PORT>/
            ProxyPassReverse http://localhost:<PORT>/
    </Location>
</VirtualHost>  
### If you need multiple node web server
<VirtualHost *:80>  
    ServerAdmin youremail@something.com
    ServerName subdomain.yourdomain.com
    ErrorLog /path/to/output/errors
    CustomLog /path/to/output/logs common
    ProxyRequests off
    <Proxy *>
            Order deny,allow
            Allow from all
    </Proxy>
    <Location />
            ProxyPass http://localhost:<PORT>/
            ProxyPassReverse http://localhost:<PORT>/
    </Location>
</VirtualHost>  

Now let's understand what is going on here. First you're telling Apache to use this given location of the ProxyPass if someone is requesting something from port 80 instead of given them the specific file in your server instead you are redirecting them to the actual IP:PORT which you used to access before.

4. Last but not least you will need to restart your httpd service in order to make this affective. sudo service httpd restart (If your httpd service is running) otherwise do sudo service httpd start.

That's it if your node server is running you should be able to visit your domain and see the content.