标签 php 下的文章

前些天把主机的PHP版本升级到8.0。博客程序typecho也升级到1.2

typecho上一个稳定版本已经是5年前的事情了。在免费项目中,这类更新速度一般会被假定为濒死或死亡项目。附着的插件和模板项目也日渐式微。我使用熊猫的模板和部分插件现在也不更新了。升级typecho后,直接500错误。

期待已死亡项目的作者会给新程序做更新匹配?不太可能。眼下只有自己动手这条路子了。

于是,我断断续续花了点时间把不兼容的模板和插件进行匹配更改,顺道也把有一段时间没有接触的PHP新版本特性给补全一下。

这次更新还让我想起前段时间的一个小事情。

事情的经过是这样的:

前段时间单位有个同事联系我,让我维护一个大约10年前开发的小项目。项目主要是为实现部门一个简单的需求。因为这需求实在太小,我选择用自己最熟悉的PHP方案,并独立完成了开发。这次问题也不大,只是当初没有设想那么长远,字段设置范围超限了,不算大问题。

这同事半新人,科班出身,应该不会在这问题遇到困难。但,这同事表示自己不会PHP,还来了一句(原话):“现在谁还会用PHP开发程序啊?”我思索了一下这同事的话,好像对又好像不对,然后回答说:哦,是啊。

我现在虽然用PHP的情况不多。不过,“PHP是最好的编程语言”(不接受任何辩驳)。


markdown.jpg
Typecho 内置的 MarkDown 引擎是 SegmentFault 开发的 HyperDown 解析器,目前主要是在 Typecho 开发版上的,我一直都很困惑 HyperDown 解析器支持的 MarkDown 语法具体都有啥,今天终于在 GitHub 上看到了,就分享出来,就当是做个笔记了。期间也试过换MonodEditor.md,但是发现效率都一般,所以弃用了。现在还是使用博客自带的MD解释器。虽然功能一般,但是效率优先就不折腾了。

阅读全文


请输入图片描述
前段时间升级了Nginx.看了一下php,版本虽然不是很老,,但是最近看了一下php框架,貌似都是在php7的方向弄了.所以就直接顺便把php也升级了,也是使用源码编译方式实行的.现在大概说一下升级过程,仅供参考.

准备工作

可以参考一下我早期写的php升级过程,大致准备工作也是差不多.建议把常用的编译依赖如openssl,curl等先升级到最新的版本.升级方法自己百度一下.

阅读全文


Movable Type

MT是我使用时间最长的一个博客程序。它基于Perl,功能强大而且可靠性十分稳妥。然而,在快餐文化异常普及的当下,它显得有点疲态百出。它没有php程序的灵活、部署简易,直白点说是折腾。因为站点移到国外的VPS的原因,国内连接速度实在不敢恭维,因此MT也成了我困难丛丛的选择。无奈只能转投效率优先的typecho。

因为博客思路设计的差异,我仅仅转换了原来的文章及相关评论。涉及到的数据表也只有3个。有需要的朋友可以参考下。将下面的源码保存为php文件,放在typecho博客根目录然后运行即可。(建议做好相关数据库备份后,在本地转换成功后再上传)

    <?php
    /**
     * Typecho Blog Platform
     *
     * @copyright  Copyright (c) 2008 Typecho team (http://www.typecho.org)
     * @license    GNU General Public License 2.0
     * @version    $Id: index.php 1153 2017-03-12 10:53:22Z ezo.biz $
     */
    
    /** 载入配置支持 */
    if (!defined('__TYPECHO_ROOT_DIR__') && !@include_once 'config.inc.php') {
        file_exists('./install.php') ? header('Location: install.php') : print('Missing Config File');
        exit;
    }
    
    /** 初始化组件 */
    #Typecho_Widget::widget('Widget_Init');
    $d_MT = "blog";
    /**
     * 初始化mt数据库
     */
    $dbMT = new Typecho_Db('Mysql', 'mt_');
    $dbMT->addServer(array (
       'host' => '127.0.0.1',
       'user' => 'root',
       'password' => '123456',
       'charset' => 'utf8',
       'port' => '3306',
       'database' => $d_MT,
     ), Typecho_Db::READ | Typecho_Db::WRITE);
     Typecho_Db::set($dbMT);
    //清空原有数据
    $db->query("DELETE FROM `typecho_contents`;");
    $db->query("DELETE FROM `typecho_relationships`;");
    $db->query("DELETE FROM `typecho_comments`;");
    
    $a=$dbMT->query("SELECT entry_id,entry_created_on,entry_modified_on,entry_title,Entry_text,entry_text_more,entry_basename FROM mt_entry;");
    $posts = $dbMT->fetchAll($a);
    foreach($posts as $post){
      unset($sql);
      $post['entry_created_on'] = strtotime($post['entry_created_on']);
      $post['entry_modified_on'] = strtotime($post['entry_modified_on']);
      if(!empty($post['entry_text_more']) || ($post['entry_text_more'] !="")){
        $post['Entry_text'] = $post['Entry_text'].$post['entry_text_more'];
      }
      $post['Entry_text'] = preg_replace('/(http|https)(.*?)(ezo\.biz\/b\/).*?(\/)((?:[a-z0-9_-|\.|-]*)(\.)(gif|jpeg|jpg|png))/i', "https://ezo.biz/b_img/$5", $post['Entry_text']);
      $post['Entry_text'] = str_replace("http://ezo.biz","https://ezo.biz",$post['Entry_text']);
      $post['Entry_text'] = str_replace("http://www.ezo.biz","https://www.ezo.biz",$post['Entry_text']);
      $post['Entry_text'] = mysql_real_escape_string($post['Entry_text']);
      $post['entry_title'] = mysql_real_escape_string($post['entry_title']);
    
    
      /**
       * 修复typecho slug主键唯一的问题
       */
      $d = gmdate('Ymd',$post['entry_created_on']);
      $post['entry_basename'] = $post['entry_basename']."-$d";
    
    
      $sql = "INSERT INTO `typecho_contents` (`cid`, `title`, `slug`, `created`, `modified`, `text`, `order`, `authorId`, `template`, `type`, `status`, `password`, `commentsNum`, `allowComment`, `allowPing`, `allowFeed`, `parent`, `views`, `viewsNum`, `likesNum`)  VALUES (NULL, '$post[entry_title]', '$post[entry_basename]', '$post[entry_created_on]', '$post[entry_modified_on]', '$post[Entry_text]', '0', '1', NULL, 'post', 'publish', NULL, '0', '1', '1', '1', '0', '0', '0', '0');";
      //返回文章id
      $cid = $db->query($sql,Typecho_Db::WRITE,Typecho_Db::INSERT);
      //绑定目录
      $db->query("INSERT INTO `typecho_relationships` (`cid`, `mid`) VALUES ('$cid', '1');");
    
      /**
       * 转换评论
       */
      $b = $dbMT->query("SELECT comment_author,comment_created_on,comment_email,comment_entry_id,comment_ip,comment_text,comment_url FROM mt_comment WHERE comment_visible=1 AND comment_entry_id=$post[entry_id];");
      $comments = $dbMT->fetchALL($b);
      foreach ($comments as $comment) {
        # 批量导入评论
        $comment['comment_created_on'] = strtotime($comment['comment_created_on']);
        $db->query("INSERT INTO `typecho_comments` (`coid`, `cid`, `created`, `author`, `authorId`, `ownerId`, `mail`, `url`, `ip`, `agent`, `text`, `type`, `status`, `parent`) VALUES (NULL, '$cid', '$comment[comment_created_on]', '$comment[comment_author]', '0', '0', '$comment[comment_email]', '$comment[comment_url]', '$comment[comment_ip]', NULL, '$comment[comment_text]', 'comment', 'approved', '0');");
      }
    }

php.jpg

因为typecho的测试版appstore需要使用curl访问https,这个DO主机是15年部署的,当时编译php时候没有考虑为curl增加对https的支持。因此打算更新php顺便将curl也更新了。网上教程很多,随便搜索一下都有很多。我这里就将过程中遇到的几个梗吐槽一下,希望也遇到相似情况的朋友可以少走点弯路。

这个主机是基于Debian 7,web架构是nginx + php (相关操作仅供指引请尽量备份好相关数据),本文是通过编译方式升级php。

准备工作
通过命令行查看PHP编译信息

php -i | grep configure

得到我的主机的php编译信息是这样的

./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-openssl=/usr --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --enable-xml --enable-discard-path --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --enable-fastcgi --enable-fpm --enable-force-cgi-redirect --enable-mbstring --with-mcrypt --with-gd --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --disable-ipv6 --without-pear --with-iconv=/usr/local/lib --disable-debug --enable-opcache

下载好新版的php并解压

wget http://php.net/distributions/php-5.6.30.tar.gz
tar -zxvf php-5.6.30.tar.gz
cd php-5.6.30

备份好原来的php防止编译失败可以直接回滚

mv /usr/local/php  /usr/local/php5.3.8  

进行./configure时遇到了第一个梗
如果你不想看编译curl的过程,可以直接跳到梗2看解决办法。
configure就直接报错,提示类似下面的

checking for CURL support... yes
checking for CURL in default path... found in /usr
checking for cURL 7.9.8 or greater... libcurl 7.21.6
checking for curl_easy_perform in -lcurl... no
configure: error: There is something wrong. Please check config.log for more information.

搜索了下说curl版本不对,遂直接下载最新的curl,通过编译(./configure;make && make install)方式升级了curl。重新在php编译目录执行configure,结果依旧上面的报错。

通过命令行curl -V查看curl版本如下

localhost:~ user$ curl -V
curl 7.52.1 (i686-pc-linux-gnu) libcurl/7.21.6

晕死,怎么curl和libucurl的版本不一致的?爬文了半天没有结果。重装了curl几遍还没有结果。后来看了下curl的文档,发现是因为共享库造成的。晕死,curl编译的配置应该是用./configure -disable-shared才对。这样编译出来的curl和libcurl版本才是一致的。通过这样编译后curl -V得到的信息是这样的:

localhost:~ user$ curl -V
curl 7.52.1 (i686-pc-linux-gnu) libcurl/7.52.1

梗2
重新在php编译目录执行configure,结果依旧上面的报错()。上面编译curl的努力不是白费了吗?晕死。。。继续爬文,最后在stackoverflow找到解决问题的方法:重新安装curl-devel或者需要指定curl的安装路径(这个仅适合编译方式安装的curl)

Ubuntu:

sudo apt-get install libcurl4-gnutls-dev

CentOS:

sudo yum install curl-devel

编译方式安装了curl的(我的那种情况):
在php的configure里面加入--with-curl=/usr/local. (/usr/local为你curl安装的路径).

安装完之后,可以到php目录顺利configure了。

准备make,梗三出现了
提示iconv函数不支持。晕死,我之前的php也是编译方式安装的,这个参数也是那样编译出来的。爬文。。。找到两个处理方法,直接在configure参数里面删除iconv,这样就无法使用iconv。

因此我选择了另外一个方法。通过打开php安装目录的Makefile,搜索到EXTRA_LIBS =

-lcrypt -lz -lcrypt -lrt -lmysqlclient -lmcrypt -lltdl -lpng -lz -ljpeg -lcurl -lz -lrt -lm -ldl -lnsl -lrt -lxml2 -lssl -lcrypto -lcurl -lssh2 -lssl -lcrypto -lldap -lz -lrt -lxml2 -lfreetype -lz -lpng16 -lmysqlclient -lm -lrt -ldl -lxml2 -lcrypt -lxml2 -lxml2 -lxml2 -lxml2 -lcrypt

的后面增加-liconv

然后重新make,然后make install,ok了。

弄完后,可以将原来在/usr/local/php/etc/备份的php.ini和php-fpm.ini恢复到当前位置。重启php-fpm即可。
查看curl版本,最新的!
curl.png

-EOF-