目录
编译环境编译过程运行配置运行环境1. 创建php.ini文件2. 创建 php-fpm.conf文件3. 创建 www.conf文件4. 配置连接socket为文件(可选)5. 配置nginx
启动phpphp与php-fpm的关系phpphp-fpm
小技巧遇到的问题
编译环境
编译机器
Ubuntu 18.04
源码包
https://www.php.net/distributions/php-8.1.5.tar.gz
源码包从何获取?https://www.php.net/releases/
编译过程
配置编译环境
./configure --prefix=/usr/local/php --enable-fpm --with-pdo-mysql --without-pdo-sqlite --without-sqlite3
不知道环境如何配置怎么办? 使用./configure -h查看使用帮助,再根据自己的需求,启用或禁用某些功能或模块。
强烈建议配置--prefix到一个指定的目录,这样make install安装时就会把相关的产物只安装到这一个目录下,在卸载软件时,可以直接删除此目录即可(没有一个方便快捷的指令可以直接卸载软件)。
开始编译
# make
.......
Generating phar.php
Generating phar.phar
PEAR package PHP_Archive not installed: generated phar will require PHP's phar extension be enabled.
clicommand.inc
pharcommand.inc
phar.inc
invertedregexiterator.inc
directorytreeiterator.inc
directorygraphiterator.inc
Build complete.
Don't forget to run 'make test'.
编译相对是比较耗时的,只需要耐心等待即可。
安装
# make install
Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20210902/
Installing PHP CLI binary: /usr/local/php/bin/
Installing PHP CLI man page: /usr/local/php/php/man/man1/
Installing PHP FPM binary: /usr/local/php/sbin/
Installing PHP FPM defconfig: /usr/local/php/etc/
Installing PHP FPM man page: /usr/local/php/php/man/man8/
Installing PHP FPM status page: /usr/local/php/php/php/fpm/
Installing phpdbg binary: /usr/local/php/bin/
Installing phpdbg man page: /usr/local/php/php/man/man1/
Installing PHP CGI binary: /usr/local/php/bin/
Installing PHP CGI man page: /usr/local/php/php/man/man1/
Installing build environment: /usr/local/php/lib/php/build/
Installing header files: /usr/local/php/include/php/
Installing helper programs: /usr/local/php/bin/
program: phpize
program: php-config
Installing man pages: /usr/local/php/php/man/man1/
page: phpize.1
page: php-config.1
/root/php/php-8.1.5/build/shtool install -c ext/phar/phar.phar /usr/local/php/bin/phar.phar
ln -s -f phar.phar /usr/local/php/bin/phar
Installing PDO headers: /usr/local/php/include/php/ext/pdo/
运行
配置运行环境
运行模式: nginx+php 配置文件直接使用默认的默认文件即可。这些配置文件在安装目录下都有相应的示例,如果没有特别的需求,直接使用默认的配置文件即可。
1. 创建php.ini文件
直接从项目源码复制php.ini.production配置文件到/usr/local/php/etc目录。 make install并没有把php.ini文件安装到配置文件中,所以需要手动copy。php.ini
2. 创建 php-fpm.conf文件
/usr/local/php/etc# cp php-fpm.conf.default php-fpm.conf
3. 创建 www.conf文件
/usr/local/php/etc/php-fpm.d# cp www.conf.default www.conf
4. 配置连接socket为文件(可选)
默认是在开启了本地的9000端口,会占用一定的系统资源,因为只给本机使用,不如socket文件效率高,且nginx又支持socket文件,所以直接配置成socket文件吧。使用分号把listen = 127.0.0.1:9000 这一行注释掉,再添加一行 listen = /usr/local/php/var/run/php-fpm.sock即可。
; The address on which to accept FastCGI requests.
; Valid syntaxes are:
; 'ip.add.re.ss:port' - to listen on a TCP socket to a specific IPv4 address on
; a specific port;
; '[ip:6:addr:ess]:port' - to listen on a TCP socket to a specific IPv6 address on
; a specific port;
; 'port' - to listen on a TCP socket to all addresses
; (IPv6 and IPv4-mapped) on a specific port;
; '/path/to/unix/socket' - to listen on a unix socket.
; Note: This value is mandatory.
;listen = 127.0.0.1:9000
listen = /usr/local/php/var/run/php-fpm.sock
5. 配置nginx
http{
server{
location ~ \.php$ {
fastcgi_pass unix:/usr/local/php/var/run/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
}
启动php
cd /usr/local/php
sbin/php-fpm -c etc -y etc/php-fpm.conf
关于启动参数的作用,可以通过php-fpm -v 来查看。
-c 指定php.ini配置文件所在目录-y 指定FastCGI进程管理的配置文件
php与php-fpm的关系
启动应用启动的php-fpm程序,而不是php,这是为什么呢?
php
php程序是在命令行中执行php脚本的
php-fpm
php-fpm是与来与web服务器通信,并执行php脚本的,它是一个守护进程,一直驻留在系统中。
小技巧
查看php的配置参数
php -i
检查php的语法是否正确
php -l xxx.php
遇到的问题
pkg-config库过旧问题
解决方法:apt install pkg-config
configure: error: in `/root/php/php-8.1.5':
configure: error: The pkg-config script could not be found or is too old. Make sure it
is in your PATH or set the PKG_CONFIG environment variable to the full
path to pkg-config.
Alternatively, you may set the environment variables LIBXML_CFLAGS
and LIBXML_LIBS to avoid the need to call pkg-config.
See the pkg-config man page for more details.
To get pkg-config, see
See `config.log' for more details
libxml-2.0库不存在
解决方法:apt install libxml2-dev
checking for libxml-2.0 >= 2.9.0... no
configure: error: Package requirements (libxml-2.0 >= 2.9.0) were not met:
No package 'libxml-2.0' found
Consider adjusting the PKG_CONFIG_PATH environment variable if you
installed software in a non-standard prefix.
sqlite3库不存在
解决方法:添加 --without-sqlite3 原因:并不使用sqlite3库,所以编译的时候可以直接去除此模块,这样也系统也能少安装一个无用的库。
checking for sqlite3 >= 3.7.7... no
configure: error: Package requirements (sqlite3 >= 3.7.7) were not met:
No package 'sqlite3' found
Consider adjusting the PKG_CONFIG_PATH environment variable if you
installed software in a non-standard prefix.
文章链接
发表评论