A detailed introduction to the acceleration of PHP application _php tutorial

Source: Internet
Author: User
Tags apc php source code server memory zend
As we all know, fast speed is the biggest advantage of PHP. In general, PHP always has enough speed to support the dynamic generation of Web content, and many times you can't even find a quicker way to do it.

However, when you have to face massive traffic, high-load applications, limited bandwidth, and other factors that bring performance bottlenecks, you might ask yourself if you can do something to make your site work better. Perhaps just add a very humble free module, your PHP application performance and Web server response speed will be significantly improved.

This article discusses how to further improve the performance of PHP applications, to give users a more wonderful browsing experience. This article is divided into three aspects (code optimization, caching, content compression) to improve the performance of PHP application of various technologies, and introduce various areas of well-known products.

Code optimization

First, let's look at code optimization. Note that the code optimization here does not mean to write the code more beautifully, because it is probably already known that there is no need to continue the discussion, and if you have already considered the speed problem, you probably have already made some optimizations to the PHP source code.

However, some tools can automatically help us do these tedious tasks, such as Zend Optimizer is such a tool. Zend Optimizer can be obtained free of charge from Zend Technologies, but you must agree to its license agreement and note that it is not distributed in GPL form. Zend Optimizer Gets the intermediate code generated by the Zend Engine Runtime compilation and optimizes it to make the intermediate code more efficient to execute.

The installation method of Zend Optimizer is very simple, just download the precompiled version provided for your platform, add the following two lines of code to php.ini, and then restart the Web server:

Zend_optimizer.optimization_level=15

zend_extension= "/path/to/zendoptimizer.so"

Zend_loader.enable=off

The additional third line of code here is optional. Banning Zend_loader seems to make Zend optimizer faster, so adding this line of code to php.ini is worth it. Note: You can disable Zend_loader only if you do not use the Zend Encoder runtime.

Cache

Caching is also a good way to make your own large PHP application more performance-based. There are now a number of caching options available, including: Zend CACHE,APC, and Afterburner cache.

All of these products belong to the "cache module". When requests for. php files appear for the first time, they store the PHP intermediate code in the Web server memory, and thereafter respond to subsequent requests with a "compiled" version. This approach does improve the performance of your application because it minimizes the amount of disk traffic (code has been read and parsed), and the code runs directly in memory to make the server respond to requests much more quickly.

Of course, the cache module also monitors changes to the PHP source file and re-caches the page if necessary, thus preventing the user from getting a page that is still generated by outdated PHP code. Because cache modules can significantly reduce server load and improve the responsiveness of PHP applications, they are ideal for use in heavily loaded web sites.

How to select these cache products

Zend Cache is the commercial software of Zend Technologies, and Zend Technologies is the company that provided us with the PHP engine and free Zend optimizer as mentioned earlier. Zend Cache is truly well-deserved! For large PHP pages, you can feel the speed increase after the first run, and the server will have more resources available. Unfortunately this product is not free, but in some cases it is still worth the value of the goods.

The afterburner cache is a free caching module from Bware Technologies, which is currently a beta version of the product. The afterburner cache approach looks similar to the Zend cache, but it does not improve performance (yet) compared to Zend cache, and it cannot work with Zend optimizer.

APC is the abbreviation for alternative PHP cache, which is another free cache module from community Connect. This product is already stable enough for formal use, and it seems to be able to improve the speed of responding to requests to a large extent.

Content Compression

Before we discussed several ways to improve the performance of PHP applications, let's look at another important factor that makes the Web site slow: Download speed. If the PHP application is running on the internal intranet and each client is connected to the server at a speed of up to two MB/s, the download speed should not be a problem. However, if the server also to provide services for slowly modem users, it is worth considering content compression.

Most browsers support content compression with gzip based on IETF standards. This means that you can compress the content with gzip and then send it to the browser, and the browser will extract the data and then display the page, which is completely transparent to the user. As for server-side content compression, there are now many different ways to use it.

For example, the free Apache module from remote communications Mod_gzip has the ability to compress static Web content for browsers that support such content encoding. For most static Web content, Mod_gzip is very effective. Mod_gzip can be easily compiled into Apache or used as DSO. According to the remote communications company, Mod_gzip can also compress dynamic content from mod_php, mod_perl, etc.

I tried again and again, but it didn't seem to work. I read a lot of forums and articles about Mod_gzip, and it seems that the next version of Mod_gzip (probably 1.3.14.6f) is expected to be resolved. Prior to this, we could use Mod_gzip in the static section of the website.

However, sometimes we do need to compress dynamic content, so we have to find other ways. One way to do this is to use class.gzip_encode.php, a PHP class that can be used to compress the contents of a page, by invoking some of the functions of the class at the beginning and end of the PHP script. If you are implementing this scenario at the site level, you can call these functions from the auto_prepend and auto_append directives of the php.ini file.

While this approach works, it undoubtedly brings more overhead to high-load web sites. For a detailed description of how to use this class, see its source code. Its source code description is quite perfect, and the author tells you all the things you have to know.

PHP 4.0.4 has a new output cache handle Ob_gzhandler, similar to the previous class, but with a different usage. The content to be added to PHP.ini when using Ob_gzhandler is as follows:

Output_handler = Ob_gzhandler;

This line of code allows PHP to activate the output cache and compress everything it sends out. If for some reason you do not want to add this line of code to php.ini, you can also change the default server behavior (not compressed) by using the. htaccess file in the directory where the PHP source files are located, with the following syntax:

Php_value Output_handler Ob_gzhandler

Or it is called from the PHP code, as follows:

Ob_start ("Ob_gzhandler");

The method of using output cache handles is really effective and does not give the server any special load. It is important to note, however, that Netscape Communicator has poor support for compressed graphics, so you should suppress JPEG and GIF graphics unless you can ensure that all users are using Internet Explorer. Generally, this compression works for all other files, but it is recommended that you test separately for various browsers, especially if you are using a special plug-in or data viewer.

Using the various techniques described earlier, you can significantly improve the performance of your site, but it should be noted that:

PHP may or may not be a performance bottleneck. Be sure to look closely at each and every factor that applies to performance, such as a database.

Using this technique alone can only improve the performance of the Web server within a certain limit. So before blaming PHP and its cache, consider whether you should upgrade the server and whether you can introduce load balancing technology (which requires a large investment).

Do not underestimate the role of content compression. Although you see the Web app responding very quickly under a LAN connection of up to a few megabytes (MB/s), users who connect with the modem will not, they will only complain that your KB HTML page is too large.

I hope that the introduction of PHP in this article can bring you help.

http://www.bkjia.com/PHPjc/314845.html www.bkjia.com true http://www.bkjia.com/PHPjc/314845.html techarticle As we all know, fast speed is the biggest advantage of PHP. In general, PHP always has enough speed to support the dynamic generation of Web content, and many times you can't even find a quicker way to do it. ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.