Ubuntu 8.04LTS (Hardy) Installing Apache2 as a Backend For Nginx
Previously we installed Nginx to proxy all our dynamic requests to Apache2 only we haven’t installed it yet. In this article will setup Apache2 and configure it to handle proxy requests from Nginx.
Installing Apache2
First we’ll install the Apache2 package as well as a few other essential packages from the Ubuntu repositories.
sudo aptitude install apache2 apache2.2-common apache2-mpm-prefork apache2-utils libexpat1 ssl-cert
You may have noticed Apache complaining that it can’t determine the server’s fully qualified domain name. Let’s open up the apache2.conf and fix this.
sudo /etc/apache2/apache2.conf
Now add your hostname or a FQDN (fully qualified domain name) to the bottom the configuration file. Next time we restart Apache we should no longer see this warning.
ServerName yourhostname
In the previous article setting up Nginx we used port 7000 as the port we will be proxying request to. So we’ll need to open up Apache’s ports.conf and change the listening port to match the Nginx setting.
sudo vim /etc/apache2/ports.conf
And now change the Listen setting to 7000
Listen 7000
Now that we’ve made all our changes Let’s fire up the server with the following command.
sudo /etc/init.d/apache2 restart
We can check to make sure Apache is serving pages by running the following command
curl -I http://your.server.ip.address:7000
You should see output similar to if everything went as planned. As you can see below the server is listed as Apache/2.2.8. Yay!
HTTP/1.1 200 OK
Date: Sat, 01 Aug 2009 18:16:27 GMT
Server: Apache/2.2.8 (Ubuntu)
Last-Modified: Sat, 01 Aug 2009 18:02:32 GMT
ETag: "120311-2d-47018573d3e00"
Accept-Ranges: bytes
Content-Length: 45
Content-Type: text/html
Configuring Apache to handle proxy requests.
Now that apache is all setup, let’s go back to our apache2.conf and make the following changes.
# The Timeout setting starts our way to high (300 seconds), We should change this to a much more
reasonable number.
Timeout 20
# We're proxying our request to Apache so we don't want KeepAlive turned on.
KeepAlive Off
# We'll optimize our settings for 256MB of available memory. Again
# you can optimize this for you're own particular needs, I've chosen to start only
# One server with a max of 3. Each with a max of 50 clients and 4000 requests per child.
<IfModule mpm_prefork_module>
StartServers 1
MinSpareServers 1
MaxSpareServers 3
MaxClients 50
MaxRequestsPerChild 4000
</IfModule>
# Let's change our DefaultType MIME settings to match our nginx configuration.
DefaultType application/octet-stream
# We don't need people knowing too much about our setup so let's change our SeverTokens
# to a Production level
ServerTokens Prod
Installing mod_rpaf.
Sweet! We’ve just about added everything we need to at this point. However there’s one thing left to do and that’s install mod_rpaf. This will allow our applications to determine the original IP address before our request is passed from Nginx. If we don’t set this up all requests will appear to have come from our localhost 127.0.0.1, which technically is correct because Nginx made them locally, but we need to know who told Nginx to make that request in the first place. And that’s what mod_rpaf is for. Let’s install it now.
sudo aptitude install libapache2-mod-rpaf
And later when we want to use it we’ll need to add the following to our vhosts settings
RPAFenable On
RPAFsethostname On
RPAFproxy_ips 127.0.0.1
Now that we have Apache all setup and configured to handle incoming requests from nginx we’ll have to setup some kind of mechanism to process those requests. I personally love building applications using Django. So in the next article we’ll setup mod_wsgi to handle incoming python requests and a virtual host pointing to a Django application.