Home  >  Article  >  Backend Development  >  Three "soft" tricks to speed up your PHP website_PHP Tutorial

Three "soft" tricks to speed up your PHP website_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 16:07:01763browse

Text/Li Kuncheng

With the popularization of the Internet, websites have penetrated into every industry. However, because the current dedicated line costs are unaffordable for many enterprises and individuals, shared bandwidth has become the preferred solution for many websites. Although the cost was reduced, a new problem soon appeared, that is, the speed of the shared bandwidth was very slow, sometimes even unbearable. How to make your website faster has become a concern for people. The author will talk about some practical applications of soft methods based on personal practice, hoping to be helpful to readers.


The author uses the Linux operating system and the Apache+PHP website construction method.


Code optimization


Programmers are usually required to have good programming habits and minimize the occurrence of redundant code. Currently, there are many tools that can complete this function. For There are many weight-loss tools for reducing redundancy in general HTML files, but there are not many such tools for PHP programs, but Zend Technologies' Zend Optimizer is a very good code optimization tool that can be downloaded from Zend for free. Technologies' website. The use of Zend Optimizer is also very simple. Just decompress the downloaded ZendOptimizer-1[1].0.0-PHP_4.0.4-Linux_glibc21-i386.tar.gz file and copy the ZendOptimizer.so file to /usr/local /Zend/lib directory, then modify the php.ini file and add the following lines at the end:

Three
The display indicates that Zend Optimizer is working normally

zend_optimizer. optimization_level=15

zend_extension="/usr/local/ Zend/lib/ ZendOptimizer.so"

After the settings are completed, restart the Apache server and write a PHP program:

?

Phpinfo();

?>

Generally speaking, Zend Optimizer can increase the efficiency of the system by 30% to 40%, which is what users are most concerned about.


Compressed page


HTTP1.1 protocol supports page compression transmission, which means that the server compresses and transmits a page to the client, and then decompresses the page on the client Then display it to the customer. There are two transmission methods on the server side. One is that the page has been compressed in advance. When transmitting, you only need to transmit the compressed page to the client. This is suitable for situations where there are many static web pages, but for most sites, dynamic pages are more Many, this method is not suitable, because many pages transmitted to the client actually do not exist. They are generated dynamically by the server upon receiving user requests from the client. Therefore, it is required that each dynamic page generated must be first generated before being transmitted to the client. Packed and compressed. From PHP version 4.0.4 onwards, you can add a line of configuration "output_handler = ob_gzhandler" in the php.ini file, so that each dynamically generated page will be compressed before being transmitted to the client, but according to the instructions on the PHP official site, This parameter cannot be used at the same time as the "zlib.output_compression = on" parameter, because it can easily cause PHP to work abnormally. In addition, it can only compress dynamically generated pages of the PHP program, but it will not work for a large number of static pages, especially image files. However, the mod_gzip module provides Apahe with the function of compressing static pages before transmitting them to the client. Its compression ratio can reach a maximum of 10, and under normal circumstances it can reach 3, which means that the transmission rate of the website has been increased by more than three times. . To use the mod_gzip function, you must first download the mod_gzip.c or mod_gzip.so file. If you download a .c file, you must use the tools provided by Apache to convert it into a .so file before it can be used. The method is to run 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 will automatically activate this module in /path/to/apache/etc/httpd.conf. If you download a .so file, copy the file to the corresponding directory, and then Add LoadModule gzip_module libexec/ mod_gzip.so in the httpd.conf file to make the module effective. Two points need to be noted. First, if you want to use .so files, Apache must include the mod_so module (you can use the httpd -l command to check whether the module is effective); second, if you download a .so file, it is the same as Regarding the version of Apache, you should pay attention to whether the downloaded version is consistent with the Apache you are using. If it is a .c file, there will be no such problem.After the module takes effect, you need to configure Apache accordingly. You need to add some parameters to the httpd.conf file:

mod_gzip_on Yes (whether the module takes 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 available memory)

mod_gzip_item_include file "..gif102SINA> ;DOUBLE_QUOTATION (in gif The file at the end should be compressed and transmitted)

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 using the compression module, when the user visits the site, the corresponding information will be 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 transmission, input file, output File sizes are indicated.


File caching


This method is usually used for CGI programs such as PHP and PERL, because these programs have a common feature of receiving user requests. The result is not returned to the user immediately, but the execution result is returned to the client after interpretation and execution by the interpreter. This process usually involves database access. This will cause a problem. When two users access the same page, the system will operate on the two requests separately, but in fact the two operations may be exactly the same, which invisibly increases the burden on the system. Therefore, the usual solution is to open up a space in the system memory. When the user accesses the page for the first time, the execution result is stored in the memory. When the user accesses the page again, the system directly removes the page from the memory. Called out without reinterpretation and execution, this memory space is called cache. There are currently two popular cache management programs, one is FastCGI and the other is Zend Cache from Zend Technologies. FastCGI is mainly designed for CGI script programs such as Perl, C, and C++. It can effectively use memory for caching. Requests from the client will be sent to the FastCGI application service program. FastCGI processes the user's request and returns the result to the user. The general CGI program will end the process and exit automatically at this time, but the FastCGI process continues to be maintained. At this time, it does not need to create a new process after receiving a new user request, and can process the user request immediately. In other words, the CGI program establishes a process and executes it sequentially. Then it exits, while the FastCGI program executes sequentially and loops forever.

Three

Zend Cache management interface

If you want to use FastCGI, you must first compile FastCGI into Apache. The method is very simple and will not be explained here. Yes, you also need to make settings in the http.conf file:

AddHandler fastcgi-script .fcg .fcgi .fpl

Options ExecCGI Indexes Includes

In this way, FastCGI can work normally It worked. Here is an example program from the FastCGI Programmer's Manual:

 #!fcgi-savvy-perl

 use FCGI; #Initialization

 #Initialization code

= 0;

# Response loop

while (FCGI::accept >= 0) {

#FastCGI creates loop body

print "Content-type:text/html "; print "<h1>FastCGI Demo Page (perl)</h1> ";

print "This is coming from a FastCGI server. <BR> ";

print "Running on <EM >publish152.internal.sina.com.cn</EM> to <EM></EM><BR> ";

 ++;

print "This is connection number ";

 }

FastCGI is very powerful for CGI programs such as Perl, but it is really powerless for PHP programs, and needs to add content during programming, which means that it requires some artificial factors can play a role. In contrast, Zend Technologies' Zend Cache has a very powerful caching function for PHP. As long as the software is installed, programmers can implement the caching function just like writing other PHP programs without adding code, which is beneficial to the system. Upgrading is very convenient. It is a paid software.It has a cache function and can manage it through a graphical interface, including: Cache Control page, which can configure Zend Cache, display its current status, and start and stop Zend Cache function; Scripts page, the content of Zend Cache, including each The status of the file can also be selected based on the number of clicks and cache size; the Benchmark page can test the cache effect of Zend Cache and display it graphically. It tests the number of PHP requests completed per second.

Its installation and verification methods are basically the same as those of Zend Optimizer. I will not explain them in detail here. Interested readers can refer to the user manual. Its powerful functions and convenient management methods are really exciting.

The above are several common ways to speed up websites. Different websites require different methods, and the corresponding speed-up plans are also different, but in general they are nothing more than the three tricks mentioned above. Readers can analyze specific issues based on their actual situation.

Note: Consider the complexity of the client software, because some client software may not support certain features. For example, mod_gzip compresses the page, but if the client is using Netscape, it will not work because it The received compressed page cannot be decompressed, resulting in the page not being displayed normally.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/315171.htmlTechArticleText/Li Kuncheng With the popularity of the Internet, websites have penetrated into every industry, but due to the current dedicated line fees This is unbearable for many companies and individuals, so shared bandwidth has become a problem for many websites...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn