php笔记

1.获取并解压 PHP 源代码

cd ../php-x.x.x
./configure --enable-fpm --with-mysql
make
sudo make install

2.配置并构建 PHP。在此步骤您可以使用很多选项自定义 PHP,例如启用某些扩展等。 运行 ./configure --help 命令来获得完整的可用选项清单。 在本示例中,我们仅进行包含 PHP-FPM 和 MySQL 支持的简单配置。

cd ../php-x.x.x
./configure --enable-fpm --with-pdo-mysql
make
#测试,输出测试报告
make test
#安装
sudo make instal
[root@master php-7.2.28]# sudo make install
Installing shared extensions:     /usr/local/lib/php/extensions/no-debug-non-zts-20170718/
Installing PHP CLI binary:        /usr/local/bin/
Installing PHP CLI man page:      /usr/local/php/man/man1/
Installing PHP FPM binary:        /usr/local/sbin/
Installing PHP FPM defconfig:     /usr/local/etc/
Installing PHP FPM man page:      /usr/local/php/man/man8/
Installing PHP FPM status page:   /usr/local/php/php/fpm/
Installing phpdbg binary:         /usr/local/bin/
Installing phpdbg man page:       /usr/local/php/man/man1/
Installing PHP CGI binary:        /usr/local/bin/
Installing PHP CGI man page:      /usr/local/php/man/man1/
Installing build environment:     /usr/local/lib/php/build/
Installing header files:          /usr/local/include/php/
Installing helper programs:       /usr/local/bin/
  program: phpize
  program: php-config
Installing man pages:             /usr/local/php/man/man1/
  page: phpize.1
  page: php-config.1
Installing PEAR environment:      /usr/local/lib/php/
[PEAR] Archive_Tar    - installed: 1.4.8
[PEAR] Console_Getopt - installed: 1.4.3
[PEAR] Structures_Graph- installed: 1.1.1
[PEAR] XML_Util       - installed: 1.4.3
[PEAR] PEAR           - installed: 1.10.10
Wrote PEAR system config file at: /usr/local/etc/pear.conf
You may want to add: /usr/local/lib/php to your php.ini include_path
/usr/src/php-7.2.28/build/shtool install -c ext/phar/phar.phar /usr/local/bin
ln -s -f phar.phar /usr/local/bin/phar
Installing PDO headers:           /usr/local/include/php/ext/pdo/

3.创建配置文件,并将其复制到正确的位置。

cp php.ini-development /usr/local/php/php.ini
cp /usr/local/etc/php-fpm.d/www.conf.default /usr/local/etc/php-fpm.d/www.conf
cp /usr/src/php-7.2.28/sapi/fpm /usr/local/bin
cp php-fpm.conf /usr/local/etc/php-fpm.conf

4.(参考)需要着重提醒的是,如果文件不存在,则阻止 Nginx 将请求发送到后端的 PHP-FPM 模块, 以避免遭受恶意脚本注入的攻击。

将 php.ini 文件中的配置项 cgi.fix_pathinfo 设置为 0

vim /usr/local/php/php.ini
#定位到 cgi.fix_pathinfo= 并将其修改为如下所示:
cgi.fix_pathinfo=0 

5.(参考,可以设置为nginx启动用户)在启动服务之前,需要修改 php-fpm.conf 配置文件,确保 php-fpm 模块使用 www-data 用户和 www-data 用户组的身份运行。

vim /usr/local/etc/php-fpm.d/www.conf

; Unix user/group of processes
; Note: The user is mandatory. If the group is not set, the default user's group
;       will be used.
user = www-data
group = www-data

6.启动 php-fpm 服务:

/usr/local/bin/php-fpm

7.配置 Nginx 使其支持 PHP 应用:

vim /usr/local/nginx/conf/nginx.conf

修改默认的 location 块,使其支持 .php 文件:

location / {
    root   html;
    index  index.php index.html index.htm;
}

下一步配置来保证对于 .php 文件的请求将被传送到后端的 PHP-FPM 模块, 取消默认的 PHP 配置块的注释,并修改为下面的内容:

location ~* \.php$ {
    fastcgi_index   index.php;
    fastcgi_pass    127.0.0.1:9000;
    include         fastcgi_params;
    fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;
    fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;
}

重启 Nginx。

8.创建测试文件。

rm /usr/local/nginx/html/index.html
echo "<?php phpinfo(); ?>" >> /usr/local/nginx/html/index.php

打开浏览器,访问 http://localhost,将会显示 phpinfo() 。

异常:

1.启动失败

[root@master2020 html]# /usr/local/bin/php-fpm
[27-Feb-2020 08:51:50] ERROR: failed to open configuration file '/usr/local/etc/php-fpm.conf': No such file or directory (2)
#修改后 include = /usr/local/etc/php-fpm.d/ *. Conf
[27-Feb-2020 08:51:50] ERROR: failed to load configuration file '/usr/local/etc/php-fpm.conf' [27-Feb-2020 08:51:50] ERROR: FPM initialization failed
#查看文件位置,若文件不存在则补充

 

上一篇:php编译安装


下一篇:Linux下安装php环境