1.
修改
httpd.conf
文件
# vi /usr/local/apache/conf/httpd.conf
1) 設置根目錄的路徑
根目錄是指Apache存放配置文件和日誌文件的目錄,配置參數爲ServerRoot,默認位於「/usr/local/apache」。命令以下:
ServerRoot /usr/local/apache
2) 設置監聽IP地址及端口號
默認偵聽本機全部IP地址的TCP80端口,命令以下:
Listen 80
用戶也能夠按本身的需求,使用多個Listen語句在多個地址和端口上偵聽客戶端請求。好比:
Listen 192.168.99.9:80
Listen 172.16.0.20:8080
3) 設置系統管理員E-mail
使用ServerAdmin參數設置管理員E-mail,好比管理員的Email地址爲root@guoxuemin.cn:
ServerAdmin root@guoxuemin.cn
4) 設置服務器主機的名稱
參數ServerName用來設置服務器的主機名稱,若是沒有域名則填入服務器的IP地址,好比服務器的IP地址爲192.168.99.9:80
ServerName 192.168.99.9:80
5) 設置主目錄的路徑
用戶可使用參數DocumentRoot配置服務器主目錄默認路徑,好比,主目錄路徑爲:
DocumentRoot /usr/local/apache/htdocs
6) 設置默認文件
Apache的默認文件名爲index.html,可使用Directory Index參數來配置,好比,將index.php設置爲默認文件名:
<IfModulf dir_moudle>
Directory Index index.html
</IfModulf>
7)測試:
2.
配置目錄權限
使用<Directory 目錄路徑>和</Directory>設置目錄的權限。好比:
<Directory 「/var/www/icons」>
Options Indexes MultiViews
AllowOverride None
Order allow,deny
Allow from all
</Directory>
說明:
1)定義目錄特性選項Options
可選參數:
Indexes:該特性代表目錄容許「目錄瀏覽」;
MultiViews:該特性代表目錄容許內容協商的多重試圖;
All:包含了除MultiViews外的全部特性;
ExecCGI:該特性代表容許在該目錄下執行CGI腳本;
FollowSymLinks:該特性代表容許在該目錄下使用符號鏈接。
2).htaccess文件
能夠經過.htaccess文件(訪問控制文件)設置目錄的權限。
AccessFileName .htaccess
配置參數AllowOverride指定目錄的.htaccess文件中指令的類型,包括All、None與Options、FileInfo、AuthConfig、Limit的任意組合。通常將AllowOverride設置爲「None」,禁止使用.htaccess文件,當AllowOverride參數爲All時,.htaccess文件能夠覆蓋任何之前的配置。
3)設置訪問控制
使用Order選項來定義訪問權限。
好比如下語句代表容許全部客戶機的訪問:
Order allow,deny
Allow from all
如下語句代表只容許網段192.168.99.0/24的客戶機訪問,但IP地址爲192.168.99.254這個客戶機除外:
Order allow,deny
Allow from 192.168.99.0/24
Deny from 192.168.99.254
用戶能夠根據須要,按上述方法配置本身的目錄權限。
3.
建立虛擬目錄
使用Alias選項建立虛擬目錄,好比,創建「/icons/」這個虛擬目錄,其對應的物理路徑爲「/var/www/icons/」:
Alias /icons/ 「/var/www/icons/」
4.
用戶認證
好比,有一個名爲myweb的虛擬目錄,其對應的物理路徑是「/usr/local/myweb」,現對其啓用用戶認證功能,只容許用戶Tonyguo和Wayne訪問。
1)創建虛擬目錄並設置用戶認證:
2) 創建口令文件併爲用戶設置口令
-c選項表示不管口令文件是否已經存在,都會從新寫入文件並刪除原內容。因此第二個用戶wayne不須要使用-c選項。
3)測試
輸入用戶名和密碼後就能夠訪問網站了:
3、配置虛擬主機
1.
配置基於
IP
的虛擬主機
1)IP地址相同,但端口號不一樣的虛擬主機配置
好比使用192.168.99.9的兩個不一樣端口80和8080發佈兩個不一樣站點, 虛擬主機分別對應的目錄爲/usr/local/apache/htdocs/web1和/usr/local/apache/htdocs/web2:
Listen 80
Listen 8080
<VirtualHost 192.168.99.9:80>
ServerSignature email
DocumentRoot /usr/local/apache/htdocs/web1
DirectoryIndex index.html index.htm
LogLevel warm
HostNameLookups off
</VirtualHost>
<VirtualHost 192.168.99.9:8080>
ServerSignature email
DocumentRoot /usr/local/apache/htdocs/web2
DirectoryIndex index.html index.htm
LogLevel warm
HostNameLookups off
</VirtualHost>
2)配置基於域名的虛擬主機
好比服務器有兩個IP地址192.168.99.9和192.168.99.10,使用這兩個IP建立兩臺虛擬主機,虛擬主機分別對應的目錄爲/usr/local/apache/htdocs/web1和/usr/local/apache/htdocs/web2。設置方法以下:
<VirtualHost 192.168.99.9>
ServerName 192.168.99.9:80
DocumentRoot /usr/local/apache/htdocs/web1
DirectoryIndex index.html index.htm
</VirtualHost>
<VirtualHost 192.168.99.10>
ServerName 192.168.99.10:80
DocumentRoot /usr/local/apache/htdocs/web2
DirectoryIndex index.html index.htm
</VirtualHost>
2.
配置基於域名的虛擬主機
好比有兩個域名guoxuemin.cn和tonyguo.com須要使用同一臺服務器192.168.99.9,那麼能夠這樣配置:
NameVirtualHost 192.168.99.9
<VirtualHost www.guoxuemin.cn>
ServerAdmin admin@guoxuemin.cn
DocumentRoot /usr/local/apache/htdocs/web1
DirectoryIndex index.html index.htm
ErrorLog logs/web1/error_log
Customlog logs/web1/access_log combined
</VirtualHost>
<VirtualHost www.tonyguo.com>
ServerAdmin admin@tonyguo.com
DocumentRoot /usr/local/apache/htdocs/web2
DirectoryIndex index.html index.htm
ErrorLog logs/web1/error_log
Customlog logs/web1/access_log combined
</VirtualHost>