将WordPress内核预加载到内存中以加快网页生成速度

标签: 

正在查看 7 个帖子:1-6 (共 6 个帖子)
  • 作者
    帖子
  • 孙锡源
    • 文章数量: 704
    @ibadboy
    楼主

    自PHP 7.4起,OPCache新增了一个Preload特性,该功能可以将指定的文件预加载到内存中,这样当程序运行时等于是直接从内存到CPU进行运算,省略了磁盘IO的过程。

    不过这个特性配置起来会有很多问题,比如说可能导致同一个类或方法被声明多次从而触发致命错误。

    具体导致错误的原因以及错误的现象千奇百怪,得见招拆招了,实在没办法整理出一个能让小白看懂的通用教程。所以这里只对WordPress核心进行预加载,这样错误范围相对可控。

    在WordPress根目录新建一个preload.php文件,写入如下内容:

    <?php
    /**
     * 这个文件被PHP的Preload功能执行以实现在PHP启动时预加载WordPress内核代码
     */
    
    error_reporting( E_ERROR );
    
    $directory = new RecursiveDirectoryIterator( __DIR__ );
    $fullTree  = new RecursiveIteratorIterator( $directory );
    $phpFiles  = new RegexIterator( $fullTree, '/.+((?<!Test)+.php$)/i', RecursiveRegexIterator::GET_MATCH );
    
    foreach ( $phpFiles as $key => $file ) {
           if (
                  stristr( $file[0], 'wp-admin/includes' ) ||
                  stristr( $file[0], 'wp-content/' )
           ) {
                  continue;
           }
    
           opcache_compile_file( $file[0] );
    }
    

    之后在PHP配置文件中的[Zend Opcache]配置区块的末尾追加如下两行:

    opcache.preload=/www/wwwroot/litepress.cn/preload.php ; 这里是你的preload.php文件的路径,别直接复制……
    opcache.preload_user=www

    最后重启PHP即可,重启的速度会比较慢,因为要预加载文件。

    如此配置后大概可以提升50ms左右的首字节速度,当然,在硬盘越差的机器上效果越明显。

    最后的最后提醒一下:如果WordPress版本更新了要重启PHP服务以重新进行预加载,这样新的文件才会生效。

    来自, 本机地址, 本机地址
    rawchen
    • 文章数量: 196
    @rawchen

    在重启PHP的时候出现了下面的警告消息,我用的PHP7.4,然后我覆盖安装beta5(之前是beta4.1),重新安装opcache扩展,刷新opcache缓存,还是下面的警告消息。

    [22-Jun-2021 09:33:33] NOTICE: PHP message: PHP Warning: Can't preload unlinked class WP_SimplePie_File: Parent with unresolved initializers SimplePie_File in /www/wwwroot/wp.bufanz.com/wp-includes/class-wp-simplepie-file.php on line 20
    [22-Jun-2021 09:33:33] NOTICE: PHP message: PHP Warning: Can't preload already declared class TypeError in /www/wwwroot/wp.bufanz.com/wp-includes/random_compat/error_polyfill.php on line 44
    [22-Jun-2021 09:33:33] NOTICE: PHP message: PHP Warning: Can't preload already declared class TypeError in /www/wwwroot/wp.bufanz.com/wp-includes/random_compat/error_polyfill.php on line 39
    [22-Jun-2021 09:33:33] NOTICE: PHP message: PHP Warning: Can't preload already declared class Error in /www/wwwroot/wp.bufanz.com/wp-includes/random_compat/error_polyfill.php on line 31
    [22-Jun-2021 09:33:33] NOTICE: PHP message: PHP Warning: Can't preload unlinked class Snoopy: Unknown reason in /www/wwwroot/wp.bufanz.com/wp-includes/class-snoopy.php on line 38
    [22-Jun-2021 09:33:33] NOTICE: PHP message: PHP Warning: Can't preload unlinked class MO: Unknown parent Gettext_Translations in /www/wwwroot/wp.bufanz.com/wp-includes/pomo/mo.php on line 14
    [22-Jun-2021 09:33:33] NOTICE: PHP message: PHP Warning: Can't preload unlinked class PO: Unknown parent Gettext_Translations in /www/wwwroot/wp.bufanz.com/wp-includes/pomo/po.php on line 22
    [22-Jun-2021 09:33:33] NOTICE: PHP message: PHP Warning: Can't preload unlinked class Plural_Forms: Unknown reason in /www/wwwroot/wp.bufanz.com/wp-includes/pomo/plural-forms.php on line 9
    [22-Jun-2021 09:33:33] NOTICE: PHP message: PHP Warning: Can't preload unlinked class NOOP_Translations: Unknown reason in /www/wwwroot/wp.bufanz.com/wp-includes/pomo/translations.php on line 303
    [22-Jun-2021 09:33:33] NOTICE: PHP message: PHP Warning: Can't preload unlinked class Gettext_Translations: Unknown parent Translations in /www/wwwroot/wp.bufanz.com/wp-includes/pomo/translations.php on line 184
    [22-Jun-2021 09:33:33] NOTICE: PHP message: PHP Warning: Can't preload unlinked class Translations: Unknown reason in /www/wwwroot/wp.bufanz.com/wp-includes/pomo/translations.php on line 14
    [22-Jun-2021 09:33:33] NOTICE: PHP message: PHP Warning: Can't preload unlinked class POMO_CachedIntFileReader: Unknown parent POMO_CachedFileReader in /www/wwwroot/wp.bufanz.com/wp-includes/pomo/streams.php on line 329
    [22-Jun-2021 09:33:33] NOTICE: PHP message: PHP Warning: Can't preload unlinked class POMO_CachedFileReader: Unknown parent POMO_StringReader in /www/wwwroot/wp.bufanz.com/wp-includes/pomo/streams.php on line 298
    [22-Jun-2021 09:33:33] NOTICE: PHP message: PHP Warning: Can't preload unlinked class POMO_StringReader: Unknown parent POMO_Reader in /www/wwwroot/wp.bufanz.com/wp-includes/pomo/streams.php on line 227
    [22-Jun-2021 09:33:33] NOTICE: PHP message: PHP Warning: Can't preload unlinked class POMO_FileReader: Unknown parent POMO_Reader in /www/wwwroot/wp.bufanz.com/wp-includes/pomo/streams.php on line 146
    [22-Jun-2021 09:33:33] NOTICE: PHP message: PHP Warning: Can't preload unlinked class POMO_Reader: Unknown reason in /www/wwwroot/wp.bufanz.com/wp-includes/pomo/streams.php on line 12
    [22-Jun-2021 09:33:33] NOTICE: PHP message: PHP Warning: Can't preload unlinked class Translation_Entry: Unknown reason in /www/wwwroot/wp.bufanz.com/wp-includes/pomo/entry.php on line 14
    [22-Jun-2021 09:33:33] NOTICE: PHP message: PHP Warning: Can't preload unlinked class Services_JSON_Error: Unknown reason in /www/wwwroot/wp.bufanz.com/wp-includes/class-json.php on line 1008
    [22-Jun-2021 09:33:33] NOTICE: PHP message: PHP Warning: Can't preload unlinked class Services_JSON_Error: Unknown parent PEAR_Error in /www/wwwroot/wp.bufanz.com/wp-includes/class-json.php on line 974
    [22-Jun-2021 09:33:33] NOTICE: PHP message: PHP Warning: Can't preload unlinked class Services_JSON: Unknown reason in /www/wwwroot/wp.bufanz.com/wp-includes/class-json.php on line 122
    [22-Jun-2021 09:33:33] NOTICE: PHP message: PHP Warning: Can't preload unlinked class SimplePie: Unknown reason in /www/wwwroot/wp.bufanz.com/wp-includes/class-simplepie.php on line 448
    [22-Jun-2021 09:33:33] NOTICE: PHP message: PHP Warning: Can't preload unlinked class SimplePie_Core: Unknown parent SimplePie in /www/wwwroot/wp.bufanz.com/wp-includes/SimplePie/Core.php on line 53
    [22-Jun-2021 09:33:33] NOTICE: PHP message: PHP Warning: Can't preload class SimplePie_File with unresolved initializer for property $method in /www/wwwroot/wp.bufanz.com/wp-includes/SimplePie/File.php on line 55
    [22-Jun-2021 09:33:33] NOTICE: PHP message: PHP Warning: Can't preload unlinked class wp_atom_server: Unknown reason in /www/wwwroot/wp.bufanz.com/wp-includes/pluggable-deprecated.php on line 199
    [22-Jun-2021 09:33:33] NOTICE: PHP message: PHP Warning: Can't preload already declared class SodiumException in /www/wwwroot/wp.bufanz.com/wp-includes/sodium_compat/src/SodiumException.php on line 7
    [22-Jun-2021 09:33:33] NOTICE: PHP message: PHP Warning: Can't preload already declared class SplFixedArray in /www/wwwroot/wp.bufanz.com/wp-includes/sodium_compat/src/PHP52/SplFixedArray.php on line 14
    [22-Jun-2021 09:33:33] NOTICE: PHP message: PHP Warning: Can't preload class getID3 with unresolved initializer for property $tempdir in /www/wwwroot/wp.bufanz.com/wp-includes/ID3/getid3.php on line 82
    [22-Jun-2021 09:33:33] NOTICE: PHP message: PHP Warning: Can't preload class getid3_matroska with unresolved initializer for property $unuseful_elements in /www/wwwroot/wp.bufanz.com/wp-includes/ID3/module.audio-video.matroska.php on line 219

     

    来自, 本机地址, 本机地址
    孙锡源
    • 文章数量: 704
    @ibadboy
    楼主

    不用管这些警告信息。

    只要php能跑起来就ok。

    因为预加载不会执行需要动态生成的代码,比如说某些宏需要运行时才确定。

    其次是代码执行顺序会被打乱,某些代码缺失上下文会弹出警告。

    这些都不会影响你程序的运行,大可放心。

    来自, 本机地址, 本机地址
    rawchen
    • 文章数量: 196
    @rawchen

    在这里上传图片的时候,源的输入框显示的这个内容

    A potentially unsafe operation has been detected in your request to this site	Your access to this service has been limited. (HTTP response code 403)	If you think you have been blocked in error, contact the owner of this site for assistance.		Block Technical Data						Block Reason:			A potentially unsafe operation has been detected in your request to this site							Time:			Tue, 22 Jun 2021 2:06:49 GMT																About Wordfence			Wordfence is a security plugin installed on over 3 million WordPress sites. The owner of this site is using Wordfence to manage access to their site.			You can also read the documentation to learn about Wordfence's blocking tools, or visit wordfence.com to learn more about Wordfence.				Click here to learn more: Documentation	Generated by Wordfence at Tue, 22 Jun 2021 2:06:49 GMT.Your computer's time: document.write(new Date().toUTCString());Tue, 22 Jun 2021 02:06:47 GMT.

     

    来自, 本机地址, 本机地址
    孙锡源
    • 文章数量: 704
    @ibadboy
    楼主

    传的图被安全软件拦截了。可能原因是加了一层代理之后服务器获取到的都是代理IP,导致防火墙软件无法很好甄别攻击,这个很快会修复。

    来自, 本机地址, 本机地址
    maqingxi
    • 文章数量: 7
    @maqingxi
      thrown in /www/wwwroot/rss.chenyan98.cn/app/Models/Feed.php on line 62" while reading response header from upstream, client: 58.208.13.11, server: rss.chenyan98.cn, request: "POST /p/i/?c=feed&a=actualize&id=7&ajax=1 HTTP/2.0", upstream: "fastcgi://unix:/tmp/php-cgi-74.sock:", host: "rss.chenyan98.cn"
    2021/06/22 10:23:30 [error] 17504#0: *253165 FastCGI sent in stderr: "PHP message: PHP Fatal error:  Uncaught Error: Call to undefined method SimplePie_Misc::url_remove_credentials() in /www/wwwroot/rss.chenyan98.cn/app/Models/Feed.php:62
    Stack trace:
    #0 /www/wwwroot/rss.chenyan98.cn/app/Controllers/feedController.php(333): FreshRSS_Feed->url()
    #1 /www/wwwroot/rss.chenyan98.cn/app/Controllers/feedController.php(564): FreshRSS_feed_Controller::actualizeFeed()
    #2 /www/wwwroot/rss.chenyan98.cn/lib/Minz/Dispatcher.php(119): FreshRSS_feed_Controller->actualizeAction()
    #3 /www/wwwroot/rss.chenyan98.cn/lib/Minz/Dispatcher.php(47): Minz_Dispatcher->launchAction()
    #4 /www/wwwroot/rss.chenyan98.cn/lib/Minz/FrontController.php(81): Minz_Dispatcher->run()
    #5 /www/wwwroot/rss.chenyan98.cn/p/i/index.php(57): Minz_FrontController->run()
    #6 {main}

    在php配置文件内加入opcache.preload之后与wordpress共用php7.4的FreshRSS无法获取内容,现在把代码去掉了,在我2H4G6M的配置下提升好像不是很大…

    来自, 本机地址, 本机地址
    孙锡源
    • 文章数量: 704
    @ibadboy
    楼主

    你这是WordPress吗……目测是thinkphp或者laravel框架做的

    来自张家口市, 河北省, 中国
正在查看 7 个帖子:1-6 (共 6 个帖子)
  • 哎呀,回复话题必需登录。

话题信息