Category: 小小草

IT 技术领域学海无涯。其实任何领域都学海无涯,无非 IT 发展太快了,让我有更多嘘唏。希望我掌握的技术有如小小草,虽然渺小,却有旺盛的生命力。

  • 一语惊醒梦中人

    由于某种原因,我删掉了 /usr/bin/php, /etc/php.ini 等文件,我指望 yum reinstall php 能把这些文件重新装回去。但是 yum install / remove / reinstall 交替使用了 n 次,还是没见这些文件。

    于是我又在想,可能 Fedora 把我以前的安装信息保存在某个地方,毕竟直接去删 /usr/bin/php, /etc/php.ini 等文件不是卸载 package 的正当做法,所以再次用 yum 安装时,它不会把这些文件送回来。怎么让 Fedora 认为我是要象第一次安装 php 那样把这些文件都给装上去呢?我想了好久不得要领,只好去 Fedora forum 提问,由于我的提问也没切中要害,一开始得到的回答也不得要领。

    直到突然一个好心人告诉我,/usr/bin/php 是在 php-cli package 里,/etc/php.ini 是在 php-common package 里。一语惊醒梦中人——怎么我总以为这些文件是由一个 php 全包了呢?

  • Session save path should be writable

    由于我系统里的 php session save path 是由当初 apache 创建的,改用 nginx 后就变成不可写了。但是,症状并不直观,php 照样能运行,phpMyAdmin 也能 login,还能查看各库各表的数据,但就是不能更新、删除,也无法 logout。无法 logout phpMyAdmin 的具体症状也不是其他人描述的那样(logout 时提示输入用户名和密码),就是点了没效果,仍在管理界面(还有人说启用 http basic auth login 的话就是无法 logout 的,只能关闭浏览器才 logout,这纯属无稽之谈)。

    我一直认为是 phpMyAdmin 或 mysql 设置出问题的,根本没往 php session save path 是否可写这方面想,我想当然地以为如果 php session save path 不可写,在 php 里启用 session 就会出错,别想进入 phpMyAdmin。

  • Reboot is still the best remedy in Linux

    Long time ago, I read an article about moving from Windows to Linux, the top differences a system administrator should keep in mind. I still remember one of the difference is reboot in Linux does not cure the problem as it does for Windows in 99% occasions.

    However, today I tried to fix a problem after I manually compiled php 5.2.9 (phpMyAdmin stop working, server response 500 error). I lack of knowledge of configure, make install, so I decided to remove php 5.2.9 and reverse to php 5.2.6 came with Fedora. I like yum install. Yum make my life so easy and it can also install packages perfect for me.

    php 5.2.9 executables was installed to /usr/local/bin (as php’s default), but php 5.2.6 was installed to /usr/bin (as fedora yum default). After I yum reinstalled php 5.2.6, I found

    If I typed “php-cgi -v”, Fedora could find php-cgi in /usr/bin
    If I typed “php -v”, Fedora still looked for php in /usr/local/bin

    I did not know why Fedora behave so strangely. After hours trying to correct Fedora mis-behavior without success, I reboot Fedora hopelessly. Then what? Fedora now is willing to load php executable in /usr/bin.

  • Install php-fpm on Fedora

    Use fedora yum install to install php is very easy. But, there is no yum install available to install php-fpm. I have to download php source and compile it from source.

    I do not know why Fedora configures its php this way, but it works fine for me. So I want to configure my php source the same way as Fedora did. I only add –enable-fpm to the configure.

    ./configure --build=i386-redhat-linux-gnu --host=i386-redhat-linux-gnu --target=i386-redhat-linux-gnu --program-prefix= --prefix=/usr --exec-prefix=/usr --bindir=/usr/bin --sbindir=/usr/sbin --sysconfdir=/etc --datadir=/usr/share --includedir=/usr/include --libdir=/usr/lib --libexecdir=/usr/libexec --localstatedir=/var --sharedstatedir=/usr/com --mandir=/usr/share/man --infodir=/usr/share/info --cache-file=../config.cache --with-libdir=lib --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --disable-debug --with-pic --disable-rpath --without-pear --with-bz2 --with-curl --with-exec-dir=/usr/bin --with-freetype-dir=/usr --with-png-dir=/usr --with-xpm-dir=/usr --enable-gd-native-ttf --with-t1lib=/usr --without-gdbm --with-gettext --with-gmp --with-iconv --with-jpeg-dir=/usr --with-openssl --with-png --with-expat-dir=/usr --with-pcre-regex=/usr --with-zlib --with-layout=GNU --enable-exif --enable-ftp --enable-magic-quotes --enable-sockets --enable-sysvsem --enable-sysvshm --enable-sysvmsg --enable-track-vars --enable-trans-sid --enable-yp --enable-wddx --with-kerberos --enable-ucd-snmp-hack --with-unixODBC=shared,/usr --enable-memory-limit --enable-shmop --enable-calendar --enable-dbx --enable-dio --without-mime-magic --without-sqlite --with-libxml-dir=/usr --with-xml --with-system-tzdata --enable-force-cgi-redirect --enable-pcntl --with-imap=shared --with-imap-ssl --enable-mbstring=shared --enable-mbstr-enc-trans --enable-mbregex --with-ncurses=shared --with-gd=shared --enable-bcmath=shared --enable-dba=shared --with-db4=/usr --with-xmlrpc=shared --with-ldap=shared --with-ldap-sasl --with-mysql=shared,/usr --with-mysqli=shared,/usr/bin/mysql_config --enable-dom=shared --with-dom-xslt=/usr --with-dom-exslt=/usr --with-pgsql=shared --with-snmp=shared,/usr --enable-soap=shared --with-xsl=shared,/usr --enable-xmlreader=shared --enable-xmlwriter=shared --enable-fastcgi --enable-fpm --enable-pdo=shared --with-pdo-odbc=shared,unixODBC,/usr --with-pdo-mysql=shared,/usr --with-pdo-pgsql=shared,/usr --with-pdo-sqlite=shared,/usr --enable-json=shared --enable-zip=shared --with-readline --enable-dbase=shared --with-pspell=shared --with-mcrypt=shared,/usr --with-mhash=shared,/usr --with-tidy=shared,/usr --with-mssql=shared,/usr

    I had several errors when run ./configure

    checking for BZip2 in default path… not found
    configure: error: Please reinstall the BZip2 distribution
    solution: yum install bzip-devel bzip-libs

    configure: error: DBA: Could not find necessary header file(s).
    solution: yum install gdbm-devel
    Well, someone suggests that, I did that, but error still exists. Then under my own decision, I run
    yum install php-dba
    Error still exists.
    yum install qdbm*
    Error still exists.
    yum install db4*
    Pass!

    configure: error: libjpeg.(a|so) not found.
    solution: yum install libjpeg libjpeg-devel

    Configure: error: libpng.(also) not found.
    solution: yum install libpng libpng-devel

    configure: error: libXpm.(a|so) not found.
    solution: yum install libXpm*

    Configure: error: freetype.h not found.
    solution: yum install freetype-devel

    configure: error: Unable to locate gmp.h
    solution: yum install mysql-devel

    configure: error: utf8_mime2text() has new signature, but U8T_CANONICAL is missing. This should not happen. Check config.log for additional information.
    solution: yum install libc-client-devel

    configure: error: Cannot find ldap.h
    solution: yum install openldap-devel

    configure: error: mcrypt.h not found. Please reinstall libmcrypt.
    solution: yum install libmcrypt-devel

    configure: error: Please reinstall libmhash – I cannot find mhash.h
    solution: yum install mhash-devel

    configure: error: Directory /usr is not a FreeTDS installation directory
    solution: yum install freetds-devel

    Configure: error: Please reinstall the ncurses distribution
    solution: yum install ncurses-devel

    checking for unixODBC support… configure: error: ODBC header file ‘/usr/include/sqlext.h’ not found!
    solution: yum install unixODBC-devel

    configure: error: Cannot find libpq-fe.h. Please specify correct PostgreSQL installation path
    solution: yum install postgresql-devel

    configure: error: Please reinstall the sqlite3 distribution
    solution: yun install sqlite-devel

    Configure: error: Cannot find pspell
    solution: yum install pspell-devel

    configure: error: Please reinstall readline – I cannot find readline.h
    solution: yum install readline-devel

    configure: error: SNMP sanity check failed. Please check config.log for more information.
    solution: yum install net-snmp-devel

    configure: error: Cannot find libtidy
    solution: yum install libtidy-devel

    configure: error: xslt-config not found. Please reinstall the libxslt >= 1.1.0 distribution
    solution: yum install libxslt-devel

  • Create a password file for Nginx basic authentication

    It is really a hard time for me to find a way to create a password file for Nginx basic authentication, because I did not realise htpasswd crpty is not available on Windows.

    Nginx documentation only mentions Use crypt(3) encryption for passwords, so I tried so hard with htpasswd.exe but could not generate a file recognised by Nginx. Hours later I found Apache documentation mentions -d is

    the default on all platforms but Windows, Netware and TPF. Though possibly supported by htpasswd on all platforms, it is not supported by the httpd server on Windows, Netware and TPF.

    I have two things to blame –

    1. I still use Windows to do my everyday work. If my first choice was htpasswd on Linux even without furthur instructions, it would have save me a lot of time.
    2. Nginx documentionation. If Nginx documentation is as good as Apache’s, Nginx may overwhelm the world.
  • Magento Skip Base URL validation before next step

    终于明白 Magento 在安装时问的一个问题是什么意思了:

    Skip Base URL validation before next step

    Check this box only if it is not possible to automatically validate Base URL. 

    如果给 Magento 指定一个 Base Url 在 internet 上不可解析,比如在开发阶段使用一个 Base Url 只在本机或内网内访问,则必须跳过 Base Url validation。

  • Perfect settings for Magento on Nginx

    Nginx 的文档不够详细,挺折腾人的。我经过两天181次黑匣试验,终于敢说入了 Nginx 的门。摸清 Nginx 的思路以后,才体会到它的先进性(不仅是效率上的)。php 和 nginx 搭配使用,感觉就不那么草根了,更容易借鉴 python 的 web infrastructure.

    从 Apache 到 Ngnix,不容易啊。难就难在要抛弃 Apache 的思维,其实同样的配置在 Nginx 总能做到的,而且更简洁。在Apache 下,我用 symbolic link 实现 Magento 和 WordPress 等一次安装多处使用,当时我认为已经是很简单的解决方案了;转移到 Nginx 平台,多处使用连 symblic link 也省了。

    以下是 Magento on Nginx 的配置,perfect,可以以不变应万变,目前我挺得意的。别笑我,这么几行配置,折腾我两天。

    location / {
    
    root  $php_doc_root;
    
    index  index.php;
    
    if ($uri ~ ^/(media|js|skin)/) {
    
    break;
    
    }
    
    if (!-e $request_filename) {
    
    rewrite .* /index.php last;
    
    }
    
    }
    
    location ~ ^/(app|lib|var)/ {
    
    deny all;
    
    }
    
    location ~ ^/report/.*\.xml {
    
    deny all;
    
    }
    
    location ~* ^.+\.(jpg|jpeg|gif|css|png|js|ico)$ {
    
    root  $php_doc_root;
    
    index  index.php;
    
    access_log        off;
    
    expires           30d;
    
    }
    
  • Bkf is not found is not true

    When I tried to restore from bkf file created by Windows in-built System Tool Backup and Restore, I was not aware another colleague was accessing (read access) this file. At the last page of Restore wizard, it threw an error with a nonsense error message – “(the file name).bkf Disk 1 is not found”. The error message scared me because this bkf file is my only backup file I have. I do not have any other file like Disk 1, Disk 2. If this file is corrupted, nothing else I can fallback.

    I was released when I retried after my colleague finishing reading the file. Everything successfully restored when I had exclusive access to the bkf file.

    我早该想到 Windows 的错误提示往往不是它字面的意思。

    早就想用 Linux 的备份工具,但是很惭愧,还没熟练掌握,所以重要的备份还是在 Windows 下完成。

  • My understanding to Nginx so far

    Location directive 没有fallback 机制。因为一个 request 只能匹配一个 location,不是常规 location 的话,就是正则表达式 location。假如有两个 location blocks,
    location / {
    index index.php;
    }

    location /abc {
    client_max_body_size 32m;
    }

    请别指望 index index.php  会在location /abc 生效。因为这是两个 location,只能有一个生效,另一个被忽略。

    正则表达式 location 优先于常规 location 匹配(除非常规 location 使用 ^~ 或 =);常规 location 之间最长匹配优先;正则表达式 location 之间位置在前优先。

    Nginx wiki 文档里关于 location 用到的 “searching stops” 这个词很不恰当,挺误导初学者的思维,让人误以为会有两个或两个以上 location同时生效。

    正则表达式 location 以 ~ 或 ~* 开头,其他的都是常规 location (以 ^~ 开头的貌似正则表达式 location,却是常规 location)。

    Root directive 和 alias directive 用处类似,只是 alias 不改变 document root。但是,它们对文件的定位方式明显不同。例:
    如配置文件使用 root –
    server {
    server_name _;
    root /default/path;

    location /abc {
    root /another/path;
    }
    }
    Request to /abc/filename.jpg 将对应 /anther/path/abc/filename.jpg。
    如配置文件使用 alias –
    server {
    server_name _;
    root /default/path;

    location /abc {
    alias /another/path;
    }
    }
    Request to /abc/filename.jpg 将对应 /anther/path/filename.jpg。

    以上是我用我的语言的去理解 Nginx 的语法。

  • Different syntax in Nginx and Apache

    今天发现一个 Nginx 和 Apache 看似细微却影响巨大的语法差异:

    在 Apache 里重定向一个域名到另一个域名,

    RewriteRule (.*) http://destination.com/$1 [r=301,l]

    而在 Nginx 里,应该多加一个斜杠/,否则 $1就多了一个斜杠。

    rewrite ^/(.*) http://destination.com/$1 permanent;