自制的laravel第三方包无法读写session的问题记录

2022年4月8日 laravel

自己开发的laravel第三方composer包,由于需要使用到session,包配置成自动发现,在ServiceProvider的boot中添加了自动加载路由 loadRoutesFrom 结果死活不能正常使用。

最后各种搜索发现,原因是 “StartSession” 仅被添加到web中间件中,必须给包路由加了 [‘middleware’=>’web’]才可以正常使用session

laravel-echo-server 记一次认证失败原因403

2022年3月24日 laravel

自定义private-channel时,避免使用private-开头的频道名,系统默认的模型认证会在频道名上自动加上‘private-’前缀,自定义频道名时,避开这规则,同时在config/channels.php里对自定义的私有频道进行登录权限验证,如:

Broadcast::channel('custom-private-channel-{id}', function ($user, $id) {
    if( is_numeric($id) ){
        return (int) $user->id === (int) $id;
    }else{
        return session()->getId() === $id;
    }
});

在laravel-echo-server.json设置devMode=true查看日志,确认所有验证能正常通过后,私有频道即可监听到。

laravel 处理 http_build_query() 参数中的 PHP_QUERY_RFC3986(URL编码协议)

2022年1月10日 laravel, PHP

原生 http_build_query() 方法定义:

function http_build_query (object|array $data, string $numeric_prefix = "", ?string $arg_separator = "&", int $encoding_type = PHP_QUERY_RFC1738)

指定使用 PHP_QUERY_RFC3986:

$query = http_build_query($query_data, null, null, PHP_QUERY_RFC3986);

laravel:

GuzzleHttp\Psr7\Query::build() 默认使用 PHP_QUERY_RFC3986

方法定义:

public static function build(array $params, $encoding = PHP_QUERY_RFC3986)

laravel6.x 使用 config:cache 导致 env() 函数不能直接读取.env 文件

2022年1月8日 laravel

线上环境一但使用 php artisan config:cache 就会各种异常,最终检查原因是使用 config:cache 后env() 是无法读取 .env文件配置的。

 

将 config 文件之外的所有 env() 方法使用 config() 代替

如:

在 config/app.php 中添加:'force_https' => env('FORCE_HTTPS', false),

调用时使用 config('app.force_https')

 

PS: 通过composer引用的包不受此影响

关于laravel6 的https踩坑记录

2022年1月6日 laravel

开发环境是http的,线上环境https,导致线上资源加载http而无法加载。

大多网友遇到问题是通过给asset()加第二个参数,或使用secure_asset()代替

两个方法都要修改所有引用的blade,还有一些第三方blade重写更加麻烦,如horizon的单页面

废话少说,解决方案:

 

1、.env 添加ASSET_URL=xxx 与APP_URL一致,用于给asset()添加固定的https前缀

2、.env 添加 FORCE_HTTPS=true,本地环境false

3、config/app.php 添加'force_https' => env('FORCE_HTTPS', false)

4、AppServiceProvider boot() 里添加如下代码,用于解决url()生成https链接

        if( config('app.force_https') ){
            \Url::forceScheme('https');
        }

5、AppServiceProvider register() 里添加如下代码,用于解决pjax加载https

        if( config('app.force_https') ) {
            $this->app['request']->server->set('HTTPS', true);
        }

6、测试,OK

gitee上fork了某项目,让自己的项目代码保持更新

2021年9月2日 git

gitee上fork了某项目,让自己的项目代码保持更新

首先保证本地仓库的upstream是源项目的URL,若没有则添加

git remote add upstream https://gitee.com/***/***.git

然后利用fetch和merge合并upstream的master分支:
git fetch upstream
git merge upstream/master

此时本地的master分支就更新至upstream的master版本
然后利用push将本地分支覆盖到git远程分支上:

git push origin master:master

完成

Key path “file:///tmp/oauth-private.key” does not exist or is not readable 原因

2021年7月1日 laravel

[LogicException]                                                                   
  Key path "file:///tmp/oauth-private.key" does not exist or is not readable  

laravel-s服务器,通过laravel/passport鉴权时总是报上面错,后面发现是php bin/laravels start的启动用户权限问题,启动laravels的用户没有passport的密钥读取权限

wrk压力测试

2021年6月30日 未分类

wrk是一款简单的HTTP压测工具,托管在Github上,https://github.com/wg/wrk.
wrk 的一个很好的特性就是能用很少的线程压出很大的并发量. 原因是它使用了一些操作系统特定的高性能 io 机制, 比如 select, epoll, kqueue 等.

git clone https://github.com/wg/wrk.git
cd wrk make
cp wrk /usr/local/bin

测试

wrk -t4 -c100 -d30s http://www.lenmot.com  

 

swoole不能通过添加php.ini扩展方式添加到PHP扩展中

2021年6月29日 PHP

Manually enabling Swoole via php.ini

On most systems Swoole can be enabled by directly editing your

php.ini

configuration file, you will want to add

extension=swoole

to the end of the file and then Swoole should be enabled.

Enabling Swoole via phpenmod

Some Linux distributions like Debian or Ubuntu use the PHP

mods-available

to load PHP extensions, in this case you can use

phpenmod

to enable Swoole.

 

一般系统 都可以通过添加扩展到php.ini的方式添加swoole支持,但我用的debian10+php7.4不支持,报如下错
 

PHP Warning: PHP Startup: Unable to load dynamic library 'swoole' (tried: /usr/lib/php/20190902/swoole (/usr/lib/php/20190902/swoole: cannot open shared object file: No such file or directory), /usr/lib/php/20190902/swoole.so (/usr/lib/php/20190902/swoole.so: undefined symbol: php_json_exception_ce)) in Unknown on line 0

 

只能通过

 phpenmod 方式添加

#!/bin/bash

# Create a Swoole extension PHP ini file, letting PHP know which modules to load
$ sudo bash -c "cat > /etc/php/<PHP_VERSION>/mods-available/swoole.ini << EOF
; Configuration for Swoole
; priority=25
extension=swoole
EOF"

# Enable the Swoole PHP extension only on CLI mode
$ sudo phpenmod -s cli swoole

# Optional: Enable Swoole for specific version of PHP CLI
$ sudo phpenmod -s cli -v 7.4 swoole

# Check if the Swoole extension has been enabled
$ php -m | grep swoole

 

You can also disable Swoole using

phpdismod

if you need to (this is not uninstalling, just turning Swoole off):

#!/bin/bash

# Turn off Swoole for CLI or a specific PHP install
sudo phpdismod -s cli swoole
sudo phpdismod -s cli -v 7.4 swoole

supervisor 启动 laravel-echo-server 关于设置 startsecs

2021年6月23日 laravel

laravel-echo-server启动需要比较长时间,如果通过supervisor来监控laravel-echo-server的运行状态,极有可能在默认情况下超时导致重新启动,很容易就死循环了。

startsecs默认是10,如果进程启动有可能超过10秒,则设置大一点吧

;[program:example]
;command=/bin/echo; the program (relative uses PATH, can take args)
;priority=999                ; the relative start priority (default 999)
;autostart=true              ; start at supervisord start (default: true)
;autorestart=true            ; retstart at unexpected quit (default: true)
;startsecs=10                ; number of secs prog must stay running (def. 10)
;startretries=3              ; max # of serial start failures (default 3)
;exitcodes=0,2               ; 'expected' exit codes for process (default 0,2)
;stopsignal=QUIT             ; signal used to kill process (default TERM)
;stopwaitsecs=10             ; max num secs to wait before SIGKILL (default 10)
;user=chrism                 ; setuid to this UNIX account to run the program
;log_stdout=true             ; if true, log program stdout (default true)
;log_stderr=true             ; if true, log program stderr (def false)
;logfile=/var/log/supervisor.log    ; child log path, use NONE for none; default AUTO
;logfile_maxbytes=1MB        ; max # logfile bytes b4 rotation (default 50MB)
;logfile_backups=10          ; # of logfile backups (default 10)
 

 

 

;command=/bin/echo;         supervisor启动时将要开启的进程。相对或绝对路径均可。若是相对路径则会从supervisord的$PATH变中查找。命令可带参数。
;priority=999                    指明进程启动和关闭的顺序。低优先级表明进程启动时较先启动关闭时较后关闭。高优先级表明进程启动时启动时较后启动关闭时较先关闭。
;autostart=true                  是否随supervisord启动而启动
;autorestart=true                进程意外退出后是否自动重启
;startsecs=10                    进程持续运行多久才认为是启动成功
;startretries=3                  重启失败的连续重试次数
;exitcodes=0,2                   若autostart设置为unexpected且监控的进程并非因为supervisord停止而退出,那么如果进程的退出码不在exitcode列表中supervisord将重启进程
;stopsignal=QUIT                 杀进程的信号
;stopwaitsecs=10                 向进程发出stopsignal后等待OS向supervisord返回SIGCHILD 的时间。若超时则supervisord将使用SIGKILL杀进程