Learn More About PHP And The Web Platform!

Struggling with your web server, or to scale your PHP application to meet growing demand?

Whether you're running one server or a whole server farm; whether you're hosting on Windows Server or on Linux.

Learn from Stuart's experience with system design, delivery, support and management to help you do a better job and have an easier time.

Beneath Whitby breakwater

On Planet PHP, I see a lot of postings about PHP the language, and applications written in PHP, but not a lot about the runtime environment that PHP needs and can take advantage of. Is anyone interested in a series of posts about server architectures – not just LAMP vs WIMP vs WISP (Linux/Apache/MySQL/PHP, Windows/IIS/MySQL/PHP, and Windows/IIS/SQL Server/PHP) – but also about shared hosting vs dedicated hosting vs server farms vs active-active, active-passive clusters?

If you are interested, leave a comment on my blog to let me know. If there’s enough interest, I’ll write up a few articles about this and publish them on my blog. If there’s anything in particular you’d like to see covered, and explained in more detail, let me know!

(One of the things I do is design hosting solutions for our customers. Not the mega-solutions that folks like Rasmus gets to work on, but the smaller – and way more common – solutions needed for small to medium-size websites here in the UK).

22 comments »

The end-of-life announcement for PHP 4 has prompted a lot of discussion about whether PHP 5 is worth adopting at all, and even whether or not PHP 5 has hurt PHP as a whole. The technical arguments have been debated (and, alas, that will continue no matter who says what) on the Net for some time now.

What I’m not seeing are the commercial arguments to move to PHP 5 (/me points an accusing finger at the folks from Zend, with whom this discussion has been had before). Businesses will only make the move when there is something to gain from the effort, when the rewards of switching are more than the cost of doing nothing. So let’s see if we can find some reasons to chivvy them along!

Here are the arguments I’ve been making for the last 3 years to pointy-haired bosses that many technical folks ultimately have to answer to. What commercial arguments have you tried in your workplace? Post the good ones that have worked in the comments below, or on the Net on your own blogs for us all to share.

— Stu
(more…)

Be the first to leave a comment »

… is this little labeling rule in the file contexts:

/usr/lib(64)?/httpd/modules/libphp5.so -- system_u:object_r:textrel_shlib_t:s0

Does this mean that RedHat is shipping a mod_php5 that isn’t compiled for relocatable code support? (See my earlier post on compiling PHP as relocatable code). If they are, according to this page by Ulrich Drepper (hey, doesn’t he work for RedHat? 🙂 ) then RedHat Enterprise Server 5’s copy of mod_php5 might be using more RAM per Apache process than necessary, which will impact scalability and capacity.

Be the first to leave a comment »

I’ve been playing around with RedHat’s newly released Enterprise Linux 5. This comes with PHP 5.1.6, which I’ve taken off my test box and replaced with a copy of PHP 4 built from source.

After installing PHP 4, the first time I tried to start up Apache, it failed with this error:

Cannot load modules/libphp4.so into server: modules/libphp4.so: cannot restore segment prot after reloc: Permission denied

This is an error message from SELinux (which is enabled by default on RHEL5). The error has been triggered because the PHP runtime code contains text relocations – a situation where the code inside the .so has to be writable in memory.

(The definitive place to look for information about text relocations and the risk they pose to the security of your server is this page from Ulrich Drepper).

The quick workaround is to run this command on your server:

chcon -t textrel_shlib_t modules/libphp4.so

This command tells SELinux to allow text relocations inside the libphp4.so Apache module. (Use the command “chcon -t usr_t modules/lib4php.so” to reverse the effect, if you need to).

There are a couple of serious downsides to this workaround. The first one is that it disables one of the protections that SELinux provides. It makes your server more vulnerable to security exploits targetted at the PHP runtime itself. The second problem is that your server uses more memory per Apache process, because each Apache process ends up with its own copy of PHP.

One fix is to recompile your copy of PHP 4 using the “–with-pic” configure option. This produces a copy of PHP that doesn’t contain text relocations. In theory (I haven’t tested this yet), it should also be able to handle more concurrent connections on a server before the server runs out of RAM. Using Apache’s ab benchmarking app, my results suggest that PHP 4 w/ “–with-pic” is about 0.7% slower – small enough not to matter for many folks.

There’s a second way to avoid text relocations – don’t compile with “–with-pic”, and use the prelink tool instead. This tool, run from the command-line, works out the text relocations once, and then writes the data back to the binaries and libraries. From my testing, this avoids the SELinux error, and performance-wise it’s about 0.1% faster than PHP w/ text relocations, and about 0.8% faster than PHP 4 w/ “–with-pic”. This approach should also bring the benefits of reduced memory usage too that go with the “–with-pic” approach, but I haven’t done any testing to confirm this.

Which approach is the best – PHP 4 w/ “–with-pic”, or using prelink? The downside of using prelink is that this approach is reported to be unsuitable for Linux installs configured to do address space randomisation. You also have to remember to re-run prelink everytime you upgrade or re-install PHP. Using PHP 4 w/ “–with-pic” avoids these issues.

Be the first to leave a comment »

Come And Work With PHP In The UK!

Posted by Stuart Herbert on March 1st, 2007 in News.

I work for Box UK, where we use PHP to craft the hugely successful Amaxus XML Content Management System, and where we use .NET to craft the double award winning clickdensity website usability toolkit. We have an impressive list of customers, and did I mention that one of our products is written in PHP? 🙂

We re expanding the team in our Cardiff city-centre office (right in the heart of Europe s youngest capital city). We re looking for exceptional people with a proven track record in designing, delivering, and managing web-based solutions to large organisations. If you re a project manager, software developer, web site designer with XSL experience, or a systems administrator, maybe we have the right opportunity for your next role.

Be the first to leave a comment »

PHP London Conference Is Packed Out

Posted by Stuart Herbert on February 23rd, 2007 in Conferences.

Mmm … the “sold out” sign on the PHP London Conference’s home page doesn’t do it justice. “Packed out” would be a much better description. There isn’t an empty seat in the house, and during the talks there are quite a few folks standing at the back or sitting on the floor.

It’s fantastic to see such a well-supported conference here in the UK. I don’t remember coming across anyone else from the UK at the PHP conferences I’ve been to overseas. Apart from Cal and Yair representing Zend, everyone else I’ve spoken to has been a developer. For all the attention that Rails has grabbed in the last twelve months, it’s great to see that interest in PHP if anything is stronger than it was a year ago.

If anyone’s interested in having a chat about the Why PHP? group, let me know. I’m sat on the back row just to the left of the main doors.

2 comments »

I’ll be making the bleary-eyed trek over from Cardiff on Friday morning (I’m more liking to be heading off to bed at 5am than getting out of one!) to the PHP Conference in London. If you’re going to the conference too, and you’d like to meet up afterwards (or over lunch) to talk about the Why PHP effort, please head on over to the group and let us know.

Be the first to leave a comment »

In my last post, I asked whether there was any interest in there being a resource that makes the business case for PHP. Many thanks to everyone who replied, especially David Goulden at Zend.

To turn this from an idea into reality, I’ve setup a Google group where anyone who is interested can join in, and help build this resource. Please come along with your ideas and concerns, and let’s see what we can achieve together.

I don’t know what other folks think, but I think the first step is to draw up a list of topics that the business case needs to cover. Run into a question from a customer that stops you selling your PHP solution? Let us know.

3 comments »

At work, we make and sell software written in a number of languages; our flagship product is written in PHP. During pre-sales, we’ve always had to handle some questions about our choice of PHP – normally from IT staff with a preference for Java or lately .NET – and we normally manage to convince the potential customer that PHP isn’t the bad choice that they’ve been led to believe.

But one of the unfortunate side-effects of Stefan Esser’s much-publicised (self publicised? 🙂 ) departure from the PHP Security Team has been an increase in the number of IT staff we’re coming across who “believe” both that open-source is inherently insecure, and that PHP in particular has incurable problems. These “beliefs” hurt ISVs trying to sell PHP-based applications into skeptical organisations.

Why isn’t there a central resource containing the answers to “Why PHP?” in a business-oriented way? Something that ISVs can refer their clients to, and it not only promotes the excellent advantages of PHP (and include success stories from vertical markets), but also include substantial rebuttals to the FUD that ISVs have to deal with during the pre-sales process.

I’m not surprised that PHP.net doesn’t contain such a resource (it’s not really the place for it, one could argue), but it’s disappointing to see that Zend doesn’t provide one. What’s good for ISVs should be good for Zend, after all, and this is an area where they could help all the ISVs that they want to sell their products to 🙂

Is there interest from other folks in having a resource like this? Or maybe working together to build such a resource?

21 comments »

I’ve recently switched my blog from b2evolution back to WordPress. The good news is both “no more spam :)” and “the admin panel works in Safari”, but on the downside I missed the multiblog feature that attracted me to b2evolution in the first place. There is WordPress MU, I suppose, but after coming across a few plugins that warned they didn’t work with WordPress MU, that option didn’t look very appealing.

Ah ha – thinks I – I can fake the multiblog by putting several different blogs on the site, and generating a homepage from the RSS feeds of the individual blogs. Should be simple enough, and it sounds like the perfect nail to hit with the SimpleXML hammer of PHP 5 🙂 Funnily enough, in work last week we were wondering whether you could use SimpleXML with XML namespaces (alas, we still use PHP 4 at work atm), so armed with the perfect excuse, I set to work.

Getting an RSS 2 feed into SimpleXML is trivial:

[code lang=”php”]
$feedUrl = ‘http://blog.stuartherbert.com/php/?feed=rss2’;
$rawFeed = file_get_contents($feedUrl);
$xml = new SimpleXmlElement($rawFeed);
[/code]

Extracting the information from the RSS ‘channel’ is equally trivial:

[code lang=”php”]
$channel[‘title’] = $xml->channel->title;
$channel[‘link’] = $xml->channel->link;
[/code]

… and so on. Getting at the individual articles starts off just as easy:

[code lang=”php”]
foreach ($xml->channel->item as $item)
{
$article = array();
$article[‘title’] = $item->title;
$article[‘link’] = $item->link;
}
[/code]

… but, if you’re relying on the very thin SimpleXML documentation on php.net, like me you’ll soon run into two problems.

Some of the elements in an item sit inside different XML namespaces. The only way to get at them is to use the children() method on a SimpleXMLElement:

[code lang=”php”]
$dc = $item->children(‘http://purl.org/dc/elements/1.1/’);
$article[‘creator’] = $dc->creator;
foreach ($dc->subject as $subject)
$article[‘subject’][] = $dc->subject;
[/code]

That’s a bit of a mouthful. It’s a bit of a shame that I can’t do this:

[code lang=”php”]
// The following does NOT work!
$article[‘creator’] = $article->dc->creator;
[/code]

… or some variation on that, but the design of XML namespaces makes that impractical. (The XML namespace is actually the URI; the ‘dc’ prefix in a tag like <dc:creator> is shorthand defined in the opening tag at the top of the XML document. Although it would look a bit odd, there’s nothing at all to stop someone defining the ‘dc’ component as ‘dublinCore’ instead if they wanted to).

Having to pass the full URI for a namespace into children() is not my idea of fun! It’d be much better if we could pass in a shorter string instead. The only way to safely do this is to define an array of shortcuts yourself:

[code lang=”php”]
// define the namespaces that we are interested in
$ns = array
(
‘content’ => ‘http://purl.org/rss/1.0/modules/content/’,
‘wfw’ => ‘http://wellformedweb.org/CommentAPI/’,
‘dc’ => ‘http://purl.org/dc/elements/1.1/’
);

// now we can get dublin core content with a lot less typing!
// we also only have to update the code in one place if the namespace URI changes
$dc = $item->children($ns[‘dc’]);
$article[‘creator’] = $dc->creator;
[/code]

You can get a list of the namespaces like this:

[code lang=”php”]
$ns = $xml->getNamespaces(true);
$dc = $item->children($ns[‘dc’]);
[/code]

… but that only works if the XML document defines the prefix ‘dc’ for the namespace ‘http://purl.org/dc/elements/1.1/’. You’ll have to decide for yourself whether it’s a risk worth taking or not.

That’s namespaces tamed, but we’re not quite home yet. The actual ‘content’ part of the article sits inside a CDATA section inside a ‘content’ namespace, and how to deal with CDATA is conspicuous by its absence in the SimpleXML docs (probably because older versions of SimpleXML simply threw CDATA sections away without asking you).

If you have a look at the source code for SimpleXML, test 004 shows how basic CDATA access works.

[code lang=”php”]
$content = $item->children($ns[‘content’]);
$article[‘content’] = (string) trim($content->encoded);
[/code]

With that, the final code to read a RSS 2 feed looks like this:

[code lang=”php”]
// define the namespaces that we are interested in
$ns = array
(
‘content’ => ‘http://purl.org/rss/1.0/modules/content/’,
‘wfw’ => ‘http://wellformedweb.org/CommentAPI/’,
‘dc’ => ‘http://purl.org/dc/elements/1.1/’
);

// obtain the articles in the feeds, and construct an array of articles

$articles = array();

// step 1: get the feed
$blog_url = ‘http://blog.stuartherbert.com/php/?feed=rss2’;

$rawFeed = file_get_contents($blog_url);
$xml = new SimpleXmlElement($rawFeed);

// step 2: extract the channel metadata

$channel = array();
$channel[‘title’] = $xml->channel->title;
$channel[‘link’] = $xml->channel->link;
$channel[‘description’] = $xml->channel->description;
$channel[‘pubDate’] = $xml->pubDate;
$channel[‘timestamp’] = strtotime($xml->pubDate);
$channel[‘generator’] = $xml->generator;
$channel[‘language’] = $xml->language;

// step 3: extract the articles

foreach ($xml->channel->item as $item)
{
$article = array();
$article[‘channel’] = $blog;
$article[‘title’] = $item->title;
$article[‘link’] = $item->link;
$article[‘comments’] = $item->comments;
$article[‘pubDate’] = $item->pubDate;
$article[‘timestamp’] = strtotime($item->pubDate);
$article[‘description’] = (string) trim($item->description);
$article[‘isPermaLink’] = $item->guid[‘isPermaLink’];

// get data held in namespaces
$content = $item->children($ns[‘content’]);
$dc = $item->children($ns[‘dc’]);
$wfw = $item->children($ns[‘wfw’]);

$article[‘creator’] = (string) $dc->creator;
foreach ($dc->subject as $subject)
$article[‘subject’][] = (string)$subject;

$article[‘content’] = (string)trim($content->encoded);
$article[‘commentRss’] = $wfw->commentRss;

// add this article to the list
$articles[$article[‘timestamp’]] = $article;
}

// at this point, $channel contains all the metadata about the RSS feed,
// and $articles contains an array of articles for us to repurpose
[/code]

Don’t forget to add error handling 🙂

I hope this example helps anyone else who needs to work with RSS 2 feeds, or who needs to know how to work with namespaces and CDATA with SimpleXML.

71 comments »
Page 18 of 18« First...10...1415161718

This Month

December 2017
M T W T F S S
« Jul    
 123
45678910
11121314151617
18192021222324
25262728293031