Code server 配置 php

安装两个插件,PHP IntelliSense 和 php cs fixer

PHP IntelliSense 安装好后,找到coder server的插件目录,我的是 /var/lib/code-server ,进入 extensions/felixfbecker.php-intellisense-2.3.14 目录下,执行composer update,才算安装完成,直接使用会报错

如果composer update时报错,则是有php拓展没有启用,我这里用国内镜像也会报错,不要用国内镜像即可

然后到coder server里配置一下

我的 setting.json 文件是这样的,配置一下php路径和php-cs-fixer路径就好了,php-cs-fixer可以在github 下载

{
    "editor.fontSize": 20,
    "workbench.colorTheme": "Default Light+",
    "php.executablePath": "/usr/bin/php7.4",
    "php-cs-fixer.autoFixBySemicolon": true,
    "php-cs-fixer.executablePath": "/root/php-cs-fixer.phar",
    "php-cs-fixer.lastDownload": 1633922462143,
    "files.autoSave": "off",
    "php-cs-fixer.formatHtml": true
}

然后在php文件里,右键就有格式化php代码的选项了

Ubuntu 安装 composer

安装时报错

The json extension is missing.
Install it or recompile php without --disable-json

The phar extension is missing.
Install it or recompile php without --disable-phar

The iconv OR mbstring extension is required and both are missing.
Install either of them or recompile php without --disable-iconv

提示缺少json等拓展

但我也安装了这个拓展,可是输入 php7.4 -m 却没有加载这个模块,php-fpm7.4 -m 却有加载所有模块

原来是cli没有加载我安装的模块,ubuntu所有安装的php 模块可以在 /etc/php/7.4/mods-available 目录下找到,而fpm的conf.d目录中链接了所有的模块,cli却没有,什么原因呢我也不知道

简单粗暴的方法是直接全部复制到cli目录下

cp -r /etc/php/7.4/fpm/conf.d/* /etc/php/7.4/cli/conf.d

这样cli就启用了所有模块了

还有就是可以用 phpenmod 命令去挨个启用模块

phpenmod json
phpenmod iconv
phpenmod phar

然后安装 composer 就正常了

curl -sS https://getcomposer.org/installer -o composer-setup.php
php composer-setup.php --install-dir=/usr/local/bin --filename=composer

在nginx配置文件中修改php最大上传值

fastcgi_param  PHP_VALUE "upload_max_filesize=100M \n post_max_size=100M";

添加在 location ~ .php$ 块中

location ~ \.php$ {
        #fastcgi_pass unix:/var/run/php/php8.0-fpm.sock;
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param  PHP_VALUE "upload_max_filesize=100M \n post_max_size=100M";
        include        fastcgi_params;
}

Ubuntu 20.04 安装 PHP8

sudo apt install software-properties-common
sudo add-apt-repository ppa:ondrej/php

sudo apt update
sudo apt install php8.0-fpm

更改nginx配置文件,把7.4换成8.0

#fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_pass unix:/var/run/php/php8.0-fpm.sock;

PHP判断是否含中文和全角字符

if(preg_match('/[\x{4e00}-\x{9fa5}]/u', $filename)>0 || preg_match('/[\x81-\xfe]/u', $filename)>0 ){
    // 含有中文或全角
  }else{
    // 不含
}

Ubuntu 20.04 apache开启http2

apt update && apt upgrade
apt install apache2 php7.4-fpm 
a2enmod proxy_fcgi setenvif
a2enconf php7.4-fpm 
a2dismod php7.4 
systemctl restart apache2
a2dismod mpm_prefork 
a2enmod mpm_event 
systemctl restart apache2
systemctl restart php7.4-fpm
a2enmod http2
systemctl restart apache2

Arch Linux服务器配置php-fpm并开启http/2

我之前是用libphp的方式安装的php,就是安装 php7-apache 这个包,但是这种方法不支持http/2,所以我换成了 php-fpm

具体的配置方法看arch wiki就可以了

Apache HTTP Server – ArchWiki (archlinux.org)

但我按wiki的方法配置好后,打开什么页面都是503

原因是我使用的是php7,安装的包也是 php7-fpm,arch linux最新的版本是php8了,所以配置默认是php8的,需要修改一下 SetHandler 

DirectoryIndex index.php index.html
<FilesMatch \.php$>
    SetHandler "proxy:unix:/run/php-fpm7/php-fpm.sock|fcgi://localhost/"
</FilesMatch>

然后重启一下 httpd 服务就行了

开启http/2也很简单 Apache HTTP Server – ArchWiki (archlinux.org)

取消注释

LoadModule http2_module modules/mod_http2.so

加入以下行就行了

Protocols h2 http/1.1

可以在以下网址测试网站是否开启http/2

解决 WP 图像后期处理失败,可能是服务器忙或没有足够的资源。请尝试上传较小的文件。推荐的最大尺寸为2500像素。

我的服务器是NGINX,所以先查看NGINX的错误日志

tail /var/log/nginx/error.log -n 10

上传一次大图像,然后看日志最后一条的信息是

2021/04/23 10:17:11 [error] 212420#212420: *166782 client intended to send too large body: 6849556 bytes, client: 120.85.112.147, server: zuoridangnian.com, request: “POST /wp-admin/async-upload.php HTTP/1.1”, host: “zuoridangnian.com”, referrer: “https://zuoridangnian.com/wp-admin/upload.php”

错误是:

client intended to send too large body

解决办法是:

在NGINX配置文件中添加

client_max_body_size 80m;

添加在 server 内对单独网站生效,添加在 http 块中对所有网站生效

如果你的服务器错误日志中是其它错误,就查其它的解决办法

Chevereto Nginx重写规则

nginx服务器需要先设置好重写规则,才能安装,否则下载好安装包,安装路径会显示404

重写规则:

# Context limits
client_max_body_size 20M;

# Disable access to sensitive files
location ~* (app|content|lib)/.*\.(po|php|lock|sql)$ {
    deny all;
}

# Image not found replacement
location ~ \.(jpe?g|png|gif|webp)$ {
    log_not_found off;
    error_page 404 /content/images/system/default/404.gif;
}

# CORS header (avoids font rendering issues)
location ~ \.(ttf|ttc|otf|eot|woff|woff2|font.css|css|js)$ {
    add_header Access-Control-Allow-Origin "*";
}

# Pretty URLs
location / {
    index index.php;
    try_files $uri $uri/ /index.php$is_args$query_string;
}

https://v3-docs.chevereto.com/setup/requirements.html#nginx-url-rewriting

Chevereto程序下载:https://download.fastgit.org/Chevereto/Chevereto-Free/archive/1.3.0.zip

WordPress主题制作之相关文章代码

查找相同分类,随机输出文章

<?php
$categories = get_the_category($post->ID);
if ($categories) {
    $category_ids = array();

    foreach ($categories as $individual_category) {
        $category_ids[] = $individual_category->term_id;
    }

    $args = array(
        'category__in' => $category_ids,
        'post__not_in' => array($post->ID),
        'ignore_sticky_posts' => 1,
        'showposts' => 6,
        'orderby' => 'rand',
    );

    $my_query = new wp_query($args);
    if ($my_query->have_posts()) {
        while ($my_query->have_posts()) {
            $my_query->the_post();
            ?>
            <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
            <?php
        }

    }
    wp_reset_postdata();
}
?>

没有更多文章了