2010年7月28日 星期三

[Tips] Change keyboard mapping

IBM鍵盤的上下左右鍵旁有兩個按鍵, 原本是設計往左往右, 可是我習慣使用Acer的鍵盤配置, 也就是這兩個鍵是PgUp和PgDn, 因此就需要作些小修改啦, 在Windows上, 有個好用的軟體AutoHotkey可以讓你自己撰寫script改變鍵盤對應, 而在Linux上, 就只要觀察實體鍵盤的代碼, 修改對應的key code即可, 廢話不多說, 筆記...
# xmodmap -e "keycode 166 = Prior"
# xmodmap -e "keycode 167 = Next"

相關的修改可參考keyboard/mouse mapping

2010年7月25日 星期日

[Tips] 常用的LAMP設定檔

安裝Linux+Apache+MySQL+PHP我的習慣順序就是照字面上的順序, 而安裝過程大部份是從source編譯, 有些library則是用套件管理程式先安裝好的, 使用的是RedHat/CentOS/Fedora Linux, 不同的Linux除了套件管理程式使用不同以外, 其他操作都是相同的(#開頭的是RedHat系列,$開頭的是Ubuntu系列), 以下開始筆記...

安裝libtool
# yum install libtool
$ sudo apt-get install libtool

安裝ltdl
# yum install libtool-ltdl
# yum install libtool-ltdl-devel
$ sudo apt-get install libltdl-dev

安裝gnutls
# yum install gnutls
# yum install gnutls-devel
# yum install gnutls-utils
$ sudo apt-get install libgnutls-dev

安裝openssl
# yum install openssl
$ sudo apt-get install openssl
$ sudo apt-get install libssl-dev

下載subversion-deps(ver. 1.6.12)
解壓縮subversion-deps到subversion目錄

安裝apr (with subversion source)
# cd {/path/to/subversion/src}/apr
# ./configure --prefix=/usr/local/apr
# make && make install && make clean

安裝apr-util (with subversion source)
# cd {/path/to/subversion/src}/apr-util
# ./configure --prefix=/usr/local/apr --with-apr=/usr/local/apr
# make && make install && make clean

安裝apr-iconv (ver. 1.2.1)
# ./configure --prefix=/usr/local/apr --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr
# make && make install && make clean

安裝pcre(配合httpd-2.3.x才需要)
# ./configure --prefix=/usr/local/pcre --enable-utf8
# make && make install && make clean

安裝httpd (ver. 2.2.16)
# ./configure --prefix=/usr/local/apache2 --enable-mods-shared=most --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr/bin/apu-1-config --enable-dav --enable-dav-fs --enable-dav-lock --enable-maintainer-mode --enable-unique-id --enable-speling --enable-deflate --enable-file-cache --enable-cache --enable-disk-cache --enable-mem-cache --enable-filter --enable-isapi --enable-mime-magic --enable-rewrite --enable-usertrack --enable-static-rotatelogs --enable-static-logresolve --enable-ssl --with-mpm=worker
# make && make install && make clean
# vim /usr/local/apache2/conf/httpd.conf
[add]
CustomLog "logs/access_log" combined
ServerTokens ProductOnly
ServerSignature Off
<Directory "/var/www/html">
AddDefaultCharset utf-8
Options FollowSymLinks
AllowOverride all
Order allow,deny
Allow from all
</Directory>
[/add]

note: 當要進行動態編譯modules時, 就不需要加上--enable-so
reference: apache動態編譯/靜態編譯問題

下載neon (ver. 0.29.3)
# tar zxvf neon.tar.gz
# mv neon {/path/to/subversion/src}

安裝libexpat (ver. 2.0.1)
# ./configure
# make && make install && make clean
# ln -s /usr/local/lib/libexpat.la {/path/to/subversion/src}/apr-util/xml/expat/lib

安裝subversion (ver. 1.6.12)
# ./configure --prefix=/usr/local/svn --with-apxs=/usr/local/apache2/bin/apxs --with-apr=/usr/local/apr/bin/apr-1-config --with-apr-util=/usr/local/apr/bin/apu-1-config --with-ssl
# make && make install & make clean

設定https(openssl已安裝)
# openssl x509 -req -days 7 -in my.demo.csr -signkey my.demo.key -out my.demo.crt
# openssl genrsa -out my.demo.key 1024
# openssl req -new -key my.demo.key -out my.demo.csr
# vim /usr/local/apache2/conf/httpd.conf
Listen 443
<VirtualHost _default_:443>
ServerName localhost
SSLEngine on
SSLCertificateFile /usr/local/apache2/conf/ca/my.demo.crt
SSLCertificateKeyFile /usr/local/apache2/conf/ca/my.demo.key
</VirtualHost>

安裝mysql (ver. 5.1.49)
# ./configure --prefix=/usr/local/mysql --datadir=/usr/local/mysql/data --with-unix-socket-path=/tmp/mysql.sock --enable-assembler --with-big-tables --with-readline --with-ssl --with-charset=utf8 --with-extra-charsets=all --enable-thread-safe-client
# make && make install && make clean
# useradd mysql
# /usr/local/mysql/bin/mysql_install_db --user=mysql --no-defaults
# cp support-files/my-medium.cnf.sh /etc/my.cnf
# chown mysql:mysql /etc/my.cnf
# vim /etc/my.cnf
[client]
port=3306
socket=/tmp/mysql.sock
[mysqld]
port=3306
socket=/tmp/mysql.sock
datadir=/usr/local/mysql/var
# chown -R mysql:mysql /usr/local/mysql/var
# cp /usr/local/mysql/data/mysql/mysql.server /etc/rc.d/init.d/mysqld
# chkconfig --add mysqld [Start As Service]
# service mysqld start
# /usr/local/mysql/bin/mysql -u root -p [ENTER]
mysql>UPDATE mysql.user SET password=PASSWORD('NEW_PASSWORD') WHERE user='root';
mysql>flush privileges;
mysql>exit

安裝libgpg-error (ver. 1.9)
# ./configure
# make && make install && make clean
$ sudo apt-get install libgpg-error-dev libgpg-error0

安裝libgcrypt (ver. 1.4.6)
# ./configure
# make && make install && make clean
$ sudo apt-get install libgcrypt11 libgcrypt11-dev

安裝libmcrypt (ver. 2.5.7)
# ./configure --enable-ltdl-install
# make && make install && make clean
$ sudo apt-get install libmcrypt4 libmcrypt-dev mcrypt

reference: http://bojack.pixnet.net/blog/post/3983596

安裝libxml (ver. 2.7.7)
# ./configure
# make && make install && make clean
$ sudo apt-get install libxml2 libxml2-dev

安裝curl (ver. 7.21)
# ./configure
# make && make install make clean

安裝t1lib
$ sudo apt-get install libt1-5 libt1-dev

安裝freetype (ver. 2.4.2)
# ./configure --prefix=/usr/local
# make && make install && make clean
$ sudo apt-get install libfreetype6 libfreetype6-dev

安裝libjpeg (ver. 8b)
# ./configure --prefix=/usr/local
# make && make install && make clean
$ sudo apt-get install libjpeg62 libjpeg62-dev

安裝libXpm (ver. 3.5.8)
# ./configure --prefix=/usr/local
# make && make install && make clean
$ sudo apt-get install libxpm4

安裝php (ver. 5.2.14)
# ./configure --prefix=/usr/local/php --with-apxs2=/usr/local/apache2/bin/apxs --enable-debug --enable-magic-quotes --enable-libgcc --enable-ftp --enable-mbstring --with-mysql=/usr/local/mysql --with-pdo-mysql=/usr/local/mysql --enable-sockets --with-gnu-ld --with-curl=/usr/local/lib --with-mcrypt=/usr/local/src/libmcrypt --with-zlib-dir=/usr/local/src/zlib --with-gd --with-t1lib --with-jpeg-dir=/usr/local --with-xpm-dir=/usr/local --with-freetype-dir=/usr/local --with-gettext
# make && make install && make clean
# cp php.ini-recommended /usr/local/php/lib/php.ini
# vim /usr/local/apache2/conf/httpd.conf
[add]
AddType application/x-httpd-php .php .phtml
DirectoryIndex index.php index.html
[/add]

測試gd
<?php
echo '<div style="margin: 10px;">';
echo '<p style="color: #444444; font-size: 130%;">GD is ';
if (function_exists("gd_info")) {
echo '<span style="color: #00AA00; font-weight: bold;">supported</span> by your server!</p>';
$gd = gd_info();
foreach ($gd as $k => $v) {
echo '<div style="width: 340px; border-bottom: 1px solid #DDDDDD; padding: 2px;">';
echo '<span style="float: left;width: 300px;">' . $k . '</span> ';
if ($v)
echo '<span style="color: #00AA00; font-weight: bold;">Yes</span>';
else
echo '<span style="color: #EE0000; font-weight: bold;">No</span>';
echo '<div style="clear:both;"><!-- --></div></div>';
}
} else {
echo '<span style="color: #EE0000; font-weight: bold;">not supported</span> by your server!</p>';
}
echo '<p>by <a href="http://www.dagondesign.com">dagondesign.com</a></p>';
echo '</div>';
?>

reference: Checking for GD support on your server

安裝xdebug
# /usr/local/php/bin/phpize
# ./configure --prefix=/usr/local/xdebug --enable-xdebug --with-php-config=/usr/local/php/bin/php-config
# make && make install && make clean
# vim /usr/local/php/lib/php.ini
[add]
zend_extension="/usr/local/php/lib/php/extensions/debug-zts-20060613/xdebug.so"
xdebug.profiler_enable=on
xdebug.trace_output_dir="/tmp/xdebug"
xdebug.profiler_output_dir="/tmp/xdebug"
[/add]
# required glibc

安裝suhosin
# /usr/local/php/bin/phpize
# ./configure --prefix=/usr/local/suhosin --with-php-config=/usr/local/php/bin/php-config
# make && make install && make clean
# cp /usr/local/php/lib/php/extensions/debug-zts-20060613/suhosin.so /usr/local/php/lib/php/extensions/
# vim php.ini
extension_dir = "/usr/local/php/lib/php/extensions"

reference:
How To Harden PHP5 With Suhosin On CentOS 5.3 | HowtoForge - Linux Howtos and Tutorials
Hardened-PHP Project - PHP Security - Installation!

安裝memcached (ver. 2.2.5)
# /usr/local/php/bin/phpize
# ./configure --enable-memcache --with-php-config=/usr/local/php/bin/php-conifg --with-zlib-dir
# make && make install && make clean
# cp /usr/local/php/lib/php/extensions/debug-zts-20060613/memcache.so /usr/local/php/lib/php/extensions/
# vim /usr/local/php/lib/php.ini
[add]
extension=memcache.so
[/add]

reference:
如何使用 memcached 做快取

安裝eaccelerator
# /usr/local/php/bin/phpize
# ./configure --with-php-config=/usr/local/php/bin/php-config
# make; make install
# cp /usr/local/php/lib/php/extensions/debug-zts-20060613/eaccelerator.so /usr/local/php/lib/php/extensions/
# vim /usr/local/php/lib/php.ini
[add]
extension=eaccelerator.so
eaccelerator.shm_size="512"
eaccelerator.cache_dir="/tmp/eaccelerator"
eaccelerator.enable="1"
eaccelerator.optimizer="0"
eaccelerator.check_mtime="1"
eaccelerator.debug="0"
eaccelerator.filter=""
eaccelerator.shm_max="0"
eaccelerator.shm_ttl="0"
eaccelerator.shm_prune_period="0"
eaccelerator.shm_only="0"
eaccelerator.compress="1"
eaccelerator.compress_level="9"
eaccelerator.cache_dir="/tmp/eaccelerator"
eaccelerator.log_file = "/var/log/eaccelerator_log"
[/add]

reference:
Installing from source
PHP - PHP 加速器的調校(php-eaccelerator)

特殊的php連結sql server需求
安裝freetds (ver. 0.82)
# ./configure --prefix=/usr/local/freetds --enable-msdblib --with-openssl=/usr/local src/openssl --with-gnutls
# make && make install && make clean
需重新編譯PHP
# cd {/path/to/php/src}
# ./configure --with-mssql=/usr/local/freetds
# make && make install && make clean

reference:
* How To Install Apache 2 with SSL on Linux (with mod_ssl, openssl)

2010年7月23日 星期五

[Tips] Fedora 13安裝設定

之前用了一陣子的ubuntu, 實在是用不習慣, 只好轉回用fedora當工作環境, 發現有些地方需要稍微修改一下比較順手, 就筆記一下了...

1. 網路設定的問題
剛安裝完fedora時, 網路經常無法正常啟動, 需先將網卡啟動, 修改/etc/sysconfig/network-scripts/ifcfg-eth0, 將ONBOOT=no改為yes, 另外再啟動NetworkManager方便網路設定

2. Yum遇到的錯誤訊息
Loaded plugins: presto, refresh-packagekit
Existing lock /var/run/yum.pid: another copy is running as pid XXXX
先kill所有正在執行的yum程序, 並且清掉所有cache的資料即可
[root@localhost]# yum clean all

若遇到repository站台回應速度緩慢的錯誤訊息, 例如:
Error: Cannot retrieve repository metadata (repomd.xml) for repository: fedora. Please verify its path and try again
則註解掉/etc/yum.repos.d/fedora.repo和/etc/yum.repos.d/fedora-updates.repo中的baseurl, 並且在/etc/hosts加上兩筆資料
80.239.156.215 mirrors.fedoraproject.org
213.129.242.84 mirrors.rpmfusion.org
另外再安裝yum-fastestmirror套件, 未來在搜尋套件時, 可搜尋速度較快的mirror站台


/** update @ 2010-08-19 **/
這幾天又試裝了一下ubuntu 10.04, 發現fedora 13上面遇到的一些鳥問題在新版本的ubuntu都不會遇到, 為了不想搞環境搞太久, 我還是先把工作環境轉過去吧...

2010年7月7日 星期三

PHP Obfuscator

若是不想要PHP的原始碼讓別人看到, 可以使用open source的PHP Screw, 以下是1.5的安裝步驟:

0.系統需求
PHP 5.x的環境
具有zlib的支援(可透過<?php gzopen(); ?>來檢查)

1.解壓縮
修改my_screw.h, 裡面的數字就是seed, 可視為密碼

2.編譯成so
$ phpize
$ ./configure –with-php-config=/path/to/php-config
$ make

若發生錯誤如
/path/to/php_screw-1.5/php_screw.c:78: error: too few arguments to function 'org_compile_file'
/path/to/php_screw-1.5/php_screw.c:84: error: too few arguments to function 'org_compile_file'
/path/to/php_screw-1.5/php_screw.c:91: error: too few arguments to function 'org_compile_file'
/path/to/php_screw-1.5/php_screw.c:93: error: too few arguments to function 'org_compile_file'
從官方bug列表找到解決方法:
modify php_screw.c
org_compile_file(file_handle, type);
to
org_compile_file(file_handle, type TSRMLS_CC);

或發生錯誤如
/path/to/php_screw-1.5/php_screw.c: In function 'zm_startup_php_screw':
/path/to/php_screw-1.5/php_screw.c:124: error: 'zend_compiler_globals' has no member named 'extended_info'
/path/to/php_screw-1.5/php_screw.c: In function 'zm_shutdown_php_screw':
/path/to/php_screw-1.5/php_screw.c:133: error: 'zend_compiler_globals' has no member named 'extended_info'
閱讀source後
modify php_screw.c
CG(extended_info) = 1;
to
// CG(extended_info) = 1;

3.修改php.ini設定
[php_screw]
extension=php_screw.so

4.編譯混淆器
$ cd tools $ make )

5.使用(對foo.php做混淆)
$ ./tools/screw foo.php
$ Success Crypting(foo.php)

若要對多個檔案進行混淆,可以簡單的使用兩行指令搞定
$ find . -name "*.php" -exec /path/to/tools/screw -exec {} \;
$ find . -name "*.screw" -exec rm -f {} ;

注意, 混淆過的檔案, 對方主機需安裝php_screw才能讀取, 所以虛擬主機類的應該就沒辦法用這一招了...

參考資料:
* 使用自由加密軟件PHP screw對PHP源碼加密
* 用php_screw加密PHP代碼