FastCGI is a scalable, high-speed communication interface between HTTP servers and dynamic scripting languages. Most popular HTTP servers support FastCGI, including Apache, Nginx, lighttpd, etc. At the same time, FastCGI is also supported by many scripting languages, including PHP, which is run with mod_fastcgi in PHP instead of the PHP acceleration mode run with mod_phpsusexec.
Features:
1. Persistent connection. No need to start and initialize for every request, it's fast.
2. It is said that it can make PHP execution 3 to 30 times faster. The reason is: the PHP interpreter is loaded into memory instead of being read from memory every time it is needed, greatly improving the performance of sites that rely on scripts.
3. Because there is no need to start and initialize every request, the usage of system resources (CPU) is also greatly reduced.
4. There is no need to modify the original PHP program at all.
5. There is only one available php.ini file. When you want to configure multiple php.ini files to meet different script needs, you need to disable PHP's fast CGI in its subdirectories. This ensures that other directories continue to be effective for CGI.
php runs in fastcgi mode under linux
1. Linux system
[root@localhost /]# cat /etc/redhat-release
CentOS release 5.2 (Final)
2. Install apache
Download and unzip apache. The version used in this article’s demonstration is 2.2.19
[root@localhost]# wget http://labs.renren.com/apache-mirror//httpd/httpd-2.2.19.tar.gz
[root@localhost]# tar xvzf httpd-2.2.19.tar.gz
Compile and install apache. The purpose of this article is just to demonstrate PHP running in fastcgi mode. For convenience, only select the necessary parameters to compile
[root@localhost]# cd httpd-2.2.19
[root@localhost]#./configure --enable-so
[root@localhost]#make
[root@localhost]#make install
3. Install mod_fcgid (note that it is mod_fcgid, not mod_fastcgi)
Download and unzip mod_fcgid. The version used in the test of this article is 2.3.6
[root@localhost]wget http://labs.renren.com/apache-mirror//httpd/mod_fcgid/mod_fcgid-2.3.6.tar.gz
[root@localhost]tar xvzf mod_fcgid-2.3.6.tar.gz
Compile and install mod_fcgid
[root@localhost]cd mod_fcgid-2.3.6
[root@localhost]APXS=/usr/local/apache2/bin/apxs ./configure.apxs
[root@localhost]make
[root@localhost]make install
Note: Some documents on the Internet do not have APXS=/usr/local/apache2/bin/apxs. For details, please refer to the README-FCGID document in the mod_fcgid software
4. Install PHP
Download and compress PHP, the PHP version is 5.3.6
[root@localhost]wget http://www.php.net/get/php-5.3.6.tar.bz2/from/cn.php.net/mirror
[root@localhost]tar xvjf php-5.3.6.tar.bz2
Compile and install PHP
[root@localhost] cd php-5.3.6
[root@localhost]./configure --prefix=/usr/local --with-configure-file-path=/etc
[root@localhost]make
[root@localhost]make install
Note: If there is an error reported by a library file, please install the relevant library file. The author's system once reported an error with libxml2. yum install libxml2-devel solved the problem. Many documents on the Internet say that the --enable-fastcgi parameter should be used. The author did not find this parameter in ./configure --help. Instead, I found a --disable-cgi parameter, so I decided not to add --enable-fastcgi. This parameter, later confirmed that this parameter can be omitted. The --with-apxs2 parameter must not be added.
5. Configuration
Create FastCGI script directory
[root@localhost]# mkdir /usr/local/apache2/fcgi-bin
[root@localhost]# ln -s /usr/local/php/bin/php-cgi /usr/local/apache2/fcgi-bin/php-cgi
Edit apache configuration file
[root@localhost]# vi /usr/local/apache/conf/httpd.conf
Add the following
ScriptAlias /fcgi-bin/ "/usr/local/php5/bin/"
AddHandler php-fastcgi .php
Action php-fastcgi /fcgi-bin/php-cgi
AddType application/x-httpd-php .php
IdleTimeout 300
ProcessLifeTime 1800
MaxProcessCount 100
DefaultMinClassProcessCount 3
DefaultMaxClassProcessCount 8
IPCConnectTimeout 15
IPCCommTimeout 300
MaxRequestsPerProcess 100
AllowOverride None
Options FollowSymLinks +ExecCGI
Order allow,deny
Allow from all
Modify
Options Indexes FollowSymLinks +ExecCGI
AllowOverride None
Order allow,deny
Allow from all
Note: The test.php file I tested is placed under the htdocs directory, so the permissions of the htdocs directory require ExecCGI. If the directories are different, please give the directory ExecCGI permissions. Another LoadModule fcgid_module modules/mod_fcgid.so is automatically added when mod_fcgid is installed.
6. Verification
Edit test page
[root@localhost]# vi /usr/local/apache2/htdocs/test.php
echo phpinfo();
?>
Configure FastCGI to run PHP in IIS7
Environment description:
Configuration steps: