LAMP_基于PHP_FPM模式的建立
实验目的:基于Php-fpm的模式搭建LAMP环境并安装PMA框架
实验环境:
客户端:CentOS6, IP:192.168.60.132,作为客户端访问http的web服务。
主机1:CentOS7,IP:192.168.60.129,作为httpd服务器,安装httpd
主机2:CentOS7,IP:192.168.60.137,作为php-fpm服务器,安装php-fpm.php-sql
主机3:CentOS7,IP:192.168.60.138,作为MariaDB服务器,安装mariadb-server
搭建主机3的MariaDB服务器环境
在主机3,138主机上操作
安装MariaDB-server
yum intall -y mariadb-server
编辑mariadb配置文件
vim /etc/my.cnf.d/server.cnf
加上
[mysqld]
skip_name_resolve=ON
innodb_file_per_table=ON
保存退出。
启动服务。
systemctl start mariadb.service
查看端口是否启动
ss -tnl
可见3306端口已经启动。
执行数据安全加强设置
mysql_secure_installation
Set root password? [Y/n] y #是否设置数据库管理员root的密码,是,123456
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
... Success!
By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.
Remove anonymous users? [Y/n] y #是否移除匿名用户
... Success!
Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] y #是否禁止root用户远程登陆,是。
... Success!
By default, MariaDB comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n] n #是否移除test数据库,否。
... skipping.
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n] y #是否重读权限表,是。
... Success!
Cleaning up...
All done! If you've completed all of the above steps, your MariaDB
installation should now be secure.
创建一个普通用户用于测试
以root用户身份登陆数据库
mysql -uroot -p123456
创建用户
GRANT ALL ON testdb.* TO 'user1'@'192.168.%.%' IDENTIFIED BY '123456';
重读授权表
FLUSH PRIVILEGES;
用user1的身份登陆数据
mysql -uuser1 -h192.168.60.138 -p123456
创建数据库
CREATE DATABASE testdb CHARACTER SET 'utf8';
CREATE TABLE tbl1 (id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE KEY,name VARCHAR(60) NOT NULL);
插入2个数据
INSERT INTO testdb.tbl1 (name) VALUES ('Yang Guo'),('Guo Jing');
搭建主机2的php-fpm环境
在主机2,137上操作
yum install -y php-fpm php-mysql php-mbstring php-mcrypt
php-mysql,php连接mysql的驱动
php-mbstring,支持中文字符串
php-mcrypt,加解密库,phpmyadmin需要
编辑php-fpm的主配置文件
vim /etc/php-fpm.d/www.conf
listen = 192.168.60.137:9000 #指明监听的ip和端口,以便129服务器访问。
listen.allowed_clients = 192.168.60.129 #指明允许129服务器访问。
创建session目录
mkdir /var/lib/php/session
此目录用于保存会话文件
修改目录属组
chown apache.apache /var/lib/php/session
编辑测试页面。
vim /data/www/html/index.php
PHP Test Page
ServerName www.ilinux.io
DocumentRoot /data/web/www
ProxyRequests Off
ProxyPassMatch ^/(.*\.php)$ fcgi://192.168.60.137:9000/data/www/html/$1
Options FollowSymLinks
AllowOverride None
Require all granted
ProxyPassMatch ^(/pmstatus.*)$ fcgi://192.168.60.137:9000/$1
ProxyPassMatch ^(/ping.*)$ fcgi://192.168.60.137:9000/$1
对上面配置文件的解析
DirectoryIndex index.php
#这是定义默认主页文件为index.php,也就是当客户端访问www.ilinux.io,默认会执行www.ilinux.io/index.php。
DocumentRoot /data/web/www
#这是定义当前httpd主机上的网页文件的根目录,可以把静态文件如html放在此处。
ProxyRequests Off
#关闭正向代理
ProxyPassMatch ^/(.*\.php)$ fcgi://192.168.60.137:9000/data/www/html/$1
#对于此主机上的.php文件,都会映射到php-fpm服务器的/data/www/html/目录下的,同名的.php文件。
也就是说,可以把index.php文件,放在php-fpm服务器的/data/www/html/目录下。
当httpd主机收到index.php的请求时,会反代理给后端的php-fpm服务器,问他有没有index.php文件,有的话,就执行并返回给我。php-fpm服务器查询本地,发现由此文件,于是便执行之。
如果php-fpm服务器没有此文件,就会返回file not found.
注意:如果仅把.php文件放在httpd主机上,而php-fpm主机上没有改文件,那么依然会返回file not found.
也就是说,反代理并不是把.php文件"发送"给后端php服务器处理,而是"跳转"到啊后端php服务器的同名文件。
fcgi://192.168.60.137:9000
#指明后端的php-fpm主机的ip和端口。
重启服务。
systemctl start httpd.service
使用客户端的浏览器访问
编辑客户端的hosts文件
192.168.60.129 www.ilinux.io
浏览器访问http://www.ilinux.io/
从状态也可见php-fpm模式已经启用。
测试ping页面,可以连通。
测试状态页面,可以连通。
pm.status的解析
pool: www – fpm池子名称,大多数为www
process manager: dynamic – 进程管理方式,值:static, dynamic
start time: 16/May/2019:10:42:22 +0800 – 启动日期,如果reload了php-fpm,时间会更新
start since: 6142 – 运行时长
accepted conn: 531 – 当前池子接受的请求数,这个是累加的值
listen queue: 0 – 请求等待队列,如果这个值不为0,那么要增加FPM的进程数量
max listen queue: 3 请求等待队列最高的数量
listen queue len: 128 socket等待队列长度
idle processes: 7 – 空闲进程数量
active processes: 1 – 活跃进程数量
total processes: 8 – 总进程数量
max active processes: 7 - 最大的活跃进程数量(FPM启动开始算)
max children reached: 0 - 进程最大数量限制的次数,如果这个数量不为0,那说明你的最大进程数量太小了,请改大一点。
slow requests: 0 – 启用了php-fpm slow-log,缓慢请求的数量
部署PhpMyadmin
在主机2,137主机上操作
下载phpMyAdmin-4.4.0-all-languages.zip到本机
解压缩
unzip phpMyAdmin-4.4.0-all-languages.zip
把phpmyadmin移动到网页文件根目录下
mv phpMyAdmin-4.4.0 /data/www/html
创建软连接文件pma
ln -sv phpmyadmin pma
复制创建pma配置文件
cp config.sample.inc.php config.inc.php
编辑配置文件
vim config.inc.php
$cfg['blowfish_secret'] = 'l6TKf7x3DxtYFNfdXv+WHDeYXKo'; #添加随机数
/* Server parameters */
$cfg['Servers'][$i]['host'] = '192.168.60.138'; #指明Mariadb服务器的ip地址
$cfg['Servers'][$i]['connect_type'] = 'tcp';
$cfg['Servers'][$i]['compress'] = false;
$cfg['Servers'][$i]['AllowNoPassword'] = false;
在主机1,129主机上操作
编辑配置文件
vim /etc/httpd/conf.d/vhost.conf
DirectoryIndex index.php
ServerName www.ilinux.io
DocumentRoot /data/web/www
ProxyRequests Off
ProxyPassMatch ^/(.*\.php)$ fcgi://192.168.60.137:9000/data/www/html/$1
Options FollowSymLinks
AllowOverride None
Require all granted
ProxyPassMatch ^(/pmstatus.*)$ fcgi://192.168.60.137:9000/$1
ProxyPassMatch ^(/ping.*)$ fcgi://192.168.60.137:9000/$1
在/data/web/www目录创建文件夹pma
mkdir /data/web/www/pma
浏览器输入http://www.ilinux.io/pma/
数据库服务器登陆成功。
总结:
1.httpd主机上仅新增了pma文件夹而已。
2.php-fpm服务器上要部署PMA程序
3.在PMA的配置文件上设置数据库服务器的ip地址。