把扩展编译进 PHP
通过 pecl 命令或 Linux 发行版自带的 yum、apt-get 等命令安装,动态加载(Linux 下是 *.so 文件,Windows 下是 *.dll 文件)
用 phpize 从源码编译共享 PECL 扩展库
相比通过 yum install 或 apt-get 等各大 Linux 发行版自带的安装方向,源码编译安装略显复杂,但是灵活性也更高,比如要安装 PCNTL 等扩展,就只能通过源码编译安装。
注意:源码编译安装时,只会安装指定的扩展,而好多扩展是默认不安装的。对于常用的数据库驱动、OpenSSL、GD 库等都需要指定。如果安装后发现遗漏,可以重新编译,加上相应的参数可得到驱动程序(Linux 下是 *.so 文件),然后复制到 PHP 的扩展目录(一般是 /usr/local/php/include/php/ext)下,并在 php.ini 配置文件中开启扩展既可以。
通过 yum list php72w-* 可以在 CentOS 中查看可以使用命令安装的扩展。
CentOS Linux release 7.7.1908 (Core)
Linux VM_0_12_centos 3.10.0-1062.9.1.el7.x86_64 #1 SMP Fri Dec 6 15:49:49 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
wget https://github.com/php/php-src/archive/php-8.0.0.tar.gz
安装依赖
yum install -y autoconf libxml2-dev libsqlite3-dev \
libcurl4-openssl-dev libssl-dev libonig-dev libtidy-dev zlib1g-dev
tar -zxvf php-src-php-8.0.0.tar.gz
yum install autoconf
运行 ./buildconf
``
然后 configure就可以安装了
参考官网地址:https://www.php.net/manual/zh/faq.build.php
执行配置选项(例如 --prefix 可以指定安装位置),判断硬件及操作系统平台,生成需要编译的 Makefile 文件
mkdir /usr/local/php800
配置扩展参数
./configure \
--prefix=/usr/local/php800 \
--exec-prefix=/usr/local/php800 \
--bindir=/usr/local/php800/bin \
--sbindir=/usr/local/php800/sbin \
--includedir=/usr/local/php800/include \
--libdir=/usr/local/php800/lib/php \
--mandir=/usr/local/php800/php/man \
--with-config-file-path=/usr/local/php800/etc \
--enable-fpm \
--with-fpm-user=www \
--with-fpm-group=www \
--enable-inline-optimization \
--disable-debug \
--disable-rpath \
--enable-shared \
--enable-soap \
--with-libxml-dir \
--with-xmlrpc \
--with-openssl \
--with-mcrypt \
--with-mhash \
--with-pcre-regex \
--with-sqlite3 \
--with-zlib \
--enable-bcmath \
--with-iconv \
--with-bz2 \
--enable-calendar \
--with-curl \
--with-cdb \
--enable-dom \
--enable-exif \
--enable-fileinfo \
--enable-filter \
--with-pcre-dir \
--enable-ftp \
--with-gd \
--with-openssl-dir \
--with-jpeg-dir \
--with-png-dir \
--with-zlib-dir \
--with-freetype-dir \
--enable-gd-native-ttf \
--enable-gd-jis-conv \
--with-gettext \
--with-gmp \
--with-mhash \
--enable-json \
--enable-mbstring \
--enable-mbregex \
--enable-mbregex-backtrack \
--with-libmbfl \
--with-onig \
--enable-pdo \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-zlib-dir \
--with-pdo-sqlite \
--with-readline \
--enable-session \
--enable-shmop \
--enable-simplexml \
--enable-sockets \
--enable-sysvmsg \
--enable-sysvsem \
--enable-sysvshm \
--enable-wddx \
--with-libxml-dir \
--with-xsl \
--enable-zip \
--enable-mysqlnd-compression-support \
--with-pear \
--enable-opcache \
需要安装re2c
yum install https://dl.fedoraproject.org/pub/epel/7/x86_64/Packages/r/re2c-0.14.3-2.el7.x86_64.rpm
wget https://sourceforge.net/projects/re2c/files/0.16/re2c-0.16.tar.gz && tar zxf re2c-0.16.tar.gz && cd re2c-0.16
./configure && make && make install
git clone https://git.code.sf.net/p/re2c/code-git re2c && cd re2c/
./autogen.sh && ./configure --prefix=/usr/local/re2c/
make && make install
ln -sf /usr/local/re2c/bin/re2c /usr/local/bin/re2c
re2c -v
re2c -h
git clone https://github.com/skvadrik/re2c.git re2c && cd re2c
./autogen.sh && ./configure
make && make install
./configure
./configure --with-openssl
./configure --with-openssl=/usr/local
./configure --with-openssl=/usr/local/xxx
结果出现了不同的结果
第一种不会安装 openssl 模块
第二种可以正常安装 openssl 模块
第三种可以正常安装 openssl 模块 (我的电脑存在 /usr/local/include/openssl/evp.h)
第四种会报错:configure: error: Cannot find OpenSSL's evp.h (我的电脑 /usr/local/xxx 是空目录)
+--------------------------------------------------------------------+
| License: |
| This software is subject to the PHP License, available in this |
| distribution in the file LICENSE. By continuing this installation |
| process, you are bound by the terms of this license agreement. |
| If you do not agree with the terms of this license, you must abort |
| the installation process at this point. |
+--------------------------------------------------------------------+
Thank you for using PHP.
configure: WARNING: unrecognized options: --enable-inline-optimization, --with-libxml-dir, --with-xmlrpc, --with-mcrypt, --with-pcre-regex, --with-pcre-dir, --with-gd, --with-jpeg-dir, --with-png-dir, --with-freetype-dir, --enable-gd-native-ttf, --enable-json, --enable-mbregex-backtrack, --with-libmbfl, --with-onig, --enable-wddx, --with-libxml-dir, --enable-zip
./configure -h
在源代码目录中,该命令可以查看所有编译参数以及对应的英文解释
--prefix=/opt/php //指定 php 安装目录
--with-apxs2=/usr/local/apache/bin/apxs //整合Apache
--with-config-file-path=/opt/php/etc //指定php.ini位置
--with-config-file-scan-dir=/opt/php/etc/php.d //指定额外拓展配置归放处文件夹
--enable-safe-mode //打开安全模式
--enable-ftp //打开ftp的支持
--enable-zip //打开对zip的支持
--with-bz2 //打开对bz2文件的支持
--with-jpeg-dir //打开对jpeg图片的支持
--with-png-dir //打开对png图片的支持
--with-freetype-dir //打开对freetype字体库的支持
--without-iconv //关闭iconv函数,各种字符集间的转换
--with-libXML-dir //打开libxml2库的支持
--with-XMLrpc //打开xml-rpc的c语言
--with-zlib-dir //打开zlib库的支持
--with-gd //打开gd库的支持
--enable-gd-native-ttf //支持TrueType字符串函数库
--with-curl //打开curl浏览工具的支持
--with-curlwrappers //运用curl工具打开url流
--with-ttf //打开freetype1.*的支持,可以不加了
--with-xsl //打开XSLT 文件支持,扩展了libXML2库 ,需要libxslt软件
--with-gettext //打开gnu 的gettext 支持,编码库用到
--with-pear //打开pear命令的支持,PHP扩展用的
--enable-calendar //打开日历扩展功能
--enable-mbstring //多字节,字符串的支持
--enable-bcmath //精度计算,解决蛋疼的数字运算精度问题
--enable-sockets //打开 sockets 支持
--enable-exif //图片的元数据支持
--enable-magic-quotes //魔术引用的支持
--disable-rpath //关闭额外的运行库文件
--disable-debug //关闭调试模式
--with-ldap-dir //轻量目录访问协议
make && make install
1、make: *** [ext/fileinfo/libmagic/apprentice.lo] Error 1
因为系统的内存不足1G造成的,然后需要加上 --disable-fileinfo,然重新编译即可;
即在执行 ./configure 时–enable-fileinfo 改为 --disable-fileinfo
清除原编译 make clean,重新生成makefile文件
Build complete.
Don't forget to run 'make test'.
[root@VM_0_12_centos php-src-php-8.0.0]# make install
Installing shared extensions: /usr/local/php800/lib/php/extensions/no-debug-non-zts-20200930/
Installing PHP CLI binary: /usr/local/php800/bin/
Installing PHP CLI man page: /usr/local/php800/php/man/man1/
Installing PHP FPM binary: /usr/local/php800/sbin/
Installing PHP FPM defconfig: /usr/local/php800/etc/
Installing PHP FPM man page: /usr/local/php800/php/man/man8/
Installing PHP FPM status page: /usr/local/php800/php/php/fpm/
Installing phpdbg binary: /usr/local/php800/bin/
Installing phpdbg man page: /usr/local/php800/php/man/man1/
Installing PHP CGI binary: /usr/local/php800/bin/
Installing PHP CGI man page: /usr/local/php800/php/man/man1/
Installing build environment: /usr/local/php800/lib/php/build/
Installing header files: /usr/local/php800/include/php/
Installing helper programs: /usr/local/php800/bin/
program: phpize
program: php-config
Installing man pages: /usr/local/php800/php/man/man1/
page: phpize.1
page: php-config.1
Installing PEAR environment: /usr/local/php800/lib/php/php/
--2020-11-26 12:29:14-- https://pear.php.net/install-pear-nozlib.phar
Resolving pear.php.net (pear.php.net)... 109.203.101.62
Connecting to pear.php.net (pear.php.net)|109.203.101.62|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 3634227 (3.5M)
Saving to: ‘pear/install-pear-nozlib.phar’
100%[==========================================================================================================================>] 3,634,227 17.5KB/s in 2m 47s
2020-11-26 12:32:02 (21.3 KB/s) - ‘pear/install-pear-nozlib.phar’ saved [3634227/3634227]
[PEAR] Archive_Tar - installed: 1.4.9
[PEAR] Console_Getopt - installed: 1.4.3
[PEAR] Structures_Graph- installed: 1.1.1
[PEAR] XML_Util - installed: 1.4.5
warning: pear/PEAR dependency package "pear/Archive_Tar" installed version 1.4.9 is not the recommended version 1.4.4
[PEAR] PEAR - installed: 1.10.12
Warning! a PEAR user config file already exists from a previous PEAR installation at '/root/.pearrc'. You may probably want to remove it.
Wrote PEAR system config file at: /usr/local/php800/etc/pear.conf
You may want to add: /usr/local/php800/lib/php/php to your php.ini include_path
/home/humx/php-src-php-8.0.0/build/shtool install -c ext/phar/phar.phar /usr/local/php800/bin/phar.phar
ln -s -f phar.phar /usr/local/php800/bin/phar
Installing PDO headers: /usr/local/php800/include/php/ext/pdo/
进入到/usr/local/php800/bin中运行 ./php -m,查看扩展安装情况
[PHP Modules]
bcmath
bz2
calendar
Core
ctype
curl
date
dba
dom
exif
filter
ftp
gettext
gmp
hash
iconv
json
libxml
mbstring
mysqli
mysqlnd
openssl
pcre
PDO
pdo_mysql
pdo_sqlite
Phar
posix
readline
Reflection
session
shmop
SimpleXML
soap
sockets
SPL
sqlite3
standard
sysvmsg
sysvsem
sysvshm
tokenizer
xml
xmlreader
xmlwriter
xsl
zlib
[Zend Modules]
进行到php8的源代码根目录中操作(编译过程中制定了位置):
cp php.ini-production /usr/local/php800/etc/php.ini
ln -s /usr/local/php800 /usr/bin/php8
或者添加 /etc/profile 进行配置,在末尾追加如下记录:
# PHP8
export PATH=$PATH:/usr/local/php800/bin
source /etc/profile 生效
配置下如下环境,可以修改监听的端口等
cd /usr/local/php800/etc
cp php-fpm.conf.default php-fpm.conf
cd /usr/local/php800/php-fpm.d
cp www.conf.default www.conf
在php-fpm.conf中配置,将pid=run/php-fpm.pid前边的“#”去掉,并修改为php-fpm800
重启php-fpm即可生效
[global]
; Pid file
; Note: the default prefix is /usr/local/php800/var
; Default Value: none
pid = run/php-fpm800.pid
cd /usr/local/php800/bin/
curl -sS https://getcomposer.org/installer | php8
cp composer.phar /usr/bin/composer8
# 阿里云 Composer 全量镜像
composer8 config -g repo.packagist composer https://mirrors.aliyun.com/composer/
# 中国全量镜像
composer config -g repo.packagist composer https://packagist.phpcomposer.com
# 更新 composer:
composer selfupdate
/etc/init.d 是 /etc/rc.d/init.d 的软链接
php源码编译目录下的 sapi/fpm/init.d.php-fpm 文件拷贝到系统配置 /etc/init.d 目录下并重命名为 php-fpm
cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod +x /etc/init.d/php-fpm
# 启动命令
service php-fpm start
service php-fpm stop
service php-fpm restart
service php-fpm reload
或
/etc/init.d/php-fpm start
/etc/init.d/php-fpm stop
/etc/init.d/php-fpm restart
/etc/init.d/php-fpm reload
vim /lib/systemd/system/php800-fpm.service
内容如下
[Unit]
Description=The PHP 8.0.0 FastCGI Process Manager
Documentation=man:php-fpm800(8)
After=network.target
[Service]
Type=simple
PIDFile=/usr/local/php800/var/run/php800-fpm.pid
ExecStart=/usr/local/php800/sbin/php-fpm --nodaemonize --fpm-config /usr/local/php800/etc/php-fpm.conf
ExecReload=/bin/kill -USR2 $MAINPID
[Install]
WantedBy=multi-user.targe
[opcache]
zend_extension=opcache.so
opcache.enable=1
opcache.jit_buffer_size=100M
opcache.jit=1255
# 启动
systemctl start php800-fpm
# 重启
systemctl restart php800-fpm
# 停止
systemctl stop php800-fpm
cd /var/www/html
composer8 create-project --prefer-dist laravel/laravel php8
配置一下.env文件
server {
listen 80;
server_name bbs.ekanshu.com.cn;
access_log /tmp/php8.log main;
root /var/www/html/php8/public;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php {
fastcgi_pass unix:/run/php/php800-fpm.sock;
fastcgi_index /index.php;
include fastcgi_params;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
laravel7的路由写法在laravel8中有点问题,改下 RouteServiceProvider.php的写法。比如API路由,将 $this->namespace改为 App\Http\Controllers\Api。
public function boot()
{
$this->configureRateLimiting();
$this->routes(function () {
Route::prefix('api')
->middleware('api')
->namespace('App\Http\Controllers\Api')
->group(base_path('routes/api.php'));
Route::middleware('web')
->namespace($this->namespace)
->group(base_path('routes/web.php'));
});
}
其他一样,不用修改。加个测试接口看看:
Route::get('/test','WxController@test');
test接口查一条数据并返回
toarray();
}
}
本次使用PHP7.3,接口代码和PHP8一致,两者都开启opcache。
服务器配置是1核2G,用ab简单测试下。
ab -n 100 -c 10 http://php8.humengxu.com/api/test
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking php8.humengxu.com (be patient).....done
Server Software: nginx
Server Hostname: php8.humengxu.com
Server Port: 80
Document Path: /api/test
Document Length: 2042 bytes
Concurrency Level: 10
Time taken for tests: 3.098 seconds
Complete requests: 100
Failed requests: 38
(Connect: 0, Receive: 0, Length: 38, Exceptions: 0)
Write errors: 0
Non-2xx responses: 38
Total transferred: 409699 bytes
HTML transferred: 377214 bytes
Requests per second: 32.28 [#/sec] (mean)
Time per request: 309.770 [ms] (mean)
Time per request: 30.977 [ms] (mean, across all concurrent requests)
Transfer rate: 129.16 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 2 1.3 2 8
Processing: 8 273 351.5 94 1474
Waiting: 8 178 245.2 94 1473
Total: 9 275 351.8 96 1476
Percentage of the requests served within a certain time (ms)
50% 96
66% 115
75% 280
80% 627
90% 823
95% 1014
98% 1476
99% 1476
100% 1476 (longest request)
ab -n 100 -c 10 http://php74.humengxu.com/api/test
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking php74.humengxu.com (be patient).....done
Server Software: nginx
Server Hostname: php74.humengxu.com
Server Port: 80
Document Path: /api/test
Document Length: 2042 bytes
Concurrency Level: 10
Time taken for tests: 3.001 seconds
Complete requests: 100
Failed requests: 32
(Connect: 0, Receive: 0, Length: 32, Exceptions: 0)
Write errors: 0
Non-2xx responses: 32
Total transferred: 379497 bytes
HTML transferred: 349896 bytes
Requests per second: 33.33 [#/sec] (mean)
Time per request: 300.061 [ms] (mean)
Time per request: 30.006 [ms] (mean, across all concurrent requests)
Transfer rate: 123.51 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 2 2.4 3 15
Processing: 9 249 379.7 102 1685
Waiting: 9 154 258.1 94 1487
Total: 9 251 379.7 106 1688
Percentage of the requests served within a certain time (ms)
50% 106
66% 122
75% 213
80% 219
90% 685
95% 1433
98% 1557
99% 1688
100% 1688 (longest request)