53
Apache @ Ubuntu

Apache @ Ubuntu

  • Upload
    silvio

  • View
    88

  • Download
    0

Embed Size (px)

DESCRIPTION

Apache @ Ubuntu. Apache2 安裝. $ sudo apt-get install apache2 $ sudo aptitude install apache2. 關閉或重新啟動 apache. $ sudo /etc/init.d/apache2 start $ sudo /etc/init.d/apache2 stop $ sudo /etc/init.d/apache2 restart $ sudo service apache2 restart. PHP 安裝. - PowerPoint PPT Presentation

Citation preview

Page 1: Apache @ Ubuntu

Apache @ Ubuntu

Page 2: Apache @ Ubuntu

Apache2 安裝• $ sudo apt-get install apache2

• $ sudo aptitude install apache2

Page 3: Apache @ Ubuntu

關閉或重新啟動 apache

• $ sudo /etc/init.d/apache2 start

• $ sudo /etc/init.d/apache2 stop

• $ sudo /etc/init.d/apache2 restart

• $ sudo service apache2 restart

Page 4: Apache @ Ubuntu

PHP 安裝• $ sudo apt-get install php5 libapache2-mo

d-php5 php5-gd php5-mysqli

• $ sudo apt-get install php4 libapache2-mod-php4 php4-gd php4-mysqli

– $ sudo /etc/init.d/apache2 restart– $ sudo service apache2 restart

Page 5: Apache @ Ubuntu

MySQL 安裝• $ sudo apt-get install mysql-server mysql-c

lient

Page 6: Apache @ Ubuntu

修改 MySQL 密碼• $ sudo mysqladmin -u root -p password

< 輸入您要的新密碼 >

Page 7: Apache @ Ubuntu

重新啟動 MySQL

• $ sudo /etc/init.d/mysql restart – sudo service mysql restart

Page 8: Apache @ Ubuntu

Apache2 設定• Debian 裡面的 Apache2 和其他發行版的

管理方式有點不同– 在 Debian 的設定檔不再是傳統的 httpd.conf ,

而是放在 apache2.conf ,並且把很多模組設定或者虛擬主機設定拆開來放到各自的目錄。

– 這些獨立出來的設定檔又可以透過 Debian 專屬的工具程式啟用或關閉

• Ubuntu 也繼承到 Debian 的這個特性

Page 9: Apache @ Ubuntu

Apache2 設定

apache2.conf以下投影片內容主要參考 …1. 鳥哥的 Linux 私房菜: http://linux.vbird.org/linux_server/0360apache.php2. http://xuxueliang.blog.51cto.com/5576502/971093

Page 10: Apache @ Ubuntu

apache2.conf 重要設定• ServerRoot "/etc/apache2"

– Apache Server 設定檔放置處

Page 11: Apache @ Ubuntu

apache2.conf 重要設定• KeepAlive On / Off

– 是否允許持續性的連線,亦即一個 TCP 連線可以具有多個檔案資料傳送的要求。

–原則上 …• 如果你的網頁內含很多圖檔,那麼這一次連線就會

將所有的資料送完,而不必每個圖檔都需要進行一次 TCP 連線 改為 On 較佳

• 如果你的網站多為動態網頁 (e.g., 內容為資料庫存取 ) 改為 Off 較佳

Page 12: Apache @ Ubuntu

Refresh 一次, GET 了無數東東 …

Page 13: Apache @ Ubuntu

112/04/21 13

Skype, Line

Page 14: Apache @ Ubuntu

112/04/21 14

In DOS command 視窗輸入: netstat -n

Page 15: Apache @ Ubuntu

112/04/21 15

TCP connection establishmentestablishing connection-oriented

connection(three-way handshake)

Page 16: Apache @ Ubuntu

apache2.conf 重要設定• Timeout 300

– 不論接收或傳送,當持續連線等待超過 300 秒則該次連線就中斷

– 一般來說,此數值在 300 秒左右即可,不需要修改這個原始值 by 鳥哥

• But, 300 is still far more than necessary in most situations. It is not set any lower by default because there may still be odd places in the code where the timer is not reset when a packet is sent.(http://users.cis.fiu.edu/~downeyt/cgs4854/timeout)

Page 17: Apache @ Ubuntu

apache2.conf 重要設定• 打開 KeepAlive 後,意味著每次使用者完成全部訪

問後,都要保持一定時間後才會關閉 TCP 連接,那麼在關閉連接之前,必然會有一個 Apache 程序 (process) 對應於該使用者而不能處理其他用戶

• 假設 KeepAlive 的時間為 100 秒,伺服器每秒處理 50 個獨立使用者訪問,那麼系統 中 Apache 的總程序數就是 100* 50 = 5000 個,如果一個程序佔用 4M 記憶體,那麼總共會消耗 20G 記憶體,所以可以看出,在這種配置中,相當消耗記憶體,但好處是系統只處理了 50 次 TCP 的握手和關閉操作。

Page 18: Apache @ Ubuntu

apache2.conf 重要設定• 如果關閉 KeepAlive ,如果還是每秒 50

個用戶訪問,如果用戶每次連續的請求數為 3 個,那麼 Apache 的總程序數就是 50 * 3= 150 個,如果還是每個程序佔用 4M 記憶體,那麼總的記憶體消耗為 600 M ,這種配置能節省大量記憶體

• 但是,系統處理了 150 次 TCP 的握手和關閉的操作,因此又會多消耗一些 CPU 資源。

Page 19: Apache @ Ubuntu

apache2.conf 重要設定• MaxKeepAliveRequests 100

– MaxKeepAliveRequests 是每次 Client 端建立連線後,可以要求最大的檔案數,設成 0 為不限制,或者可以把它設高一點,尤其是在網頁很複雜的時候, 效能會有明顯改善

– 為了增進效能則可以改大一點, e.g. 500

Page 20: Apache @ Ubuntu

apache2.conf 重要設定

<IfModule prefork.c> StartServers 5 MinSpareServers 5 MaxSpareServers 10 MaxClients 150 MaxRequestsPerChild 0 </IfModule>

Page 21: Apache @ Ubuntu

What is “prefork”?

• mpm-worker– 數個 child processes ,每個 processes 都會再有數

個 threads– 很多第三方的套件沒有 thread safe ,也就是沒有考慮

到 multi-thread 的問題 不穩定!• mpm-prefork

– 最傳統的模式,運作時會有數個 child processes ,每個 processes 都只跑一個 thread

– 效能較差且耗費比較多記憶體,穩定性比較好! • etc

Page 22: Apache @ Ubuntu

apache2.conf 重要設定• StartServers 5

– # StartServers 為 Apache2 啟動時,會啟用幾個程序 (processes) ,可以透過 ps -aux 來驗證。若您的網站流量很大,可以改大一點,但是若用量沒有那麼大,會浪費系統資源。   

Page 23: Apache @ Ubuntu

apache2.conf 重要設定• MinSpareServers 5

• MaxSpareServers 10– 最小 & 最大的預備使用的程序 (processes)

數量。   

Page 24: Apache @ Ubuntu

apache2.conf 重要設定• MaxClients 150

– 最大的同時連線數量,也就是程序 (processes) 不會超過此一數量,可以視您網站的規模修改

Page 25: Apache @ Ubuntu

apache2.conf 重要設定• MaxRequestsPerChild 0 無限制

– 每個程序能夠提供的最大傳輸次數要求。 –舉例來說,如果有個使用者連上伺服器後 ( 一

個 process) ,卻要求數百個網頁,當他的要求數量超過此一數值, 則該程序會被丟棄,另外切換一個新程序。

– 這個設定可以有效的控管每個 process 在系統上的『存活時間』。 因為根據觀察所得,新程序的效能較佳

Page 26: Apache @ Ubuntu

用白話文說 …

<IfModule prefork.c> StartServers 5 MinSpareServers 5 MaxSpareServers 10 MaxClients 150 MaxRequestsPerChild 0 </IfModule>

假設有 10 個人連上來,左邊這個設定代表 …1. apache 此時的程序數應有 15-20 個2. 而這個最終程序數不可超過 150 個

Page 27: Apache @ Ubuntu

$ top

Page 28: Apache @ Ubuntu

apache2.conf 重要設定(sites-enable)

• DocumentRoot /www/htdocs – 網頁放置的根目錄

• ServerName www.im.ncue.edu.tw– 主機所傾聽 (listen) 的網域

Page 29: Apache @ Ubuntu

apache2.conf 重要設定(mods-enable)

• DirectoryIndex index.html index.cgi index.pl index.php index.xhtml –若沒有明確指定網頁檔名, Apache2 會依序

以下順序來嘗試送給 client 端瀏覽器– In mods-enabled

• dir.conf

Page 30: Apache @ Ubuntu

apache2.conf 重要設定(/etc/apache2/envvars)

• User www-data (${APACHE_RUN_USER})

• Group www-data (${APACHE_RUN_GROUP})– Apache2 Server 執行時所用的帳號和群組,若沒有特別的理由可以不用更動

• Now in /etc/apache2/envvars

Page 31: Apache @ Ubuntu

apache2.conf 重要設定(in conf.d/charset)

• #AddDefaultCharset Big5 AddCharset utf-8 .utf8 AddCharset big5 .big5 .b5 – AddDefaultCharset 千萬不要加上去,這個方式會在表頭就送出說這個網頁是用什麼語言編碼,但是若您的網頁有 big5 和 utf-8 混用的話,就會造成錯誤!

– 正確的作法是在網頁 html 內都指定編碼,如 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

– 後面那些 AddCharset 是指可以支援的編碼,預設都設好了,不要改動。

Page 32: Apache @ Ubuntu

apache2.conf 重要設定• AccessFileName .htaccess

– The name of the file to look for in each directory for additional configuration directives

– Ref:http://sparc.nfu.edu.tw/teacher/htaccess/index.htm

Page 33: Apache @ Ubuntu

apache2.conf 重要設定• Include …

Page 34: Apache @ Ubuntu

Include …

• Include conf.d/

• Include sites-enabled/

Page 35: Apache @ Ubuntu

httpd.conf

• empty

Page 36: Apache @ Ubuntu

ports.conf

Page 37: Apache @ Ubuntu

Directory conf.d

Page 38: Apache @ Ubuntu

mods-enabled

Page 39: Apache @ Ubuntu

mods-available

Page 40: Apache @ Ubuntu

Add / Remove module

$ a2enmod userdir

$ a2dismod userdir

(Anytime you change some configuration, do remember to restart apache…)

Page 41: Apache @ Ubuntu

新增模組 - 1( 以啟用 “個人網頁” 為例 )

• What is “ 個人網頁”?– Recall … you create a sub-directory “www” un

der your home directory, put your web files there.

– there you go …http://120.107.152.251/~tkwu/

Page 42: Apache @ Ubuntu

新增模組 - 2( 以啟用 “個人網頁” 為例 )

• 在非 Debian and Ubuntu 系統, it’s …– UserDir www

^^^^^預設為 public_html

• But ,在 Ubuntu 裡,使用者個人網頁獨立到一個額外的模組和設定檔

Page 43: Apache @ Ubuntu

新增模組 - 3( 以啟用 “個人網頁” 為例 )

• 啟用個人網頁$ a2enmod userdir $ /etc/init.d/apache2 restart

$ service apache2 restart

• 關閉個人網頁: $ a2dismod userdir

$ /etc/init.d/apache2 restart $ service apache2 restart

Page 44: Apache @ Ubuntu

新增模組 - 4( 以啟用 “個人網頁” 為例 )

• 個人網頁設定檔 /etc/apache2/mods-available/userdir.conf

Page 45: Apache @ Ubuntu

虛擬主機

• 一主機多網站– IP-based– Name-based

• 需搭配 DNS

– Port-basedPort-based

Page 46: Apache @ Ubuntu

IP-based

Page 47: Apache @ Ubuntu

Name-based

Page 48: Apache @ Ubuntu

Port-based

Page 49: Apache @ Ubuntu

Name-based 虛擬主機設定 - 1( 以新增 “ tkwu.im.ncue.edu.tw” 為例 )

• Copy 範本– $ cd /etc/apache2/sites-available – $ cp default tkwu

Page 50: Apache @ Ubuntu

Name-based 虛擬主機設定( 以新增 “ tkwu.im.ncue.edu.tw” 為例 )

• 編輯新的設定檔 (tkwu)

Page 51: Apache @ Ubuntu

Name-based 虛擬主機設定( 以新增 “ tkwu.im.ncue.edu.tw” 為例 )

• 啟用新的虛擬主機站台– $ a2ensite tkwu

$ /etc/init.d/apache2 restart

• Done !– Well, not yet! You need to configure your DNS

server appropriately…

Page 52: Apache @ Ubuntu

112/04/21 52

Hierarchy of DNS Servers

Page 53: Apache @ Ubuntu

112/04/21 53