Raspberry Pi 2 model B basic benchmarks

Just want to compare Raspberry Pi 2’s performance with Roseapple Pi’s, not only which one is faster, but also how faster, so I just did the same basic benchmarks on Raspberry Pi 2, and maybe will also do the same benchmarks (Hardinfo 、sysbench、PHP benchmark Script) on the 1st generation of Raspberry Pi.

Roseapple Pi benchmark:

https://www.peterdavehello.org/2016/02/roseapple-pi-%e8%93%ae%e9%9c%a7%e6%b4%be-%e6%95%88%e8%83%bd%e7%b0%a1%e6%b8%ac-benchmark/

The platform is Raspberry Pi 2 model B using Raspbian Jessie, no Android this time.
Full desktop image based on Debian Jessie

Version: February 2016
Release date: 2016-02-09
Kernel version: 4.1

 

PHP benchmark Script using PHP 5.6.17:

--------------------------------------
| PHP BENCHMARK SCRIPT |
--------------------------------------
Start : 2016-02-23 22:47:00
Server : @
PHP version : 5.6.17-0+deb8u1
Platform : Linux
--------------------------------------
test_math : 15.713 sec.
test_stringmanipulation : 16.238 sec.
test_loops : 8.794 sec.
test_ifelse : 6.379 sec.
--------------------------------------
Total time: : 47.124 sec.

PHP benchmark Script using PHP 7.0.3 (Using Raspbian testing repository):

--------------------------------------
| PHP BENCHMARK SCRIPT |
--------------------------------------
Start : 2016-02-23 23:03:53
Server : @
PHP version : 7.0.3-3
Platform : Linux
--------------------------------------
test_math : 4.325 sec.
test_stringmanipulation : 6.012 sec.
test_loops : 4.241 sec.
test_ifelse : 2.784 sec.
--------------------------------------
Total time: : 17.362 sec.

 

sysbench v0.4.12 single thread:

$ sysbench --test=cpu --cpu-max-prime=20000 run
sysbench 0.4.12:  multi-threaded system evaluation benchmark

Running the test with following options:
Number of threads: 1

Doing CPU performance benchmark

Threads started!
Done.

Maximum prime number checked in CPU test: 20000


Test execution summary:
    total time:                          764.9781s
    total number of events:              10000
    total time taken by event execution: 764.9617
    per-request statistics:
         min:                                 76.40ms
         avg:                                 76.50ms
         max:                                 92.16ms
         approx.  95 percentile:              76.50ms

Threads fairness:
    events (avg/stddev):           10000.0000/0.00
    execution time (avg/stddev):   764.9617/0.00

閱讀全文

Roseapple Pi 蓮霧派 效能簡測 (benchmark)

有關之前玩的蓮霧派(Roseapple Pi):

https://www.peterdavehello.org/2016/02/roseapple-pi-rapi-a-clone-of-raspberry-pi/

原先是想做一些仔細的效能測試,無奈最近實在太忙,身不由己,懶得詳測了 … 直接上圖吧,分別是在 Debian 底下用 Hardinfo 、sysbench、PHP benchmark Script 以及在 Android 底下用 QuadrantAntutu (安兔兔) 的效能”簡”測結果,比較詳細的測試,以後有緣再說囉。

系統環境的部份都和上一篇的開箱環境基本上都相同,也就是 Debian 8.1 和 Android 5.1.1,相關的映像檔資訊有需要請直接參考上面的連結。

先看在 Debian 8.1 Jessie 底下的測試結果:

Debian, Hardinfo, 直接看圖:

Debian, PHP benchmark Script using php 5.6.17:

--------------------------------------
| PHP BENCHMARK SCRIPT |
--------------------------------------
Start : 2016-02-22 13:51:08
Server : @
PHP version : 5.6.17-0+deb8u1
Platform : Linux
--------------------------------------
test_math : 8.409 sec.
test_stringmanipulation : 8.321 sec.
test_loops : 6.907 sec.
test_ifelse : 4.479 sec.
--------------------------------------
Total time: : 28.116 sec.

Debian, PHP benchmark Script using php 7.0.3 (Using Debian testing repository):

--------------------------------------
| PHP BENCHMARK SCRIPT |
--------------------------------------
Start : 2016-02-22 14:15:05
Server : @
PHP version : 7.0.3-3
Platform : Linux
--------------------------------------
test_math : 2.422 sec.
test_stringmanipulation : 3.245 sec.
test_loops : 3.002 sec.
test_ifelse : 1.955 sec.
--------------------------------------
Total time: : 10.624 sec.

上面兩個結果可以看出 PHP 7.0 的效能進步真的很大!有在寫 PHP 或 Web 相關應用的人可以當作參考。

Debian, sysbench --test=cpu --cpu-max-prime=20000 run (single-thread, 單核心):

閱讀全文

用 Apache, nginx, PHP 架網站要注意的安全事項

在 Linux 底下,拿 Apache 或 nginx + PHP 是架網站非常常見的組合 (LAMP/LNMP),這篇要筆記一下怎麼藏伺服器相關的版本資訊

對於開發平台來說這些 http header 資訊可以幫忙除錯,但對於開放使用者存取的服務,若非使用上的需求,建議是把相關的 http header 藏起來,減少資訊洩漏

圖例,用 Chrome 瀏覽器看出 Server版本為 Ubuntu 14.04 + Apache 2.4.7 + PHP 5.5.9

本篇拿 Ubuntu/Debian based GNU/Linux 示範,開始動手之前,大家可以先用 curl 這個工具把網站的 header 撈出來,改完之後就可以比較看看結果!

命令如下,127.0.0.1 請換成自己的網址~

$ curl -LI 127.0.0.1

結果大概會長這樣:

HTTP/1.1 200 OK
Date: Wed, 30 Dec 2015 08:36:26 GMT
Server: Apache/2.4.7 (Ubuntu)
X-Powered-By: PHP/5.5.9-1ubuntu4.14
Cache-Control: no-cache, must-revalidate, max-age=0
Content-Type: text/html; charset=utf-8

有看出端倪了嗎?

接下來講動手做,先說 http 伺服器的部分:

– nginx:
在 nginx.conf 的 http 區段(用大括號括起來的部分)裡面找到 server_tokens 這個設定並設為 off,記得拿掉前面的註解,如果找不到這行也可以自己新增:

server_tokens off;

重啟 nginx 後大功告成:

$ sudo service nginx restart

– apache:
以 Apache 2.4.x為例子,相關設定預設會在 Apache 設定檔路徑 (/etc/apache2) 的 conf-enabled/security.conf 裡面,看設定檔名稱就可以顧名思義知道和安全性有些關係

如果找不到 conf-enabled/security.conf 但找的到 conf-available/security.conf,表示設定檔沒被啟用,可以透過這個命令啟用:

$ sudo ln -s /etc/apache2/conf-available/security.conf /etc/apache2/conf-enabled/security.conf

這邊我們要改兩個設定,分別是 ServerTokens 和 ServerSignature

ServerTokens 會在每一次伺服器回應請求的時候送出,設定值分別有 Full | OS | Minimal | Minor | Major | Prod 這六個選項
從左至右分別為洩漏最多的資訊量到最少的資訊量,預設通常是 OS、建議改成 Prod,這樣就只不會顯示伺服器名稱以外的任何資訊

ServerSignature 不是在 http header 裡面的資訊,但會出現在一些 Apache 自動產生的頁面,顯示出伺服器版本以及 virtual host 等資訊
預設值為 On,這邊為 Off,當 Server 產生一些錯誤或是檔案列表頁面時,就不會洩漏太多的伺服器版本資訊

重啟 Apache 後大功告成:

$ sudo service apache2 restart

改完之後再用curl測試一次結果會發現 Server header 後面版本、系統等資訊都藏好了:

HTTP/1.1 200 OK
Date: Wed, 30 Dec 2015 08:37:20 GMT
Server: Apache
X-Powered-By: PHP/5.5.9-1ubuntu4.14
Cache-Control: no-cache, must-revalidate, max-age=0
Content-Type: text/html; charset=utf-8

但上面的 PHP 還在,而且還看得出來是 Ubuntu 系統,如果你有啟用一些額外的module 可能都會順便被印出來,所以接下來講 PHP:

PHP 這邊要改的這個設定叫做 expose_php,這個設定會放在 php.ini 裡面、預設是 On、這邊要調成 Off
expose_php = Off

如果該選項為 On,則 http header 裡面會含有一個 X-Powered-By 的資訊,除了讓大家知道你用的是 PHP 以外,還有使用到的相關模組,還有可能會因此透漏PHP以及作業系統的版本,因各個unix系統幾乎都有自己維護的套件版本,在編譯時可能會因此把作業系統及其版本資訊包進去(如 5.4.4-14+deb7u9 就透漏了他可能正在使用 Debian Wheezy (7.x)),這個資訊不關的話,Apache2的 ServerTokens 很有可能就白設了XD

改完後這邊要重啟的服務因人而異,如果是 PHP 搭配 nginx 可能是要重啟 php-fpm 或 php-cgi,如果是把 PHP 編成 Apache 的 module 的話就是重啟 Apache

重啟後大功告成(自己看要重新啟動哪個服務~因人而異):

$ sudo service apache2/php-fpm/php-cgi restart

HTTP/1.1 200 OK
Date: Wed, 30 Dec 2015 08:39:05 GMT
Server: Apache
Cache-Control: no-cache, must-revalidate, max-age=0
Content-Type: text/html; charset=utf-8

恩,有沒有看到 X-Powered-By 不見了?

補充一點,php.ini 可能因為不同的 PHP 使用方式 (apache module / cgi / cli 等) 會有 “很多份”,以 php5 為例,如果你是用 Apache 的 php module,則設定檔會在 /etc/php5/apache2/,若是使用 command line的 PHP,設定檔會在 /etc/php5/cli/,真的不確定的話 … 保險起見,可以將 /etc/php5/ 底下一層各個目錄都翻一下,每份 php.ini 都改一改 (mods-available 是放模組的就不用了)

最後補充,以上方法僅用於 “藏” 資訊,對於找工具針對特定版本伺服器的攻擊也許有點幫助,但並不能真的提升伺服器以及網頁程式本身的安全性,不能就此掉以輕心! 程式面該過濾、跳脫、阻擋的還是要做,永遠不能相信client傳過來的訊息啊~!

另外有一些像是 libapache2-mod-security2 這類的工具可以幫忙防禦一些常見攻擊或是弱點,不過設定不好的情況下也是有很大機率遇到其他狀況,要不要用就見仁見智了,我是遇過用了 mod security 之後預設設定會造成 Discuz 的上傳故障的問題 …

在 IE 瀏覽器傳遞 GET 參數中文遇到亂碼

昨天被丟了一個問題:同一隻 PHP 程式在 Chrome/Firefox 丟中文參數沒問題,怎麼在 IE (Microsoft Internet Explorer) 就會亂碼?

code 大概長這樣,主要是把傳入的參數存入 DB 裡面,資料庫用的是 UTF-8 的編碼

$con = mysql_connect('127.0.0.1','id','pw');
mysql_select_db('test', $con);
mysql_query('set names 'utf8'');
$msg = $_GET['msg'];
$sql = 'INSERT INTO `db` . `table` (`column`) VALUES (' . $msg . ')';
mysql_query($sql, $con);
mysql_close($con);

問題其實是出在 IE 傳遞參數時會以 ANSI 來編碼 (即便網頁編碼是UTF-8),自然會出現亂碼的問題,基於現在 UTF-8 是主流,所以可以在使用傳入的參數之前使用 PHP 內建的 mb_detect_encoding 做這樣的處理:

$encode = mb_detect_encoding($msg, array ('UTF-8', 'BIG5', 'GB2312'));
if ($encode && $encode != 'UTF-8') {
    $msg = iconv ($encode, 'UTF-8', $msg);
}

{ "UTF-8", "BIG5", "GB2312" } 這段可以依序換成要被識別的編碼,在編碼可以辨識(有被列入)且不是 UTF-8 的情況下會用 PHP 內建的 iconv 來做轉換的動作, 就可以解決這樣的問題啦~