Optimizing PHP, Revisited.

I wrote an article a while back on PHP optimization, but it was pretty lacking in most aspects, probably because I’m a lazy poster. I’ve revisited that article and reposted to hopfully have it be a little more helpful on the area.

“My website is slow”

Yes, we’ve already established that. And while your visitors are waiting for it to load, they are probably Mapquest-ing your house and buying a shovel at their local hardware store. No one cares how hot your website is if they have to wait an eternity to see it.

Before we continue on this topic, it’s important for you to understand how PHP works. PHP is an HTML-embedded scripting language that is executed server-side, therefore it generates dynamic content more quickly and efficiently than most other programming languages. The obvous conclusion is that the server your site is hosted on needs to provide a good PHP environment, and your site needs to be optimized to take advantage of that.

FYI, One of my other posts demonstrates a code addition that you can implement into your website to display how long a page takes to generate. This can be helpful in relializing exactly how long your site takes to load!

Setting the Server Environment

This section will probably not have much relevance to websites that are hosted on shared or free hosting plans, as most hosting companies shy away from making server-side changes. You may choose to skip this section and go straight to the programming concepts, my feelings won’t be hurt.

Your Hosting

If you are serious about your website and want the best performance possible, you should probably look into a v-dedicated hosted environment. The benefits of this is that 1) you are on your own server, so other websites will not affect yours, and 2) you can usually choose your own server configuration. If you need a quick and easy solution, InMotion Hosting offers a nice selection of v-dedicated hosting plans.

If VPS hosting is not an option to you yet, you can ask your host if they have any shared servers based on the configuration you need. There’s also the option of hosting your own website, but we won’t go into that here.

MySQL/PHP Versions

My website is currently running on CentOS 4, Apache 1.3.37, PHP 5.2.1, and MySQL 5. This does well for my website, but you’ll want to make sure that whatever combination you choose will work well together. I would say that PHP 5.2.1 and MySQL 5 are the best combination to use as long as your website software is compatible. If not, the alternative PHP 4.4.6 and MySQL 4.1 should be just fine. Keep in mind that as far as MySQL and PHP are concerned, newer is usually better.

PHP Accelerators

Hands down, a Zend/eAccelerator platform is the best way to go. Zend Optimizer is the most popular and stable (and free!) PHP acceleration agent there is, and eAccelerator compliments it nicely with its advanced caching and processing engine. Since I installed both on my server, I’ve noticed a considerably faster loading time in my website.

Zend can be installed easily by it’s autoinstaller, but if you need help installing eAccelerator, have a quick look at Shelby’s walkthrough.

Optimizing your Website

This section mainly deals with small modifications you can make to your site to help it load faster. I’m not going to go too much into programming here, but there are a lot resources online that can help you out in that department.

Keep Your Stuff up to Date

If you’re using bundled software like a CMS, blog, Gallery, etc., software developers are constantly making updates and improvements to up the speed and security of their programs. If you’re not willing to keep up with the newest releases, don’t expect peak performance of your site.

Caching

You know how when you go to a website a few minutes after you’ve already been there it seems to have loaded a ton faster? That’s because there are two caching agents in place — your ISP has cached that website into their nameservers to avoid having to do another external lookup, and your browser cached the website on your computer to avoid having to look it up, period. If you implement the same concept into your website, the logical conclusion is that the server will serve your pages more efficiently. The benefit of caching is that is generally uses less resources since the database isn’t being utilized as much, which cuts down on resources. Some bundled software will have an option or third-party plugin that can easily set up caching on your site. You can also use something like mod_cache for Apache to accomplish similar results.

What is all this junk?

Do you really find it necessary to bog your website down with ads and useless plugins? You’re really not doing your site (or your visitors for that matter) a favor by loading your site with a lot of junk. I’m not saying to stay away from plugins and ads altogether (being that my site uses both of all of the above), but to use them in moderation and be aware of the ones that suck. Honestly, there are a lot of really nice additions to some of my favorite software, but some of them can cause your site to load a lot more slowly.

Images

Lastly, most novice web designers tend to make this mistake when adding images to their sites. Since digital media has become more popular, it’s become a lot easier just to upload images straight from your camera onto the web. Most digital cameras nowadays take multi-pixel snapshots, which can amount to pictures that are several megs in size. What most beginner programmers don’t know is that when you upload an image this size and then just resize it with HTML <img> tags, it does NOT change the actual MB size of the image itself. Resizing the images with HTML also makes the pictures look crappy by reducing their quality.

If you are adding images to your site, you should take their loading time into consideration. Resize them first on your computer, then upload them to your site. You also try to use web-friendly image formats like GIF, JPEG, and PNG, which yield high-quality images without the massive size.

Be Sociable, Share!

Leave a Reply

Your email address will not be published. Required fields are marked *