标签: 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://lwbj.cn
    server {
        listen 80;
        server_name lwbj.cn www.lwbj.cn;  # 同时匹配带 www 和不带 www 的域名
    
        # 所有请求都重定向到 https://lwbj.cn
        return 301 https://lwbj.cn$request_uri;
    }
    
    # 处理不带 www 的 HTTPS 请求,重定向到带 www 的域名
    server {
        listen 443 ssl http2;
        server_name lwbj.cn;  # 不带 www 的域名
    
        ssl_certificate /etc/nginx/ssl/www.lwbj.cn.pem;  # 证书路径
        ssl_certificate_key /etc/nginx/ssl/www.lwbj.cn.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 请求到 www.lwbj.cn
        return 301 https://lwbj.cn$request_uri;
    }
    
    # 处理带 www 的 HTTPS 请求
    server {
        listen 443 ssl http2;
        root /var/www/www.lwbj.cn; # 确保这里是正确的根目录路径
        index index.html index.htm index.php index.nginx-debian.html;
    
        server_name  www.lwbj.cn;
        client_max_body_size 20m;
    
        ssl_certificate /etc/nginx/ssl/www.lwbj.cn.pem; # 确保证书路径正确
        ssl_certificate_key /etc/nginx/ssl/www.lwbj.cn.net.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生成随机符点数

    可以使用下面的代码生成随机的符点数:

    <?php
    function randFloat($min = 0, $max = 1)
    {
        return round($min + mt_rand() / mt_getrandmax() * ($max - $min),2);
    }

  • 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');
  • PHP7.4 报错:Trying to access array offset on value of type null

    主要原因是版本7.4以后PHP解析器会对null类型的下标访问直接报错。

    解决办法:把PHP版本换成PHP7.3🤣。

    如果使用Laravel框架,可以像下面这样修改:

    app/Providers/AppServiceProvider.php代码文件中 boot 方法里面添加 error_reporting (E_ERROR);

    这样就屏蔽了所有错误,不是很推荐。有条件还是改回PHP7.3吧。