More Related Content Similar to 台北市研習_LAMP_20140815 (20) 台北市研習_LAMP_201408153. 今日課程目標
●
瞭解 Apache 設定
– 能自行建立網站
– 瞭解如何建立多個虛擬網站
– 瞭解如何維護 Apache 網站安全性設定
– 瞭解如何建立 SSL 加密網站
●
Apache + PHP 安裝與應用
●
Apache + PHP + MySQL 安裝與應用
5. Apache 的歷史
●
NCSA – 國家超級電腦應用中心於 1995 年釋出初始的
Apache httpd 版本。
●
為何稱之為 Apache ?
– "A patchy" web server of NCSA HTTPd 1.3
– 官方說法:紀念最後一支屈服於美國政府的原住民族 Apache
●
原始開發者不再維護後,由開放源碼開發者接手,不斷改進,
效能開始超越其他 Unix based web server
6. Apache 的歷史 ( 續 )
●
這些開發者形成了一個 Apache 組織,以維護 Apache 網頁伺服
器為主,並於 1999 年 7 月成立了 Apache Software
Foundation 。
●
以 Apache 伺服器為核心,他們也 動了許多與啟 Apache 並列的
計畫。逐漸的,除了網頁相關的計畫之外,也經由過濾與育成
( Incubator )機制納入了更多開源計畫。
7. Apache 基金會下的計畫
●
舉幾個例子:
– Hadoop : 應用於雲端平台上的分散式 案系統檔
– mod_perl : Apache 上的 perl 模組
– Tomcat : Java Servlet 與 JSP 容器
– Subversion (svn) : 版本控制系統
– OpenOffice : 需要多解釋 ?嗎
8. 在 Ubuntu 上安裝 Apache 2
● sudo apt-get install apache2 apache2-utils
●
安裝完畢,課程結束
9. 我們來看看安裝了些什麼
● dpkg -L apache2
– /etc/init.d/apache2
– /etc/apache2
– /var/log/apache2
– /var/www
– …...... and many, many more
12. Apache 設定
● /etc/apache2
– apache2.conf
– conf-available, conf-enabled
– mods-available, mods-enabled
– sites-available, sites-enabled
13. 主要設定 :檔 apache2.conf
●
看看裡面完整的說明
– ServerRoot
– ErrorLog, LogLevel
– IncludeOptional mods-enabled/*.load
– IncludeOptional mods-enabled/*.conf
– Include ports.conf
– IncludeOptional conf-enabled/*.conf
– IncludeOptional sites-enabled/*.conf
– <Directory> </Directory>
17. 重頭戲之一: Virtual Hosts
● Wikipedia: Virtual hosting is a method for hosting multiple
domain names (with separate handling of each name) on a
single server (or pool of servers).
●
簡單地說就是:「多個網站或 Domain 架在同一個 IP
上。」
●
分為 Named-based (名稱對應)、 IP-based (位址對應)
與 Port-based (連接埠對應)三種。
21. 建立 Virtual Host
●
編輯 /etc/apache2/sites-available/apache.taipei
<VirtualHost *:80>
ServerAdmin franklin@localhost
DocumentRoot /opt/Taipei
ServerName apache.taipei
ErrorLog ${APACHE_LOG_DIR}/tpe.error.log
</VirtualHost>
●
建立符號連結:
cd ../sites-enabled
ln -s ../sites-available/apache.taipei 001-tpe.conf
22. 建立 Virtual Host ( 續)
●
主機名稱 apache.taipei 在 DNS 要 得到查
– 目前沒有 DNS 可以用,所以我們先用 /etc/hosts
來取代:
● 127.0.1.1 apache.taipei
●
寫入 /opt/Taipei/index.html
● sudo service apache2 restart
24. 建立 Virtual Host ( 續)
●
我們來看看發生什麼事
了: /var/log/apache2/tpe.error.log
– client denied by server configuration: /opt/Taipei
●
被伺服器拒絕了!怎麼辦?
25. 建立 Virtual Host ( 續)
●
秘密在這裡: /etc/apache2/apache2.conf
<Directory />
Options FollowSymLinks
AllowOverride None
Require all denied
</Directory>
….........
●
解法有兩種
– Require all denied 改成 Require all granted
– 在 virtual host 設定中加入對目錄的權限
26. 建立 Virtual Host ( 續)
●
修改 sites-available/apache.taipei ,加入:
<Directory /opt/Taipei>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
● sudo service apache2 reload
●
再試一次看看!
27. 建立 Virtual Host ( 續 )
●
整理一下建立一個 virtual host 的重點:
– sites-enabled/<confname>.conf
●
注意,伺服器參考的不是 sites-available 而是 sites-enabled
●
還有注意附 名要是檔 .conf
– conf 裡面檔 ServerName, DocumentRoot 設定
– ServerName 的 DNS 設定
– 目錄權限設定
29. 執行 CGI
● What is CGI?
– Common Gateway Interface: 在伺服器上執行程式,輸出成網
頁,產生動態網頁內容
– 可以用任何程式語言,包括 shell script
● CGI modules
– mod_perl, mod_php, mod_python, ...
●
AddHandler: 指定 CGI 副 名檔
●
ScriptAlias: 裡面的都會當成是 CGI
34. 限制網頁的存取: Order, deny,
allow● Order: allow, deny
– 先看所有的 Allow,若至少符合一筆則 Pass
– 再看所有的 Deny,若至少符合一筆則 Reject
– 如果都不符合,則 Reject( Deny 在後)
– 如果都符合 ?呢
● Order: deny, allow
– 先看所有的 Deny,若至少符合一筆,則 ...
– 看看 Allow 是否有該筆。若有則 Pass。
– 如果都不符合,則 Pass( Allow 在後)
– 如果都符合 ?呢
35. 限制網頁的存取: Order, deny,
allow
●
試試幾個例子: <Directory /opt/Taipei>
– Order deny, allow
Deny from 192.168.0.0/24
Allow from 192.168.0.150
– Order allow, deny
Allow from 192.168.0.0/24
Deny from all
– Order deny,allow
Deny from all
Allow from 192.168.0.0/24
36. 針對方法的限制: Limit,LimitExcept
●
針對 HTTP Method 做出限制
– <Limit POST PUT DELETE>
Require valid-user
</Limit>
– <LimitExcept POST GET>
Require ip 192.168.0
</LimitExcept>
– <LimitExcept> 最好擺在 <Limit> 之前。因為 <Limit> 沒指定到的方法
就完全不受保護, <LimitExcept> 則正好相反,保護範圍較大。
38. 建立使用者認證網頁
●
第一步:建立 secret_page 目錄,放入 index.html ,並
設定為需要使用者認證:
/etc/sites-available/apache-taipei:
<Directory /opt/Taipei/secret_page>
AllowOverride AuthConfig
Order deny,allow
</Directory>
●
重新 動啟 apache : sudo service apache2 restart
39. 建立使用者認證網頁(續)
●
第二步:在目錄內放入 .htaccess 。
– 可以用 AccessFileName 來改變 名。檔
– 案內容:檔
AuthType Basic
AuthUserFile "/opt/Taipei/secret_page/.password"
AuthName "Authentication needed"
Require user franklin marcus
44. 建立 https 加密網站
●
第一步:開啟 SSL 模組
– cd mods-enabled
ln -s ../mods-available/ssl.load
ln -s ../mods-available/ssl.conf
ln -s ../mods-available/socache-shmcb.load
– 最後一行是因為 ssl.conf 中要求使用 shmcb 的
cache 機制
45. 建立 https 加密網站(續)
●
在 sites-enabled 中加入 SSL vhost
– sites-available/apache.taipei.ssl
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerAdmin … DocumentRoot … ErrorLog …
Directory …
SSLEngine on
SSLCertificateFile /etc/ssl/certs/ssl-cert-
snakeoil.pem
SSLCertificateKeyFile /etc/ssl/certs/ssl-cert-
snakeoil.key
</VirtualHost>
</IfModule>
46. 建立 https 加密網站(續)
●
看一下成果: https://apache.taipei/
●
因為憑證不是公開的 CA 所簽署,所以瀏覽器會說無法信
任。可以設定 ExceptionSite 來避開。
●
範例是使用系統自建的 snakeoil 憑證。我們也可以用
make-ssl-cert generate-default-snakeoil --force-overwrite
來取代掉,或是自己用 openssl 來建新的憑證與金鑰。
47. PHP
● sudo apt-get install php5 libapache2-mod-php5
●
我們把原來的 index.html 移除,在 /opt/Taipei 中建一個
index.php :
<html><body>
Hello world!<br>
<?php
phpinfo();
?>
</body></html>
●
http://apache.taipei/ 看看。
49. MySQL
● sudo apt-get install mysql-server libapache2-mod-auth-mysql php5-
mysql
– 輸入 root 密碼
●
安裝好以後要重啟 apache2 確保 php5-mysql 模組已載入。
●
可以再執行一次剛剛的 phpinfo() 看看有沒有 MySQL 資訊。
●
另外我們寫個簡單的網頁來測試看是否成功。