Go PHP

PHP 不会死 —— 我们如何使用 Golang 来阻止 PHP 走向衰亡

https://zhuanlan.zhihu.com/p/256246753

GO+PHP, 让全宇宙最好的两种语言合体的神器——RoadRunner

https://zhuanlan.zhihu.com/p/60599237

https://blog.p2hp.com/archives/7496

简单的介绍(蹩脚的机翻)

享受PHP与Go的强大合体【RoadRunner】的乐趣!官方介绍的翻译

关于RST SOAP RPC的简单对比

浅谈 RPC 和 REST: SOAP, gRPC, REST

浅谈 RPC、RMI、REST 这些很绕的概念

API怎么选?比较SOAP、REST、GraphQL和RPC

在容器内安装扩展

I have to install it manually.
First: Login into your php container as root, then install via pecl.

docker exec -it -u 0 php /bin/sh
pecl install ast-1.0.1

After that, you need to enable it in php.ini like:

extension=ast.so

restart php-fpm  in docker with

kill -USR2 1

This solves my problem and may help. (edited) 

docker-compose容器间的通讯之link篇

容器之间的通讯有几种。

1 通过端口。

比如,php fpm容器通过暴露9000端口,nginx利用该端口,把php交给fpm容器

再复杂一点,还可以通过配置虚拟网络的。让不同的服务运行在不同的ip的端口上。

2. 通过link
更简单的方法是通过link。当一个容器需要另外一个容器的是时候,只要在docker-compse.yml文件里写上link 服务名service : 别名alias 即可。在该容器内,这个 service(或者别名)就是指向另外的容器的。

例子

比如有2个容器,php容器和db容器。php容器需要访问db,发现通过127.0.0.1这样的并不可以。因为,db是运行在另外一个容器内的,127.0.0.1则是本php容器。而非db容器- 通过expose暴露的端口也仅仅是暴露给宿主,而不是其他容器。这样只要在php容器的配置加上link:

php:
    ....
    links:
      - "mysql"

mysql:
    ....

然后在 php 的配置里,链接数据库只要写 mysql 即可

DATABASE_URL="mysqli://user:user@mysql:3306/user-db?serverVersion=5.7"

进入php容器查看ping mysql,会发现docker已经把mysql这个写入了类似hosts文件的地方。具体哪我也不知道。

Symfony 5.2 Secutrity 之 Auth Form Login 的流程

AuthLog属于securtiy模块,配置文件是security.yaml文件。配置中主要的key是 firewall.

负责Form登陆鉴定的类主要是LoginFormAuthenticator , 实现了 AuthenticatorInterface .

关于几个概念, 可以参考这篇文章

  • authen-tication 鉴权 (who are you?)
  • autho-rization 授权 (what permission do you have?)

AbstractFormLoginAuthenticator (以下简称AFA)

  • AFA::supports($request $req) bool
    Does the authenticator support the given Request? If this returns false, the authenticator will be skipped.
    用来判断是否需要使用登录鉴定
  • AFA::getCredentials(Request $req)::array
    用来获取客户输入的登录凭证(用户名和密码),同时把用户的最后一次输入的用户名保存到session中,已备现实错误信息时使用
  • AFA::getUser($credentials, $userProvider): userInterface
    从userProvider获取用户模型。如果用户没有找到呢?
  • AFA::checkCredentials(credentials, user): bool
    比较credentials中的输入密码和user中保存的密码
  • AFA::onAuthenticationSuccess()
    登陆成后的回调函数
  • AFA::onAuthenticationFailure($request, $exception)
    登陆失败的回调函数。用于比如记录异常的信息到session。
  • AFA::getLoginUrl(): string
    用于但登陆失败,需要把用户重新引到到登陆页面地址

备注: 完成文章后发现Symfony 5.3对Auth做了改动。所以请注意版本。

How to install and uninstall Elasticsearch on Mac

How to install:

brew tap elastic/tap
brew install elastic/tap/elasticsearch-full

From:

https://www.elastic.co/guide/en/elasticsearch/reference/current/brew.html

How to uninstall

#!/usr/bin/env sh

# checks to see if running 
launchctl list | grep elasticsearch
 
launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.elasticsearch.plist
launchctl remove homebrew.mxcl.elasticsearch

pkill -f elasticsearch

rm -f ~/Library/LaunchAgents/homebrew.mxcl.elasticsearch.plist

brew uninstall elasticsearch

# double check existence
ls -al /usr/local/bin/elasticsearch*
ls -al ~/Library/LaunchAgents

From:

https://gist.github.com/jkubacki/e2dd904bd648b0bd4554

PHP的运行模式:Apache 2.0 Handler 到 FPM

长久以来,我们一直使用Apache作为Web服务器,用Apache2.0 Handler 方式来运行PHP, 也就是mod_php, 把PHP中作为Apache的一个module的来运行。当时算是比较快的配置方式。不过这都是10多年前的配置了。

现在常用的搭配是nginx + PHP-FPM. 这种配置在目前docker时代显得比较合适。web server选用nginx 运行在一个容器内. php 作为一个单独的容器运行,通常打开9000端口来和nginx通讯。

在PHP端的dockerfile配置

选择 php:fpm-alpine 的镜像。记得最后要expose 9000端口。

在 Nginx 段的配置

/etc/nginx/cond.d/default.conf

upstream php-upstream {
    server php:9000
}

这样配置,通过phpfinfo查看, Server API就是 FPM/FastCGI 了。

附录 1: php的dockerfile如下

# newest PHP version
FROM php:fpm-alpine

# Add git
RUN apk --update --no-cache add git

# PostgreSQL
RUN set -ex && apk --no-cache add postgresql-dev
RUN docker-php-ext-install pdo_mysql

COPY --from=composer /usr/bin/composer /usr/bin/composer
RUN rm -rf /var/cache/apk/* && rm -rf /tmp/*

WORKDIR /var/www

CMD composer -V; php-fpm

EXPOSE 9000

注意,需要手动的启动 php-fpm

附录2:nginx的Dockerfile:

FROM nginx:alpine

WORKDIR /var/www

ADD nginx.conf /etc/nginx
ADD sites /etc/nginx/sites-available
ADD conf.d /etc/nginx/conf.d

CMD ["nginx"]

EXPOSE 80 443

附录3: Docker-compose.yml

version: '3'

services:

  php:
    build: ./docker/php
    image: base-php:latest
    volumes:
      - ./app:/var/www:cached

  nginx:
    build: ./docker/nginx
    image: base-nginx:latest
    volumes:
      - ./app:/var/www:cached
      - ./logs/nginx:/var/log/nginx:cached
    ports: [80:80, 443:443]

PHP在CGI模式下无法获取http auth的问题

昨天发现一个问题。添加了一个用php发出http auth的脚本,在本地是测试正常,上线后却反复弹出auth窗口,似乎无法通过验证。

分析后发现,本地php是用moudle方式运行的。在live是CGI/FastCGI 。 查阅后得知,由于收到CGI协议的限制,无法使用类似 PHP_AUTH_*这样的超级变量。

解决办法是通过 Apache 的rewrite功能,手动地把http auth 请求加入到环境变量中。

.htaccess 大致如下

    #
    # For CGI/FastCGI , attach Authroziation header in Env Variabels.
    #
    RewriteCond %{HTTP:Authorization} ^(.*)
    RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]

这样,在php内就可以通过$_SERVER[‘HTTP_AUTHORZATION’] 来获取http 的 Authorization的头部信息了。然偶通过字符串和base64解码就能获得用户输入的密码了。