- 安裝前的扯犢子科普工作
PHP 三大模塊:
- SAPI:接受并處理請求。
- Zend Engine:PHP 的核心,負(fù)責(zé)分析 PHP 代碼并轉(zhuǎn)為 opcode,然后在 Zend VM 虛擬機(jī)上執(zhí)行。
- PHP 功能函數(shù)及擴(kuò)展層:實現(xiàn)各種功能,例如 curl 通信、連接 MySQL 等。
PHP 中,有以下幾種方式添加擴(kuò)展:
-
把擴(kuò)展編譯進(jìn) PHP
-
通過 pecl 命令或 Linux 發(fā)行版自帶的 yum、apt-get 等命令安裝,動態(tài)加載(Linux 下是 *.so 文件,Windows 下是 *.dll 文件)
-
用 phpize 從源碼編譯共享 PECL 擴(kuò)展庫
相比通過 yum install 或 apt-get 等各大 Linux 發(fā)行版自帶的安裝方向,源碼編譯安裝略顯復(fù)雜,但是靈活性也更高,比如要安裝 PCNTL 等擴(kuò)展,就只能通過源碼編譯安裝。 -
注意:源碼編譯安裝時,只會安裝指定的擴(kuò)展,而好多擴(kuò)展是默認(rèn)不安裝的。對于常用的數(shù)據(jù)庫驅(qū)動、OpenSSL、GD 庫等都需要指定。如果安裝后發(fā)現(xiàn)遺漏,可以重新編譯,加上相應(yīng)的參數(shù)可得到驅(qū)動程序(Linux 下是 *.so 文件),然后復(fù)制到 PHP 的擴(kuò)展目錄(一般是 /usr/local/php/include/php/ext)下,并在 php.ini 配置文件中開啟擴(kuò)展既可以。
通過 yum list php72w-* 可以在 CentOS 中查看可以使用命令安裝的擴(kuò)展。
- 概述啰嗦完成,接著來干正事
安裝環(huán)境
- 1核2GB配置,建議玩家能高就高,下文就有編譯過程中低配置的問題體現(xiàn),仔細(xì)閱讀,謹(jǐn)慎跳躍查看
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
0、下載地址
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
1、壓縮包解壓常規(guī)操作 php-src-php-8.0.0.tar.gz
tar -zxvf php-src-php-8.0.0.tar.gz
2、從官方包中拿到的數(shù)據(jù)不含configure腳本,需要將安裝GNU autoconf,來生成configure
yum install autoconf
運(yùn)行 ./buildconf
``
然后 configure就可以安裝了
參考官網(wǎng)地址:https://www.php.net/manual/zh/faq.build.php
3、調(diào)用configure生成Makefile文件
執(zhí)行配置選項(例如 --prefix 可以指定安裝位置),判斷硬件及操作系統(tǒng)平臺,生成需要編譯的 Makefile 文件
執(zhí)行configure的前置操作
mkdir /usr/local/php800
配置擴(kuò)展參數(shù)
./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 \
錯誤清單
(1)configure: error: re2c 0.13.4 is required to generate PHP lexers.
需要安裝re2c
yum安裝rpm包
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源碼編譯)
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
(2)測試安裝 php 的 openssl 模塊的時候,分別使用了以下方式(以具體的openssl擴(kuò)展為例)
./configure
./configure --with-openssl
./configure --with-openssl=/usr/local
./configure --with-openssl=/usr/local/xxx
結(jié)果出現(xiàn)了不同的結(jié)果
第一種不會安裝 openssl 模塊
第二種可以正常安裝 openssl 模塊
第三種可以正常安裝 openssl 模塊 (我的電腦存在 /usr/local/include/openssl/evp.h)
第四種會報錯:configure: error: Cannot find OpenSSL's evp.h (我的電腦 /usr/local/xxx 是空目錄)
預(yù)編譯擴(kuò)展信息完成提示
+--------------------------------------------------------------------+
| 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
編譯參數(shù)-使用
./configure -h
在源代碼目錄中,該命令可以查看所有編譯參數(shù)以及對應(yīng)的英文解釋
編譯參數(shù)-說明
--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 //關(guān)閉iconv函數(shù),各種字符集間的轉(zhuǎn)換
--with-libXML-dir //打開libxml2庫的支持
--with-XMLrpc //打開xml-rpc的c語言
--with-zlib-dir //打開zlib庫的支持
--with-gd //打開gd庫的支持
--enable-gd-native-ttf //支持TrueType字符串函數(shù)庫
--with-curl //打開curl瀏覽工具的支持
--with-curlwrappers //運(yùn)用curl工具打開url流
--with-ttf //打開freetype1.*的支持,可以不加了
--with-xsl //打開XSLT 文件支持,擴(kuò)展了libXML2庫 ,需要libxslt軟件
--with-gettext //打開gnu 的gettext 支持,編碼庫用到
--with-pear //打開pear命令的支持,PHP擴(kuò)展用的
--enable-calendar //打開日歷擴(kuò)展功能
--enable-mbstring //多字節(jié),字符串的支持
--enable-bcmath //精度計算,解決蛋疼的數(shù)字運(yùn)算精度問題
--enable-sockets //打開 sockets 支持
--enable-exif //圖片的元數(shù)據(jù)支持
--enable-magic-quotes //魔術(shù)引用的支持
--disable-rpath //關(guān)閉額外的運(yùn)行庫文件
--disable-debug //關(guān)閉調(diào)試模式
--with-ldap-dir //輕量目錄訪問協(xié)議
4、編譯和安裝
make && make install
錯誤提示
1、make: *** [ext/fileinfo/libmagic/apprentice.lo] Error 1
因為系統(tǒng)的內(nèi)存不足1G造成的,然后需要加上 --disable-fileinfo,然重新編譯即可;
即在執(zhí)行 ./configure 時–enable-fileinfo 改為 --disable-fileinfo
清除原編譯 make clean,重新生成makefile文件
成功make提示
Build complete.
Don't forget to run 'make test'.
成功make install 提示
[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/
5、成功安裝的測試工作
進(jìn)入到/usr/local/php800/bin中運(yùn)行 ./php -m,查看擴(kuò)展安裝情況
[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一些配置
配置php.ini
進(jìn)行到php8的源代碼根目錄中操作(編譯過程中制定了位置):
cp php.ini-production /usr/local/php800/etc/php.ini
配置環(huán)境變量
ln -s /usr/local/php800 /usr/bin/php8
或者添加 /etc/profile 進(jìn)行配置,在末尾追加如下記錄:
# PHP8
export PATH=$PATH:/usr/local/php800/bin
source /etc/profile 生效
配置php-fpm
配置下如下環(huán)境,可以修改監(jiān)聽的端口等
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的pid文件
在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
安裝composer
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
init.d的php-fpm服務(wù)的添加(跟systemd配置一個即可,php738采用此法啟動)
/etc/init.d 是 /etc/rc.d/init.d 的軟鏈接
php源碼編譯目錄下的 sapi/fpm/init.d.php-fpm 文件拷貝到系統(tǒng)配置 /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
添加一個php8.0.0的systemd
vim /lib/systemd/system/php800-fpm.service
內(nèi)容如下
[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配置
[opcache]
zend_extension=opcache.so
opcache.enable=1
opcache.jit_buffer_size=100M
opcache.jit=1255
systemctl運(yùn)行php800-fpm服務(wù)器
# 啟動
systemctl start php800-fpm
# 重啟
systemctl restart php800-fpm
# 停止
systemctl stop php800-fpm
laravel
創(chuàng)建一個laravel項目,名稱叫php8
cd /var/www/html
composer8 create-project --prefer-dist laravel/laravel php8
配置一下.env文件
nginx配置
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接口查一條數(shù)據(jù)并返回
<?php
namespace App\Http\Controllers\Api;
use App\Models\WareHouses;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller as BaseController;
class WxController extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
public function test(Request $request)
{
return WareHouses::find(1)->toarray();
}
}
對比測試PHP7
本次使用PHP7.3,接口代碼和PHP8一致,兩者都開啟opcache。文章來源:http://www.zghlxwxcb.cn/news/detail-780746.html
服務(wù)器配置是1核2G,用ab簡單測試下。文章來源地址http://www.zghlxwxcb.cn/news/detail-780746.html
先測試php8的ab結(jié)果
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)
再測試php7.4的ab結(jié)果
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)
到了這里,關(guān)于php8.0.0安裝體驗和擴(kuò)展介紹流程的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!