Skip to content

Latest commit

 

History

History
108 lines (71 loc) · 3.91 KB

run-node-server-alongside-apache.md

File metadata and controls

108 lines (71 loc) · 3.91 KB

Run a Node.js server alongside Apache

This guide shows how to run a Node.js daemon process alongside Apache, with a nice, clean URL.

The best way to develop with Node.js is using a dedicated hosting provider, but sometimes you just want to tinker. A node daemon can easily be run on a different port to Apache, but nobody likes a dirty URL.

Linux (Ubuntu)

The first step is going to be making sure you have mod_proxy and mod_proxy_http installed. There are detailed guides which should be looked at before following this guide verbatim.

If you're fortunate and the proxy_http module is in the mods-enabled directory then you might simply have to run:

$ sudo a2enmod proxy_http

Otherwise follow these steps:

Install reverse_proxy module:

$ sudo apt-get install libapache2-mod-proxy-html

Install libxml if it's not already installed:

$ sudo apt-get install libxml2-dev

Load the modules:

$ sudo a2enmod proxy_module proxy_http_module headers_module deflate_module

You can now add the following rules to your Apache virtualhost config:

# /etc/apache2/sites-available/your-node-virtualhost-domain.com

<VirtualHost *:80>
	# Admin email, Server Name (domain name), and any aliases
	ServerAdmin your@email.com
	ServerName  www.amazeapp.com
	ServerAlias amazeapp.com

	# Index file and Document Root (where the public files are located)
	DirectoryIndex index.html index.php
	DocumentRoot /var/www/amazeapp.com/public_html

	ProxyRequests off
	<Proxy *>
		Order deny,allow
		Allow from all
	</Proxy>
	<Location />
		ProxyPass http://localhost:3000/
		ProxyPassReverse http://localhost:3000/
	</Location>

	# Log file locations
	LogLevel warn
	ErrorLog  /var/www/amazeapp.com/log/error.log
	CustomLog /var/www/amazeapp.com/log/access.log combined
</VirtualHost>

You should only have to add the proxy content. We're telling Apache that every request that comes through the standard port (80) should be routed through our soon to be running node server and its own port (3000).

You'll want to make sure Apache knows the site is available:

$ sudo a2ensite amazeapp.com
$ sudo service apache2 reload

Apache should now be doing its thing, so all that's left it to run our Node app:

$ node /path/to/your/app.js

However, if you're connected remotely via ssh your Node process is going to shut down when you log out. Therefore we need to make sure the process is run as a daemon:

$ node /path/to/your/app.js >/dev/null 2>&1 &

I'll admit I stumbled upon that command somewhere in Stack Overflow. I can't say it's the most correct way to do it, but it's essentially running the node process silently, outputting no errors, and pushing it to a background task. If we choose to logout at this point it should continue to run as long as there aren't any errors. I would recommend running Remy Sharp's nodemon, but that's your call.

If you've gotten this to work you're more than likely going to want to do it many more times. I created a gist just to refer to when I need to set up a new domain virtualhost and run a new background task.

References And Links

Tuts+ - Going Live With Node

Hosting A Node.js Site Through Apache

mod_proxy

mod_proxy_http

Detailed guide to mod_proxy in Ubuntu

Virtualhost setup along with daemon node task