Web Apps with Tiger: Getting Started
Pages: 1, 2
Enabling PHP
Now that we know where our configuration files are, the first thing to fiddle with is enabling PHP, an easy-to-learn web programming language that most of our forthcoming applications are coded in. Open your favorite text editor (BBEdit, nano, etc.), and load /etc/httpd/httpd.conf. You can read a great primer on using nano (and sudo to authenticate) with O'Reilly's An Introduction to the Tiger Terminal. BBEdit will take care of the authentication required when we save our changes.
The Apache configuration file is heavily commented and contains sane defaults for pretty much anything you'd want to do. To change these defaults or, in this case, to enable PHP, we'll simply search for a string matching what we're interested in. Tell your text editor to search for php, and the first and second matches should be:
#LoadModule php4_module libexec/httpd/libphp4.so
#AddModule mod_php4.c
These two lines tell Apache to load the module that enables PHP--here they are commented out (i.e. prefixed with the # character). Uncomment these two lines (remove the # character but don't touch anything else) and save your changes. Changes to these configuration files take effect the next time Apache has restarted. I'll show you how to do that below but first, let's see what else we find in our php search:
<IfModule mod_php4.c>
# If php is turned on, we respect .php and .phps files.
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
# Since most users will want index.php to work we
# also automatically enable index.php
<IfModule mod_dir.c>
DirectoryIndex index.html index.php
</IfModule>
</IfModule>
This block of configuration only comes into play if the PHP module has been enabled (IfModule...). They tell us that PHP will be interpreted in any file that has an extension of php, and that a source file ends in phps. In addition, index.php should be treated as a default page when nothing more specific is requested and index.html doesn't exist. This is just fine; save your configuration.
Restarting Apache
We've now got to restart Apache so that it can enact our configuration changes. One way is just to toggle the Stop button for the Personal Web Sharing service in the Sharing System Preference, and then click Start again. Unfortunately, if we've made a mistake in our configuration edits, the GUI won't tell us, instead opting to endlessly try unsuccessfully. A far more reliable way is through the apachectl shell utility shipped with Apache. Hop into your Terminal and type apachectl configtest; you should see something like:
morbus@MoistTowelette:~ > apachectl configtest
Processing config directory: /private/etc/httpd/users/*.conf
Processing config file: /private/etc/httpd/users/morbus.conf
Syntax OK
This is just perfect. On the other hand, if we made a mistake--say, deleting one character too many on the LoadModule line--we'd see a stern rebuking of our mistake with line number:
morbus@MoistTowelette:~ > apachectl configtest
Syntax error on line 240 of /etc/httpd/httpd.conf:
Invalid command 'oadModule', perhaps mis-spelled or defined
by a module not included in the server configuration
Get into the habit of running apachectl configtest after any configuration change; as long as you see Syntax OK, you're cleared for restarting. We'll use apachectl again, this time prefixed with sudo to authenticate as the root user, and with the restart parameter:
morbus@MoistTowelette:~ > sudo apachectl restart
Password:
/usr/sbin/apachectl restart: httpd restarted
restart stops the Apache server if it is currently running, and then starts it immediately. It is, oddly, equivalent to running sudo apachectl stop and sudo apachectl start. If you don't see any errors (common ones include a configuration typo or forgetting to use sudo), then Apache is now running with your config changes.
Checking for PHP
You can confirm that PHP has been enabled with your error_log. Whenever you enable a third-party module (such as PHP), Apache will happily add that information into the Server header of every request it serves (though it has become common practice by most Linux distros to "obscure" the header with ServerTokens in an attempt at "security"). It also logs this information in the error_log. Use tail to check out the latest information logged there, where you should see something along these lines (shortened for brevity):
morbus@MoistTowelette:~ > tail /var/log/httpd/error_log
[notice] Apache/1.3.33 (Darwin) configured -- resuming normal operations
[notice] Accept mutex: flock (Default: flock)
[notice] SIGHUP received. Attempting to restart
Processing config directory: /private/etc/httpd/users/*.conf
Processing config file: /private/etc/httpd/users/morbus.conf
[notice] Apache/1.3.33 (Darwin) PHP/4.3.11 configured
-- resuming normal operations
[notice] Accept mutex: flock (Default: flock)
Take a look at the two lines that start with Apache/1.3.33. The first one is before your configuration change; the second, with the addition of PHP/4.3.11, confirms that the PHP module has loaded. Depending on your activity, you may not see both entries in the last 10 lines of tail. Try grep notice /var/log/httpd/error_log, or the tailDash widget can F12 your way to logfile monitoring.
Oftentimes, however, simply enabling a module in the configuration file doesn't mean it has been configured properly. Thankfully, with PHP it's easy to test; simply create an index.php file with the following contents and save it into /Library/WebServer/Documents/:
<?php phpinfo(); ?>
Load up http://127.0.0.1/ and, if everything is roses, you should see a rather large page full of PHP debugging information: what it was compiled with, what settings are currently enabled, and blah, blah, blah. An awful lot of stuff here, and only a wee bit you'll need to care about.
Caring Is for Next Time
Unfortunately, this concludes our whirlwind start of Apache web serving under Tiger. As mentioned, I've skimped on the hand-holding so that those with a firm foothold from my previous articles, or their own spelunking, won't wander off in a daydream.
Next time, we'll be focusing on tweaking PHP to be more secure, and installing and configuring the MySQL database server. With that scaffolding in place, we'll start looking at some of the best-of-breed applications that have made LAMP (Linux + Apache + MySQL + PHP/Perl/Python) the preferred architecture of many a web developer. If you have suggestions for where you'd like this series to take you, wax poetic in the comments below.
Kevin Hemenway is the coauthor of Mac OS X Hacks, author of Spidering Hacks, and the alter ego of the pervasively strange Morbus Iff, creator of disobey.com, which bills itself as "content for the discontented."
Return to the Mac DevCenter
You must be logged in to the O'Reilly Network to post a talkback.
Showing messages 1 through 10 of 10.
-
Web Apps with Tiger: Getting Started: Enabling PHP: Checking PHP
2007-04-07 03:22:22 rupertreid [Reply | View]
-
Method to install PHP
2007-03-08 20:43:51 jimmech [Reply | View]
Long ago I installed the Complete Apache2 binary package from Server Logistics to run my web site. Now I want to start using PHP and MySQL with it, but I can't quite figure out the best way to install them. If I try to install the packages from Fink or Darwin Ports, there seem to be weird dependency issues, plus I'm not sure they'd be able to find the non-standard installation of Apache 2 (i.e. in ~/Library/Apache2.
If I download the Complete packages from Server Logistics for PHP and MySQL, I will have outdated versions of each. Mark Linyage has binary versions, but they only use the Apache version that came installed with OSX. As you can see, none of these choices is ideal; I'd appreciate a recommendation as to the least hassle way to get these apps running without having to rip everything out! Thanks.
-
Serving a simple Python Script
2005-08-23 00:58:37 infield [Reply | View]
I my reading the people who use Python really love it. OS X ships with version 2.3.5 out of the box. How about something about setting up apache to serve a python script with an example to get us going.
-
Serving a simple Python Script
2005-09-21 10:24:05 b_hanna [Reply | View]
Python is cool :p. The setup is basically the same as php, mind you, you may need to enable other cgi modules to get it to run. From then on just 'import cgi' and get cracking with FieldStorage() --> gives you all the parameters passed to your CGI script. I did a lot of tail /var/log/httpd/error_log but I got it working in the end. Just a little tip, make sure the script runs before you try to run it as cgi, I wasted about half an hour wrestling with the conf file only to find I had a typo in my script!!! :-( Oh well. A good test is to manually test the script by directly putting the parameters in the URL(i.e.localhost/mypycgi.cgi?name=Ronald%McDonald) and getting python to print out the parameters, from then on your in python land and can stop sweating about the server. -
Serving a simple Python Script
2005-09-21 10:24:02 b_hanna [Reply | View]
Python is cool :p. The setup is basically the same as php, mind you, you may need to enable other cgi modules to get it to run. From then on just 'import cgi' and get cracking with FieldStorage() --> gives you all the parameters passed to your CGI script. I did a lot of tail /var/log/httpd/error_log but I got it working in the end. Just a little tip, make sure the script runs before you try to run it as cgi, I wasted about half an hour wrestling with the conf file only to find I had a typo in my script!!! :-( Oh well. A good test is to manually test the script by directly putting the parameters in the URL(i.e.localhost/mypycgi.cgi?name=Ronald%McDonald) and getting python to print out the parameters, from then on your in python land and can stop sweating about the server.
-
Apache & Rendezvous
2005-08-20 18:03:07 ShaheenG [Reply | View]
Thanks for the aritcle, I was hoping you would touch on rendezvous and apache to 1. explain any usefullness the technology has in relations to apache and 2. to explain Tiger's new bug.
Anytime I enter in the web address for my server (Tiger using the shipped version of Apache) and don't put a slash after a folder name apache redirects my request to a .local address. As an example:
testserver.com/folder
Will be translated into:
testserver.local/folder/
Which does work for users outside of my computer. Any thoughts? Seems like a pretty dumb feature. -
Apache & Rendezvous
2005-08-22 15:20:32 Kevin Hemenway |
[Reply | View]
This is a FAQ:
http://httpd.apache.org/docs/1.3/misc/FAQ.html#set-servername
-
re: Rails
2005-08-17 07:17:43 dogzilla [Reply | View]
While I can understand why folks get excited over the Rails package, I've never been able to get very excited over the Ruby language itself. Personally, I much prefer PHP - I've never been one to hop onto the latest language bandwagon and PHP has been a solid performer for me for the last 5 years. So it's pretty much mandatory that I plug the Cake (http://cakephp.org/) framework - all the Rails goodness without the Ruby bits sticking in your teeth.
I'd also like to make a suggestion for some article topics: I'd love to see an article covering the compiling our own Apache and PHP, especially how to get some of the more esoteric mods and DSOs running, and also an article covering using PHP as a scripting language on it's own, both as a cli language and wrapped in something like Pashua (http://www.bluem.net/downloads/pashua_en/) .
-
Apache and Rails
2005-08-17 05:07:14 batesc [Reply | View]
Good to see this series moving on rather than just re-working the previous two editions. You asked for ideas - How about picking up on the current Ruby / Rails buzz by covering how to get your Rails apps served up through Apache rather than the demo-level WebBrick server that comes in the Rails package?






I followed the tutorial and enable PHP by removing the # in the httpd.conf file, to enable php, using terminal. See extract below.
I tested the file using the apachetl configtest command and terminal returned syntax OK:
isinglive:~ rosariodelaguardia$ apachectl configtest
Processing config directory: /private/etc/httpd/users/*.conf
Processing config file: /private/etc/httpd/users/+entropy-php.conf
Processing config file: /private/etc/httpd/users/rosariodelaguardia.conf
Syntax OK
So far so good! I then tried to restart Apache using the restart command below. This time terminal returned an error. see extract below.
isinglive:~ rosariodelaguardia$ sudo apachectl restart
Password:
/usr/sbin/apachectl restart: httpd not running, trying to start
Processing config directory: /private/etc/httpd/users/*.conf
Processing config file: /private/etc/httpd/users/+entropy-php.conf
Processing config file: /private/etc/httpd/users/rosariodelaguardia.conf
/usr/sbin/apachectl: line 193: 275 Bus error $HTTPD
/usr/sbin/apachectl restart: httpd could not be started
isinglive:~ rosariodelaguardia$
Questions:
1. Can you advise why there is an error at line 193?
2. How can I correct it to make apache serve .php files.
3. Could it be that I am running php 5 and not 4 as shown in the .conf file? I also tried changing the references to php "4" in the .conf file to "5" but this did not work either.
4. is it necessary to remove also the #s at the "if" statement. This is not clear in the tutorial.
5. Your tutorial was written in 2005. Could it be that OSX saince then has affected the processing of the .conf file
I would be grate ful for any help on this.
Krgds Rupert