lamp架构安装之apache2.4+Centos7.6+mariadb(mysql)+php5.4.6+php-fpm模式 综述

yum httpd
 yum install httpd-tools #工具模块,包括ab命令等调试工具
 yum install mod_ssl #与https协议实现有关
 yum php-fpm php-mysql


yum mariadb mariadb-server #mariadb-server会关联依赖mariadb-libs包(该包包括数据库的核心库,核心配置等,如my.cnf就是该包安装时候新建的。),也会关联依赖安装和mariadb客户端包,当然也可以直接yum 我们自己安装,如果已经安装就安装了,还有估计十几个依赖包如一些perl包也会一同安装,所以yum解决了依赖性问题,很方便。

systemctl enable httpd #开机启动
 systemctl enable php-fpm
 systemctl enable mariadb //这里启动的是mariadb服务进程

#确认apache与php的通信方式为php-fpm,我们看一下/etc/httpd/conf.d目录下新建文件/etc/httpd/conf.d/php-fpm.conf,Then tell Apache to send all PHP requests over to PHP-FPM by creating a new configuration file:

[root@web01 ~]# vim /etc/httpd/conf.d/php-fpm.conf
#Tell the PHP interpreter to handle files with a .php extension.
#Proxy declaration
<Proxy "unix:/var/run/php-fpm/default.sock|fcgi://php-fpm">
	# we must declare a parameter in here (doesn't matter which) or it'll not register the proxy ahead of time
    	ProxySet disablereuse=off
</Proxy>

#Redirect to the proxy
<FilesMatch \.php$>
	SetHandler proxy:fcgi://php-fpm
</FilesMatch>

#
#Allow php to handle Multiviews
#
AddType text/html .php

#
#Add index.php to the list of files that will be served as directory
#indexes.
#
DirectoryIndex index.php
#
#Uncomment the following lines to allow PHP to pretty-print .phps
#files as PHP source code:
#
#<FilesMatch \.phps$>
#SetHandler application/x-httpd-php-source
#</FilesMatch>

Tweak PHP-FPM to use sockets instead of TCP connections for performance purposes as follows:

[root@web01 ~]# vim /etc/php-fpm.d/www.conf
; listen = 127.0.0.1:9000
listen = /var/run/php-fpm/default.sock
...
listen.allowed_clients = 127.0.0.1
listen.owner = apache
listen.group = apache
listen.mode = 0660
user = apache
group = apache

systemctl start httpd
systemctl start php-fpm
systemctl start mariadb //这里启动的是mariadb服务进程

1 相关linux安装命令分析:

yum install httpd//web服务
yum install php-fpm //php-fpm cgi脚本程序,包括/etc/httpd/conf.d/php.conf,该文件会作为apache的一个配置部分,被引入到apache的配置。内容为.php为扩展名的文件使用proxy代码模块关联到phpfpm中进行通信。
//php-fpm安装的时候,会自动安装yum php-common包(会自动安装yum php-common包,该包是php的核心公共包,包括php的数组扩展,对象扩展,zend解析引擎,/etc/php.ini文件等),php-fpm包,会安装包括自身的一些配置,执行引擎。
//systemctl restart php-fpm 可以重新载入/etc/php.ini配置文件,重启httpd服务则不能重新载入php.ini包
yum install mariadb –y //安装mysql客户端
yum install mariadb-server //mysql服务在centos7系统中叫做mariadb,安装mysql的服务器端,该包的依赖包有近十个(包括mariadb-libs(核心库包,如/etc/my.cnf文件就是安装该包时候安装的),mariadb,以及一些perl包等),如果发现这些依赖包没有安装,那么会同时安装这些依赖包。

2 apache与php通信分析:

a. apache的mod_php模块,apache将该php解析模块集成到apache中了。
通过load加载该模块,然后配置.php网络公司的架构是什么样的 网络公司部门职位架构_apache扩展名的处理器指向该绑定php-fpm的代理。

3 php与mysql通信分析:

php的mysql配置文件中配置与mysql服务之间的通信方式((可以用tcp socket绑定或unix socket文件通信))。当我们yum install mariadb-server的时候,会自动在php的相关目录下新增相关sock文件。实际通信一般php的函数如mysqli_connect通过unix domain socket或tcp socket通信,具体哪个应该可以在php.ini中可以配置。

[root@localhost php-fpm]# rpm -ql php-mysql

/etc/php.d/mysql.ini

/etc/php.d/mysqli.ini

/etc/php.d/pdo_mysql.ini

/usr/lib64/php/modules/mysql.so

/usr/lib64/php/modules/mysqli.so

/usr/lib64/php/modules/pdo_mysql.so

[root@localhost php-fpm]# cat /etc/php.d/mysqli.ini

; Enable mysqli extension module

extension=mysqli.so

[root@localhost php-fpm]# cat /etc/php.d/mysql.ini

; Enable mysql extension module

extension=mysql.so

[root@localhost php-fpm]# cat /etc/php.d/pdo_mysql.ini

; Enable pdo_mysql extension module

extension=pdo_mysql.so

[root@localhost php-fpm]# systemctl restart php-fpm

这样只需要,通过重启httpd(mod_php模式)或重启php-fpm(php-fpm模式)来重新载入php配置文件就可以啦。

4 测试案例info.php

<?php

echo 51;
$num = 9000;
$i = 0;
while($i<$num){
      $i++;
}
echo php_sapi_name();
var_dump(ini_get('post_max_size'));
 phpinfo();
$mysqli = new mysqli("localhost", "root", "", "test");
if ($mysqli->connect_errno) {
    echo "11Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
echo $mysqli->host_info . "\n";
echo '==========';
$mysqli = new mysqli("127.0.0.1", "root", "", "test", 3306);
if ($mysqli->connect_errno) {
    echo "22Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
echo $mysqli->host_info . "\n";
?>