Proxy Node.js Application Requests through Apache

Running a Node.js application along side other non node web applications can work well if configured correctly. This tutorial will show you how to easily configure a Virtual Host to proxy Node.js application requests through Apache.


First, we need to make sure that we have Node.js running on a port other than the port Apache is running on (usually port 80). Our goal is to run the Node.js server on a port other than 80, and then use Apache to proxy incoming Node.js application requests to the port that the Node.js server is running on.


First, we need to make sure that Apache has the mod_proxy.so and mod_proxy_http.so modules loaded. If they are not loaded, go uncomment them in the Apache configuration file, and then restart the server.

Next, we need to configure an Apache Virtual Host just like we would any other VHost, except we want to proxy all domain requests for your Node.js application to the Node.js server.


**Note: The process is also the same if only a portion of an application is using Node.js.


Let’s take a look.

Apache Virtual Host for the Node.js Application



    ServerName www.yoursite.com
    ServerAlias yoursite.com
    DocumentRoot /your/site/location
    ErrorLog logs/yoursite.com-error_log
    CustomLog logs/yoursite.com-access_log common

    ProxyRequests off
    
        Order deny,allow
        Allow from all
    
    
        ProxyPass http://127.0.0.1:3000/
        ProxyPassReverse http://127.0.0.1:3000/
    




**Note: This configuration assumes that the node.js server is already running locally on port 3000, but it could be configured to use any unused port and/or an IP, that points to a system that is not the same as the one that Apache is running on.


Next, we need to tell Apache to proxy all node.js requests to node.js. This is done in our location directive with ProxyPass. All domain requests for www.yoursite.com will proxy to the node.js server running locally on port 3000.

Our ProxyPassReverse is used if/when our application performs a redirect, however its usually good practice to add even if your application isn’t redirecting.

For example, if our Virtual Host proxies incoming requests to our node.js application running at localhost on port 3000, and if our application performs a HTTP 301 or 302 redirect for one of those requests, there we be an error.

Our response headers will contain a location of http://127.0.0.1:3000/your/site/location/redirected-location when sent back to Apache.

And as you can tell, if Apache sends this location back to the client, then there will more than likely be an error.


ProxyPassReverse solves this by rewriting the location in our response headers so that Apache sends yoursite.com/redirected-location back to the client’s browser.


After that, save your Virtual Host configuration for your node.js application, and restart Apache.


And that’s it!

All incoming requests to yoursite.com should now be proxied to your node.js application.


Happy Coding! 😀

Phusion Passenger on Apache 2.4: ‘undefined symbol: unixd_config’

If you or your company has come across this error while trying to start Apache 2.4 with Passenger, don’t worry there’s an easy fix. Your Passenger module is compiled for the wrong version of Apache, usually 2.2. Apache 2.4 has many changes and unixd_config has been renamed to ap_unixd_config in version 2.4. The fix is to recompile the Passenger module against your correct Apache version.


Verify your APXS2 Environment Variable location
First you need to make sure your APXS2 Environment Variable is pointing to the correct Apache version path.

Usually located at


cd /etc/sbin
ls | less

apxs_f

Verify the apxs location with what’s set as APXS2 in your environment variables.


env

If the path is not correct, update it (only will be for this shell)

Update for your shell session


export APXS2=/location/to/apxs



Next locate where your Apache 2.2 mod_passenger.so module is located.

Usually located at


/usr/lib/ruby/gems/1.x/gems/passenger-4.0.x/buildout/apache2/mod_passenger.so

You can also verify where Apache is loading it from by checking your PuppetMaster Vhost configuration file.

Usually located in Apache’s conf.d directory


vim /etc/httpd/conf.d/puppetmaster.conf

After you have located it, rename your Apache 2.2 mod_passenger.so module to something else. You don’t want it to get overwritten in case you still want to use Passenger with your old Apache version.

Rename Apache 2.2 mod_passenger.so


cd /usr/lib/ruby/gems/1.x/gems/passenger-4.0.x/buildout/apache2
mv ./mod_passenger.so ./mod_passenger_httpd22.so



Now all that’s left is recompiling mod_passenger.so against Apache.2.4.

Recompile Passenger


passenger-install-apache2-module

passenger-install_f

Now follow the directions for the recompile.

After the recompile, verify the LoadModule path is correct in your PuppetMaster.conf VHost config file.

If it is, just start Apache and you should be good to go.

Have fun! 🙂

Installing HHVM with Apache 2.4 on CentOS 6.5 (64 bit)

This is for those of you who love being on the bleeding edge of technology like me. This will be a brief introduction to installing Apache 2.4 and Facebook’s HHVM (Hip Hop Virtual Machine) in order to increase web application performance.

Apache 2.4 is the newest iteration of Apache’s web server with multiple performance improvements and Facebook’s HHVM is the new kid on the block that brings massive performance gains to PHP. HHVM achieves this by compiling PHP into bytecode, which is then translated into machine code by HHVM’s JIT (Just in Time) compiler. When using HHVM, this also gives the ability to use Facebook’s new statically typed language called HACK.

HHVM also works with most of PHP’s popular frameworks. If you work with popular enterprise level frameworks like Symfony 2, this can be an amazing asset.


First we need to prepare the install process by adding a few repositories for yum to use. Since CentOS 6.5 (as of the time of this post) doesn’t contain Apache 2.4 in their base or epel repositories, we need to look for another option. We could compile from source, however Remi Collet and Jan Kaluza from Red Hat have created a RHEL repo httpd24-epel that contains Apache 2.4 that is perfect for our needs. This repo installs Apache 2.4 in /opt. View why here.


For HHVM we have the same couple of options. We could either build it from source, or we can use Naresh Kumar’s wonderful repo called the Hop 5 Repo.

Using these repos is the best choice since we can manage our Apache 2.4 and HHVM installs with our package manager and not have to worry about future problems.


— PREPARATION —

Go to the repo directory

cd /etc/yum.repos.d

Add the Remi Repo

sudo wget http://repos.fedorapeople.org/repos/jkaluza/httpd24/epel-httpd24.repo

Add the Hop 5 Repo

sudo wget http://www.hop5.in/yum/el6/hop5.repo


— INSTALL PROCESS —

Install Apache 2.4

sudo yum install httpd24-httpd httpd24-httpd-devel httpd24-mod_ssl

This will place Apache 2.4 under /opt.
See why here

Install HHVM

sudo yum install hhvm

hhvm_install_f


— CONFIGURATION PROCESS —

Configure Apache
This article uses a VHost and assumes you already know how to setup one for Apache, however a Virtual Host is not necessary for HHVM.

Edit httpd.conf

cd /opt/rh/httpd24/root/etc/conf
vim httpd.conf

Ensure this line is not commented out

IncludeOptional conf.d/*.conf

Configure a Virtual Host for Apache

Go to the Apache 2.4 config directory

cd /opt/rh/httpd24/root/etc/conf.d

Create a Virtual Host config file as root (if non existent)

vim httpd24-vhosts.conf

Add the following lines

<VirtualHost *:80>
  ServerName hhvmtest.dev
  DocumentRoot /var/www/html/hhvmtest
  ErrorLog logs/hhvmtest.dev-error_log
  CustomLog logs/hhvmtest.dev-access_log common

  # where HHVM is running   # use either Proxy Pass or ProxyPassMatch   # ProxyPass routes all traffic to FastCGI   ProxyPass / fcgi://127.0.0.1:9000/var/www/html/hhvmtest   # ProxyPassMatch regular expression routes only PHP files   ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://127.0.0.1:9000/var/www/html/hhvmtest/$1
  <Directory /var/www/html/hhvmtest> # relaxed permissions for testing     AllowOverride all     Options -MultiViews     Require all granted   <Directory> </VirtualHost>


— TESTING —

Start HHVM as a server in FastCGI mode in the background (run as root or sudo it)

/usr/bin/hhvm --mode server -vServer.Port=9000 -vServer.Type=fastcgi &

Verify HHVM is running and PID

ps aux | grep hhvm

hhvm-running

Start Apache

sudo service htttpd24-httpd start

Create Document Root

cd /opt/rh/httpd24/root/var/www/html
mkdir hhvmtest

Create index.php

cd /opt/rh/httpd24/root/var/www/html/hhvmtest
vim index.php

Add the following lines


 <?hh echo 'test ';
  $test = 2 + 2;
  echo  $test;
  echo phpinfo();

Create index.hh

vim index.hh

Add the following lines


 <?hh echo 'test ';
  $test = 2 + 2;
  echo  $test;
  echo phpinfo();


Now open your browser and go to

http://hhvmtest.dev/index.php

If everything goes correctly you should see the string

test 4 HipHop

Now open your browser and go to

http://hhvmtest.dev/index.hh

If everything goes correctly you should see the string

test 4 HipHop

When you output

phpinfo();

You should see the string 'HipHop'.

HHVM returns the string HipHop when you output phpinfo.



Also verify HHVM is working for php tags by changing in both index.php and index.hh

<?hh

to

<?php 


If you want to run HHVM on system boot. Either create an init bash script or just add the above HHVM line to rc.local

Go to rc.local

cd /etc/rc.local
vim rc.local

Add the following line to the bottom of rc.local

/usr/bin/hhvm --mode server -vServer.Port=9000 -vServer.Type=fastcgi &

That’s it enjoy! 😀