When a friend of mine upgraded to the latest Suse distro and along with that (unwillingly) to
Apache 2.2.x, an internal website written by me broke. After a short phone call it was clear that this was an Apache-configuration-problem, but that Apache was about 250 km away from my (current) home and behind the firewall of the University; so the only way to tell him which configuration to edit was
installing Apache 2.2 on my PowerBook running the Tiger.
I know there are several packages bringing you a whole
MAMP system to your Mac, but I wanted to have a standard-install of the Apache, so I decided to compile the latest version from source.
Note: I used a standard Mac OS X 10.4.6 environment with self-compiled MySQL. My Console is the Bash.
Compiling the Apache HTTP server 2.2.2
- Download the latest Apache source (Unix) from httpd.apache.org/download.cgi
- unpack the archive (simply by doubleclicking it in the Finder)
- open the Terminal and navigate to the unpacked folder named http-2.2.x
- configure the server; I used the following flags which install most modules shipped with Apache2 including mod_rewrite, which I desperately needed:
$ ./configure --enable-module=most --enable-shared=max --enable-rewrite=shared
- if there occur no errors install the Apache with $ make && sudo make install
That's it! The interesting stuff now lies here:
Apache-Root: /usr/local/apache2/
Apache-Bin: /usr/local/apache2/bin/httpd
Apache-Config: /usr/local/apache2/conf/httpd.conf
Since this installs the Apache2 beneath the already installed Apache 1.3 (shipped with every Mac OS X), I decided I want to control the Apple-Apache from the SystemPrefs > Sharing and the newly installed Apache2 from the Terminal (Bash). So I
prepended the path to the httpd to my existing PATH-variable.
$ PATH=/usr/local/apache2/bin:$PATH
$ export PATH
Ajdusting httpd.conf
After that, I slightly modified the
httpd.conf. The first line tells the Apache2 to use the same root-directory as the standard Apache, so my websites can stay where they are and everything should run like it always has. Second line tells him to listen to port 8080 and third line enables my beloved mod_rewrite. The last three lines set the Script-dir to the same one used by the default Apache. (Do not forget to change the
<Directory>–tags to the appropriate paths as in line 1 and 5)
1: DocumentRoot /Library/WebServer/Documents
2: Listen 8080
3: LoadModule rewrite_module modules/mod_rewrite.so
4: <IfModule alias_module>
5: ScriptAlias /cgi/ "/Library/WebServer/CGI-Executables/"
6: </IfModule>
Then check the config and start your new Apache if no error occurs:
$ sudo apachectl configtest
$ sudo apachectl start
That was it and I was happy to see that all my Perl-websites (including those using MySQL) were still up and running. And after restarting my PowerBook, I realized the server has started automagically; looks like it saves its state across reboots and you do not need to install any Login-Items.
That's all great,
but two problems emerged...
- PHP code does not work; see below
- some .htaccess files became illegal. That is a minor problem though; I had to delete all entries specifying an error-document and: Problem solved
The issue with PHP is that
the PHP-module from the old Apache cannot be used by Apache 2, so on this occasion, I decided to finally upgrade to PHP 5.x by compiling it from source...
Compiling PHP 5 from source
- Download latest PHP 5 source from www.php.net/downloads.php
- unpack the downloaded archive (easiest is doubleclicking it in the Finder)
- configure PHP like so:
$ ./configure --quiet --with-apxs2=/usr/local/apache2/bin/apxs --with-mysql=/usr/local/mysql --with-zlib-dir=/usr/local/lib --enable-mbstring --with-gd --with-xml
Note that for --with-gd to work, you have to install zlib and libpng as written here: paginar.net. If you do not want to do this, just omit the --with-zlib-dir and --with-gd part and PHP 5 will compile just fine.
Note 2: The supplied Apache2- and MySQL-paths above are the paths to the Apache2- and MySQL-root-directory, not the path to the executable!)
Note 3: If you are on an Intel-Mac, you need to get a configurable version of libpng (as of version 1.2.12) which can be downloaded here. You then need to compile libpng with ./configure --prefix=/usr/local && make && sudo make install. And be sure to read the note about compiling GD on Intel-Macs on the paginar.net website, compiling GD else will fail!)
- if all went well, finish it with $ make && sudo make install
One last edit to the
httpd.conf and your new Apache2 is ready to run PHP-code. That is, load the PHP module:
LoadModule php5_module modules/libphp5.so
AddType application/x-httpd-php .php .phtml
AddType application/x-httpd-php-source .phps
Restart the Apache:
$ sudo apachectl graceful
Edit: Create a new launchd item to start your new Apache at boot
Well, soon (after publishing this article) I discovered that on a reboot, the built-in Apache will be launched instead of my new Apache 2.2, even if I disabled WebSharing in the System Preferences.
The solution is to disable the original Startup-Item that starts the built-in Apache and create a new launchd-item which tells launchd to start your new Apache.
- disable the Apache Startup-Item by setting WEBSERVER=-NO- (from -YES-) within /etc/hostconfig
- create a new launchd-item with Lingon (or another similiar application; this one is Open Source, though, and does a great job) as a user-daemon, or
use mine and put it into /Library/LaunchDaemons/
- reboot
Here is what my launchd-plist looks like. Note that I gave the server a nice-value of 10 since I dont want the Apache to lock up my computer; I only use my machine for local testing, not real serving.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>org.apache2.httpd</string>
<key>Nice</key>
<integer>10</integer>
<key>ProgramArguments</key>
<array>
<string>/usr/local/apache2/bin/apachectl</string>
<string>start</string>
</array>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
Rafael: Thank you very much, I was having a huge headache to solve the very same problem!