Windows PHP8.3 安装 imagick 扩展

从PECL中下载imagick扩展,需要注意对应的PHP版本,下载地址如下:

https://pecl.php.net/package/imagick/3.7.0/windows

然后把 php_imagick.dll 放到 PHP8.3 的扩展目录 ext 目录中。

然后把相关运行时文件放到PHP目录下,可以执行下面的命令一键复制过去:

copy CORE_RL_* E:\php\php-8.3.3-Win32-vs16-x64\
copy IM_MOD_* E:\php\php-8.3.3-Win32-vs16-x64\

注意:E:\php\php-8.3.3-Win32-vs16-x64\ 是我电脑上的PHP8.3路径,需要替换为你自己的PHP存放路径。

Nginx PHP 配置模板

server {
    listen 80;
    root /var/www/www.example.com;
    # Add index.php to the list if you are using PHP
    index index.html index.htm index.php index.nginx-debian.html;

    server_name www.example.com;

    client_max_body_size 20m;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;

        # With php-fpm (or other unix sockets):
        fastcgi_pass unix:/run/php/php8.3-fpm.sock;
    }
}

Debian12安装PHP7.4

请注意:PHP7.4已经于2022年11月28日停止维护。

安装sury源:

#!/bin/sh
# To add this repository please do:
if [ "$(whoami)" != "root" ]; then
    SUDO=sudo
fi
${SUDO} apt-get update
${SUDO} apt-get -y install lsb-release ca-certificates curl
${SUDO} curl -sSLo /tmp/debsuryorg-archive-keyring.deb https://packages.sury.org/debsuryorg-archive-keyring.deb
${SUDO} dpkg -i /tmp/debsuryorg-archive-keyring.deb
${SUDO} sh -c 'echo "deb [signed-by=/usr/share/keyrings/deb.sury.org-php.gpg] https://packages.sury.org/php/ $(lsb_release -sc) main" > /etc/apt/sources.list.d/php.list'
${SUDO} apt-get update

安装PHP7.4:

apt install -y php7.4-fpm php7.4-mysql php7.4-mbstring php7.4-curl php7.4-xml php7.4-imagick php7.4-zip php7.4-gd php7.4-intl php7.4-bcmath

Debian+Nginx+PHP HTTPS配置模板

Nginx+PHP+HTTPS的配置模板,如果要部署其它网站,可以基于此进行修改:

# 处理 HTTP 请求,所有请求都重定向到 https://notes.wujie.me
server {
    listen 80;
    server_name notes.wujie.me notes.wujie.me;  # 同时匹配带 www 和不带 www 的域名

    # 所有请求都重定向到 https://notes.wujie.me
    return 301 https://notes.wujie.me$request_uri;
}

# 处理不带 www 的 HTTPS 请求,重定向到带 www 的域名
server {
    listen 443 ssl http2;
    server_name notes.wujie.me;  # 不带 www 的域名

    ssl_certificate /etc/nginx/ssl/notes.wujie.me.pem;  # 证书路径
    ssl_certificate_key /etc/nginx/ssl/notes.wujie.me.key;  # 证书路径

    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_prefer_server_ciphers on;
    ssl_ciphers EECDH+AESGCM:EDH+AESGCM;
    ssl_ecdh_curve secp384r1;
    ssl_session_timeout 10m;
    ssl_session_cache shared:SSL:10m;
    ssl_session_tickets off;
    ssl_stapling on;
    ssl_stapling_verify on;

    # 重定向所有不带 www 的 HTTPS 请求到 notes.wujie.me
    return 301 https://notes.wujie.me$request_uri;
}

# 处理带 www 的 HTTPS 请求
server {
    listen 443 ssl http2;
    root /var/www/notes.wujie.me; # 确保这里是正确的根目录路径
    index index.html index.htm index.php index.nginx-debian.html;

    server_name  notes.wujie.me;
    client_max_body_size 20m;

    ssl_certificate /etc/nginx/ssl/notes.wujie.me.pem; # 确保证书路径正确
    ssl_certificate_key /etc/nginx/ssl/notes.wujie.me.key; # 确保证书路径正确

    #加上TLSv1,HTTPS检测会报PCI DSS不合规
    ssl_protocols  TLSv1.2 TLSv1.3;# Requires nginx >= 1.13.0 else use TLSv1.2
    ssl_prefer_server_ciphers on;
    ssl_ciphers EECDH+AESGCM:EDH+AESGCM;
    ssl_ecdh_curve secp384r1; # Requires nginx >= 1.1.0
    ssl_session_timeout 10m;
    ssl_session_cache shared:SSL:10m;
    ssl_session_tickets off; # Requires nginx >= 1.5.9
    ssl_stapling on; # Requires nginx >= 1.3.7
    ssl_stapling_verify on; # Requires nginx => 1.3.7

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php8.3-fpm.sock;
    }
}

Debian+Nginx+PHP配置模板

Nginx+PHP+HTTP的配置模板,如果要部署其它网站,可以基于此进行修改:

server {
    listen 80;
    root /var/www/www.example.com;
    # Add index.php to the list if you are using PHP
    index index.html index.htm index.php index.nginx-debian.html;

    server_name www.example.com example.com;

    client_max_body_size 20m;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;

        # With php-fpm (or other unix sockets):
        fastcgi_pass unix:/run/php/php8.3-fpm.sock;
    }
}

PHP解决http_build_query中&not被转义的问题

在开发一个支付项目时,遇到PHP把&not给转义了,可以通过htmlspecialchars再转一次就可以了:

<?php
 $vars = array(
             'book' => 'humpty',
             'newspaper' => 'onion',
             'note' => 'not working');
$query = http_build_query($vars);
$fixed_query = htmlspecialchars($query, ENT_QUOTES, 'UTF-8');

该段代码的功能是http_build_query根据数组产生一个urlencode之后的请求字符串。

PHP创建日志文件

在开发,经常需要使用日志文件进行调试和排错,PHP可以使用下面的代码简单的创建一个日志文件:

<?php
file_put_contents('log.txt', date('Y-m-d H:i:s', time()) . " 签名串:" . $this->getSignContent($temp) . "\r\n", FILE_APPEND);

PHP删除指定路径下的指定后缀的文件

有时,我们需要使用PHP对文件进行操作,例如删除目录下指定后缀的文件,代码如下:

<?php
/**
 *@param $path文件夹绝对路径 $file_type待删除文件的后缀名
 *return void
 */
function clearn_file($path, $file_type = 'bak')
{
    //判断要清除的文件类型是否合格
    if (!preg_match('/^[a-zA-Z]{2,}$/', $file_type)) {
        return false;
    }
    //当前路径是否为文件夹或可读的文件
    if (!is_dir($path) || !is_readable($path)) {
        return false;
    }
    //遍历当前目录下所有文件
    $all_files = scandir($path);
    foreach ($all_files as $filename) {
        //跳过当前目录和上一级目录
        if (in_array($filename, array(".", ".."))) {
            continue;
        }
        //进入到$filename文件夹下
        $full_name = $path . '/' . $filename;
        //判断当前路径是否是一个文件夹,是则递归调用函数
        //否则判断文件类型,匹配则删除
        if (is_dir($full_name)) {
            clearn_file($full_name, $file_type);
        } else {
            preg_match("/(.*)\.$file_type/", $filename, $match);
            if (!empty($match[0][0])) {
                echo $full_name;
                echo '<br>';
                unlink($full_name);
            }
        }
    }
}

测试代码:

$folderpath = $_SERVER["DOCUMENT_ROOT"] . "/abc";//要操作的目录
$deltype = array('gif', 'jpg', 'pdf');
foreach ($deltype as $file_type) {
    clearn_file($folderpath, $file_type);
}

我的实际项目中的代码:删除当前文件所在目录下的tmp后缀的文件:

clearn_file(dirname(__FILE__), 'tmp');