大纲: 1. CGI和fastcgi对比分析 2. 用三台主机以fast-cgi的方式实现lamp并安装wordpress 3. 对输出结果的详解
一、CGI和fastcgi对比分析 lamp 安装http和php的结合方式可以分为三种:
1.php作为http的模块
2.以cgi模式结合(极少使用)
3.以fastcgi模式结合
CGI 简介
CGI全称是“通用网关接口”(Common Gateway Interface),它可以让一个客户端,从网页浏览器向执行在Web服务器上的程序请求数据。 CGI描述了客户端和这个程序之间传输数据的一种标准。 CGI的一个目的是要独立于任何语言的,所以CGI可以用任何一种语言编写,只要这种语言具有标准输入、输出和环境变量。 如php,perl,tcl等。
CGI 的运行原理
客户端访问某个 URL 地址之后,通过 GET/POST/PUT 等方式提交数据,并通过 HTTP 协议向 Web 服务器发出请求。
服务器端的 HTTP Daemon(守护进程)启动一个子进程。然后在子进程中,将 HTTP 请求里描述的信息通过标准输入 stdin 和环境变量传递给 URL 指定的 CGI 程序,并启动此应用程序进行处理,处理结果通过标准输出 stdout 返回给 HTTP Daemon 子进程。
再由 HTTP Daemon 子进程通过 HTTP 协议返回给客户端。
上面的这段话理解可能还是比较抽象,下面我们就通过一次 GET 请求为例进行详细说明。
如图所示,本次请求的流程如下:
客户端访问 http://127.0.0.1:9003/cgi-bin/user?id=1
127.0.0.1 上监听 9003 端口的守护进程接受到该请求
通过解析 HTTP 头信息,得知是 GET 请求,并且请求的是 /cgi-bin/ 目录下的 user 文件。
将 uri 里的 id=1 通过存入 QUERY_STRING 环境变量。
Web 守护进程 fork 一个子进程,然后在子进程中执行 user 程序,通过环境变量获取到id。
执行完毕之后,将结果通过标准输出返回到子进程。
子进程将结果返回给客户端。
FastCGI 简介
FastCGI是Web服务器和处理程序之间通信的一种协议, 是CGI的一种改进方案,FastCGI像是一个常驻(long-lived)型的CGI, 它可以一直执行,在请求到达时不会花费时间去fork一个进程来处理(这是CGI最为人诟病的fork-and-execute模式)。 正是因为他只是一个通信协议,它还支持分布式的运算,所以 FastCGI 程序可以在网站服务器以外的主机上执行,并且可以接受来自其它网站服务器的请求。
FastCGI 是与语言无关的、可伸缩架构的 CGI 开放扩展,将 CGI 解释器进程保持在内存中,以此获得较高的性能。 CGI 程序反复加载是 CGI 性能低下的主要原因,如果 CGI 程序保持在内存中并接受 FastCGI 进程管理器调度, 则可以提供良好的性能、伸缩性、Fail-Over 特性等。
总结:
- mode_php 是Apache 的一个模块,把PHP 解释器嵌入到Apache 进程中。
- CGI 和FastCGI 分别是一种协议。Web Server 实现了CGI 或FastCGI 协议的相应的应用程序(以下简称CGI 或FastCGI),就可以启动PHP 解释器处理PHP 请求。它们都是以独立进程的形式存在。
- mode_php 和FastCGI 在 单个进程中可以处理多个请求,CGI 在单个进程中只能处理一个请求。
php-cgi 是一种CGI 协议的实现。
- php-cgi 其实就是PHP 解析器。
- 在CGI 模式时,当Web Server 收到 xx/index.php 请求时,会启动php-cgi,php-cgi 会解析php.ini 文件,初始化环境,然后根据请求参数进行处理,再返回处理后的结果。(都是以CGI 协议规范来进行)
- php-cgi 在每个请求时都会启动一个进程,然后读取php.ini 进行解析,可想而知效率相对比较低。
- php-cgi 无法实现平滑重启。修改php.ini 配置后,后面启动的php-cgi 程序还是不会感知。
php-fpm 即FastCGI Process Management,是一种FastCGI 协议的实现。
- 当请求到来时,php-fpm 启动并读取php.ini 文件完成初始化环境,然后启动一个master,再启动多个worker。当请求过来时,master 会传递给一个worker,然后等待下一个请求。php-fpm 会动态配置worker 的数量。
- 一个php-fpm 进程可以处理多个请求,会启动多个php-cgi 程序。当worker不够用时,master可以根据配置预先启动几个worker等着;当然空闲worker太多时,也会停掉一些,这样就提高了性能,也节约了资源
- php-fpm 可以实现平衡重启。修改php.ini 后,当启用新的worker 会使用新的配置。
在实际生产中由于压力较大,所以amp一般是分别独立的服务器,甚至是服务器组,而且使用的是高可用集群,以分担单个服务器的压力,同时避免单点故障,所以本次将利用三台主机来模拟lamp的工作场景。
二、用三台主机以fast-cgi的方式实现lamp并安装wordpress
环境:
主机
| 系统
| IP
| Apache
| Centos7
| 10.1.19.2
| Php-fpm
| Centos7
| 10.1.19.3
| Mariadb
| Centos7
| 10.1.19.1
|
Apache:
1.配置httpd服务
1
2
3
4
5
6
7
8
9
| [iyunv@centos7-2 ~]#setenforce 0
[iyunv@centos7-2 ~]#iptables -F
[iyunv@centos7-2 ~]# yum -y install httpd
[iyunv@centos7-2 ~]# systemctl start httpd
[iyunv@centos7-2 ~]# ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 5 192.168.122.1:53 *:*
LISTEN 0 128 *:10050 *:*
LISTEN 0 128 :::80
|
2.确保proxy模块已经装载
1
2
3
4
5
6
7
8
9
10
11
12
| [iyunv@centos7-2 conf.modules.d]# httpd -M | grep proxy
proxy_module (shared) #主模块
proxy_ajp_module (shared)
proxy_balancer_module (shared)
proxy_connect_module (shared)
proxy_express_module (shared)
proxy_fcgi_module (shared) #fcgi代理模块
proxy_fdpass_module (shared)
proxy_ftp_module (shared)
proxy_http_module (shared)
proxy_scgi_module (shared)
proxy_wstunnel_module (shared)
|
3.设置动态资源的代理,本次采用虚拟主机的方式
a.注销中心主机
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| [iyunv@centos7-2 ~]# vim /etc/httpd/conf/httpd.conf
#DocumentRoot "/var/www/html"
[iyunv@centos7-2 ~]# cat /etc/httpd/conf.d/vhost.conf
<Virtualhost 10.1.19.2:80>
servername www.a.com
DocumentRoot /www/a/
<Directory /www/a>
options none
allowoverride none
require all granted
</Directory>
ProxyRequests off
ProxypassMatch /(.*\.php)$ fcgi://10.1.19.3:9000/fpmroot/php/$1
ProxypassMatch /(pm-status|ping)$ fcgi://10.1.19.3:9000/$1
</Virtualhost>
[iyunv@centos7-2 ~]# httpd -t
Syntax OK
[iyunv@centos7-2 ~]# systemctl restart httpd
|
4、在php-fpm服务器上,提供php测试页面,进行访问测试因为前端httpd服务接收到用户对php页面的请求时,是直接将请求转交给php来处理,所以,php的页面资源应该存放在php自身的服务器上
php-fpm
1.安装php-fpm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
| [iyunv@centos7-2 ~]#setenforce 0
[iyunv@centos7-2 ~]#iptables -F
[iyunv@centos7-3 ~]# yum -y install php-fpm php-mysql
[iyunv@centos7-3 ~]# systemctl start php-fpm
[iyunv@centos7-3 ~]# vim /etc/php-fpm.d/www.conf
listen = 0.0.0.0:9000 #监听的地址,应该修改一个可被访问的
listen.allowed_clients = 10.1.19.2 #允许访问的客户端
pm.status_path = /pm-status
ping.path = /ping
ping.response = pong
[iyunv@centos7-3 ~]# ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:9000
[iyunv@centos7-3 php]# mkdir /fpmroot/php/
[iyunv@centos7-3 php]# mkdir /fpmroot/php/test.php
<?php
phpinfo();
?>
|
4、在php-fpm服务器上,提供php测试页面,进行访问测试因为前端httpd服务接收到用户对php页面的请求时,是直接将请求转交给php来处理,所以,php的页面资源应该存放在php自身的服务器上
mariadb
a.配置mariadb服务
1
2
3
| [iyunv@centos7-1 html]# yum -y install mariadb
[iyunv@centos7-1 html]# mysql_secure_installation
[iyunv@centos7-1 html]# systemctl start mariadb.service
|
b.配置mysql,授权一个账号,供php连接使用
1
2
3
| [iyunv@centos7-1 html]# mysql -uroot -predhat
MariaDB [(none)]> grant all ON *.* to 'fpmuser'@'10.1.19.3' identified by 'fpmpass';
Query OK, 0 rows affected (2.52 sec)
|
c.在php-fpm上提供页面,测试php是否可以连接mariadb
1
2
3
4
5
6
7
8
| [iyunv@centos7-3 php]# cat mysql.php
<?php
$conn = mysql_connect("10.1.19.1","fpmuser","fpmpass");
if ($conn)
echo "success";
else
echo "fail";
?>
|
第四部分 https的实现1、在任意一个服务上建立私有CA(本例在mysql所在的节点上构建私有CA)
4、在httpd服务器上安装https所需的httpd的模块mod_ssl,修改httpd关于ssl的配置文件,让其符合当前工作环境 ##################生成私钥文件##################[iyunv@mysql ~]# (umask 077;openssl genrsa -out /etc/pki/CA/private/cakey.pem 2048)Generating RSA private key, 2048 bit long modulus
...............+++
...........................+++
e is 65537 (0x10001)
[iyunv@mysql ~]# ##################确保CA工作的目录存在certs、newcerts、crl##################[iyunv@mysql ~]# ls /etc/pki/CA/ certs crl newcerts private
[iyunv@mysql ~]# ##################创建CA工作需要的证书序列号文件和证书数据库索引文件##################[iyunv@mysql ~]# touch /etc/pki/CA/{serial,index.txt} [iyunv@mysql ~]# ##################提供证书初始编号##################[iyunv@mysql ~]# echo 01 > /etc/pki/CA/serial[iyunv@mysql ~]# ##################生成CA自签证书##################[iyunv@mysql ~]# openssl req -new -x509 -key /etc/pki/CA/private/cakey.pem -out /etc/pki/CA/cacert.pem -days 3650You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
----- ##################填入相关信息##################Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:BeiJing
Locality Name (eg, city) [Default City]:BeiJing
Organization Name (eg, company) [Default Company Ltd]:nwccompany
Organizational Unit Name (eg, section) []:ops
Common Name (eg, your name or your server's hostname) []:ca.nwc.com
Email Address []:caadmin@nwc.com
[iyunv@mysql ~]#
2、在httpd服务器上生成证书签署请求,发送给私有CA所在服务器
[iyunv@httpd ~]# mkdir /etc/httpd/ssl ######生成证书相关文件存放目录#######[iyunv@httpd ~]# ######生成私钥文件#######[iyunv@httpd ~]# (umask 077;openssl genrsa -out /etc/httpd/ssl/httpd.key 1024)Generating RSA private key, 1024 bit long modulus
......................++++++
.............++++++
e is 65537 (0x10001)
[iyunv@httpd ~]# [iyunv@httpd ~]# [iyunv@httpd ~]# ######生成证书签署请求#######[iyunv@httpd ~]# openssl req -new -key /etc/httpd/ssl/httpd.key -out /etc/httpd/ssl/httpd.csrYou are about to be asked to enter information that will be incorporatedinto your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blankFor some fields there will be a default value,If you enter '.', the field will be left blank.----- ######填入相关信息,注意hostname要与用户访问时的域名一致#######Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:BeiJing
Locality Name (eg, city) [Default City]:BeiJing
Organization Name (eg, company) [Default Company Ltd]:nwccompany
Organizational Unit Name (eg, section) []:ops
Common Name (eg, your name or your server's hostname) []:www.a.comEmail Address []:wwwadmin@a.com
Please enter the following 'extra' attributesto be sent with your certificate request
A challenge password []:
An optional company name []:
[iyunv@httpd ~]# ######拷贝证书签署请求到私有CA服务器#######[iyunv@httpd ~]# scp /etc/httpd/ssl/httpd.csr 10.1.32.68:/tmproot@10.1.32.68's password: httpd.csr 100% 696 0.7KB/s 00:00 [iyunv@httpd ~]#
3、在私有CA服务器上进行证书的签署,并将签署后的证书发送给httpd服务器
[iyunv@mysql ~]# ls /tmphttpd.csr yum.log
[iyunv@mysql ~]# ####### 签署证书 ########[iyunv@mysql ~]# openssl ca -in /tmp/httpd.csr -out /etc/pki/CA/certs/httpd.crt -days 365Using configuration from /etc/pki/tls/openssl.cnf
Check that the request matches the signature
Signature ok
Certificate Details:
Serial Number: 1 (0x1)
Validity
Not Before: Jul 13 09:46:29 2016 GMT
Not After : Jul 13 09:46:29 2017 GMT Subject:
countryName = CN
stateOrProvinceName = BeiJing
organizationName = nwccompany
organizationalUnitName = ops
commonName = www.a.com
emailAddress = wwwadmin@a.com
X509v3 extensions:
X509v3 Basic Constraints:
CA:FALSE
Netscape Comment:
OpenSSL Generated Certificate
X509v3 Subject Key Identifier:
42:AF:40:00:F2:CD:2F:05:2E:91:C3:AB:66:DB:04:D4:8C:E2:90:A4
X509v3 Authority Key Identifier:
keyid:55:E3:92:99:17:92:2B:53:19:AE:57:29:34:AA:D0:1E:C3:04:88:54Certificate is to be certified until Jul 13 09:46:29 2017 GMT (365 days)
Sign the certificate? [y/n]:y 1 out of 1 certificate requests certified, commit? [y/n]y
Write out database with 1 new entries
Data Base Updated
[iyunv@mysql ~]# ####### 复制证书到httpd服务器 ########[iyunv@mysql ~]# scp /etc/pki/CA/certs/httpd.crt 10.1.32.72:/etc/httpd/ssl/The authenticity of host '10.1.32.72 (10.1.32.72)' can't be established.
RSA key fingerprint is 61:20:77:df:ac:5c:a5:5c:8d:05:54:dc:f0:77:bc:ba.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '10.1.32.72' (RSA) to the list of known hosts.
root@10.1.32.72's password: httpd.crt 100% 3845 3.8KB/s 00:00 [iyunv@mysql ~]# [iyunv@mysql ~]#
修改配置文件 由于同一个IP只支持定义一个https,故本例中将之前定义的哪个虚拟主机定义为https的页面 将准备定义为https的虚拟主机的定义注释掉
在ssl的虚拟主机中定义:
5、测试https访问注意,因为是私有CA颁发的证书因此要将私有CA的自签证书导入浏览器的可信任的根CA颁发机构后,然后进行测试访问
3.编辑fpm的配置文件:
[iyunv@localhost pma]# vim /etc/php-fpm.d/www.conf
[pool_id]
listen = 127.0.0.1:9000
listen.backlog = -1
listen.allowed_clients = 127.0.0.1
user = apache
group = apache
pm = dynamic
定义processor管理机制:static, dynamic
pm.max_children:最大子进程数量;连接池的最大容量;
pm.start_servers:服务启动时所启动的子进程数量;
pm.min_spare_servers
pm.max_spare_servers
rlimit_files = 1024
rlimit_core = 0
pm.status_path = /status
ping.path = /ping
ping.response = pong
php_value[session.save_path] = /var/lib/php/session
session的存储位置
4.重新启动httpd和php-fpm服务
5.测试
a在浏览器中输入http://10.1.252.228/status?full 以及http://10.1.252.228/ping 看看是否会响应pong
pool: www
process manager: dynamic
start time: 11/Oct/2016:21:15:46 +0800
start since: 577
accepted conn: 16
listen queue: 0
max listen queue: 1
listen queue len: 128
idle processes: 4
active processes: 3
total processes: 7
max active processes: 3
max children reached: 0
slow requests: 0
b解压phpmysqladmin到/vhost/www下看是否可以进入
#yum install -y php-mbstring, php-mcrypt, php-xcache
注意:一定要记得创建/var/lib/php/session目录,并且给apache用户权限
|