Three "soft" tricks for LinuxPHP website Acceleration

Source: Internet
Author: User
Tags php website
The Linux Enterprise Application-Linux server application information, the three "soft" tricks for the Linux PHP website to speed up, is described below. With the popularization of the network, websites have penetrated into every industry. However, because the current leased line costs make it difficult for many enterprises and individuals to afford, sharing bandwidth has become the preferred solution for many websites. Although the cost has been reduced, a new problem has quickly emerged, that is, the Internet speed of shared bandwidth is slow and sometimes unbearable. How to make your website faster becomes a matter of concern to people. I will talk about some practical applications of soft methods in my hands, hoping to help readers.

I use the Linux operating system and Apache + PHP website construction method.

   Code optimization

Programmers are usually required to have good programming habits to minimize the emergence of redundant code. There are also a lot of tools to complete this function. For general html files, there are many tools for reducing redundancy and losing weight, for PHP programs, there are not too many such tools, but Zend Optimizer of Zend Technologies is a very good code optimization tool that can be obtained free of charge from the Zend Technologies 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-extract the Linux_glibc21-i386.tar.gz file and paste the ZendOptimizer. copy the so file to the/usr/local/Zend/lib directory and modify php. add the following lines at the end of the INI file:

It indicates that Zend Optimizer works normally.

Zend_optimizer.optimization_level = 15

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

After the configuration is complete, restart the Apache server and write a PHP program:


Phpinfo ();


Generally, Zend Optimizer can increase the system efficiency by 30% ~ 40%. This is what users are most concerned about.

   Page Compression

HTTP1.1 supports page compression and transmission. That is to say, the server compresses a page and sends it to the client, decompress the page on the client, and then displays it to the customer. There are two transmission modes on the server. One is that the page has been compressed in advance, and only the compressed page can be transferred to the client during transmission. This is suitable for scenarios where there are many static webpages, however, for most websites, there are many dynamic pages. This method is not suitable, because many pages that are uploaded to the client do not actually exist and are generated dynamically when the server receives requests from the client user, therefore, each generated dynamic page must be compressed before being uploaded to the client. After PHP version 4.0.4. add a line in the INI file to configure "output_handler = ob_gzhandler", so that each dynamically generated page will be compressed before being transferred to the client. However, according to the instructions on the official site of PHP, this parameter cannot be used with "zlib. the output_compression = on parameter is used at the same time, because it is easy to cause PHP to work abnormally. In addition, it can only compress the dynamically generated pages of the PHP program, and it cannot work for a large number of static pages, especially image files. However, the mod_gzip module provides the Apahe function to compress a static page before it is sent to the client. The maximum compression ratio is 10, which is usually 3, that is to say, the website transmission rate has increased by more than three times. To use the mod_gzip function, you must first download the mod_gzip.c or file. the c file also needs to be converted. to use the so file, run the following command:

-I-a mod_gzio.c


Chmod 755/path/to/apache/libexec/

The system will go to/path/to/apache/etc/httpd. this module is automatically activated in conf. If the download is. so file, copy the file to the corresponding directory, and then in httpd. add LoadModule gzip_module libexec/ to the conf file to make the module take effect. Pay attention to two points. First, you need to use it. so file. Apache must contain the mod_so module (you can run the httpd-l command to check whether the module is effective). Second, if the download is. so file, which is related to the Apache version. Be sure to check whether the downloaded version is consistent with that of your own Apache version. the c file does not have this problem. After the module takes effect, configure Apache. You need to add some parameters to the httpd. conf file:

Mod_gzip_on Yes (whether the module is effective)

Mod_gzip_minimum_file_size 1002 (minimum compressed file size)

Mod_gzip_maximum_file_size 0 (maximum compressed file size, 0 indicates no limit)

Mod_gzip_maximum_inmem_size 60000 (maximum memory occupation)

Mod_gzip_item_include file "..gif 102 SINA> DOUBLE_QUOTATION (files ending with gif must 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 the compression module is used, the user will record the relevant information In the log file when accessing the site, for example, "mod_gzip: OK In: file_length Out: gzipfile_length ", indicates that the gzip function is used in the page transmission. The size of the input and output files is described.

   File Cache

This method is usually for CGI programs such as PHP and PERL, because these programs share a common feature that after receiving a user request, the results are not immediately returned to the user, instead, the interpreter returns the execution result to the customer after the interpreter is interpreted and executed, during which the database access is usually involved. In this case, a problem occurs. When two users access the same page, the system will perform operations on the two requests respectively, but in fact these two operations may be identical, this increases the burden on the system. Therefore, the common solution is to create a space in the system memory. When the user visits the page for the first time and stores the execution results in the memory, when a user visits the page again, the system calls the page directly from the memory without re-interpreting the execution. This memory space is called cache. Currently, there are two popular Cache Management Programs: FastCGI and Zend Cache of Zend Technologies. FastCGI is designed for CGI script programs such as Perl, C, and C ++. It can effectively use the memory for caching and send requests from clients to the FastCGI application service, fastCGI processes a user's request and returns the result to the user. Generally, the CGI program stops the process and exits automatically, but the FastCGI process continues to maintain. At this time, it does not need to create a new process after receiving a new user request, and can process user requests immediately, that is to say, the CGI program establishes the sequential execution of processes and then exits, while the FastCGI program executes sequentially and loops forever.

   Zend Cache Management Interface

To use FastCGI, you must first compile FastCGI into Apache. The method is very simple and is not described here. You must also set it in the http. conf file:

AddHandler fastcgi-script. fcg. fcgi. fpl

Options ExecCGI Indexes Includes

In this way, FastCGI can work normally. Below is an example program in the FastCGI programmer's Manual:

#! Fcgi-savvy-perl

Use FCGI; # Initialization

# Initialization code

= 0;

# Response loop

While (FCGI: accept> = 0 ){

# FastCGI create a loop body

Print "Content-type: text/html"; # Program Execution

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

Print "Running on <EM> </EM> to <EM> </EM> <BR> ";


Print "This is connection number ";


FastCGI is very powerful for CGI programs such as Perl, but it is powerless for PHP programs, and it needs to add content during programming, that is, it needs some artificial factors to play a role. In contrast, Zend Technologies's Zend Cache has powerful PHP caching functions. As long as the software is installed, programmers do not need to add code like other PHP programs, this allows you to implement the cache function, which facilitates system upgrades and is very convenient. It is a paid software. It has the Cache function and can manage it through the graphical interface. On the Cache Control page, you can configure Zend Cache to display its current status, and enable and disable the Zend Cache function. On the Scripts page, zend Cache content, including the status of each file. You can also select a file based on the number of clicks and Cache occupancy. On the Benchmark page, you can test the Cache Effect of Zend Cache, it is displayed graphically. it tests the number of PHP requests completed per second.

The installation and verification methods of Zend Optimizer are basically the same as those of Zend Optimizer, which is not described in detail here. Interested readers can refer to the user manual. Its powerful functions and convenient management methods are truly exciting.

The above are several common methods for website acceleration. Different methods are required for different websites, and the corresponding acceleration solutions are also different. However, the above three methods are similar in general, you can analyze the problem based on your actual situation.

Note: considering the complexity of the client software, some client software may not support some features. For example, mod_gzip compresses the page, but if the client uses Netscape, because it cannot decompress the received compressed page, the page cannot be displayed normally.
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: 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.