This section describes how to speed up PHP applications. We all know that fast speed is the biggest advantage of PHP. In general, PHP always has enough speed to support dynamic generation of Web content. many times you cannot even find a way faster than it. we all know that the speed is faster.
PHPThe biggest advantage. In general, PHP always has enough speed to support dynamic generation of Web content. in many cases, you cannot even find a faster method than it.
However, when you have to deal with huge traffic volumes, high-load applications, limited bandwidth, and other factors that bring performance bottlenecks, you may ask yourself if you can do something to make your website run better. Perhaps your PHP application performance and Web server response speed will be significantly improved by adding a very inconspicuous free module.
This article discusses how to further improve the performance of php applications and give users a better browsing experience. This article focuses on three aspects (code optimization, caching, and content compression), describes various technologies to improve the performance of PHP applications, and introduces well-known products in various fields.
Code Optimization
First, let's look at code optimization. Note: The code optimization here does not mean to write the code more beautiful and beautiful, because it is already known that there is no need to continue the discussion. In addition, if you have already considered the speed issue, it is very likely that you have already optimized the source code of PHP.
However, some tools can automatically help us with these complicated tasks, such as Zend Optimizer. Zend Optimizer can be obtained free of charge from Zend Technologies, but you must agree with its license agreement, note that it is not released in GPL mode. Zend Optimizer obtains and optimizes the intermediate code generated during Zend Engine runtime, so that the intermediate code can be executed more efficiently.
The Zend Optimizer installation method is very simple. you only need to download the pre-compiled 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 third line of code added here is optional. Disabling zend_loader seems to make Zend Optimizer faster, so it is worth adding this line of code in php. ini. Note: zend_loader can be disabled only when Zend Encoder Runtime is not used.
Cache
If you want to improve the performance of your huge PHP application, using cache is also a good method. There are already many caching schemes available, including Zend Cache, APC, and Afterburner Cache.
All these products belong to the cache module ". When a request to the. PHP file appears for the first time, it will save the PHP intermediate code in the memory of the Web server, and then respond to the subsequent request with the "compiled" version. This method can indeed improve the performance of the application, because it minimizes the disk access volume (the code has been read and parsed ), the code runs directly in the memory, greatly improving the server's response speed.
Of course, the cache module also monitors changes in PHP source files and caches pages again when necessary to prevent the pages from being generated by outdated PHP code. Because the cache module can significantly reduce the server load and improve the response efficiency of PHP applications, they are often suitable for websites with large loads.
How to choose these Cache products
Zend Cache is the commercial software of Zend Technologies, and Zend Technologies is the one mentioned earlier that provides us with PHP engines and free Zend Optimizer. Zend Cache is indeed a good name! For large PHP pages, you can feel that the speed will increase after the first running, and more available resources will be available on the server. Unfortunately, this product is not free, but in some cases it is still worth the money.
Afterburner Cache is a free Cache module from Bware Technologies. Currently, this product is a Beta version. The practice of Afterburner Cache seems to be similar to that of Zend Cache, but its performance improvement (also) cannot be compared with that of Zend Cache, and it cannot work with Zend Optimizer.
APC is the abbreviation of Alternative PHP Cache. it is another free Cache module from Community Connect. This product is already stable enough for official use, and it seems to be able to greatly speed up response to requests.
Content compression
We have discussed several ways to improve the performance of PHP applications. Next, let's take a look at another important factor that makes viewers feel that the website is too slow: download speed. If the PHP application runs on the internal Intranet and each client connects to the server at a speed of 100 MB/s, the download speed should not be a problem. However, if the server still needs to provide services for slow Modem users, it is worth considering the internal compression.
Most browsers support gzip compression based on IETF standards. This means that you can use gzip to compress the content but send it to the browser. after the browser decompress the data, the page is displayed. this entire process is completely transparent to users. As for server-side content compression, there are already many different methods available.
For example, the free Apache module mod_gzip from Remote Communications is capable of compressing static Web content for browsers that support such content encoding. Mod_gzip is very effective for most static Web content. Mod_gzip can be easily compiled into Apache or used as a DSO. According to Remote communications, mod_gzip can also compress dynamic content from mod_php and mod_perl.
I tried again and again, but it still doesn't seem to work. I have read many forums and articles about mod_gzip and read the next version of mod_gzip (may be 1.3.14.6f). This issue is expected to be solved. Previously, we could use mod_gzip in the static part of the website.
However, sometimes we do need to compress dynamic content, so we have to look for other methods. One method is to use class.gzip _ encode. php. this is a PHP class that can be used to compress page content. the specific method is to call some functions of this class at the beginning and end of the PHP script. To implement this scheme at the website level, you can call these functions from the auto_prepend and auto_append commands of the php. ini file.
Although this method is effective, it undoubtedly brings more overhead to high-load websites. For more information about how to use this class, see its source code. The source code description is quite complete, and the author tells you everything you must know.
PHP 4.0.4 has a new output cache handle, ob_gzhandler, which is similar to the previous class but has different usage. When using ob_gzhandler, add the following content to php. ini:
- output_handler = ob_gzhandler ;
This line of code enables PHP to activate the output cache and compress all the content it sends. If you do not want to add this line of code in php. ini for some reason, you can also change the default server behavior (not compressed) through the. htaccess file in the directory where the PHP source file is located. The syntax is as follows:
- php_value output_handler ob_gzhandler
Or you can call it from the PHP code as follows:
- ob_start("ob_gzhandler");
The method of using the output cache handle is indeed very effective and does not impose any special load on the server. However, it must be noted that Netscape Communicator does not provide good support for compressed images. Therefore, unless you can ensure that all users use IE browsers, you should disable JPEG and GIF image compression. Generally, this compression is effective for all other files, but we recommend that you perform tests on different browsers, this is especially important when you use a special plug-in or data viewer.
Using the various technologies described above, you can significantly improve the performance of your website, but note the following:
PHP may or may not be a performance bottleneck. Be sure to carefully observe every factor related to application performance, such as databases.
Simply using this technology can only improve the performance of Web servers to a certain extent. Therefore, before you blame PHP and its cache, you may want to see whether you should upgrade the server and whether the server load balancer technology can be introduced (the latter requires a large investment ).
Do not underestimate the effect of content compression. Although you can see that the Web application responds very quickly under the 100 Mbit/s LAN connection, the user who uses the Modem connection will not, they will only complain that your 100 Kb HTML page is too large.
I hope this article will help you with PHP introduction.
PHP has the biggest advantage. In general, PHP always has enough speed to support dynamic generation of Web content. in many cases, you cannot even find a faster method than it...