PHP 软件版本 5.6.39
一, PHP 的编译安装管理:
1 : PHP 编译安装, PHP 的编译安装依赖的库比较多, 编译安装会稍微难一点
yum -y install epel-release [安装 epel 源]
yum -y install gcc gcc-c++ make pcre pcre-devel zlib zlib-devel openssl openssl-devel libxml2 libxml2-devel libcurl libcurl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel openldap openldap-devel libmcrypt libmcrypt-devel
[安装 RZ 上传软件: yum install lrzsz -y] XSHELL 好使, putty 使用后无反应. 上传命令: rz -E
- 2 :tar -zxf PHP-5.6.39.tar.gz
- cd PHP-5.6.39
- ./configure --prefix=/usr/local/PHP --with-config-file-path=/usr/local/PHP/etc --enable-ctype --with-MySQL=mysqlnd --with-mysqli=mysqlnd --with-freetype-dir --with-jpeg-dir --with-PNG-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-mbstring --with-mcrypt --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-ldap-sasl --with-xmlrpc --enable-zip --enable-SOAP --with-gettext --enable-fpm
- make
- make install
cp PHP.INI-production /usr/local/PHP/etc/PHP.INI 源码安装时我们指定配置文件放在 / usr/local/PHP/etc, 我们把默认的配置文件改名并放到相应目录.
[编译安装说明] :
- --prefix=/usr/local/PHP #安装路径
- --with-config-file-path=/usr/local/PHP/etc #配置文件路径
- --enable-fpm #需要支持 fpm,nginx 是通过 fpm 来调用 PHP 程序的
3 PHP 安装验证: cd /usr/local/PHP/bin/
./PHP -v
bin 目录放置 PHP 的程序, 像 nginx 一样,
etc 目录放置 PHP 的配置文件,
include 目录放置头文件,
Lib 目录放置一些包和一些依赖的库,
sbin 目录放置 PHP-fpm var 目录放置日志等
4 PHP 的管理命令
- /usr/local/PHP/bin/PHP -i|grep INI #查看配置文件路径
- /usr/local/PHP/bin/PHP -m #查看 PHP 的模块
- /usr/local/PHP/bin/PHP -i|grep configure #查看 PHP 的环境检查参数, 网站搬家要用到
二, PHP 的日志和动态安装扩展模块:
1 cd /usr/local/PHP/bin VIM PHP.INI
PHP 错误日志配置:
error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT
记录所有的日志, 不包含过时函数提示和 PHP 严格语法提示, 通常也会忽略 E_NOTICE 的报错.
PHP 错误日志记录到文件
- log_errors = On
- log_errors_max_len = 1024
- error_log = /tmp/php_errors.log
错误日志直接输出到屏幕, 注意生产环境需要关闭
display_errors = On
PHP 需要更改时区配置
date.timezone = PRC
PRC 的英文含义是 People's Republic of China
[PHP 的模块再说明:/usr/local/PHP/bin/PHP -m #需要 gd 模块才能绘图, 需要 MySQL 相关模块才能操作 MySQL]
2动态安装模块 [即后期安装所需的功能模块]
首先需要安装动态模块的包: yum install autoconf -y
其次需要使用动态扩展工具: /usr/local/PHP/bin/phpize #PHP 动态扩展模块的工具
假设 PHP 需要操作 Redis 的话, 可以动态安装 PHP Redis 模块, 不需要重新编译 PHP.
去 pecl 网站下载 Redis 的扩展 [网址 https://pecl.php.net/package/redis ]
- tar -zxvf Redis-4.2.0.tgz
- cd Redis-4.2.0
- /usr/local/PHP/bin/phpize
- ./configure --with-PHP-config=/usr/local/PHP/bin/PHP-config make && make install
3php 配置文件里添加 Redis.so, 并检查 Redis 扩展有没有安装成功
VIM /usr/local/PHP/etc/PHP.INI
配置文件添加 extension=/usr/local/PHP/lib/PHP/extensions/no-debug-non-zts-20131226/Redis.so
/usr/local/PHP/bin/PHP -m
三, PHP-fpm 的配置和管理:
1php-fpm 作用: nginx 调用 PHP 程序是通过 PHP-fpm 这个接口程序来的, PHP-fpm 专门为 nginx+PHP 的架构开发.
2php-fpm 的默认配置样例文件 / usr/local/PHP/etc/PHP-fpm.conf.default
提取样例配置文件内容 cat /usr/local/PHP/etc/PHP-fpm.conf.default|egrep -v '^;|^$'
- [global]
- [www]
- user = nobody
- group = nobody
- listen = 127.0.0.1:9000
- pm = dynamic
- pm.max_children = 5
- pm.start_servers = 2
- pm.min_spare_servers = 1
- pm.max_spare_servers = 3
3php-fpm 的默认配置文件位置 /usr/local/PHP/etc/PHP-fpm.conf; PHP-FPH 启动时读取该文件, 需要自己创建配置文件, 默认安装完成后是没有的.
VIM /usr/local/PHP/etc/PHP-fpm.conf 将提取的步骤二的内容复制到该文件中.
4 PHP-fpm 的启动和关闭
- /usr/local/PHP/sbin/PHP-fpm -t #先检查配置文件
- /usr/local/PHP/sbin/PHP-fpm #启动 PHP-fpm
- pkill -9 PHP-fpm #闭关 PHP-fpm
5 PHP-fpm 加入 systemctl 管理 / usr/lib/systemd/system/PHP-fpm.service
VIM /usr/lib/systemd/system/PHP-fpm.service 复制以下内容:
- [Unit]
- Description=PHP-fpm
- After=network.target remote-fs.target nss-lookup.target
- [Service]
- Type=forking
- ExecStart=/usr/local/PHP/sbin/PHP-fpm
- ExecStop=pkill -9 PHP-fpm
- [Install]
- WantedBy=multi-user.target
管理 PHP-fpm
- systemctl enable PHP-fpm #开机启动 PHP-fpm
- systemctl start PHP-fpm #启动 PHP-fpm
默认启动检查:
- ps auxf|grep PHP-fpm
- netstat -tulnp|grep PHP-fpm
6 默认配置说明
user,group 代表 PHP-fpm 运行的用户和组
listen 表示 fpm 监听的方式, 默认使用的是网络的监听方式, 还有一种 socket 的监听方式
socket 监听的配置方式
- listen = /dev/shm/PHP-fpm.sock
- listen.owner = www
- listen.group = www
两种监听方式的选择
如果 nginx+PHP-fpm 是在同一台机器的话, 建议使用 sock 的方式
如果 nginx+PHP-fpm 在不同机器的话, 只能选择网络方式
PHP-fpm 的工作进程数的配置
pm = dynamic #pm 可以配置 PHP-fpm 的进程管理方式, 有动态 dynamic 或者静态 static 两种方式
pm 的相关配置: 可配置最大 PHP-fpm 的数量 pm.max_children = 5
启动需要多个 fpm pm.start_servers = 2
fpm 最小要空闲几个 pm.min_spare_servers = 1
fpm 最大要空闲几个 pm.max_spare_servers = 3 pm.min_spare_servers = 1
7nginx+fpm 的两种结合方式:
7->1第一种, 使用网络连接的方式. PHP-fpm 监听在 127.0.0.1:9000
- [global]
- [www]
- user = www
- group = www
- listen = 127.0.0.1:9000
- pm = dynamic
- pm.max_children = 5
- pm.start_servers = 2
- pm.min_spare_servers = 1
- pm.max_spare_servers = 3
nginx 使用网络的方式连接 PHP-fpm
- user www; #更改用户 [特别注意: nginx+PHP-fpm 整合, 启动这两个进程的用户名一定要一致!!!]
- index index.html index.htm index.PHP; #默认的网页展现增加一个 index.PHP
- location ~ .PHP$ {
- root HTML;
- fastcgi_pass 127.0.0.1:9000;
- fastcgi_index index.PHP;
- fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
- include fastcgi_params;
- }
测试 nginx+fpm 网络连接方式是否成功: 浏览器访问一个 PHP 页面, 如能正常显示表明配置成功.
7->2第二种, 使用 socket 的方式. PHP-fpm 监听在 / dev/shm/PHP-fpm.sock
- [global]
- [www]
- user = www
- group = www
- listen = /dev/shm/PHP-fpm.sock
- listen.owner = www listen.group = www
- pm = dynamic
- pm.max_children = 5
- pm.start_servers = 2
- pm.min_spare_servers = 1
- pm.max_spare_servers = 3
nginx 使用 socket 的方式连接 PHP-fpm
- user www; #更改用户 [特别注意: nginx+PHP-fpm 整合, 一]
- index index.HTML index.htm index.PHP; #默认的网页展现增加一个 index.PHP
- location ~ .PHP$ {
- root HTML;
- fastcgi_pass unix:/dev/shm/PHP-fpm.sock;
- fastcgi_index index.PHP;
- fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
- include fastcgi_params;
- }
测试 nginx+fpm 的 socket 方式是否成功
8nginx+fpm 的日志说明:
客户访问一个 PHP 程序的过程: 客户 -> nginx -> PHP-fpm -> nginx -> 客户
nginx 的 upstream_response_time 观察调用 PHP 程序花费的时间 $upstream_response_time $request_time
nginx 的 error 日志说明: error_log logs/error.log notice;
PHP 的错误日志显示打开的话, 会直接显示网页上: display_errors = On [ /usr/local/PHP/etc/PHP.INI]
PHP-fpm 的错误日志记录 [/usr/local/PHP/etc/PHP-fpm.conf]
- [global]
- error_log = /tmp/fpm_error_log
- log_level = notice
PHP-fpm 的慢日志, PHP 只要处理超过 1s 就会有记录
- slowlog = /tmp/phpslow.log
- request_slowlog_timeout = 1s
来源: http://www.bubuko.com/infodetail-2916066.html