在Ubuntu中安裝apache
安裝指令:
sudo apt-get install apache2
安裝結束後:
產生的啟動和停止檔案是:/etc/init.d/apache2
啟動:
sudo apache2ctl -k start
停止:
sudo apache2ctl -k stop
重新啟動:
sudo apache2ctl -k restart
配置檔案儲存在:/etc/apache2
需要說明的是,普通的apache發行版本配置檔案是:httpd.conf
Ubuntu發行版本的主配置檔案是:apache2.conf
在apache2.conf引用到了以下檔案:
# 包含動態模組的配置:
Include /etc/apache2/mods-enabled/*.load
Include /etc/apache2/mods-enabled/*.conf
# 包含使用者自己的配置:
Include /etc/apache2/httpd.conf
# 包含埠監聽的配置:
Include /etc/apache2/ports.conf
# 包含一般性的配置語句片斷:
Include /etc/apache2/conf.d/
# 包含虛擬主機的配置指令:
Include /etc/apache2/sites-enabled/
修改httpd.conf
增加以下內容:
ServerName 127.0.0.1:80
ubuntu apache2配置
1.apache2.conf 是主配置檔案,httpd.conf 使用者配置檔案
2.虛擬目錄在 httpd.conf 中
<VirtualHost *>
DocumentRoot "路徑"
ServerName 名稱
<Directory "路徑"> allow from all Options Indexes </Directory>
</VirtualHost>
3.根設定(預設主目錄)在 /etc/apache2/sites-available/default
4.重啟命令
sudo /etc/init.d/apache2 restart或者
cd /etc/init.d
sudo apache2 -k restart
stop 停止;start 啟動5.日誌檔案在 /var/log/apache2/
<VirtualHost *:80>
ServerName www.kimoqi.com
DocumentRoot /home/vsftpd/kimoqi
</VirtualHost>
<VirtualHost *:80>
ServerName www.arwenedu.com
DocumentRoot /home/vsftpd/wangguan/webapps
</VirtualHost>
<VirtualHost *:80>
ServerName www.arwenedu.org.cn
DocumentRoot /home/vsftpd/wangguan/chem
</VirtualHost>
vi /etc/httpd/conf/httpd.conf
在Windows下,Apache的配置檔案通常只有一個,就是httpd.conf。但我在Ubuntu Linux上用apt-get install apache2命令安裝了Apache2後,竟然發現它的httpd.conf(位於/etc/apache2目錄)是空的!進而發現Ubuntu的 Apache軟體包的配置檔案並不像Windows的那樣簡單,它把各個設定項分在了不同的配置檔案中,看起來複雜,但仔細想想設計得確實很合理。
嚴格地說,Ubuntu的Apache(或者應該說Linux下的Apache?我不清楚其他發行版的apache軟體包)的配置檔案是 /etc/apache2/apache2.conf,Apache在啟動時會自動讀取這個檔案的配置資訊。而其他的一些配置檔案,如 httpd.conf等,則是通過Include指令包含進來。在apache2.conf中可以找到這些Include行:
引用
# Include module configuration:
Include /etc/apache2/mods-enabled/*.load
Include /etc/apache2/mods-enabled/*.conf
# Include all the user configurations:
Include /etc/apache2/httpd.conf
# Include ports listing
Include /etc/apache2/ports.conf
……
# Include generic snippets of statements
Include /etc/apache2/conf.d/
# Include the virtual host configurations:
Include /etc/apache2/sites-enabled/
結合註釋,可以很清楚地看出每個配置檔案的大體作用。當然,你完全可以把所有的設定放在apache2.conf或者httpd.conf或者任何一個配置檔案中。Apache2的這種劃分只是一種比較好的習慣。
安裝完Apache後的最重要的一件事就是要知道Web文件根目錄在什麼地方,對於Ubuntu而言,預設的是/var/www。怎麼知道的呢? apache2.conf裡並沒有DocumentRoot項,httpd.conf又是空的,因此肯定在其他的檔案中。經過搜尋,發現在 /etc/apache2/sites-enabled/000-default中,裡面有這樣的內容:
引用
NameVirtualHost *
<VirtualHost *>
ServerAdmin [email protected]
DocumentRoot /var/www/
……
這是設定虛擬主機的,對我來說沒什麼意義。所以我就把apache2.conf裡的Include /etc/apache2/sites-enabled/一行註釋掉了,並且在httpd.conf裡設定DocumentRoot為我的使用者目錄下的某 個目錄,這樣方便開發。
再看看/etc/apache2目錄下的東西。剛才在apache2.conf裡發現了sites-enabled目錄,而在 /etc/apache2下還有一個sites-available目錄,這裡面是放什麼的呢?其實,這裡面才是真正的配置檔案,而sites- enabled目錄存放的只是一些指向這裡的檔案的符號連結,你可以用ls /etc/apache2/sites-enabled/來證實一下。所以,如果apache上配置了多個虛擬主機,每個虛擬主機的配置檔案都放在 sites-available下,那麼對於虛擬主機的停用、啟用就非常方便了:當在sites-enabled下建立一個指向某個虛擬主機配置檔案的鏈 接時,就啟用了它;如果要關閉某個虛擬主機的話,只需刪除相應的連結即可,根本不用去改配置檔案。
mods-available、mods-enabled和上面說的sites-available、sites-enabled類似,這兩個目錄 是存放apache功能模組的配置檔案和連結的。當我用apt-get install php5安裝了PHP模組後,在這兩個目錄裡就有了php5.load、php5.conf和指向這兩個檔案的連結。這種目錄結果對於啟用、停用某個 Apache模組是非常方便的。
最後一個要說的是ports.conf,這裡面設定了Apache使用的埠。如果需要調整預設的埠設定,建議編輯這個檔案。或者你嫌它實在多 餘,也可以先把apache2.conf中的Include /etc/apache2/ports.conf一行去掉,在httpd.conf裡設定Apache埠。
ubuntu裡預設安裝的目錄結構很有一點不同。在ubuntu中module和 virtual host的配置都有兩個目錄,一個是available,一個是enabled,available目錄是存放有效的內容,但不起作用,只有用ln 連到enabled過去才可以起作用。對除錯使用都很方便,但是如果事先不知道,找起來也有點麻煩。
/etc/apache2/sites-available 裡放的是VH的配置,但不起作用,要把檔案link到 sites-enabled 目錄裡才行。
<VirtualHost *>
ServerName 域名
DocumentRoot 把rails專案裡的public當根目錄
<Directory public根目錄>
Options ExecCGI FollowSymLinks
AllowOverride all
allow from all
Order allow,deny
</Directory>
ErrorLog /var/log/apache2/error-域名log
</VirtualHost>
進一步的配置和使用,就可以查閱APACHE的手冊了
Apache配置檔案httpd.conf說明
DocumentRoot “/var/www/html” —Apache預設伺服器主目錄路徑
DirectoryIndex index.html index.htm index.php index.html.var —預設文件,多個檔案之間用空格分開
Listen 192.168.1.1:80 設定監聽ip是192.168.1.1的地址和埠為80
Listen 192.168.1.2:8080 設定監聽ip是192.168.1.2的地址和埠為8080
ServerRoot “/etc/httpd” 設定相對根目錄的路徑 ,通常是指存放配置檔案和日誌檔案的地方。預設是:/etc/httpd 一般包括conf和logs子目錄
ErrorLog logs/error_log 設定錯誤日誌 注意:如果日誌檔案存放路徑不是以“/”開頭,意味著該檔案是相對於 ServerRoot目錄
CustomLog logs/access_log combined 訪問日誌 (combined指明日誌使用的格式,還有common格式)
ServerAdmin [email protected] 設定網路管理員的Email -當客戶端伺服器發生錯誤時,伺服器通常會向客戶端返回錯誤提示頁面,為了方便解決錯誤,這個網頁中通常有管理員的Email地址,可以通過使用 ServerAdmin語句來設定管理員的EMail地址
ServerName www.iigoogle.com:80 設定伺服器主機名稱 (如果有域名可以填入域名,沒有域名則可填入伺服器IP地址)
AddDefaultCharset GB2312 設定預設字符集,定義伺服器返回給客戶機預設字符集(由於西歐UTF-8是Apache預設字符集,因此當訪問有中文的網頁時會出現亂碼,這時只要將字符集改成GB2312,再重啟Apache服務即可)
Alias /down “/software /download” 建立虛擬目錄(建立名為down的虛擬目錄,它對應的物理路徑是:/software /download)
Alias /ftp “/var/ftp” 建立虛擬目錄(建立名為ftp的虛擬目錄,它對應的物理路徑是:/var/ftp)
<Directory "/var/www/html"> 設定目錄許可權(<Directory "目錄路徑">此次寫設定目錄許可權的語句</Directory>)
Options FollowSymLinks page:116
AllowOverride None
</Directory>
基於域名的虛擬主機
NameVirtualHost 220.123.55.99 ---先用NameVirtualHost指令指定哪個IP地址負責響應對虛擬主機的請求
<VirtualHost www.iigoogle.com>
ServerName www.iigoogle.com:80
ServerAdmin [email protected]
DocumentRoot /www/docs/iigoogle
DirectoryIndex index.jsp
ErrorLog logs/www/iigoogle/error_log
CustomLog logs/www/iigoogle/access_log common
</VirtualHost>
另一種寫法
NameVirtualHost 220.123.55.99:80
<VirtualHost www.iigoogle.com:80>
ServerName www.iigoogle.com
ServerAdmin [email protected]
DocumentRoot /www/docs/iigoogle.com
ErrorLog logs/www/iigoogle/error_log
CustomLog logs/www/iigoogle/access_log common
</VirtualHost>
写评论
很抱歉,必須登入網站才能發佈留言。