Improving PHP Execution speed

Source: Internet
Author: User
Tags apc


PHP setup Problems & acceleration suggestions
Application in use Program If you cannot use PHP due to incorrect settings, check the following parameter settings in PHP. ini.
Assume that your PHP is installed on D:/PHP/

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Error handling and logging;

; error_reporting is a bit-field. or each number up to get desired error
; Reporting level
; e_all-all errors and warnings
; e_error-fatal run-time errors
; e_warning-run-time warnings (non-fatal errors)
; e_parse-compile-time parse errors
; e_notice-run-time notices (these are warnings which often result
; from a bug in your code, but it's possible that it was
; intentional (e.g ., using an uninitialized variable and
; relying on the fact it's automatically initialized to an
; empty string)
; e_core_error-fatal errors that occur during PHP's Initial Startup
; e_core_warning-warnings (non-fatal errors) that occur during PHP's
; initial Startup
; e_compile_error-fatal compile-time errors
; e_compile_warning-compile-time warnings (non-fatal errors)
; e_user_error-user-generated error message
; e_user_warning-user-generated warning message
; e_user_notice-user-generated notice message

; Examples:

;-Show all errors, except T for notices

; Error_reporting = e_all &~ E_notice

;-Show only errors

; Error_reporting = e_compile_error | e_error | e_core_error

;-Show all errors tables t for notices

Error_reporting = e_all &~ E_notice
; Print out errors (as a part of the output). For production Web sites,
; You're stronugly encouraged to turn this feature off, and use error logging
; Instead (see below). Keeping display_errors enabled on a production Web Site
; May reveal security information to end users, such as file paths on your web
; Server, your database schema or other information.
Display_errors = on

; You shoshould do your best to write your scripts so that they do not require
; Register_globals to be on; using form variables as globals can easily lead
; To possible security problems, if the code is not very well thought.
Register_globals = on

; Argument passed to save_handler. In the case of files, this is the path
; Where data files are stored. Note: Windows users have to change this
; Variable in order to use PHP's session functions.
Session. save_path = "C:/winnt/Temp" (you can change it to your existing Directory)

; CGI. force_redirect is necessary to provide security running php as a CGI under
; Most Web servers. left undefined, PHP turns this on by default. You can
; Turn it off here at your own risk
; ** You can safely turn this off for IIS, in fact, you must .**
CGI. force_redirect = 0

; Directory in which the loadable extensions (modules) reside.
Extension_dir =./extensions/
Or directly set it to your absolute directory, such as: D:/PHP/extensions/

The GD library is used by the sample chip management system.
This file is under D:/PHP/extensions.
Extension = php_gd.dll
One of the advantages of PHP is that it is fast. It is enough for General website applications. However, if the site's traffic is high, bandwidth is narrow, or other factors cause a performance bottleneck on the server, you may have to think of other ways to further speed up PHP. This articleArticleWe will introduce how to do this in several ways to make browsing easier ".

CodeOptimization

I don't want to tell you again here
I think everyone knows how to write clean code. When you need speed, you may already be in PHPSource codeThe optimization above has done a lot of work. Here we propose that this tedious work can be done by other tools. This is Zend optimizer, which is available for free from Zend technologies's website (http://www.zend.com. Its principle is very simple. It detects intermediate code generated by the Zend engine and optimizes it to get a higher execution speed. I think code optimization is a cumbersome task, and the optimized code may become hard to understand, especially when you put down the PHP program for a while, suddenly, when the customer asks you to make some changes, you may not understand it yourself ;-). Therefore, I suggest you use Zend optimizer to optimize PHP when the source code is complex. The advantage is that it will not make your code complex and difficult to understand.

Installing Zend optimizer is very simple. Download the relevant pre-compiled libraries based on your platform, and add two lines in your php. ini to restart your web server!

Zend_optimizer.optimization_level = 15zend_extension = "/path/to/zendoptimizer. So" zend_loader.enable = off

You may be a bit strange. Isn't it just two rows? How is it changed to three rows. However, the third line is optional. It seems that disabling this zend_loader will speed up optimization, so you may wish to add this line to your php. ini file. Note that zend_loader can be disabled only when you do not use Zend encoder runtime. Zend encoder Runtime is also mentioned below.

Is it faster? Use cache (buffer)

If your php application requires a higher speed, the next method is buffering. There are several different ways to achieve this. I have tried Zend cache (evaluation version), APC, and afterburner cache myself.

All of the above are "buffer modules ". They all work in the same way. When the PHP file is requested for the first time, the intermediate code of your PHP source code is stored in the memory of the Web server. For the same requests in the future, both provide the "compile" version in the memory. Since it can minimize disk access, this method can indeed greatly improve PHP performance. More conveniently, when your PHP source code is modified, the buffered module can detect these changes and reload them the same, so you don't have to worry that the customer will get an old version of the program. These buffer modules are really good, but which one should I choose? The following sections describe:

Zend cache is a commercial product of Zend technologies (it is also a free company that provides us with the PHP engine and Zend Optimizer ). It is really good. After the first run, you can obviously notice that the PHP speed has been greatly improved, and there are more idle resources on the server. The disadvantage is that you have to pay for it, but it is very worthwhile for cost effectiveness.

Afterburner cache is a free buffer module provided by bware technologies (bwcache. bware. it. The current version is only a beta version. The work it does seems to be similar to Zend cache, but the performance improvement is not as good as Zend cache, and the existing version cannot work with Zend optimizer, but it is free.

APC (Alternative PHP cache) is another free module provided by community connect (apc.communityconnect.com. It works very stably and has a lot of speed improvement. Please note that I have not found an official test data. These are just tests on my application, therefore, we cannot draw a conclusion.
Web content compression (making your customers feel better ")

After the above two methods, I believe that the performance of your php application has been greatly improved. Now we should consider the download speed from another aspect. If your application is only running in the company, all customers will connect to the server using 100 Mbit/s Ethernet, which may not be a problem, however, if your customers use a slow modem connection, you should consider using the content compression method.
According to the IETF specification, most browsers support
Compression. This means that you can use gzip to compress the web content before sending it to the client's browser. When receiving the content, the browser automatically decompress the data and allows the user to see the original page. Similarly, there are several different ways to compress the content of a Web page.

Mod_gzip is a free Apache module provided by remote communications (http://www.phpbuilder.com/columns/www.remotecommunications.com) that compresses static Web pages. It works well. You just need to compile it with Apache (or use it as a DSO ). Remotecommunications people say it can also compress dynamic content, including mod_php and mod_perl. However, I tried it, but it does not seem to work. I learned from the mod_gzip mailing list that this bug will be fixed in the next version (I think it should be 1.3.14.6f ). However, you can still use it to compress static content.

However, we still want to compress dynamic content, so we must find another method. One way is to use class.gzip encode. php (http://leknor.com/code/), as long as you call this PHP class at the beginning and end of your PHP script, you can compress your page content. If the whole site requires such compression, you can call these functions in auto_prepend and auto_append in your php. ini file. It works well, but on websites with heavy loads, it obviously brings about a little system overhead. To learn more about how it works, take a look at its class code (you must at least add zlib support when compiling PHP ). The instructions provided by the author are also very detailed. You can get anything you need to know.

Recently, I also saw an article about PhP output buffering. Php4.0.4 introduces a new processing method for output buffering-ob_gzhandler, which functions the same as the class described above, but the difference is that you only need to use it in your php. use the following syntax in ini:

Output_handler = ob_gzhandler;

This will activate the PHP output buffer function and compress everything it sends. For some special reason, if you do not want to set it here, you only need to change the default setting (not compressed) Where necessary, as long as you are in the PHP source code directory to be compressed, modify it. the htaccess file is ready. The syntax is as follows:

Php_value output_handler ob_gzhandler

... Or call it directly in your PHP code, the following method:

Ob_start ("ob_gzhandler ");

This output buffering method is good and does not bring additional system overhead to the server. I strongly recommend that you use this method. The change can be illustrated in the following example. If the customer uses a 28.8k modem, after this processing, he will think that suddenly it is changed to an ISDN access. Note that Netscape Communicator does not support image compression, so it cannot be displayed. Therefore, unless all your customers use Internet Explorer, you must disable JPEG and GIF image compression. Compression of other files should be fine, but I suggest you test it. In particular, browsers use uncommon plug-ins or browsers that are rarely used.

Other useful things...

Zend technologies's online store opened in January 24 this year and sells some interesting PHP-related products. Including the Zend cache and Zend encoder mentioned above (in short, it is a PHP code compiler that can generate compiled classes so that you can sell them to customers without worrying about leaking source code. On the web servers that need to run these classes, Zend encoder runtime will be used for decoding), Zend IDE (an integrated development environment for PHP, with a lot of powerful performance ), it also provides support services for PHP developers.

Conclusion

Using the technology mentioned in this article, you can greatly improve the performance of the site, but pay attention to the following points:

1. the bottleneck may not be in PHP. You need to check every object (such as a database) in the application)

2. the performance of a Web server is limited. Therefore, do not consider the poor performance as the reason for PHP. It may also be because of a large traffic volume and your server needs to be upgraded, or consider using a Server Load balancer system (which will cost a lot)

3. Do not think that content compression is not important. In a 100 Mbit/s LAN, Your PHP application may have good performance, but consider users using slow modem.

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.