php-fpm 介绍及实现(详解)

php-fpm 介绍及实现php-fpm 安装CentOS 6 安装CentOS 71、安装2、配置文件3、具体配置4、验证httpd + php 实现方式第一种 `module: php`第二种 `fastcgi : php-fpm`php-fpm 示例一、mariadb 相关安装及配置1、安装 `mariadb-server`2、修改 `/etc/my.cnf.d/server.cnf`3、重...
摘要由CSDN通过智能技术生成

php-fpm 介绍及实现

php-fpm 安装

CentOS 6 安装

PHP-5.3.2:默认不支持fpm机制;需要自行打补丁并编译安装
httpd-2.2:默认不支持fcgi协议,需要自行编译此模块
			
解决方案:编译安装httpd-2.4, php-5.3.3+

CentOS 7

1、安装

httpd-2.4:rpm包默认编译支持了fcgi模块
php-fpm包:专用于将php运行于fpm模式

2、配置文件

服务配置文件        # /etc/php-fpm.conf,  /etc/php-fpm.d/*.conf
php环境配置文件     # /etc/php.ini, /etc/php.d/*.ini 

3、具体配置

[root@Neo ~]# cat /etc/php-fpm.conf     # 主配置文件中有关于连接池的配置
;;;;;;;;;;;;;;;;;;;;
; Pool Definitions ; 
;;;;;;;;;;;;;;;;;;;;

; See /etc/php-fpm.d/*.conf              # 连接池的主要配置需要看 /etc/php-fpm.d/*.conf 文件
连接池:
	pm = static|dynamic
		static      # 固定数量的子进程;pm.max_children
		dynamic     # 子进程数据以动态模式管理
			pm.start_servers
			pm.min_spare_servers
			pm.max_spare_servers
			pm.max_requests = 500
  • 创建session目录,并确保运行php-fpm进程的用户对此目录有读写权限
# mkdir  /var/lib/php/session
# chown apache.apache /var/lib/php/session	

(1) 配置httpd,添加/etc/httpd/conf.d/fcgi.conf配置文件,内容类似:

# 创建完 fcgi.conf 后,需要添加以下内容
# 主页支持 index.php
# 关闭正向代理
# 反向代理设置:
	# $1 代表前面 () 内的内容
	# Match 进行正则表达式匹配
	# 运行内容是以 .php 结尾的话,是会反代到 fcgi://127.0.0.1:9000的某个配置文件中
	# /var/www/html 也就相当于 fcgi 的 DocumentRoot ,可以变更
	
DirectoryIndex index.php
ProxyRequests Off
ProxyPassMatch ^/(.*\.php)$  fcgi://127.0.0.1:9000/var/www/html/$1

(2) 虚拟主机配置(有以上配置后,就不需要配置虚拟主机)

DirectoryIndex index.php

<VirtualHost *:80>
	ServerName www.b.net
	DocumentRoot /apps/vhosts/b.net
	ProxyRequests Off
	ProxyPassMatch ^/(.*\.php)$  fcgi://127.0.0.1:9000/apps/vhosts/b.net/$1

	<Directory "/apps/vhosts/b.net">
		Options None
		AllowOverride None
		Require all granted
	</Directory>
</VirtualHost>

4、验证

假设fpm的status页面输出URL为/pmstatus,测试接口的输出位置为/ping ,都会送入到 fpm 去进行处理:

ProxyPassMatch ^/(ping|pmstatus)$  fcgi://127.0.0.1:9000/$1

httpd + php 实现方式

module: php
fastcgi : php-fpm 

第一种 module: php

直接加载 httpd 模块。

[root@Neo ~]# httpd -M | grep fcgi
 proxy_fcgi_module (shared)

第二种 fastcgi : php-fpm

直接安装 php-fpm。这种方式适用于负载较大的场景。

[root@Neo ~]# yum info php-fpm
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.tuna.tsinghua.edu.cn
 * extras: mirrors.tuna.tsinghua.edu.cn
 * updates: mirrors.tuna.tsinghua.edu.cn
Available Packages
Name        : php-fpm
Arch        : x86_64
Version     : 5.4.16
Release     : 46.el7
Size        : 1.4 M
Repo        : base/7/x86_64
Summary     : PHP FastCGI Process Manager
URL         : http://www.php.net/
License     : PHP and Zend and BSD
Description : PHP-FPM (FastCGI Process Manager) is an alternative PHP FastCGI
            : implementation with some additional features useful for sites of
            : any size, especially busier sites.

php-fpm 示例

一、mariadb 相关安装及配置

1、安装 mariadb-server

[root@Neo ~]# yum install mariadb-server -y

2、修改 /etc/my.cnf.d/server.cnf

安装完 mariadb 以后,一般都需要默认添加以下两条命令。

[root@Neo ~]# vim /etc/my.cnf.d/server.cnf 
[mysqld]
skip_name_resolve=ON             # 跳过名称解析
innodb_file_per_table=ON         # innodb 表每个表使用单独的表空间文件

3、重启 mariadb 服务,并进行查看

[root@Neo ~]# systemctl start mariadb.service   
[root@Neo ~]# ss -tnl            # 有3306 监听端口代表启动成功
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                       *:*            
LISTEN      0      32                                        :::21                      :::*     
LISTEN      0      128                                       :::22                      :::*    
LISTEN      0      100                                      ::1:25 
[root@Neo ~]# mysql               # 默认登陆方式也能登陆
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.60-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> quit
Bye

4、加固 mysql 服务器

加固mysql服务器,在安装完成后,运行mysql_secure_installation命令。

[root@Neo ~]# mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):  # 默认空密码,直接回车
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] y                         # 是否设定 root 用户的密码,Y 代表设定
New password:                                      # root
Re-enter new password:                             # root
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!                                        
  • 0
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值