關於 PHP, Linux, Open Source 及個人生活記載的網誌。
RSS icon
  • 透過 htaccess 設定 Character Set

    Posted on August 15th, 2011 Sam Tang No comments

    要設定網站的 Character Set,除了在 httpd.conf 設定外,還可以在 .htaccess 對個別網站設定,格式如下:

    AddDefaultCharset utf-8

    以上這行便會設定網站預設使用 utf-8 編碼,如果只想對特定檔案設定 Character Set,可以這樣做:

    AddCharset utf-8 .html .htm .css .js .xml .json .rss

    這樣就可以設定 .html, .htm, .css, .js, .xml, .json 及 .rss 的預設 Character Set 使用 utf-8.


  • MySQL 效能優化工具 — MySQLTuner

    Posted on August 9th, 2011 Sam Tang No comments

    MySQLTuner 是一個針對 MySQL 效能的分析 Perl script,它會根據採集到的數據,給予修改 my.cnf 的設定值建議,從而改善 MySQL 的執行效率。

    要執行 MySQLTuner 十分簡單,只要下載後並給予執行權限便可直接執行:

    wget http://mysqltuner.com/mysqltuner.pl
    chmod +x mysqltuner.pl

     
    然後便可以直接執行 mysqltuner.pl:

    ./mysqltuner.pl

     
    這時 MySQLTuner 會詢問 mysql 的管理員用戶名稱及密碼,然後便會採集數據並產生報告。而報告的最後是 Recommendations 部份,是可以在 my.cnf 的 [mysqld] 調整的設定值,修改的設定值後需要重新啟動 mysql 才會生效。


  • 變更 MySQL client 的提示符號

    Posted on August 8th, 2011 Sam Tang No comments

    在預設的情況下,在 command line 連接 MySQL 資料庫時,會使用以下的提示符號:

    mysql>

    如果將以上提示符號變更,可以顯示更多資訊的話,操作起來會相當方便。要變更這個提示符號首先開啟 my.cnf 檔案(Windows 用戶是 my.ini),然後在 [mysql] 區間加入以下語句:

    prompt=\u@\h:[\d]>\_

    上面語句的 "\u" 及 "\h" 分別會顯示用戶名稱及主機名稱,"\d" 會顯示所選擇的資料庫,以下是上面設定的提示符號:

    samtang@localhost:[hkcode]>

    以下是 MySQL 提示符號可用的參數:

    \c   A counter that increments for each statement you issue
    \D   The full current date
    \d   The default database
    \h   The server host
    \l   The current delimiter (new in 5.0.25)
    \m   Minutes of the current time
    \n   A newline character
    \O   The current month in three-letter format (Jan, Feb, …)
    \o   The current month in numeric format
    \P   am/pm
    \p   The current TCP/IP port or socket file
    \R   The current time, in 24-hour military time (0–23)
    \r   The current time, standard 12-hour time (1–12)
    \S   Semicolon
    \s   Seconds of the current time
    \t   A tab character
    \U   Your full user_name@host_name account name
    \u   Your user name
    \v   The server version
    \w   The current day of the week in three-letter format (Mon, Tue, …)
    \Y   The current year, four digits
    \y   The current year, two digits
    \_   A space
    \   A space (a space follows the backslash)
    \’   Single quote
    \”   Double quote
    \\   A literal “\” backslash character
    \x   x, for any ‘x’ not listed above


  • 將 .htaccess 檔案改名

    Posted on July 26th, 2011 Sam Tang No comments

    .htaccess 可以改變很多網站的設定,這也造成一個安全性的問題,因為有心人都會估計到網站可能會用 .htaccess,如果未有對 .htaccess 進行權限定,所有人都可以瀏覽 .htaccess 的內容。

    除了變更權限外,也可以設定 Apache 讀取其他檔案名稱,那麼其他人便不會那麼容易知道設檔的位置,但這個方法需要編輯 httpd.conf 才可以實現。

    在 Apache 建立的 virtualhost 裡面,只要加入 "AccessFileName" 便可以自訂設定檔的名稱,例如:

    AccessFileName .htaccess.hidden

    以上語句會指定 .htaccess.hidden 代替原本的 .htaccess 檔案。


  • Linux 使用 TMOUT 設定自動斷線

    Posted on June 9th, 2011 Sam Tang No comments

    Linux 下可以透過設定 TMOUT 變數,來實現當用戶在特定時內沒有任何動作就終斷連線,設定指令為:

    export TMOUT=300

    以上指令設定預設終斷連線時間為 5 分鐘,也可以編輯 ~/.bash_profile 或 /etc/profile 進行設定。

    如果要取消 TMOUT 的設定,可以用以下指令實現:

    export TMOUT=0

    unset TMOUT

  • QMail 新增額外 SMTP Port

    Posted on September 11th, 2010 Sam Tang No comments

    現在很多 ISP 也會將用戶的 port 25 封鎖,只容許用戶使用 ISP 提供的 smtp server。如果想架一台 smtp server 給被封鎖 port 25 的用戶使用,可以在 email server 多開一個 port 作為與用戶的連線,只要用戶在電郵軟件修改 smtp port 便可以,以下是在 QMail 設定多一個 port 的方法:

    假設要開啟的 port 為 1225,首先將 /var/qmail/supervise/qmail-smtpd 整個目錄複製多一份,一般上我會在複製目錄名稱後面加上 port 以便不會忘記:

    # cp -Rp /var/qmail/supervise/qmail-smtpd /var/qmail/supervise/qmail-smtpd-1225

    然後要修改 /var/qmail/supervise/qmail-smtpd-1225/run 檔案,設定要監聽的 port,應該會找到類以這一行:

    -u "$QMAILDUID" -g "$NOFILESGID" 0 smtp \

    將 “smtp” 改成要新增的 port,那麼就是要改成:

    -u "$QMAILDUID" -g "$NOFILESGID" 0 1225 \

    修改完以上檔案後,便要修改 /var/qmail/supervise/qmail-smtpd-1225/log/run 檔案,最後應該會有一行是指向 smtpd 的紀綠檔,要指向一個新目錄:

    exec /usr/local/bin/setuidgid qmaill /usr/local/bin/multilog t /var/log/qmail/smtpd-1225

    然後建立儲存 smtpd-1225 的紀錄檔目錄:

    # mkdir /var/log/qmail/smtpd-1225/
    # chown qmaill /var/log/qmail/smtpd-1225/

    在 /service 目錄下建立一個 soft link 到 /var/qmail/supervise/qmail-smtpd-1225/:

    # ln -s /var/qmail/supervise/qmail-smtpd-1225/ /service/qmail-smtpd-1225/

    到這裡就只剩下修改 qmailctl 檔案,裡面有一行是:

    svclist="qmail-send qmail-smtpd"

    需要加上新增的 port 才可正確啟動:

    svclist="qmail-send qmail-smtpd qmail-smtpd-1225"

    修改後重新啟動 qmail 便完成了。


  • Linux 掛載 Freebsd UFS 分割區

    Posted on August 3rd, 2010 Sam Tang No comments

    今日想將一台 Freebsd 的主機硬碟掛載到 Linux 下,原本以為很簡單,但用 mount 指令出現錯誤,即是指定了 filesystem 類型也一樣。

    google 了一下,發現原來只可以掛載成 read only,假設 ufs 分割區為 /dev/sdc1,以下是在 Ubuntu 下掛載 UFS 分割區的方法:

    sudo mkdir /mnt/ufsdisk
    sudo mount -t ufs -r -o ufstype=ufs2 /dev/sdc1 /mnt/ufsdisk

  • Linux: 將檔案的 Tabs 字元用空白取代

    Posted on March 11th, 2010 Sam Tang No comments

    在 Linux 下有一個工具 “expand”,它可以將檔案內所有 tabs 字元轉換為空白字元, 用法如下:

    expand input.file > output.file
    expand data.txt > output.txt
    expand -t 2 data.txt > output.txt

    上面第三行的參數 -t 是定義用多少個空白字元取代 tabs。而另一個工具 “unexpand” 則與 “expand” 剛剛相反, 是將空間字元轉換為 tabs 字元。


  • Ubuntu – Firefox 的 Flash 亂碼問題

    Posted on March 5th, 2010 Sam Tang 2 comments

    在 Google 找了一下,應該是 /etc/fonts/conf.d/49-sansserif.conf 出問題,只要將檔案移除或移到其他地方就可以:

    sudo mv /etc/fonts/conf.d/49-sansserif.conf /etc/fonts/conf.d/49-sansserif.conf.bak

    輸入以上指令後,只要重新啟動 Firefox,就可以看到問題解決了。


  • vi 方向鍵及 backspace 的問題

    Posted on March 5th, 2010 Sam Tang 1 comment

    在 Ubuntu 上面有時編輯一些設定檔時會直接用 vi 來做,但預設安裝 Ubuntu 後用 vi 開啟檔案,會發覺方向鍵及 backspace 鍵不能使用,解法方法是在家目錄建立 .vimrc 檔案,然後加入以下內容:

    set nocompatible
    set backspace=2

    儲存檔案後離開,下次使用 vi 開啟檔案就可以使用方向鍵及 backspace 了。


  • Linux: 操作包含特殊字元的檔案

    Posted on February 22nd, 2010 Sam Tang No comments

    在 Unix 或 Linux 下很多特殊字元也是指令的一部份,例如空格 (” “)、問號 (“?”) 及橫線 (“-”) 等。所以如果檔案名稱包含了一些特殊字元,在操作上便會出現錯誤,例如:

    # touch -foo.txt
    touch: invalid option — ‘o’
    Try `touch –help’ for more information.
    # touch –foo.txt
    touch: unrecognized option ‘–foo.txt’
    Try `touch –help’ for more information.

    上面輸入了兩行指令,分別用 touch 建立檔案 “-foo.txt” 及 “–foo.txt”,但同樣發生錯誤。要解決這個問題,只要在指令後面加上兩個橫線 “–” 即可,例如:

    # touch — –foo.txt
    # touch — -foo.txt

    而這個方法同樣可以在其他檔案操作指令使用,例如 cp, mv, rm, rmdir。

    以下是 cp 及 mv 的用法:

    cp – - ‘-foo.txt’ /path/to/dest
    mv – - ‘-foo.txt’ /path/to/dest


  • 執行 sudo 時電郵通知

    Posted on February 19th, 2010 Sam Tang No comments

    sudo 提供了簡單的方法追蹤執行紀錄,其中一項就是當透過 sudo 執行 指令時發送電郵到指定電郵地址,方法如下:

    1. 開啟 sudo 的設定檔:
    visudo

    2. 加入下面兩行:

    mailto “admin@localhost”
    mail_always on

    mailto 後面是設定發送電郵通知的地址。
    mail_always 預設是 off,開啟後會在每次執行 sudo 後發出電郵通知。


  • Iptables 限制每個 ip 連線數

    Posted on February 18th, 2010 Sam Tang No comments

    如果在 Linux 下要限制每個 ip 的連線數,可以透過 iptables 實現。詳細指令語法如下:

    /sbin/iptables -A INPUT -p tcp –syn –dport 22 -m connlimit –connlimit-above 3 -j REJECT
    限制每個 ip 只可以有 3 個 ssh 連線 (預設 ssh 使用 port 22)。

    /sbin/iptables -A INPUT -p tcp –syn –dport 80 -m connlimit –connlimit-above 20 -j REJECT –reject-with tcp-reset
    只接受每個 ip 20 個 http 連線 (httpd.conf 裡面的 MaxClients 預設是 60)。
    要留意的是,這個設定可能會把 proxy servers 阻隔,因為每個 proxy servers 可能會建立大量的連線。

    Skip proxy server IP 1.2.3.4 from this kind of limitations:

    /sbin/iptables -A INPUT -p tcp –syn –dport 80 -d ! 1.2.3.4 -m connlimit –connlimit-above 20 -j REJECT –reject-with tcp-reset
    這句的作用跟上面語法一樣,只是把已知的 proxy server (1.2.3.4) 給開通,避免阻隔 proxy servers 的連線。

    In this example, limit the parallel http requests to 20 per class C sized network (24 bit netmask)

    /sbin/iptables -A INPUT -p tcp –syn –dport 80 -m connlimit –connlimit-above 20 –connlimit-mask 24 -j REJECT –reject-with tcp-reset
    這個是限制同一個 class C 網絡同時建立 20 個連線。

    如果想把在指定時間內建立過多連線的 ip 阻隔,這便要編輯 iptables 的 shell script。
    以下例子會阻隔在 100 秒內建立多於 10 個 http 連線的 ip

    #!/bin/bash
    IPT=/sbin/iptables
    # Max connection in seconds
    SECONDS=100
    # Max connections per IP
    BLOCKCOUNT=10
    # ….
    # ..
    # default action can be DROP or REJECT
    DACTION=”DROP”
    $IPT -A INPUT -p tcp –dport 80 -i eth0 -m state –state NEW -m recent –set
    $IPT -A INPUT -p tcp –dport 80 -i eth0 -m state –state NEW -m recent –update –seconds ${SECONDS} –hitcount ${BLOCKCOUNT} -j ${DACTION}
    # ….
    # ..

    要儲存 iptables 的修改可以查看 iptables-save 的 man page,在 redhat 下是用以下指令:
    service iptables save


  • ProFTPd 登入援慢問題

    Posted on February 10th, 2010 Sam Tang No comments

    在安裝 ProFTPd 後,會發覺登入時非常援慢,但只要登入成功後,速度就會變得很暢順。

    造成這個問題的原因是 ProFTPd 預設在登入時會做「反查」的動作,解決方法是將 ProFTPd 設定為不做「反查」。方法如下:

    1. 開啟 proftpd.conf,一般上檔案位置會是 /etc/proftpd.conf 或 /usr/local/etc/proftpd.conf

    2. 加入以下兩行:

    UseReverseDNS off
    IdentLookups off

    3. 重新啟動 ProFTPd。


  • Linux 系統管理電子書

    Posted on February 9th, 2010 Sam Tang No comments

    The Free Technology Academy (FTA) 釋出一本免費電子書 “The GNU/Linux operating system“,內容主要圍繞系統管理,例如如何安裝及設定多種不同的伺服器及優化系統等。

    官方網站介紹:

    The GNU/Linux systems have reached an important level of maturity, allowing to integrate them in almost any kind of work environment, from a desktop PC to the sever facilities of a big company.

    In the module called “The GNU/Linux operating system”, the main contents are related with system administration. This book is the main documentation for the module.

    書本資料:

    * Authors: Remo Suppi Boldrito, Josep Jorba Esteve
    * Coordinator: Josep Jorba Esteve
    * Licenses: GNU Free Documentation License, Creative Commons Attribute ShareAlike License
    * Information: 545 Pages; 18.8 Mb
    * Download PDF version