using PHP in large websites - redone
This was originally an article by Aaron Crane, but because of overwhelming amount of inaacurate and outdated information, I have decided to write a new and up-to-date article.
The following methods can help improve scalability with php applications.
1) object code caching
Each time a request comes to your server for a php script, it has to go through the compiler and then execute the object code. If this is cached, the 1st step is skipped and you end up with a faster and more responsive script.
There are many object code caching packages available on the Internet (some free, some commercial):
|B) Zend Encoder||http://www.zend.com/products/zend_safeguard|
|C) Turckl MMCache||http://freshmeat.net/projects/turck-mmcache/|
2) Template systems
Template systems provide a different type of caching. Content caching. Template systems work well in a situation where there is static data on one or many of your pages that doesn't have to be reloaded. Caching systems also provide a separation of code and html, which will not only improve completion time of the overall project, but make it easier for future improvments. Most template systems for php are available for free:
|A) Smarty Templates||http://smarty.php.net/|
|B) Pear Templates||http://pear.php.net/package/html_template_it/redirected|
|C) PHP savant||http://phpsavant.com/yawiki/|
3) Distributed object caching systems
The most widely used system of this type is memcached (http://www.danga.com/memcached/).
This type of system makes your overall site faster by caching the majority of your database data into a large memory pool.
an interesting excerpt from their site:
"Danga Interactive developed memcached to enhance the speed of LiveJournal.com, a site which was already doing 20 million+ dynamic page views per day for 1 million users with a bunch of webservers and a bunch of database servers. memcached dropped the database load to almost nothing, yielding faster page load times for users, better resource utilization, and faster access to the databases on a memcache miss."
4) PHP variables that can be set
variables_order = 'GPC'
register_argc_argv = 'Off'
register_globals = 'Off' (this is a good idea to keep off for security purposes as well)
always_populate_raw_post_data = 'Off'
magic_quotes_gpc = 'Off'
Disable Error Logging. This is a good idea to keep on when you are developing your scripts, but it has been known to decrease overall performance.
Use IP address to access your database. Although this is sometimes not possible, you will get a slight boost in lookup speed if the IP address is used to access your database rather than its hostname.
5) Output Compression
Almost all browsers these days support something called gzip compression. Gzip compression can decrease the overall size of your output by up to 80%, but with a tradeoff: cpu usage will go up by around 10%. The benefit of using this compression type is the fact that not only will your bandwidth be decreased, but your pages will load faster.
enabling it in php (add the following lines to php.ini):
zlib.output_compression = On
zlib.output_compression_level = (level) (where level is 1-9. Youy may want to try different values to see what is best for your system).
if you are using apache, you can also enable the mod_gzip module. It is highly configurable, with the ability to modify output based on MIME types, files, or browser settings.
6) Other things that may help
when using a database, only retrieve the data that you are actually going to use. This may sound like a no-brainer, but I have often times worked on projects where the original programmer used (select * from mytable) when they could have used (select fieldIneed from mytable).
index database tables whenever possible
Learn more about this Here
specific language tricks
An interesting blog article I found mentions many interesting tricks that can be used: http://ilia.ws/archives/12-PHP-Optimization-Tricks.html
an article on zend.com about measuring performance: http://www.zend.com/zend/trick/trick-optimizing-php.php