09089 发表于 2016-1-29 08:27:00

基于php-fpm方式部署LAMP

                      前言
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
# ifconfig       #http服务器的地址
eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>mtu 1500
      inet 172.16.61.2netmask 255.255.0.0broadcast 172.16.255.255
      inet6 fe80::20c:29ff:fefe:9633prefixlen 64scopeid 0x20<link>
      ether 00:0c:29:fe:96:33txqueuelen 1000(Ethernet)
      RX packets 203985bytes 53699541 (51.2 MiB)
      RX errors 0dropped 31overruns 0frame 0
      TX packets 5381bytes 1764387 (1.6 MiB)
      TX errors 0dropped 0 overruns 0carrier 0collisions 0
# yum install -y httpd       #安装httpd程序
# rpm -q httpd
httpd-2.4.6-31.el7.centos.x86_64

# vim /etc/httpd/conf.d/vhost.conf    #创建两个虚拟主机

DirectoryIndexindex.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>
# mkdir -pv /vhost/{a.com,b.com}    #为虚拟主机创建DocumentRoot
mkdir: 已创建目录 "/vhost"
mkdir: 已创建目录 "/vhost/a.com"
mkdir: 已创建目录 "/vhost/b.com"
# vim /etc/httpd/conf/httpd.conf   
....
#DocumentRoot "/var/www/html"       #注释掉中心主机的DocumentRoot
....

# systemctl start httpd    #启动httpd服务
# 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
# ifconfig                  #php主机的地址
eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>mtu 1500
      inet 172.16.61.100netmask 255.255.0.0broadcast 172.16.255.255
      inet6 fe80::20c:29ff:fe70:e227prefixlen 64scopeid 0x20<link>
      ether 00:0c:29:70:e2:27txqueuelen 1000(Ethernet)
      RX packets 199355bytes 14076483 (13.4 MiB)
      RX errors 0dropped 35overruns 0frame 0
      TX packets 433bytes 42949 (41.9 KiB)
      TX errors 0dropped 0 overruns 0carrier 0collisions 0
# yum install -y php-fpm      #安装php-fpm程序
# mkdir/www                   #创建php资源路径
# systemctl start php-fpm       #开启php-fpm程序
# 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                        :::*   
# 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
# ifconfig             #数据库主机的地址
eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>mtu 1500
      inet 172.16.249.130netmask 255.255.0.0broadcast 172.16.255.255
      inet6 fe80::20c:29ff:feb7:d79dprefixlen 64scopeid 0x20<link>
      ether 00:0c:29:b7:d7:9dtxqueuelen 1000(Ethernet)
      RX packets 37656bytes 2602169 (2.4 MiB)
      RX errors 0dropped 0overruns 0frame 0
      TX packets 584bytes 72342 (70.6 KiB)
      TX errors 0dropped 0 overruns 0carrier 0collisions 0
# yum install -y mariadb   #安装mariadb
# rpm -q mariadb   
mariadb-5.5.44-2.el7.centos.x86_64
# systemctl start mariadb   #启动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.htmlwordpresswordpress-4.3.1-zh_CN.zip
[root@PHP a.com]# ls         #PHP主机
index.phpwordpresswordpress-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创建可以远程连接数据库的用户

# vim /etc/my.cnf         

...
                                    #修改数据库配置文件并在mysqld选项下增加两项内容

skip_name_resolve = ONinnodb_file_per_table = ON

....


# 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');

# cp wp-config-sample.php wp-config.php   #把示例配置文件复制为配置文件

   

在windows主机的hosts文件中加入虚拟主机的解析,使用浏览器安装程序:


(2)phpMyAdmin

1
2
3
4
5
6
7
8
9
# lsphpmyadminphpMyAdmin-4.4.14.1-all-languages.zip
# lsphpmyadminphpMyAdmin-4.4.14.1-all-languages.zip
# openssl rand -base64 20wDYCF/VjCC84UO7Qi5iqfVNaquE=    #生成一段随机数
# cp config.sample.inc.phpconfig.inc.php
# vim config.inc.php
....
$cfg['blowfish_secret'] = 'wDYCF/VjCC84UO7Qi5iqfVNaquE'#在配置文件中填入随机数,生成cookie$cfg['Servers'][$i]['host'] = '172.16.249.130';    #设定要连接的数据库地址
# yum install -y php-mbstring #安装phpmyadmin需要使用的扩展包
# systemctl restart php-fpm #重启php-fpm










                   

页: [1]
查看完整版本: 基于php-fpm方式部署LAMP