在ubuntu下,apache的設定檔的路徑為/etc/apache2。在此目錄下,可以看到http.conf,但是檔案確是空的。在ubuntu下,它把apache的配置發散到了各個設定檔中,此目錄下的apache2.conf檔案,這是個總體設定檔,在此檔案中,可以看到Include命令,它把很多其他檔案包含進來,其中包括http.conf。因此在http.conf寫配置的話,也是生效的。
下面舉例說明多虛擬機器主機的配置。我有兩台機器,一台windows,ip:192.168.1.105, 一台虛擬機器ubuntu,ip:192.168.1.115,現在有兩個網站運行在ubuntu上做開發,名稱分別為phptest和dbmanage,如果通過windows去訪問呢?
在apache2.conf的檔案中,可以看到
其實,sites-enabled是對sites-available的映射,因此我們在sites-available目錄下配置就行。在sites-available下複製一份default檔案,命名為phptest,然後更改phptest檔案就行,phptest檔案如下:
1 <VirtualHost *:80> 2 ServerAdmin webmaster@localhost 3 ServerName phptest 4 ServerAlias www.phptest.com 5 DocumentRoot /home/dingjing/web/www 6 <Directory /> 7 Options FollowSymLinks 8 AllowOverride All 9 </Directory>10 <Directory /home/dingjing/web/www>11 Options Indexes FollowSymLinks MultiViews12 AllowOverride All13 Order allow,deny14 allow from all15 </Directory>16 17 ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/18 <Directory "/usr/lib/cgi-bin">19 AllowOverride None20 Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch21 Order allow,deny22 Allow from all23 </Directory>24 25 ErrorLog ${APACHE_LOG_DIR}/error.log26 27 # Possible values include: debug, info, notice, warn, error, crit,28 # alert, emerg.29 LogLevel warn30 31 CustomLog ${APACHE_LOG_DIR}/access.log combined32 <IfModule mod_rewrite.c>33 RewriteEngine on34 RewriteCond %{REQUEST_FILENAME} !-f35 RewriteCond %{REQUEST_FILENAME} !-d36 RewriteRule !\.(js|ico|gif|jpg|png|css|xml|swf|cur)$ /index.php [NC]37 </IfModule>38 39 Alias /doc/ "/usr/share/doc/"40 <Directory "/usr/share/doc/">41 Options Indexes MultiViews FollowSymLinks42 AllowOverride All43 Order deny,allow44 Deny from all45 Allow from 127.0.0.0/255.0.0.0 ::1/12846 </Directory>47 48 </VirtualHost>
其中,ServerName是伺服器名稱,ServerAlias是別名,DocumentRoot配置的是網站的檔案目錄。在此設定檔中,我們開啟了重寫規則,見32-37行的配置。
在此說明的是,基於名稱的虛擬機器主機要配置:NameVirtualHost *:80,可以把此語句寫在/etc/apache2/ports.conf檔案中。同樣方法,可以建立dbmanage檔案,在此不再說明。
此時,檔案配置完成,但是新增加的檔案沒有映射到sites-enable目錄,增加映射需要執行:
此時,sites-enable目錄下就有相應的設定檔了,同樣步驟操作增加dbmanage檔案。
然後在windows中,配置hosts:
這樣,在windows中就可以訪問這兩個網站了。