前言
CentOS 7:
httpd-2.4:rpm包默认编译支持了fcgi模块;
php-fpm包:专用于将php运行于fpm模式;
当我们使用php-fpm方式部署LAMP时,需要使用三台服务器,一台服务器作为http服务器,一台当作php-fpm服务器,一台作为数据库服务器。当http服务器接收到客户端的请求时,会通过本地磁盘IO返回出请求中的静态请求资源,当http服务器匹配到请求数据中的动态资源后,通过fastcgi将动态资源请求转发给php-fpm服务器的9000端口,php-fpm服务器通过磁盘IO请求本地的动态资源并返回给http服务器,如果有请求数据库资源,就请求数据库的3306端口从数据库服务器中获取资源并返回http服务器,最后服务器统一将请求到的资源返回客户端。
基于这种方式在并发请求数量很大的时候,会提高数据的相应速率。
第一部分
准备三台主机一台为http服务器,一台为php-fpm服务器,一台为mariadb服务器,基于fastcgi方式部署LAMP(CentOS 7)
第一步:(部署http服务器)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
[iyunv@HTTP ~]# ifconfig #http服务器的地址
eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.16.61.2 netmask 255.255.0.0 broadcast 172.16.255.255
inet6 fe80::20c:29ff:fefe:9633 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:fe:96:33 txqueuelen 1000 (Ethernet)
RX packets 203985 bytes 53699541 (51.2 MiB)
RX errors 0 dropped 31 overruns 0 frame 0
TX packets 5381 bytes 1764387 (1.6 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[iyunv@HTTP ~]# yum install -y httpd #安装httpd程序
[iyunv@HTTP ~]# rpm -q httpd
httpd-2.4.6-31.el7.centos.x86_64
[iyunv@HTTP ~]# vim /etc/httpd/conf.d/vhost.conf #创建两个虚拟主机
DirectoryIndex index.php
<VirtualHost *:80>
ServerName www.a.com
DocumentRoot /vhost/a.com
ProxyRequests off #关闭正向解析
ProxyPassMatch ^/(.*\.php)$ fcgi://172.16.61.100:9000 /www/$1 #通过fcgi发送给php主机的并从/www/路径下寻找资源
<Directory "/vhost/a.com">
Options None
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerName www.b.com
DocumentRoot /vhost/b.com
ProxyRequests off
ProxyPassMatch ^/(.*\.php)$ fcgi://172.16.61.100:9000 /www/$1
<Directory "/vhost/b.com">
Options None
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
[iyunv@HTTP ~]# mkdir -pv /vhost/{a.com,b.com} #为虚拟主机创建DocumentRoot
mkdir: 已创建目录 "/vhost"
mkdir: 已创建目录 "/vhost/a.com"
mkdir: 已创建目录 "/vhost/b.com"
[iyunv@localhost ~]# vim /etc/httpd/conf/httpd.conf
....
#DocumentRoot "/var/www/html" #注释掉中心主机的DocumentRoot
....
[iyunv@HTTP ~]# systemctl start httpd #启动httpd服务
[iyunv@HTTP ~]# ss -tan
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 50 *:3306 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
ESTAB 0 52 172.16.61.2:22 172.16.61.1:64597
LISTEN 0 128 :::80 :::* #确定80端口处于监听状态
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
第二步:(部署php主机)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
[iyunv@PHP ~]# ifconfig #php主机的地址
eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.16.61.100 netmask 255.255.0.0 broadcast 172.16.255.255
inet6 fe80::20c:29ff:fe70:e227 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:70:e2:27 txqueuelen 1000 (Ethernet)
RX packets 199355 bytes 14076483 (13.4 MiB)
RX errors 0 dropped 35 overruns 0 frame 0
TX packets 433 bytes 42949 (41.9 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[iyunv@PHP ~]# yum install -y php-fpm #安装php-fpm程序
[iyunv@PHP ~]# mkdir /www #创建php资源路径
[iyunv@PHP ~]# systemctl start php-fpm #开启php-fpm程序
[iyunv@PHP ~]# ss -tan
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 127.0.0.1:9000 *:* #确定9000端口处于监听状态
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
ESTAB 0 0 172.16.61.100:22 172.16.61.1:51354
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
[iyunv@PHP ~]# vim /etc/php-fpm.d/ #编辑php-fpm的配置文件
...
listen = 172.16.61.100:9000 #监听于本机可以与外界通信的地址的9000端口
...
listen.allowed_clients = 172.16.61.2 #允许http服务器请求
第三步:(部署数据库服务器)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[iyunv@MariaDB ~]# ifconfig #数据库主机的地址
eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.16.249.130 netmask 255.255.0.0 broadcast 172.16.255.255
inet6 fe80::20c:29ff:feb7:d79d prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:b7:d7:9d txqueuelen 1000 (Ethernet)
RX packets 37656 bytes 2602169 (2.4 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 584 bytes 72342 (70.6 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[iyunv@MariaDB ~]# yum install -y mariadb #安装mariadb
[iyunv@MariaDB ~]# rpm -q mariadb
mariadb-5.5.44-2.el7.centos.x86_64
[iyunv@MariaDB ~]# systemctl start mariadb #启动mariadb
[iyunv@MariaDB ~]# ss -tan
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 50 *:3306 *:* #确定3306端口处于监听状态
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
ESTAB 0 0 172.16.249.130:22 172.16.61.1:49826
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
第二部分
分别为两台虚拟主机提供phpMyAdmin和wordpress
因为这两个应用程序的动态资源和静态资源都放在了一个安装包内,所以我们需要在http主机和php主机各放一份程序资源。
(1)wordpress
[ root@HTTP a . com ]# ls #HTTP主机
index . html wordpress wordpress - 4.3 . 1 - zh_CN . zip
[ root@PHP a . com ]# ls #PHP主机
index . php wordpress wordpress - 4.3 . 1 - zh_CN . zip
MariaDB [(none)]> CREATE DATABASE wpdb; #为wordpress创建数据库wpdbQuery OK, 1 row affected (0.00 sec)
MariaDB [(none)]> GRANT ALL ON wpdb.* TO 'tz'@'172.16.%.%' IDENTIFIED BY 'tianzhuang';
Query OK, 0 rows affected (0.01 sec) #在数据库主机中为wordpress创建可以远程连接数据库的用户
[iyunv@localhost ~]# vim /etc/my.cnf
...
[mysqld] #修改数据库配置文件并在mysqld选项下增加两项内容
skip_name_resolve = ONinnodb_file_per_table = ON
....
[iyunv@PHP wordpress]# vim wp-config-sample.php #在php主机中更改wordpress的配置文件
....
/** WordPress数据库的名称 */ define('DB_NAME', 'wpdb'); #使用在数据库主机中创建的数据库/** MySQL数据库用户名 */ #使用已经授权的能远程连接的用户名define('DB_USER', 'tz');/** MySQL数据库密码 */define('DB_PASSWORD', 'tianzhuang');/** MySQL主机 */define('DB_HOST', '172.16.249.130');
[iyunv@PHP wordpress]# cp wp-config-sample.php wp-config.php #把示例配置文件复制为配置文件
在windows主机的hosts文件中加入虚拟主机的解析,使用浏览器安装程序:
(2)phpMyAdmin
1
2
3
4
5
6
7
8
9
[iyunv@HTTP b.com]# lsphpmyadmin phpMyAdmin-4.4.14.1-all-languages.zip
[iyunv@PHP b.com]# lsphpmyadmin phpMyAdmin-4.4.14.1-all-languages.zip
[iyunv@localhost phpmyadmin]# openssl rand -base64 20wDYCF/VjCC84UO7Qi5iqfVNaquE= #生成一段随机数
[iyunv@PHP phpmyadmin]# cp config.sample.inc.php config.inc.php
[iyunv@PHP phpmyadmin]# vim config.inc.php
....
$cfg['blowfish_secret'] = 'wDYCF/VjCC84UO7Qi5iqfVNaquE' #在配置文件中填入随机数,生成cookie$cfg['Servers'][$i]['host'] = '172.16.249.130'; #设定要连接的数据库地址
[iyunv@PHP phpmyadmin]# yum install -y php-mbstring #安装phpmyadmin需要使用的扩展包
[iyunv@PHP phpmyadmin]# systemctl restart php-fpm #重启php-fpm
运维网声明
1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网 享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com