标签: php

  • Docker php:7.2.34-fpm-alpine3.12 安装zip扩展

    基于 php:7.2.34-fpm-alpine3.12 创建docker安装 zip 扩展

    apk add --no-cache libzip-dev && docker-php-ext-configure zip --with-libzip=/usr/include && docker-php-ext-install zip

    参考:

    https://github.com/docker-library/php/issues/797

  • 获取已知年月第一天和最后一天的日期【PHP/Carbon】

    获取已知年月的第一天和最后一天的日期

    get the first day date and last day date of the month

    第一天是 1 不变,最后一天可能是 28,29,30,31

    PHP编程实现:

    1、使用 Composer 安装 Carbon

    composer require nesbot/carbon

    2、编写代码:

    <?php
    
    require 'vendor/autoload.php';
    
    use Carbon\Carbon;
    
    $year = 2023;
    $month = 2;
    
    // Get the first day of the month.
    $firstDayOfMonth = Carbon::create($year, $month, 1)->startOfMonth();
    
    echo 'First day of the month: ' . $firstDayOfMonth->format('Y-m-d') . PHP_EOL;
    
    // Get the last day of the month.
    $lastDayOfMonth = Carbon::create($year, $month, 1)->endOfMonth();
    
    // Print the first day and last day of the month.
    echo 'Last day of the month: ' . $lastDayOfMonth->format('Y-m-d') . PHP_EOL;
    
    ?>
    

    代码由 Google Bard 生成,

    提示词 (prompt words) :

    known year and month,get the first day date and last day date of the month in PHP use carbon package

    3、输出结果

    First day of the month: 2023-02-01
    Last day of the month: 2023-02-28
  • 在Docker PHP中安装Redis扩展

    本文描述如何在一个基于Docker 镜像 php:8.2.5-fpm-bullseye 创建的容器中,安装 redis 扩展。

    检测 Redis 扩展是否安装:

    docker exec 245962d9fe58 php -m | grep -i redis

    245962d9fe58 为容器实例 ID

    安装 Redis 扩展步骤:

    进入 Docker 容器Shell:

    docker exec -it 245962d9fe58 /bin/bash

    安装扩展命令:

    pecl install redis 

    激活扩展:

    docker-php-ext-enable redis

    重启 php-fpm :

    kill -USR2 1

    检查扩展是否安装成功:

    php -m | grep -i redis

    如果出现 redis 即安装成功。

    参考:

    https://stackoverflow.com/questions/31369867/how-to-install-php-redis-extension-using-the-official-php-docker-image-approach

  • Laravel 调试打印 SQL

    如果在.env中设置了 APP_DEBUG=true

    则在 storage/logs/目录下的 文件中laravel.log 记录所有sql查询。

     

    也可以中断代码执行打印来调试:

    <?php
    $bindings = $query->getBindings();
    $sql = str_replace('?', '%s', $query->toSql());
    $sql = sprintf($sql, ...$bindings);
    dd($sql);
    
  • php 扩展 xml

    如果php未安装扩展xml,可能会出现如下错误。

    Composer detected issues in your platform: Your Composer dependencies require the following PHP extensions to be installed: xml

    解决办法

    apt install php7.4-xml

  • ubuntu 20.04 安装 PHP/Nginx/MySQL/Redis

    视频版:

    https://www.bilibili.com/video/BV1PK4y1n74R/

    基于Ubuntu 20.04 LTS 操作系统
    部署安装PHP
    Nginx web 服务器
    MySQL 数据库
    Redis 内存服务器

    由于服务器位于远端的机房
    所以需要远程登录服务器

    如果是Linux系统
    使用自带的terminal就可以登陆
    MacOS也有自带终端
    也可以使用 iTerm2
    Windows 可以使用的软件有很多
    例如 Windows Terminal
    共享软件 SecureCRT
    开源软件PuTTY

     

    登录系统后

    查看主机的操作系统类型
    cat /etc/os-release

    如果服务器位于国内
    建议将默认的源镜像修改为国内的地址
    https://developer.aliyun.com/mirror/ubuntu
    这样下载软件包速度会比较快

    安装 php
    apt install php

    安装 PHP进程管理器 php-fpm
    apt install php7.4-fpm

    卸载 apache2
    systemctl stop apache2.service
    apt purge apache2

    安装 nginx
    apt install nginx

    安装 MySQL
    apt install mysql-server

    安装Redis
    apt install redis-server

    安装多字节字符串扩展
    apt install php7.4-mbstring

    安装 curl 扩展
    apt install php7.4-curl

    安装 MySQL 扩展
    apt install php7.4-mysql

    安装 Redis 扩展
    apt install php7.4-redis

    安装图形库扩展
    apt install php7.4-gd

    php-imagick

    到此网站运行的基础软件都安装好了
    软件的配置也都使用了默认的配置
    但这样的配置仅用于开发测试
    用于生产环境的话要配置的细节就比较多

    修改mysql root 密码
    首先输入mysql登录mysql
    然后输入
    ALTER USER ‘root’@’localhost’ IDENTIFIED WITH mysql_native_password BY ‘abcd12345’;
    将密码修改为 abcd12345
    使用 flush privileges
    刷新权限
    至此开发环境搭建完毕

  • phpMyAdmin管理MySQL用户

    通过命令行管理MySQL用户不是很方便,

    尤其是变更用户密码和授权的操作,

    需要输入较为复杂的命令,

    如果在机器上部署了phpMyAdmin的话,

    就可以很方便快捷地通过图形界面来操作。

    例如新增用户,删除用户,修改用户密码,数据库授权等。

    参考视频:

    https://www.bilibili.com/video/bv1CK4y177Zr

     

  • phpMyAdmin连接MySQL配置SSL证书

    视频演示:https://www.bilibili.com/video/BV1oy4y1b7yR/

    phpMyAdmin连接 localhost上的MySQL Server是通过unix socket通信的。

    phpMyAdmin连接远程MySQL服务器,如果期望加密通信数据,此时通过配置SSL来实现。

    以Ubuntu为例,MySQL 安装后默认是开启SSL的,在数据目录/var/lib/mysql下通常会有以下文件

    ca.pem

    client-cert.pem

    client-key.pem

    只需要把这3个文件复制到phpMyAdmin所在的服务器上,然后修改phpMyAdmin SSL配置即可。

    切到 phpMyAdmin 部署的根目录,复制文件libraries/config.default.php里面有关ssl的选项到根目录下的config.inc.php中。

    也就是说,在根目录文件config.inc.php中添加下面的配置

    如果根目录没有config.inc.php,应该复制config.sample.inc.php为config.inc.php

    /**
    * Use SSL for connecting to MySQL server?
    *
    * @global boolean $cfg['Servers'][$i]['ssl']
    */
    $cfg['Servers'][$i]['ssl'] = true;
    
    /**
    * Path to the key file when using SSL for connecting to the MySQL server
    *
    * @global string $cfg['Servers'][$i]['ssl_key']
    */
    $cfg['Servers'][$i]['ssl_key'] = 'path/to/client-key.pem';
    
    /**
    * Path to the cert file when using SSL for connecting to the MySQL server
    *
    * @global string $cfg['Servers'][$i]['ssl_cert']
    */
    $cfg['Servers'][$i]['ssl_cert'] = 'path/to/client-cert.pem';
    
    /**
    * Path to the CA file when using SSL for connecting to the MySQL server
    *
    * @global string $cfg['Servers'][$i]['ssl_ca']
    */
    $cfg['Servers'][$i]['ssl_ca'] = 'path/to/ca.pem';
    
    /**
    * Directory containing trusted SSL CA certificates in PEM format
    *
    * @global string $cfg['Servers'][$i]['ssl_ca_path']
    */
    $cfg['Servers'][$i]['ssl_ca_path'] = 'path/to/';
    
    /**
    * List of allowable ciphers for SSL connections to the MySQL server
    *
    * @global string $cfg['Servers'][$i]['ssl_ciphers']
    */
    $cfg['Servers'][$i]['ssl_ciphers'] = null;
    
    /**
    * MySQL 5.6 or later triggers the mysqlnd driver in PHP to validate the
    * peer_name of the SSL certifcate
    * For most self-signed certificates this is a problem. Setting this to false
    * will disable the check and allow the connection (PHP 5.6.16 or later)
    *
    * @link https://bugs.php.net/68344
    * @global string $cfg['Servers'][$i]['ssl_verify']
    */
    $cfg['Servers'][$i]['ssl_verify'] = true;

    配置中的路径为上述三个证书相关文件的实际路径。

    此时,需要确认MySQL是否允许远程连接,

    MySQL配置文件路径 /etc/mysql/mysql.conf.d/mysqld.cnf

    bind-address = 127.0.0.1

    表示只允许本地连接,应该在行首加#注释此行,

    再次,确认MySQL服务器是否允许入站端口3306 ,

    此时应该就可以安全的连接MySQL了。

     

  • WordPress在线客服插件tawk介绍

    基于WordPress开发的电商类型的网站,通常有客户希望询盘,经典的联络表单方式,沟通答复间隔时间比较长,很容易造成客户流失,而通过tawk提供的服务,我们可以实时响应访客的询问,从而更快捷的达成交易。

    tawk支持多种常见应用平台,像Windows/Mac OS X/Android/iPhone都可以安装使用,这里仅介绍团队针对WordPress开发的插件。在使用之前,需要去官方网站https://www.tawk.to/注册账号,注册之后,填写自己网站的名称URL,可以得到Site IDAPI Key,然后在WordPress后台的设置/Tawk.to/Account Setting里面登陆并选择对应的property即可。

    tawk 的后台功能有仪表盘【dashboard】/监控【Monitoring】/会话【Message】/设置【Setting】等模块,可以满足日常的基础需求。

    为了实时响应用户咨询消息,推荐在自己常用的设备上安装对应版本的tawk。

    官方使用指导文档请访问下面的链接:

    https://www.tawk.to/knowledgebase/plugins-and-modules/add-a-tawk-to-widget-to-wordpress/

  • 使用插件advanced custom fields来扩展WordPress文章字段

    WordPress文章的默认属性,例如分类、标签、题图,摘要,状态等,可以满足基本的使用需求,

    但是对于特殊类型的业务,需要扩展文章属性的时候,就需要借助插件来拓展,虽然有自带的自定义栏目,但是功能比较基础。

    插件advanced custom fields正是为满足这种需求而生,在WordPress插件页面显示此插件的安装量超过一百万,可见扩展属性的需求是比较普遍的。

    借助此款插件,可以正对文章(post)、页面(page)、用户(user)、媒体(media)等模块添加扩充字段。

    需求示例:

    1)文章都是关于乐队的资料,你想添加主唱、吉他、贝斯这几个字段作为文章的独立属性;

    2)用户资料想要记录用户的年龄和电话号码;