PHP

PHP8编译安装

已邀请:

安装

1.准备必要库

  1. apt-get install -y autoconf libxml2-dev libsqlite3-dev \
  2. libcurl4-openssl-dev libssl-dev libonig-dev libtidy-dev zlib1g-dev

2.去官网下载8.0正式版 https://www.php.net/releases/8.0/en.php

3.解压安装

  1. tar php-8.0.0.tar.gzcd php-8.0.0
  2. ./configure --prefix=/opt/php8 --with-config-file-path=/opt/php8/etc \
  3. --enable-fpm --enable-mysqlnd --enable-opcache --enable-pcntl \
  4. --enable-mbstring --enable-soap --enable-zip --enable-calendar \
  5. --enable-bcmath --enable-exif --enable-ftp --enable-intl --with-mysqli \
  6. --with-pdo-mysql --with-openssl --with-curl --with-gd --with-gettext \
  7. --with-mhash --with-openssl --with-mcrypt --with-tidy --enable-wddx \
  8. --with-xmlrpc --with-zlib
  9. make 
  10. make install
  11. cp php.ini-production /opt/php/etc/php.ini
  12. cd /opt/php8/etc
  13. cp php-fpm.conf.default php-fpm.conf
  14. cp ./php-fpm.d/www.conf.default ./php-fpm.d/www.conf

4.做个软连接

  1. ln -s /opt/php8/bin/php /usr/bin/php8

5.安装composer

  1. cd /opt/php8/bin/curl -sS https://getcomposer.org/installer | php8ln -s /opt/php8/bin/composer.phar /usr/bin/composer8
  2. composer8 config -g repo.packagist composer https://mirrors.aliyun.com/composer/

6.添加一个php8.0的system service

  1. vim /lib/systemd/system/php8.0-fpm.service

内容如下

  1. [Unit]
  2. Description=The PHP 8.0 FastCGI Process Manager
  3. Documentation=man:php-fpm8.0(8)
  4. After=network.target
  5. [Service]
  6. Type=simple
  7. PIDFile=/var/run/php8.0-fpm.pid
  8. ExecStart=/opt/php8/sbin/php-fpm --nodaemonize --fpm-config /opt/php8/etc/php-fpm.conf
  9. ExecReload=/bin/kill -USR2 $MAINPID
  10. [Install]
  11. WantedBy=multi-user.target

配置

fpm-fpm,php.ini配置

和PHP7一样,注意下用户权限

opcache配置

PHP8多了一个jit配置,如下

  1. [opcache]
  2. zend_extension=opcache.so
  3. opcache.enable=1
  4. opcache.jit_buffer_size=100M
  5. opcache.jit=1255

启动

  1. systemctl start php8.0-fpm

laravel

创建一个laravel项目【推荐:laravel视频教程】

  1. cd /opt/web
  2. composer8 create-project --prefer-dist laravel/laravel php8

配置一下.env文件

nginx配置

nginx的配置和PHP7的一致

  1. server {
  2. listen 94;
  3. access_log /tmp/test94.log main;
  4. root /opt/web/php8/public;
  5. index index.php index.html index.htm;
  6. location / {
  7. try_files $uri $uri/ /index.php?$query_string;
  8. }
  9. location ~ \.php {
  10. fastcgi_pass unix:/run/php/php8.0-fpm.sock;
  11. fastcgi_index /index.php;
  12. include fastcgi_params;
  13. fastcgi_split_path_info ^(.+\.php)(/.+)$;
  14. fastcgi_param PATH_INFO $fastcgi_path_info;
  15. fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
  16. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  17. }
  18. }

添加一个接口

laravel7的路由写法在laravel8中有点问题,改下RouteServiceProvider.php的写法。
比如API路由,将$this->namespace改为App\Http\Controllers\Api

  1. public function boot()
  2. {
  3. $this->configureRateLimiting();
  4. $this->routes(function () {
  5. Route::prefix('api')
  6. ->middleware('api')
  7. ->namespace('App\Http\Controllers\Api')
  8. ->group(base_path('routes/api.php'));
  9. Route::middleware('web')
  10. ->namespace($this->namespace)
  11. ->group(base_path('routes/web.php'));
  12. });
  13. }

其他一样,不用修改。

加个测试接口看看:

  1. Route::get('/test','WxController@test');

test接口查一条数据并返回

  1. use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
  2. public function test(Request $request)
  3. {
  4. return WareHouses::find(1)->toarray();
  5. }}

对比测试PHP7

本次使用PHP7.3,接口代码和PHP8一致,两者都开启opcache。

服务器配置是1核2G,用ab简单测试下。

  1. ab -n 100 -c 10

PHP7.3的测试结果如下:

  1. This is ApacheBench, Version 2.3 <$Revision: 1843412 $>
  2. Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
  3. Licensed to The Apache Software Foundation, http://www.apache.org/
  4. Benchmarking 192.168.10.10 (be patient).....done
  5. Server Software: nginx/1.14.0
  6. Server Hostname: 192.168.10.10
  7. Server Port: 94
  8. Document Path: /api/test
  9. Document Length: 255 bytes
  10. Concurrency Level: 10
  11. Time taken for tests: 0.400 seconds
  12. Complete requests: 10
  13. Failed requests: 0
  14. Total transferred: 5720 bytes
  15. HTML transferred: 2550 bytes
  16. Requests per second: 25.00 [#/sec] (mean)
  17. Time per request: 399.994 [ms] (mean)
  18. Time per request: 39.999 [ms] (mean, across all concurrent requests)
  19. Transfer rate: 13.97 [Kbytes/sec] received
  20. Connection Times (ms)
  21. min mean[+/-sd] median max
  22. Connect: 8 10 2.1 11 13
  23. Processing: 101 159 42.8 174 228
  24. Waiting: 101 159 42.8 174 228
  25. Total: 114 170 42.0 186 235
  26. Percentage of the requests served within a certain time (ms)
  27. 50% 186
  28. 66% 186
  29. 75% 197
  30. 80% 219
  31. 90% 235
  32. 95% 235
  33. 98% 235
  34. 99% 235
  35. 100% 235 (longest request)

PHP8.0的测试结果如下:

  1. This is ApacheBench, Version 2.3 <$Revision: 1843412 $>
  2. Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
  3. Licensed to The Apache Software Foundation, http://www.apache.org/
  4. Benchmarking 192.168.10.10 (be patient).....done
  5. Server Software: nginx/1.14.0
  6. Server Hostname: 192.168.10.10
  7. Server Port: 94
  8. Document Path: /api/test
  9. Document Length: 255 bytes
  10. Concurrency Level: 10
  11. Time taken for tests: 2.441 seconds
  12. Complete requests: 100
  13. Failed requests: 33
  14. (Connect: 0, Receive: 0, Length: 33, Exceptions: 0)
  15. Non-2xx responses: 33
  16. Total transferred: 268489 bytes
  17. HTML transferred: 234720 bytes
  18. Requests per second: 40.97 [#/sec] (mean)
  19. Time per request: 244.096 [ms] (mean)
  20. Time per request: 24.410 [ms] (mean, across all concurrent requests)
  21. Transfer rate: 107.42 [Kbytes/sec] received
  22. Connection Times (ms)
  23. min mean[+/-sd] median max
  24. Connect: 7 15 18.1 10 132
  25. Processing: 47 210 224.4 106 817
  26. Waiting: 17 118 107.4 101 787
  27. Total: 55 226 222.1 122 828
  28. Percentage of the requests served within a certain time (ms)
  29. 50% 122
  30. 66% 137
  31. 75% 164
  32. 80% 289
  33. 90% 640
  34. 95% 809
  35. 98% 820
  36. 99% 828
  37. 100% 828 (longest request)

以上就是PHP8.0的编译安装与使用(详解)的详细内容

要回复问题请先登录注册