apache 練習 (NASA 課程)
一、Virtual host:
1、先在 pc 端的 hosts 增加 2 個測試用的 domain,並指定 ip 到 vm 上 在 windows 上的路徑為 C:\Windows\System32\drivers\etc\hosts
在 linux 上的路徑為 /etc/hosts 192.168.254.15hello1.csie.edu.tw 192.168.254.15hello2.csie.edu.tw
設定完成後,用 ping 確認可以解析到。
#ping hello1.csie.edu.tw
#ping hello2.csie.edu.tw
2、copy vhosts.conf 到 apache 的 conf/extra 下
#cp vhosts.conf /usr/local/apache/conf/extra (路徑跟檔名都是可以自行定義的) (vhosts.conf 檔,請參考課程的附件) 3、在 httpd.conf 裡面新增
#vi /usr/loca/apache/conf/httpd.conf 將
include extra/vhosts.conf 加至 config 最下方
**(config 內容一樣是由上到下的順序,後方的設定會覆蓋前方的設定)
4、依 vhosts 建立 2 個 virtual host 的 document root,並建立一識別用的 index.html
#mkdir -p /home/httpd/hello1.csie.edu.tw/www/admin (admin 是等下測 htaccess 用的,先建立)
#mkdir -p /home/httpd/hello2.csie.edu.tw/www/admin
#vi /home/httpd/hello1.csie.edu.tw/www/index.html (html 內容自訂,可供辨識即可)
#vi /home/httpd/hello2.csie.edu.tw/www/index.html 5、重啟 apache
#/usr/local/apache/bin/apachectrl start
PC 端測試瀏覽 hello1.csie.edu.tw、hello2.csie.edu.tw 。
**去看看 vhosts.conf 的內容,再去了解 name base 跟 ip base virtualhost 的差別。
二、.htaccess 存取驗證功能:
1、先瀏覽測試 http://hello1.csie.edu.tw/admin (此時沒有驗證,可以直接存取) 2、先建立驗證用的 user csie
/usr/local/apache/bin/htpasswd -c /usr/local/apache/conf/users csie 建立認證用的 user:csie,並指定驗證檔的存放處。
3、在 admin 下建立 .htaccess 檔 請參考課程的附件。
4、瀏覽測試
現在瀏覽 http://hello1.csie.edu.tw/admin 時,會彈出要求輸入帳號、密碼的小視窗,可以前面 新增的帳號來登入。
**去了解如何用 user 跟 group 來做管理。
三、使用 mod_rewrite:
1、先瀏覽
http://hello1.csie.edu.tw/system/index.html (此時無法瀏覽,因為不存在) 2、建立 admin1 ,並在下面新增 index.html 檔案
#mkdir -p /home/httpd/hello2.csie.edu.tw/www/admin1
#vi /home/httpd/hello2.csie.edu.tw/www/admin1/index.html 3、在 hello1.csie.edu.tw 的 vhosts.conf 加上 rewrite 設定:
RewriteEngine on
RewriteRule ^/system/(.*)$ /admin1/$1
**這邊要注意要放在 hello1.csie.edu.tw 的<VirtualHost></VirtualHost>之間。
4、重啟 apache
/usr/local/apache/bin/apachectl 5、測試瀏覽
http://hello1.csie.edu.tw/system/index.html 此時會看到 admin1 下的 index.html
**web server 的 resource 很珍貴,要用 cache 或是 reverse proxy 等機制來配合。
** rewrite 可以做的事情很多
官方文件:http://httpd.apache.org/docs/current/mod/mod_rewrite.html