Fastcgi
CGI全称是“公共网关接口”(Common Gateway Interface),HTTP服务器与你的或其它机器上的程序进行“交谈”的一种工具,其程序一般运行在网络服务器上。 CGI可以用任何一种语言编写,只要这种语言具有标准输入、输出和环境变量。如php,perl,tcl等,是最早期动态网站的实现,但诟病也是很多,现在基本是处于废弃状态。
FastCGI像是一个常驻(long-live)型的CGI,它可以一直执行着,只要激活后,不会每次都要花费时间去fork一次(这是CGI最为人诟病的fork-and-execute 模式)。它还支持分布式的运算, 即 FastCGI 程序可以在网站服务器以外的主机上执行并且接受来自其它网站服务器来的请求。
FastCGI是语言无关的、可伸缩架构的CGI开放扩展,其主要行为是将CGI解释器进程保持在内存中并因此获得较高的性能。众所周知,CGI解释器的反复加载是CGI性能低下的主要原因,如果CGI解释器保持在内存中并接受FastCGI进程管理器调度,则可以提供良好的性能、伸缩性、Fail- Over特性等等,Fastcgi是最常用的应用环境就是lamp和lnmp环境。
php
现在我将php编译成fastcgi的工作模式,按照现在最新的版本php-7.1来实现。
实验环境centos 6
1 | ~]# ./configure --prefix=/usr/local/php \ |
2 | --sysconfdir=/usr/local/php/etc \ |
3 | --with-curl \ |
4 | --with-freetype-dir \ |
5 | --with-gd \ |
6 | --with-gettext \ |
7 | --with-iconv-dir \ |
8 | --with-kerberos \ |
9 | --with-libdir=lib64 \ |
10 | --with-libxml-dir \ |
11 | --with-mysqli \ |
12 | --with-openssl \ |
13 | --with-pcre-regex \ |
14 | --with-pdo-mysql=/usr/local/mariadb \ |
15 | --with-pdo-sqlite \ |
16 | --with-pear \ |
17 | --with-png-dir \ |
18 | --with-xmlrpc \ |
19 | --with-xsl \ |
20 | --with-zlib \ |
21 | --enable-fpm \ |
22 | --enable-bcmath \ |
23 | --enable-libxml \ |
24 | --enable-inline-optimization \ |
25 | --enable-gd-native-ttf \ |
26 | --enable-mbregex \ |
27 | --enable-mbstring \ |
28 | --enable-opcache \ |
29 | --enable-pcntl \ |
30 | --enable-shmop \ |
31 | --enable-soap \ |
32 | --enable-sockets \ |
33 | --enable-sysvsem \ |
34 | --enable-xml \ |
35 | --enable-zip |
36 | |
37 | ~]# make && make install |
38 | ~]# cp php.ini-production /usr/local/php/etc/php.ini |
39 | ~]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm |
40 | ~]# cd /usr/local/php/etc |
41 | ~]# cp php-fpm.conf.default php-fpm.conf |
42 | ~]# cd php-fpm.d |
43 | ~]# cp www.conf.default www.conf |
44 | ~]# cat www.conf |
45 | pm = dynamic |
46 | pm.max_children php-fpm 最大fork的子进程 |
47 | pm.start_servers php-fpm 开始的进程 |
48 | pm.min_spare_servers php-fpm 最小的空闲进程数 |
49 | pm.max_spare_servers php-fpm 最大的空闲进程数 |
50 | pm.process_idle_timeout php-fpm 空闲的超时时间 |
51 | pm.max_requests php-fpm 每个进程响应的最大的请求数 |
52 | ~]# service php-fpm start |
httpd2.4+Fastcgi
htpd-2.2原生的是不支持fastcgi模式,只支持编译成httpd内部模块的形式工作,所以这里我选择httpd-2.4来完成fastcgi的功能。
1 | 依赖apr-1.4+,apr-util-1.4+ [apr-icon] |
2 | apr:apache protable runtime |
3 | |
4 | CentOS 6: |
5 | 默认:apr-1.3.9 apr-util-1.3.9 |
6 | |
7 | 编译安装步骤: |
8 | (1)apr-1.4+ |
9 | ./configure --prefix=/usr/local/apr |
10 | make && make install |
11 | (2)apr-util-1.4+ |
12 | ./configure --prefix=/usr/local/apr-util |
13 | make && make install |
14 | (3)httpd-2.4 |
15 | ./configure |
16 | --prefix=/usr/local/httpd24 |
17 | --sysconfdir=/usr/local/httpd24/conf |
18 | --enable-proxy |
19 | --enable-proxy-fcgi |
20 | --enable-so |
21 | --enable-ssl |
22 | --enable-cgi |
23 | --enable-rewrite |
24 | --with-zlib |
25 | --with-pcre |
26 | --with-apr=/usr/local/apr |
27 | --with-apr-util=/usr/local/apr-util/ |
28 | --enable-modules=most |
29 | --enable-mpms-shared=all |
30 | --with-mpm=prefork |
31 | make && make install |
配置httpd
1 | ~]# cat /usr/local/httpd24/conf/httpd.conf |
2 | 关键配置: |
3 | LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so |
4 | LoadModule proxy_module modules/mod_proxy.so |
5 | AddType application/x-httpd-php .php |
6 | AddType application/x-httpd-php-source .phps |
7 | Include conf/extra/httpd-vhosts.conf |
8 | <IfModule dir_module> |
9 | DirectoryIndex index.html index.php |
10 | </IfModule> |
11 | |
12 | ~]# cat /usr/local/httpd24/extra/httpd-vhosts.conf |
13 | <VirtualHost *:80> |
14 | DocumentRoot '/www' |
15 | ServerName www.jusene.com |
16 | CustomLog "logs/jusene.log" combined |
17 | ProxyPassMatch ^(.*\.php)$ fcgi://127.0.0.1:9000/www/$1 |
18 | <Directory '/www'> |
19 | Options None |
20 | AllowOverride None |
21 | Require all granted |
22 | </Directory> |
23 | </VirtualHost> |
24 | |
25 | <VirtualHost *:80> |
26 | DocumentRoot '/www1' |
27 | ServerName www.jusene1.com |
28 | CustomLog "logs/jusene1.log" combined |
29 | <FilesMatch '\.php$'> |
30 | SetHandler "proxy:fcgi://127.0.0.1:9000" |
31 | </FilesMatch> |
32 | <Directory '/www1'> |
33 | Options None |
34 | AllowOverride None |
35 | Require all granted |
36 | </Directory> |
37 | </VirtualHost> |
38 | |
39 | <VirtualHost *:80> |
40 | DocumentRoot '/www2' |
41 | ServerName www.jusene2.com |
42 | CustomLog "logs/jusene2.log" combined |
43 | <Directory '/www2'> |
44 | Options None |
45 | AllowOverride None |
46 | Require all granted |
47 | </Directory> |
48 | <LocationMatch ^(.*\.php)$> |
49 | ProxyPass fcgi://127.0.0.1:9000/www |
50 | ProxyErrorOverride on |
51 | </LocationMatch> |
52 | </VirtualHost> |
以上提供了三种虚拟主机使用php-fpm的方法。
httpd2.4+php_mod
需要将php编译成httpd的模块,这里只是简单写下,因为网上太多将php编译成模块工作的文章了,而php-fpm就相对较少,因为这样的工作方式更好应用在nginx上,配置还相对简单。
1 | ~]# ./configure --prefix=/usr/local/php \ |
2 | --sysconfdir=/usr/local/php/etc \ |
3 | --with-curl \ |
4 | --with-freetype-dir \ |
5 | --with-gd \ |
6 | --with-gettext \ |
7 | --with-iconv-dir \ |
8 | --with-kerberos \ |
9 | --with-libdir=lib64 \ |
10 | --with-libxml-dir \ |
11 | --with-mysqli \ |
12 | --with-openssl \ |
13 | --with-pcre-regex \ |
14 | --with-pdo-mysql=/usr/local/mariadb \ |
15 | --with-pdo-sqlite \ |
16 | --with-pear \ |
17 | --with-png-dir \ |
18 | --with-xmlrpc \ |
19 | --with-xsl \ |
20 | --with-zlib \ |
21 | --with-apxs2=/usr/local/http24/bin/apxs \ 需要使用到httpd的第三方扩展工具,所以必须先安装httpd |
22 | --enable-bcmath \ |
23 | --enable-libxml \ |
24 | --enable-inline-optimization \ |
25 | --enable-gd-native-ttf \ |
26 | --enable-mbregex \ |
27 | --enable-mbstring \ |
28 | --enable-opcache \ |
29 | --enable-pcntl \ |
30 | --enable-shmop \ |
31 | --enable-soap \ |
32 | --enable-sockets \ |
33 | --enable-sysvsem \ |
34 | --enable-xml \ |
35 | --enable-zip |
36 | |
37 | ~]# make && make install |
配置httpd.conf
加载模块
LoadModule php7_module modules/libphp7.so
添加MIME
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
修改默认首页文件
DirectoryIndex index.php index.html
配置虚拟主机
打开虚拟主机配置文件
Virtual hosts
Include conf/extra/httpd-vhosts.conf
1 | ~]# cat /usr/local/httpd24/conf/extra/httpd-vhosts.conf |
2 | <VirtualHost *:80> |
3 | DocumentRoot "/www" |
4 | ServerName www.jusene.com |
5 | DirectoryIndex index.php |
6 | ErrorLog "logs/dummy-host.example.com-error_log" |
7 | CustomLog "logs/dummy-host.example.com-access_log" common |
8 | <Directory '/www'> |
9 | Options None |
10 | AllowOverride None |
11 | RequireAll all granted |
12 | </Directory> |
13 | </VirtualHost> |