lamp基础之lamp(php
大纲:1. CGI和fastcgi对比分析
2.用三台主机以fast-cgi的方式实现lamp并安装wordpress
3. https的实现过程
一、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服务
#setenforce 0
#iptables -F
# yum -y install httpd
# systemctlstart httpd
# 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模块已经装载
# 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.注销中心主机
# vim /etc/httpd/conf/httpd.conf
#DocumentRoot "/var/www/html"
# cat /etc/httpd/conf.d/vhost.conf
servername www.a.com
DocumentRoot /www/a/
options none
allowoverride none
require all granted
ProxyRequests off
ProxypassMatch /(.*\.php)$ fcgi://10.1.19.3:9000/fpmroot/php/$1
ProxypassMatch /(pm-status|ping)$ fcgi://10.1.19.3:9000/$1
# httpd -t
Syntax OK
# systemctl restart httpd
php-fpm
1.安装php-fpm
#setenforce 0
#iptables -F
# yum -y install php-fpm php-mysql
# vim /etc/php-fpm.d/www.conf
listen = 10.1.193:9000
listen.backlog = -1
listen.allowed_clients = 10.1.19.2
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 = /var/lib/php/session
session的存储位置
# systemctl start php-fpm
# ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:9000
在php-fpm服务器上,提供php测试页面,进行访问测试
因为前端httpd服务接收到用户对php页面的请求时,是直接将请求转交给php来处理,所以,php的页面
资源应该存放在php自身的服务器上
# mkdir /fpmroot/php/
# mkdir /fpmroot/php/test.php
简单测试一下,看看是否连接成功
status输出:
pool: www #连接池名称
process manager: dynamic#进程管理器类型
start time: 11/Oct/2016:11:22:04 +0800 #启动日期时间
start since: 917# 运行时长
accepted conn: 9# 连接池已经处理过的请求数
listen queue: 0# 请求队列长度
max listen queue: 0 # 请求队列的最大长度
listen queue len: 128 # socket等待队列的长度
idle processes: 4# 空闲进程数量
active processes: 1 # 活跃的进程数量
total processes: 5 # 连接池中的总进程数量
max active processes: 2 # 连接池中最大的活跃进程数量
max children reached: 0 # 达到连接池中最大进程数量限制的上限的次数
slow requests: 0 :启用了slow-log时,记录慢请求的数量
其它格式的输出:
/status?json
/status?xml
/status?html
/status?full
full格式的输出:
pid: 1378
state: Idle # 当前进程状态,idle, running, ...
start time: 11/Oct/2016:11:22:04 +0800#进程的启动日期时间
start since: 1386# 运行时长
requests: 3# 处理过的请求数量
request duration: 119# 请求时长,单位是微秒
request method: GET# 请求方法,GET,POST等;
request URI: /status?html# 请求的URL
content length: 0 #请求内容的长度,POST方法才有意义 ;
user: -# 用户,由PHP_auth_user认证的用户;
script: - # 运行的php脚本;
last request cpu: 0.00# 最后一次请求的CPU使用量;
last request memory:262144# 最后一次请求的内存使用量;
注意:一定要记得创建/var/lib/php/session目录,并且给apache用户权限
mariadb
a.配置mariadb服务
# yum -y installmariadb
# mysql_secure_installation
# systemctl start mariadb.service b.配置mysql,授权一个账号,供php连接使用
# 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
# cat mysql.php
现在测试实现wordpress服务
1.在php-fpm主机上 :下载wordpress并将其解压缩后放置到php-fpm的根目录下
# unzip wordpress-4.3.1-zh_CN.zip
# cp -awordpress/*/fpmroot/php/ 2.在php-fpm主机上: 编辑wordpress的配置文件
# vim wp-config.php
define('DB_NAME', 'wdpress');
/** MySQL数据库用户名 */
define('DB_USER', 'wduser');
/** MySQL数据库密码 */
define('DB_PASSWORD', 'wdpass');
/** MySQL主机 */
define('DB_HOST', '10.1.19.1');#定义数据库
# systemctl restart php-fpm
3.在php-fpm主机上: 创建session,并且赋予apache用户权限
# mkdir /var/lib/php/session
# chown apache /var/lib/php/session
4.在mariadb主机上 : 创建wordpress所需要的库,并且授权用户
# mysql
MariaDB [(none)]> create database wdpress;
MariaDB [(none)]> grant all on wdpress.* to 'wduser'@'10.1.19.3' identified by 'wdpass';
Query OK, 0 rows affected (0.08 sec)
5.安装wordpress
第四部分 https的实现
1、在任意一个服务上建立私有CA(本例在mysql所在的节点上构建私有CA)
a)生成私钥文件
#(umask 077;openssl genrsa -out private/cakey.pem 2048) b)确保CA工作的目录
# ls /etc/pki/CA/
certscrlnewcertsprivate c)创建CA工作需要的证书序列号文件和证书数据库
# touch /etc/pki/CA/{serial,index.txt} d)提供证书初始编号
# echo 01 > /etc/pki/CA/serial e)生成CA自签证书
# openssl req -new -x509 -key private/cakey.pem -out cacert.pem -days 365 #此时工作目录是/etc/pki/CA
Country Name (2 letter code) :CN
State or Province Name (full name) []:beijing
Locality Name (eg, city) :beijing
Organization Name (eg, company) :magdu.com
Organizational Unit Name (eg, section) []:ops
Common Name (eg, your name or your server's hostname) []:magedu.com
# tree
.
├── cacert.pem
├── certs
├── crl
├── index.txt
├── newcerts
│ └── 01.pem
├── private
│ └── cakey.pem
└── serial2、在httpd服务器上生成证书签署请求,发送给私有CA所在服务器
a 生成httpd服务的私钥文件
#mkdir /etc/httpd/ssl
#cd /etc/httpd/ssl
# (umask 077; openssl genrsa -out httpd.key 2048)
Generating RSA private key, 2048 bit long modulus
.....................................................................+++
......................+++
e is 65537 (0x10001) b生成证书签署请求
# openssl req -new -key httpd.key -out httpd.csr -days 365
Country Name (2 letter code) :CN
State or Province Name (full name) []:beijing
Locality Name (eg, city) :beijing
Organization Name (eg, company) :magdu.com
Organizational Unit Name (eg, section) []:ops
Common Name (eg, your name or your server's hostname) []:magedu.com
Email Address []: c 将请求证书发给CA ,现实生活中证书请求和证书的获得都要安全严格
# scp httpd.csr root@10.1.19.1:/root
root@10.1.19.1's password:
httpd.csr 100% 1005 1.0KB/s 00:00
3、在CA服务器上签署请求,并将证书发给httpd服务器
a 签署证书
# openssl ca -in httpd.csr -out httpd.crt -days 365
b 将证书发给httpd服务器
# scp httpd.crt root@10.1.19.2:/etc/httpd/ssl/
httpd.crt 100% 3829 3.7KB/s 00:00
4、配置httpd服务器支持ssl
# ls #将证书请求文件在httpd服务器和CA端都删除,确保安全
httpd.crt httpd.key
2.配置http服务支持ssl
1
2
3
4
5
6
7
8
9
# rpm -q mod_ssl
package mod_ssl is not installed
# yum -y install mod_ssl
# rpm -ql mod_ssl
/etc/httpd/conf.d/ssl.conf
/etc/httpd/conf.modules.d/00-ssl.conf
/usr/lib64/httpd/modules/mod_ssl.so
/usr/libexec/httpd-ssl-pass-dialog
/var/cache/httpd/ssl
3.配置ssl.conf
1
2
3
4
5
6
7
8
9
# vim /etc/httpd/conf.d/ssl.conf
ServerName magedu.com:443
SSLCertificateFile /etc/httpd/ssl/http.crt
SSLCertificateKeyFile /etc/httpd/ssl/http.key
# httpd -t
Syntax OK
# service httpd restart
# ss -tan | grep 443
LISTEN 0 128 :::443 :::*
5、测试https是否正常访问
[*]将服务器CA根证书导入到浏览器,然后访问;
[*]测试成功了
页:
[1]