2020-11-26发布了PHP8.0的正式版
介绍下它的安装,并使用laravel对比下PHP7
安装与配置
本次使用的操作系统Ubuntu 18.04.4 LTS
安装
1.准备必要库
apt-get install -y autoconf libxml2-dev libsqlite3-dev \libcurl4-openssl-dev libssl-dev libonig-dev libtidy-dev zlib1g-dev
2.去官网下载8.0正式版 https://www.php.net/releases/8.0/en.php
3.解压安装
tar php-8.0.0.tar.gzcd php-8.0.0./configure --prefix=/opt/php8 --with-config-file-path=/opt/php8/etc \--enable-fpm --enable-mysqlnd --enable-opcache --enable-pcntl \--enable-mbstring --enable-soap --enable-zip --enable-calendar \--enable-bcmath --enable-exif --enable-ftp --enable-intl --with-mysqli \--with-pdo-mysql --with-openssl --with-curl --with-gd --with-gettext \--with-mhash --with-openssl --with-mcrypt --with-tidy --enable-wddx \--with-xmlrpc --with-zlibmakemake installcp php.ini-production /opt/php/etc/php.inicd /opt/php8/etccp php-fpm.conf.default php-fpm.confcp ./php-fpm.d/www.conf.default ./php-fpm.d/www.conf
4.做个软连接
ln -s /opt/php8/bin/php /usr/bin/php8
4.安装composer
cd /opt/php8/bin/curl -sS https://getcomposer.org/installer | php8ln -s /opt/php8/bin/composer.phar /usr/bin/composer8composer8 config -g repo.packagist composer https://mirrors.aliyun.com/composer/
4.添加一个php8.0的system service
vim /lib/systemd/system/php8.0-fpm.service
内容如下
[Unit]Description=The PHP 8.0 FastCGI Process ManagerDocumentation=man:php-fpm8.0(8)After=network.target[Service]Type=simplePIDFile=/var/run/php8.0-fpm.pidExecStart=/opt/php8/sbin/php-fpm --nodaemonize --fpm-config /opt/php8/etc/php-fpm.confExecReload=/bin/kill -USR2 $MAINPID[Install]WantedBy=multi-user.target
配置
fpm-fpm,php.ini配置
和PHP7一样,注意下用户权限
opcache配置
PHP8多了一个jit配置,如下
[opcache]zend_extension=opcache.soopcache.enable=1 opcache.jit_buffer_size=100Mopcache.jit=1255
启动
systemctl start php8.0-fpm
laravel
创建一个laravel项目
cd /opt/webcomposer8 create-project --prefer-dist laravel/laravel php8
配置一下.env文件
nginx配置
nginx的配置和PHP7的一致
server {listen 94;access_log/tmp/test94.log main;root /opt/web/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/php8.0-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
接口查一条数据并返回
<?phpnamespace 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。
服务器配置是1核2G,用ab简单测试下。
ab -n 100 -c 10
PHP7.3的测试结果如下:
This is ApacheBench, Version 2.3 <$Revision: 1843412 $>Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/Licensed to The Apache Software Foundation, http://www.apache.org/Benchmarking 192.168.10.10 (be patient).....doneServer Software:nginx/1.14.0Server Hostname:192.168.10.10Server Port:94Document Path:/api/testDocument Length:255 bytesConcurrency Level:10Time taken for tests: 0.400 secondsComplete requests:10Failed requests:0Total transferred:5720 bytesHTML transferred: 2550 bytesRequests per second:25.00 [#/sec] (mean)Time per request: 399.994 [ms] (mean)Time per request: 39.999 [ms] (mean, across all concurrent requests)Transfer rate:13.97 [Kbytes/sec] receivedConnection Times (ms)minmean[+/-sd] median maxConnect:8 10 2.1 1113Processing: 10115942.8174 228Waiting:10115942.8174 228Total:11417042.0186 235Percentage of the requests served within a certain time (ms)50%18666%18675%19780%21990%23595%23598%23599%235 100%235 (longest request)
PHP8.0的测试结果如下:
This is ApacheBench, Version 2.3 <$Revision: 1843412 $>Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/Licensed to The Apache Software Foundation, http://www.apache.org/Benchmarking 192.168.10.10 (be patient).....doneServer Software:nginx/1.14.0Server Hostname:192.168.10.10Server Port:94Document Path:/api/testDocument Length:255 bytesConcurrency Level:10Time taken for tests: 2.441 secondsComplete requests:100Failed requests:33 (Connect: 0, Receive: 0, Length: 33, Exceptions: 0)Non-2xx responses:33Total transferred:268489 bytesHTML transferred: 234720 bytesRequests per second:40.97 [#/sec] (mean)Time per request: 244.096 [ms] (mean)Time per request: 24.410 [ms] (mean, across all concurrent requests)Transfer rate:107.42 [Kbytes/sec] receivedConnection Times (ms)minmean[+/-sd] median maxConnect:7 1518.1 10 132Processing:47210 224.4106 817Waiting: 17118 107.4101 787Total: 55226 222.1122 828Percentage of the requests served within a certain time (ms)50%12266%13775%16480%28990%64095%80998%82099%828 100%828 (longest request)