推薦:《PHP視頻教程》
因為平時經(jīng)常會執(zhí)行一些PHP代碼片段,或臨時的項目等,做測試使用,辦公環(huán)境在虛擬機中,我不想因為一些個人的測試而去破壞辦公環(huán)境;
因此可以在win本地僅安裝一個PHP(安裝省略),或者虛擬機中的Linux安裝PHP來運行測試任務(wù);(建議用虛擬機中Linux方式)
再次提醒,這種方式僅本地測試使用;
# 在自己家目錄下創(chuàng)建www目錄 [root@localhost ~]# mkdir www [root@localhost ~]# cd www/ # 創(chuàng)建幾個php腳本用于測試 index.php info.php # 啟動一個Web服務(wù)器 [root@localhost www]# php -S 192.168.204.151:8000 # 注意:因為我是采取虛擬機中Linux,所以這里直接使用了ip,如果是本地win下,可以直接localhost:8000
請求http://192.168.204.151:8000/,返回效果如下
請求http://192.168.204.151:8000/info.php,返回效果如下
啟動時指定一個根目錄
# 在~/www下創(chuàng)建一個test目錄,并添加php腳本文件(~/www/test/index.php)mkdir ~/www/test # 啟動web[root@localhost www]# php -S 192.168.204.151:8000 -t test/
訪問測試
指定某個腳本,使其作為router
# 先創(chuàng)建一個router.php [root@localhost www]# vi router.php [root@localhost www]# cat router.php <?php if (preg_match('/.(?:png|jpg|jpeg|gif|txt)$/', $_SERVER["REQUEST_URI"])) return false; // 直接返回請求的文件 else { echo "<p>Welcome to PHP</p>"; } # 創(chuàng)建一個txt文件或者圖片 [root@localhost www]# ll -rw-r--r-- 1 root root 31 12月 4 10:56 hello.txt 測試用 -rw-r--r-- 1 root root 65 12月 4 10:35 index.php -rw-r--r-- 1 root root 17 12月 4 10:36 info.php -rw-r--r-- 1 root root 177 12月 4 10:55 router.php drwxr-xr-x 2 root root 23 12月 4 10:49 test # 啟動web [root@localhost www]# php -S 192.168.204.151:8000 router.php # 請求需要經(jīng)過router.php處理
直接請求192.168.204.151:8000/
請求一個txt文件,返回改文件的內(nèi)容
總結(jié):使用這種方式,個人做測試時比較方便,如有錯誤之處,歡迎指正!