Apache安裝配置管理
一、安裝Apache
下載地址:http://httpd.apache.org/
1. 安裝Apache
# tar zxvf httpd-2.2.11.tar.gz
# cd httpd-2.2.11
# ./configure --prefix=/usr/local/apache --enable-so
//編譯時加上加載模塊參數--enable-so
# make
# make install
2. 配置系統啟動時自動啟動Apache服務。
# vi /etc/rc.d/rc.local
//在rc.local上加入一行/usr/local/apache/bin/apachectl –k start。
二、配置Apache
1. 修改httpd.conf文件
# vi /usr/local/apache/conf/httpd.conf
1) 設置根目錄的路徑
根目錄是指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
4) 設置服務器主機的名稱
參數ServerName用來設置服務器的主機名稱,如果沒有域名則填入服務器的IP地址,比如服務器的IP地址為192.168.99.9:
5) 設置主目錄的路徑
用戶可以使用參數DocumentRoot配置服務器主目錄默認路徑,比如,主目錄路徑為:/usr/local/apache2/htdocs
6) 設置默認文件
Apache的默認文件名為index.html,可以使用Directory Index參數來配置,比如,將index.php設置為默認文件名:index.php index.html
7)測試:
打開瀏覽器,輸入地址:http://192.168.99.9,可以打開站點了:
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)建立虛擬目錄并設置用戶認證:
Alias /myweb/ “/usr/local/myweb/”
<Directory “/usr/local/myweb/”>
Options none
AllowOverride None
Order allow,deny
Allow from all
AuthType Basic
AuthName “Please Login: ”
AuthUserFile/usr/local/apache/bin/mywebpwd
Require User Tongguo wayne
</Directory>
2) 建立口令文件并為用戶設置口令
/usr/local/apache/bin/htpasswd –c /usr/local/apache/bin/mywebpwd Tonyguo
-c選項表示無論口令文件是否已經存在,都會重新寫入文件并刪除原內容。所以第二個用戶wayne不需要使用-c選項
3)測試
在瀏覽器中輸入:http://192.168.99.9/myweb,可以看到如下對話框:
輸入用戶名和密碼后就可以訪問網站了:
三、配置虛擬主機
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不同的虛擬主機配置
比如服務器有兩個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> ServerName www.guoxuemin.cn:80 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> ServerName www.tonyguo.com:80 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> <VirtualHost *:8088> serverAdmin new@student.com DocumentRoot "/web/web1" <Directory /web/web1> Options FollowSymlinks AllowOverride None Order Allow,Deny Allow from all </Directory> DirectoryIndex index.html index.php index.htm </VirtualHost> <VirtualHost *:8089> serverAdmin new@student.com DocumentRoot "/web/web2" <Directory /web/web2> Options FollowSymlinks AllowOverride None Order Allow,Deny Allow from all </Directory> DirectoryIndex index.html index.php index.htm </VirtualHost> <VirtualHost 192.168.88.144:80> serverAdmin new@student.com DocumentRoot "/web/web3" <Directory /web/web3> Options FollowSymlinks AllowOverride None Order Allow,Deny Allow from all </Directory> DirectoryIndex index.html index.php index.htm </VirtualHost> <VirtualHost 192.168.88.145:80> serverAdmin new@student.com DocumentRoot "/web/web4" <Directory /web/web4> Options FollowSymlinks AllowOverride None Order Allow,Deny Allow from all </Directory> DirectoryIndex index.html index.php index.htm </VirtualHost> <VirtualHost ftp.com> ServerName ftp.com:80 DocumentRoot /web/ftp <Directory /web/ftp> Options FollowSymlinks AllowOverride None Order Allow,Deny Allow from all </Directory> DirectoryIndex index.html index.php index.htm </VirtualHost> <VirtualHost mail.com> ServerName mail.com:80 DocumentRoot /web/mail <Directory /web/mail> Options FollowSymlinks AllowOverride None Order Allow,Deny Allow from all </Directory> DirectoryIndex index.html index.php index.htm </VirtualHost> |
負載均衡
#訪問test目錄時負載均衡
在modules目錄下:導入mod
/usr/local/apache2/bin/apxs -c -i mod_proxy.c proxy_util.c /usr/local/apache2/bin/apxs -c -i mod_proxy_balancer.c /usr/local/apache2/bin/apxs -c -i mod_proxy_http.c vi http.conf LoadModule proxy_module modules/mod_proxy.so LoadModule proxy_balancer_module modules/mod_proxy_balancer.so LoadModule proxy_http_module modules/mod_proxy_http.so ProxyRequests Off <Proxy balancer://clusterphpinfo> BalancerMember http://192.168.88.134:8089 loadfactor=5 BalancerMember http://192.168.88.134:8088 loadfactor=1 #weight ProxySet lbmethod=bytraffic </Proxy> ProxyPass /test balancer://clusterphpinfo stickysession=STICK_PORT_TOKEN nofailover=On ProxyPassReverse /test balancer://clusterphpinfo <Location /balancer-manager> SetHandler balancer-manager Order Deny,Allow Allow from all #Allow from 192.168.88.* </Location> |
posted on 2014-08-12 09:31 順其自然EVO 閱讀(318) 評論(0) 編輯 收藏 所屬分類: 測試學習專欄 、web 前端性能測試