Apache 2.2 With Load Balancing For Mongrel + Ruby on Rails
Tags: apache,
deployment,
load balancing,
mongrel,
php,
ruby on rails
Posted 23. November 2006.
You would like to set up a Mongrel cluster for your Rails application, running on top of Apache 2.2 with fancy load balancing enabled, but your Linux distro features only an older version of Apache?
This article will take you through compiling the software for yourself. Just make sure you have all the tools installed, such as a C-Compiler and GNU make. If you are unsure what to install, look for a meta-package called "Devel" or "Development", that will install all the necessary packages for you.
Build Apache
Start up a terminal window. Whenever you see the bordered grey box, it means you have to enter the text after the dollar sign in your terminal. If a line ends with a backslash, it means the command is continued on the next line.
Now download and untar Apache. Go to the Apache download page. It will automatically detect a mirror close to you. Scroll down until you find the link to the Unix source and copy the URL to your clip board.
We will now fetch the file with wget. This is just an example, you should paste the mirror URL from your clip board:
$ wget http://www.apache.org/dist/httpd/httpd-2.2.4.tar.gz $ tar xvzf httpd-2.2.4.tar.gz
This will create a directory named 'httpd-2.2.4'. Cd into that directory and configure:
$ cd httpd-2.2.4 $ export CFLAGS=-O2 $ ./configure \ --prefix=/usr/local/apache2 \ --with-included-apr \ --with-mpm=worker \ --enable-modules=all \ --enable-mods-shared=all \ --enable-ssl=shared \ --enable-proxy=shared \ --enable-proxy-http=shared \ --enable-proxy-balancer=shared
The CFLAGS variable tells the C-Compiler to use a second level optimization.
The second command will configure Apache to have all features enabled as shared modules, including load balancing and SSL. For some more in depth information, take a look at the official Apache installation instructions.
If all goes well, run make and make install to put Apache in place:
This will install Apache under /usr/local/apache2, so it doesn't conflict with your distro's Apache installation. The config files can be found in /usr/local/apache2/conf.
All compiled modules are enabled by default. This is a whole lot, since we configured '--enable-modules=all'. If you like, open /usr/local/apache2/conf/httpd.conf with your favorite text editor and comment out all unneeded modules by placing a '#' before the 'LoadModule ...' line. Skip this step, if you have absolutely no idea what you are doing.
If you later get an error message when starting Apache or something doesn't work as expected, check /usr/local/apache2/logs/error_log for hints. You might have disabled something vital.
After your made changes to your httpd.conf, restart apache like this:
If you get an error message such as stating that Apache is already running on that port, you need to stop your old Apache server. Try '/usr/sbin/apache2ctl stop' or even better: Use your runlevel editor to let the Apache startup script point to your compiled version of Apache so it gets started after a reboot.
To find out how to run a Rails Mongrel cluster on your brand new shiny Apache server, go over to Coda Hal's tutorial.
Optional: Build PHP 5
Most people still need PHP besides Ruby on Rails. Your distro's packaged PHP will probably brake with the new Apache server. Here's how to build yourself a new one:
Visit the PHP Download page. It will detect a mirror site close to you. Right-click on the mirror download link and choose "Copy link" (or something like this, depending on your browser) from the context menu.
In your terminal type 'wget' and paste the link from your clipboard like this:
Then untar and cd into the directory:
$ cd php-5.2.1
Next run the configure script. As you can see, I have turned on many common options like support for MySQL, SQLite and SSL. The '--with-apxs2' directive states where your Apache is installed to copy the PHP module to. The line 'LoadModule php5_module modules/libphp5.so' will also automatically be appended to your httpd.conf.
To get more information about configuration options, visit the PHP documentation on that topic.
$ ./configure \
--prefix=/usr/local/php \
--with-apxs2=/usr/local/apache2/bin/apxs \
--disable-ipv6 \
--enable-sockets \
--enable-soap \
--with-pcre-regex \
--with-mysql \
--with-zlib \
--with-gettext \
--with-sqlite \
--enable-sqlite-utf8 \
--with-openssl \
--with-mcrypt \
--with-ncurses \
--with-jpeg-dir=/usr \
--with-gd \
--with-ctype \
--enable-mbstring \
--with-curl
If this all worked out for you, build and install like this:
Then again restart Apache:
To check your PHP installation, drop a file named 'index.php' in your document root, containing something like this:
All done! Now go get a cold drink and enjoy. You have just compiled your own Apache server, along with all shipped modules and PHP.
By the way: You can adjust PHP settings in the ini file located here: /usr/local/php/lib/php.ini.
Edit: The PHP website recommends not to use Apache 2 with multithreading support together with PHP. I have not had any problems with my PHP applications so far – this weblog runs on Textpattern with Apache 2 Worker and PHP 5. However, if you plan to use Apache 2 with PHP mostly and stability is more important than performance, you should disable the Apache worker MPM by changing '--with-mpm=worker' to '--with-mpm=prefork' in Apache's configure options. If you have any new insights about this matter, please drop a comment!
Edit: Added --with-jpeg-dir=/usr to the PHP configure options, because on some systems configure is not able to figure out the standard path itself.
Edit: Updated this article for Apache 2.2.4.
Subscribe to this Feed
Social bookmark
Comments
Niko said 511 days ago:
James: Thank you for your interest. That’s a topic worth writing about and I definitly will cover that soon, however I would not be able to compare Rails with J2EE since I never used the latter.
jc said 478 days ago:
Damn. Tried these exact instructions and the apache modules didnt get loaded at all. I had to manually go in and enable each module one by one. Bummer
Anyone know what gives? My dedicated host came with Fedora Core 6 on it.
Niko said 477 days ago:
jc: Maybe the Apache that came with Fedora is conflicting with your new installation? If you installed from source, you should find all modules enabled in /usr/local/apache2/conf/httpd.conf by default. You can query what modules are loaded like this:
/usr/local/apache2/bin/apachectl -t -D DUMP_MODULES