TweetFollow Us on Twitter

Enabling the Embedded: PHP

Volume Number: 19 (2003)
Issue Number: 11
Column Tag: Programming

UNTANGLING THE WEB

Enabling the Embedded: PHP

by Kevin Hemenway

We've covered CGI scripts, but how is PHP any better?

In the past five months, we've turned on our built-in Apache web server, fiddled with the quick and dirty Server Side Includes (SSI), configured CGI to run scripts written in languages like Perl, Python, and Ruby (see also Jim Menard's article on Ruby, MacTech, March 2003) and, in general, found that this high-falutin' web serving stuff ain't all that difficult. However, we've yet to launch into a peaceful meandering of PHP, one of the easiest and more popular programming languages available for web development.

In the first of assuredly many self-congratulatory, pointless, and cliched "personal milestone" articles, our amazing, exciting, over-the-top, and action packed sixth entry will... will... welp, it'll teach you about configuring and customizing PHP with your Apache web server. Quick, over there! Bearded lady! Vegas pole dancer! Scarily painted clown!

But First, The Who... Oh, Look! Historical References!

As most developers know, it's hard to talk about a language without getting into religious wars ("my recursively-named is better than your yet-another!"), personal dislikes ("whitespace?! I can't believe Python considers it syntax!"), and programming theory ("procedural is to OOP as 'that's odd' is to 'eureka'!"). Regardless, there is one clear advantage to using PHP under Apache: "forking" (or rather, the lack thereof).

See, anytime a CGI script is requested, Apache forks (or spawns) a new process to handle this request, and then executes the code within this newly created environment. This happens pretty quickly and without fuss or muss, but with one downside: every time a process is spawned, the interpreter (be it Perl, Python, Ruby, etc.) has to be reloaded into memory--nothing is remembered or cached from previous runs. The more and more times this happens (due to heavy incoming traffic, for example), the slower the web server will get: Apache will spend more time waiting for processes to finish than actually serving their results.

This doesn't happen with Apache's Server Side Includes. Since SSIs are built into the web server via a default module called mod_includes, everything is handled internally and no forking is required. This same approach is used with the mod_php module: by preloading PHP into each and every Apache process, you remove the need for the forking and interpreter loading overhead. The only downside is slightly larger memory, a trade-off worth taking.

Let's see how mod_php is configured within Apache.

Enabling The PHP Module Within Apache

As you'll see, configuring PHP under Apache is very similar to what we've seen in our previous articles--this stuff should be old hat to you by now. As we've been doing from the start, to find out more about a feature, we'll search for the keyword within the httpd.conf file. Our first matches for "PHP" are our familiar LoadModule and AddModule lines:

LoadModule php4_module        libexec/httpd/libphp4.so
AddModule mod_php4.c

Similar to our previous articles, these two uncommented lines (i.e., not prefaced with a # character) load the module located at /usr/libexec/httpd/libphp4.so into our Apache web server. You may notice that the module name doesn't match what we've grown to expect (mod_includes for Server Side Includes, mod_cgi for CGI scripts, etc.), as it's called libphp4.so instead. There's nothing special about this... everyone calls it mod_php regardless of what the actual file representation is.

Our next "PHP" search research should also look familiar:

#
# To use PHP files:
#
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps

Similar to Server Side Includes, these two lines tell files ending with a php or phps extension to become associated with the PHP module. You'll notice there's no AddHandler equivalent like with SSI or CGI; largely, that's because the "application" of the MIME-type instructs mod_php to become their "handler". It's sort of like Adobe Acrobat Reader handling .pdf files (which have a MIME-type of application/pdf). If you've already designed your entire site around .html files and don't feel like revamping your structure or redirecting old URLs, you can modify the first line like so:

AddType application/x-httpd-php .php .html

This will enable support for PHP code in both .php and .html files. Be careful not to go nuts with this: you don't want to add file extensions "just because". Whether you actually use PHP code within an .html file or not, mod_php will process it like you did, and that can unnecessarily slow down your server when your traffic starts getting heavier.

With that, we've run out of search results in our httpd.conf--PHP has already been configured for our use. But how do we know for sure, besides attempting to run some PHP code and introducing varying levels of user error? To find out which third-party modules are loaded into your web server, check Apache's error_log where, for every startup, the "server tokens" will be logged. These tokens reflect the server version number, what operating system it's running on, and information on which third-party modules have been loaded.

Since we've been searching for "PHP" throughout the httpd.conf, let's do the same with our error_log. Figure 1 shows the results of a grep PHP /var/log/httpd/error_log shell command, listing the server tokens each restart of our Apache server logged. grep is a great tool for quickly searching a file from the command line.


Figure 1: Grepping our error_log for Apache's server tokens.

Now that we know PHP is enabled, let's learn more by writing our first script.

Far More phpinfo() Then You've Ever Wanted

Since PHP has been enabled for any file that ends with .php, we're going to create a test.php file within our personal user directory (/Library/username/Sites/). The contents of this file will be simply:

<? phpinfo(); ?>

Any PHP code you write will need to be sandwiched within a starting and ending delimiter, <? and ?> in this example. You may also see and use starting delimiters like <?php (often recommended for greater portability over the shorter <?), and <?= (which can be used to quickly echo a variable or expression). With our delimiters in place, we'll use one of PHP's built-in functions, phpinfo(), to spit out gobs of information about our installation. Figure 2 shows partial output of http://127.0.0.1/~username/test.php:


Figure 2: The first page of phpinfo()'s many.

Obviously, there's a lot of stuff here, and a good portion of it won't be immediately (or even ever) useful, but there are a number of interesting things to discover. I'll touch briefly on a few of the more helpful entries below, but you can always find out more by searching through the online documentation at http://www.php.net/.

  • The first section we come across gives us the version number of PHP (which we previously saw in the output of our error_log), the time the module was built, and more importantly, the configuration line used to build it. This becomes helpful if we ever build our own version of the module, adding a new feature or tweaking an existing one. Also helpful is the configuration file path, which tells us where the php.ini file lives (or should live). The .ini file is similar to Apache's httpd.conf and allows us to tweak the runtime settings of the module.

  • Next up is a long list of configuration directives. asp_tags, off by default, allows you to use <% and %> as your delimiters, whereas short_open_tag controls whether you can use the <? we've already encountered. display_errors should ultimately be turned off when we're ready to use PHP on a production system, and any errors configured with error_reporting should be sent to an error_log instead (though we'll use log_errors to send them to our Apache error_log -- confused yet?). Whether incoming data will automatically be escaped for database use is magic_quotes_gpc's intent; since it's on by default, we'll have to be careful to stripslashes if we use the data elsewhere. Finally, register_globals will ensure that visitors or users can't easily pollute your namespace with GET or POST parameters, though some programs (most notably, the excellent osCommerce, http://www.oscommerce.com/) require it to be turned off.

With PHP certifiably enabled, let's configure it to log errors into our Apache error_log. Knowing about errors is always a very good thing, and by enabling all of them, we'll be able to write better scripts (similar to the Perl equivalents strict and warnings; see last column). To do this, we'll have to modify the php.ini file which lives in /usr/lib/.

Tweaking PHP's Initialization

There's one problem with editing this file: it doesn't exist. Since PHP is currently configured with all the standard and expected defaults, Apple never shipped a dummy php.ini file for us to modify. This isn't cataclysmic... most of the settings we care about can be modified in the relevant PHP file itself. Take, for example, the following two scripts:

script example #1:
<?php humiliation; ?>

script example #2:
<?php error_reporting(E_ALL); humiliation; ?>

They're combined output can be seen in Figure 3. As is obvious, there's an error in the code, but only when reporting is enabled would we actually be informed--meanwhile our script would trudge on regardless, perhaps getting deeper and deeper into a well of cascading problems. Further error configuration is possible by using the ini_set function to set relevant values, but who wants to worry about doing that for every .php file?


Figure 3: Our PHP script, with and without error reporting.

Our first task, then, is to create a /usr/lib/php.ini file. Thankfully, we don't have to build one from scratch, as the latest and greatest default version is available from CVS: http://cvs.php.net/co.php/php-src/php.ini-disthttp://cvs.php.net/co.php/php-src/php.ini-dist. If you're new to PHP or security-conscious (rightfully so), it may be better to blindly trust the more secure version, available from http://cvs.php.net/co.php/php-src/php.ini-recommended. Both are heavily commented and should be read fully to understand their ramifications.

For our purposes, we're going to use the php.ini-recommended file. It's always best to start with the most secured installation you can, then slowly open it up when you know what you're doing. The side effect of using this version is, happily enough, logging exactly how we want: none to the browser, everything to the error_log. Save the php.ini-recommended file to your Desktop, open a Terminal, and type the following:

cd Desktop
sudo cp php.ini-recommended /usr/lib/php.ini
sudo apachectl restart

Since /usr/lib/ is a protected directory, you'll need temporary super-user privileges to copy the new configuration into place. That's where sudo comes in. Likewise, since we're making a change to PHP, which is loaded as part of the Apache web server, we'll need to restart Apache with apachectl, which also assumes super-user privileges. Any time you make a change to httpd.conf or your new php.ini file, you'll need to restart Apache before the changes will take effect.

You can ensure your configuration changes are enabled by checking the output of phpinfo, as well as running the first of our script examples. The browser output will remain unchanged, but the error will be logged into Apache's error_log--see Figure 4.


Figure 4: With our new php.ini file, errors are logged to Apache's error_log.

With our new php.ini file working correctly, you'll want to keep in mind that register_globals is off (and you may have to turn it on for some applications to work properly), magic_quotes_gpc has been disabled, so that you'll need to addslashes if you plan on putting content into a database, and that included files and libraries can only exist in the current directory (although you can add more lookups via include_path, which previously had include /usr/lib/php).

Homework Malignments

In our next column, we'll take a look into databases, specifically the MySQL server, and how to integrate our PHP scripts with them. Because it's so easy to use PHP to hook into any database, you'll often be hard-pressed to find a script or application that doesn't require the existence of one. Also, with Panther available by the time you read this, we'll cover any relevant differences in your web serving capabilities. For now, students may contact the teacher at morbus@disobey.com.

  • I missed an opportunity to say "stick a fork in it; it's done".

  • If you're looking to pick up a book on developing sites with PHP, check out PHP and MySQL Web Development by Luke Welling and Laura Thomson. The Second Edition is available from Sams Publishing and covers programming in PHP, fiddling with your first database and SQL statements, and then combining the two together to create a number of different applications to learn practical techniques from.

  • You may have noticed that the PHP shipped with OS X is only 4.1.2, even though, at time of writing, 4.3.3 is available. If you absolutely must be using the latest version, be sure to check our Marc Liyanage's downloadable package available from http://www.entropy.ch/software/macosx/php/. It's full of features not enabled in Apple's version (compare http://www.entropy.ch/software/macosx/php/test.php to ours), and is easily removable for when you want to take a step back to the defaults.


Kevin Hemenway, coauthor of Mac OS X Hacks and Spidering Hacks, is better known as Morbus Iff, the creator of disobey.com, which bills itself as "content for the discontented." Publisher and developer of more home cooking than you could ever imagine (like the popular open-sourced aggregator AmphetaDesk, the best-kept gaming secret Gamegrene.com, the ever ignorable Nonsense Network, etc.), he's trying desperately to find time to work on his next book outline. Soon, he says, soon. Contact him at morbus@disobey.com.

 
AAPL
$105.22
Apple Inc.
+0.39
MSFT
$46.13
Microsoft Corpora
+1.11
GOOG
$539.78
Google Inc.
-4.20

MacTech Search:
Community Search:

Software Updates via MacUpdate

OS X Server 4.0 - For OS X 10.10 Yosemit...
Designed for OS X and iOS devices, OS X Server makes it easy to share files, schedule meetings, synchronize contacts, develop software, host your own website, publish wikis, configure Mac, iPhone,... Read more
TotalFinder 1.6.12 - Adds tabs, hotkeys,...
TotalFinder is a universally acclaimed navigational companion for your Mac. Enhance your Mac's Finder with features so smart and convenient, you won't believe you ever lived without them. Tab-based... Read more
BusyCal 2.6.3 - Powerful calendar app wi...
BusyCal is an award-winning desktop calendar that combines personal productivity features for individuals with powerful calendar sharing capabilities for families and workgroups. BusyCal's unique... Read more
calibre 2.7 - Complete e-library managem...
Calibre is a complete e-book library manager. Organize your collection, convert your books to multiple formats, and sync with all of your devices. Let Calibre be your multi-tasking digital... Read more
Skitch 2.7.3 - Take screenshots, annotat...
With Skitch, taking, annotating, and sharing screenshots or images is as fun as it is simple.Communicate and collaborate with images using Skitch and its intuitive, engaging drawing and annotating... Read more
Delicious Library 3.3.2 - Import, browse...
Delicious Library allows you to import, browse, and share all your books, movies, music, and video games with Delicious Library. Run your very own library from your home or office using our... Read more
Art Text 2.4.8 - Create high quality hea...
Art Text is an OS X application for creating high quality textual graphics, headings, logos, icons, Web site elements, and buttons. Thanks to multi-layer support, creating complex graphics is no... Read more
Live Interior 3D Pro 2.9.6 - Powerful an...
Live Interior 3D Pro is a powerful yet very intuitive interior designing application. View Video Tutorials It has every feature of Live Interior 3D Standard, plus some exclusive ones: Create multi... Read more
The Hit List 1.1.7 - Advanced reminder a...
The Hit List manages the daily chaos of your modern life. It's easy to learn - it's as easy as making lists. And it's powerful enough to let you plan, then forget, then act when the time is right.... Read more
jAlbum Pro 12.2.4 - Organize your digita...
jAlbum Pro has all the features you love in jAlbum, but comes with a commercial license. With jAlbum, you can create gorgeous custom photo galleries for the Web without writing a line of code!... Read more

Latest Forum Discussions

See All

Rami Ismail Opens Up distribute​() for D...
Rami Ismail Opens Up distribute​() for Developers Posted by Jessica Fisher on October 24th, 2014 [ permalink ] Rami Ismail, Chief Executive of Business and Development at indie game studio | Read more »
Great Hitman GO Goes on Sale and Gets Ne...
Great Hitman GO Goes on Sale and Gets New Update – Say That Three Times Fast Posted by Jessica Fisher on October 24th, 2014 [ permalink ] | Read more »
Rival Stars Basketball Review
Rival Stars Basketball Review By Jennifer Allen on October 24th, 2014 Our Rating: :: RESTRICTIVE BUT FUNUniversal App - Designed for iPhone and iPad Rival Stars Basketball is a fun mixture of basketball and card collecting but its... | Read more »
Rubicon Development Makes Over a Dozen o...
Rubicon Development Makes Over a Dozen of Their Games Free For This Weekend Only Posted by Jessica Fisher on October 24th, 2014 [ permalink ] | Read more »
I Am Dolphin Review
I Am Dolphin Review By Jennifer Allen on October 24th, 2014 Our Rating: :: NEARLY FIN-TASTICUniversal App - Designed for iPhone and iPad Swim around and eat nearly everything that moves in I Am Dolphin, a fun Ecco-ish kind of game... | Read more »
nPlayer looks to be the ultimate choice...
Developed by Newin Inc, nPlayer may seem like your standard video player – but is aiming to be the best in its field by providing high quality video play performance and support for a huge number of video formats and codecs. User reviews include... | Read more »
Fighting Fantasy: Caverns of the Snow Wi...
Fighting Fantasy: Caverns of the Snow Witch Review By Jennifer Allen on October 24th, 2014 Our Rating: :: CLASSY STORYTELLINGUniversal App - Designed for iPhone and iPad Fighting Fantasy: Caverns of the Snow Witch is a sterling... | Read more »
A Few Days Left (Games)
A Few Days Left 1.01 Device: iOS Universal Category: Games Price: $3.99, Version: 1.01 (iTunes) Description: Screenshots are in compliance to App Store's 4+ age rating! Please see App Preview for real game play! **Important: Make... | Read more »
Toca Boo (Education)
Toca Boo 1.0.2 Device: iOS Universal Category: Education Price: $2.99, Version: 1.0.2 (iTunes) Description: BOO! Did I scare you!? My name is Bonnie and my family loves to spook! Do you want to scare them back? Follow me and I'll... | Read more »
Intuon (Games)
Intuon 1.1 Device: iOS Universal Category: Games Price: $.99, Version: 1.1 (iTunes) Description: Join the battle with your intuition in a new hardcore game Intuon! How well do you trust your intuition? Can you find a needle in a... | Read more »

Price Scanner via MacPrices.net

Weekend sale: 13-inch 128GB MacBook Air for $...
Best Buy has the 2014 13-inch 1.4GHz 128GB MacBook Air on sale for $849.99, or $150 off MSRP, on their online store. Choose free home shipping or free local store pickup (if available). Price valid... Read more
Nimbus Note Cross=Platform Notes Utility
Nimbus Note will make sure you never forget or lose your valuable data again. Create and edit notes, save web pages, screenshots and any other type of data – and share it all with your friends and... Read more
NewerTech’s Snuglet Makes MagSafe 2 Power Con...
NewerTech has introduced the Snuglet, a precision-manufactured ring designed to sit inside your MagSafe 2 connector port, providing a more snug fit to prevent your power cable from unintentional... Read more
Apple Planning To Sacrifice Gross Margins To...
Digitimes Research’s Jim Hsiao says its analysts believe Apple is planning to sacrifice its gross margins to save its tablet business, which has recently fallen into decline. They project that Apple’... Read more
Who’s On Now? – First Instant-Connect Search...
It’s nighttime and your car has broken down on the side of the highway. You need a tow truck right away, so you open an app on your iPhone, search for the closest tow truck and send an instant... Read more
13-inch 2.5GHz MacBook Pro on sale for $949,...
Best Buy has the 13″ 2.5GHz MacBook Pro available for $949.99 on their online store. Choose free shipping or free instant local store pickup (if available). Their price is $150 off MSRP. Price is... Read more
Save up to $125 on Retina MacBook Pros
B&H Photo has the new 2014 13″ and 15″ Retina MacBook Pros on sale for up to $125 off MSRP. Shipping is free, and B&H charges NY sales tax only. They’ll also include free copies of Parallels... Read more
Apple refurbished Time Capsules available sta...
The Apple Store has certified refurbished Time Capsules available for up to $60 off MSRP. Apple’s one-year warranty is included with each Time Capsule, and shipping is free: - 2TB Time Capsule: $255... Read more
Textilus New Word, Notes and PDF Processor fo...
Textilus is new word-crunching, notes, and PDF processor designed exclusively for the iPad. I haven’t had time to thoroughly check it out yet, but it looks great and early reviews are positive.... Read more
WD My Passport Pro Bus-Powered Thunderbolt RA...
WD’s My Passport Pro RAID solution is powered by an integrated Thunderbolt cable for true portability and speeds as high as 233 MB/s. HighlightsOverviewSpecifications Transfer, Back Up And Edit In... Read more

Jobs Board

*Apple* Solutions Consultant - Apple Inc. (U...
…important role that the ASC serves is that of providing an excellent Apple Customer Experience. Responsibilities include: * Promoting Apple products and solutions Read more
Senior Event Manager, *Apple* Retail Market...
…This senior level position is responsible for leading and imagining the Apple Retail Team's global event strategy. Delivering an overarching brand story; in-store, Read more
*Apple* Solutions Consultant (ASC) - Apple (...
**Job Summary** The ASC is an Apple employee who serves as an Apple brand ambassador and influencer in a Reseller's store. The ASC's role is to grow Apple Read more
Project Manager / Business Analyst, WW *Appl...
…a senior project manager / business analyst to work within our Worldwide Apple Fulfillment Operations and the Business Process Re-engineering team. This role will work Read more
*Apple* Retail - Multiple Positions (US) - A...
Job Description: Sales Specialist - Retail Customer Service and Sales Transform Apple Store visitors into loyal Apple customers. When customers enter the store, Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.