PHP website speed up three big soft strokes

Source: Internet
Author: User
Tags copy execution file size ini php and php website client zend

Code optimization

Often require programmers to have good programming habits, to minimize the emergence of redundant code, there are many tools to complete this function, for the general HTML files, to reduce the redundancy of the weight loss tools, and for PHP programs such a tool is not too much, but Zend Technologies's Zend Optimizer is a very good code optimization tool that can be obtained free of charge from Zend Technologies's website. Zend Optimizer is also very simple to use, as long as the download of the Zendoptimizer-1[1].0.0-php_4.0.4-linux_ glibc21-i386.tar.gz the file, copy the zendoptimizer.so file to the/usr/local/zend/lib directory, and then modify the php.ini file, adding the following lines at the end:

-->

Display Instructions Zend Optimizer works fine

Zend_optimizer.optimization_level=15

zend_extension= "/usr/local/zend/lib/zendoptimizer.so"

When Setup is complete, restart the Apache server and write a PHP program:

?

Phpinfo ();

? >

In general Zend Optimizer can improve the efficiency of the system 30%~40%, which is the user's most concern.

Compress pages

The HTTP1.1 protocol supports page compression transmission, which means that the server sends a page compression to the client, and then the page is uncompressed and then displayed to the client. There are two modes of transmission on the server side, one is that the page has been compressed in advance, as long as the transfer of compressed pages to the client on the line, this applies to static web more cases, but for most sites, dynamic page more, this method is not very suitable, because many uploaded to the client page is not actually, Is the server receives the client user request dynamic generation, therefore requests each generation to generate each dynamic page to be before uploads to the client first packs the compression. After PHP version 4.0.4, you can add a row to the php.ini file to configure "Output_handler = Ob_gzhandler" so that each dynamically generated page is compressed before it is delivered to the client, but according to the PHP official site instructions, This parameter cannot be used at the same time as the "zlib.output_compression = on" parameter, because it is easy to cause PHP to work abnormally, in addition it can only compress the dynamically generated pages of the PHP program, for a large number of static pages, especially image files. But the Mod_gzip module for Apahe provides the static page to the client before the compression of the function, its compression than the maximum can to 10, under normal circumstances can to 3, that is, the site's transmission rate increased by three times times more. To use the Mod_gzip feature, first download the mod_gzip.c or mod_gzip.so file, and if you download the. c file, convert it to the. so file using the Apache tool, by running the following command:

-i-a MOD_GZIO.C

CP mod_foo.so/path/to/apache/libexec/mod_gzip.so

chmod 755/path/to/apache/libexec/mod_foo.so

The system activates the module automatically in/path/to/apache/etc/httpd.conf, and if the. so file is downloaded, copy the file to the appropriate directory and add the LoadModule to the httpd.conf file Gzip_module Libexec/mod_gzip.so to make the module effective. To be aware of two points, first, to use the. so file, Apache must include the Mod_so module (which can be queried by the httpd-l command to see if the module is in effect); If the. So file is downloaded, it is related to the Apache version, and be aware that the downloaded version is consistent with the Apache you are using, and if it is. c files, there is no such problem. After the module is in effect, the Apache should be configured accordingly, and some parameters need to be added to the httpd.conf file:

mod_gzip_on Yes (module is in effect)

Mod_gzip_minimum_file_size 1002 (Minimum compressed file size)

Mod_gzip_maximum_file_size 0 (Maximum compressed file size, 0 means no limit)

Mod_gzip_maximum_inmem_size 60000 (maximum memory footprint)

Mod_gzip_item_include file ". Gif102sina>double_quotation (file with GIF end to compress transfer)

Mod_gzip_item_include file ". txt102sina>double_quotation

Mod_gzip_item_include file ". html102sina>double_quotation

Mod_gzip_item_exclude file ". css102sina>double_quotation

After the compression module is used, when the user visits the site, the corresponding information is recorded in the log file, such as "Mod_gzip:ok in:file_length out:gzipfile_length", which means that the GZIP function is used in the page transfer, the input file, The output file size is indicated.

File caching

This approach is usually for CGI programs such as PHP and Perl, because they have a common feature of not returning the results to the user immediately after a user's request, but explaining the execution results back to the customer after the interpreter has been interpreted, often involving access to the database. As a matter of fact, when two users visit the same page, the system will operate on two requests, but in reality the two operations may be identical, which adds to the burden of the system. So the usual solution is to open up a space in the system memory, when a user accesses the page for the first time, the execution results are stored in that memory, and when a user accesses the page again, the system calls the page directly from memory without having to perform a new interpretation, which is called caching. The current popular cache management program has two, one is fastcgi, the other is Zend Technologies company Zend Cache. FastCGI mainly for Perl, C, C + + and other CGI script design, can effectively use memory for caching, requests from the client will be sent to the FASTCGI application service program, FASTCGI processing the user's request to return the results to the user. The general CGI program will end the process automatically exits, but the fastcgi process continues to persist, when it receives a new user request does not need to establish a new process, you can immediately process the user request, that is, the CGI program to establish processes in order to execute and then exit, and fastcgi program sequence execution and Forever loop.

Zend Cache Management Interface

To use fastcgi first to compile the fastcgi into Apache, the method is very simple, here does not explain, but also in the http.conf file settings:

AddHandler fastcgi-script. FCG fcgi. FPL

Options execcgi Indexes Includes

So fastcgi can work, the following is a sample program in the FASTCGI Programmer's Manual:

#!fcgi-savvy-perl

Use fcgi; # initialization

# initialization code

= 0;

# Response Loop

while (fcgi::accept >= 0) {

#FastCGI建立循环体

print "content-type:text/html"; #程序执行

Print FastCGI Demo Page (perl) ;

Print "

FastCGI Demo Page (perl)

";

print ' This is coming from a FastCGI server.
";

Print "Running on publish152.internal.sina.com.cn to
";

++;

Print "This is connection number";

}

FASTCGI is powerful for CGI programs like Perl, but it does nothing for PHP programs and needs to add content to programming, meaning it requires some human factors to work. In contrast Zend Technologies Company's Zend Cache for PHP caching is very powerful, as long as the software installed, programmers like writing other PHP programs, do not need to add code, you can implement caching functions, is conducive to system upgrades, very convenient. It is a paid software. It has a caching function and can be managed through the graphical interface, which: Cache control page, can configure Zend cache, display its current state, but also can start and stop Zend cache function; Scripts page, Zend cache content, Including the status of each file, can also be based on the number of clicks and cache size to select files, Benchmark page, you can test the cache effect of Zend cache, and graphically show that it tests the number of PHP requests completed per second.

Its installation, verification methods and Zend Optimizer is basically the same, here is not detailed, interested readers can refer to the user manual, its powerful function and convenient management method is really exciting.

The above is a common way to speed up the site, for different sites need to use different means, the corresponding speed-increasing program is not the same, but the overall is the above mentioned three strokes, the reader can according to their actual situation specific problems specific analysis.

Note: Given the complexity of the client software, some client software may not support certain features, such as Mod_gzip the page, but if the client is using Netscape, it will not be able to decompress the received compressed page, causing the page to not display properly.





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.