How to speed up PHP application

Source: Internet
Author: User
Tags apc functions ini php class php file php code php script zend
One of the biggest advantages of PHP is obviously its rapidity. In general, PHP always has enough speed to support Web content dynamic generation, many times you can not even find a faster way than it. However, when you have to deal with huge amounts of traffic, high load applications, limited bandwidth, and other factors that cause performance bottlenecks, you may ask yourself if you can do something to make your site work better. Perhaps with 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 applications of various technologies, and introduce the well-known products in various fields.

Code optimization
Let's take a look at code optimization first. Note that code optimization here does not mean that the code is more beautiful, because it is already well known that there is no need to continue the discussion, and if you have considered the speed problem, it is likely that you have already made some optimizations for the source code of PHP. However, some tools can automatically help us complete these miscellaneous 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, noting that it is not issued under the GPL. Zend Optimizer Gets the intermediate code generated by the Zend engine Run-time compilation and optimizes it to make the intermediate code more efficient for execution.

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

Zend_optimizer.optimization_level=15
zend_extension= "/path/to/zendoptimizer.so"
Zend_loader.enable=off

The extra 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 approach if you want to make your own large PHP applications perform better. There are already many caching options available, including: Zend CACHE,APC, and Afterburner cache.

All of these products are part of the "Caching module". When a request for a. php file appears for the first time, they save the Intermediate code of PHP in the Web server memory, and then respond to the subsequent request with a "compiled" version. This approach does improve the performance of the application because it minimizes the amount of disk traffic (code has been read and parsed), and the code runs directly in memory, making the server respond to requests much more quickly. Of course, the cache module will also monitor changes to the PHP source files and, if necessary, cache the pages, preventing users from getting pages that are still generated by outdated PHP code. Because caching modules can significantly reduce the load on the server and improve the responsiveness of PHP applications, they are ideal for Web sites with larger workloads.

How to select these caching products
Zend Cache is the commercial software of Zend Technologies company, and Zend Technologies is the aforementioned company that provides us with PHP engines and free Zend optimizer. Zend Cache is indeed a well-deserved reputation! For a large PHP page, you can feel that the speed will improve after the first run, and that the server will have more resources available. Unfortunately, this product is not free, but in some cases it is still a matter of value.

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 its performance improvement (also) cannot be compared to the Zend cache, and it cannot work with Zend Optimizer.

APC is the acronym for Alternative PHP Cache, which is another free cache module from community Connect. The product is already stable enough for formal use, and it also seems to be able to greatly increase the speed of response requests.

Content Compression
We discussed several ways to improve PHP application performance, and here's another important factor that makes viewers feel too slow: download speed. If the PHP application is running on an internal intranet and each client is connected to the server at a speed of MB/s, the download speed should not be a problem. However, if the server is also to provide services for the slow modem users, it is worth considering content compression. Most browsers support content compression with gzip based on IETF standards. This means that you can use gzip compressed content to send to the browser, the browser to decompress the data and then display the page, the entire process of the user is completely transparent. As for server-side content compression, there are already many different ways to use it.

For example, the free Apache module mod_gzip from remote communications 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, can also be used as DSO. According to remote communications company said, Mod_gzip can also compress from mod_php, mod_perl and other dynamic content. I tried it again and again, but it didn't seem to work. I've seen a lot of forums and articles about Mod_gzip, and it appears that the next version of Mod_gzip (probably 1.3.14.6f) is expected to be resolved. Prior to this, we can use Mod_gzip in the static part of the site.

However, sometimes we do need to compress dynamic content, so we have to look for other ways. One approach is to use class.gzip_encode.php, a PHP class that can compress page content by calling certain functions of the class at the beginning and end of the PHP script. If you want to implement this scenario at the site level, you can call these functions from the auto_prepend of the php.ini file and from the Auto_append instructions. While this approach works, it certainly brings more overhead to a highly loaded web site. 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 everything you need to know.

PHP 4.0.4 has a new output-cache handle Ob_gzhandler, which is similar to the previous class but differs in usage. The contents to be added to the php.ini when using Ob_gzhandler are as follows:

Output_handler = Ob_gzhandler;


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

Php_value Output_handler Ob_gzhandler


or call from the PHP code, as follows:

Ob_start ("Ob_gzhandler");


The method of using output-cache handles is really very effective and does not give the server any special load. However, it is important to note that Netscape Communicator has poor support for compressed graphics, so you should suppress JPEG and GIF graphics unless you can guarantee that all users will use IE. Generally, this compression works for all other files, but it is recommended that you test the various browsers individually, especially if you are using a special plugin or a data viewer.


Original Author: Panasonic


Related Article

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.