linux下爲apache配置虛擬主機

linux下爲apache配置虛擬主機

區別於Windows 下Apache,配置文件通常只有一個,就是httpd.conf。

本機環境是通過 apt-get install xxx

Linux下 Apache的配置文件是 /etc/apache2/apache2.conf,Apache在啓動時會自動讀取這個文件的配置信息。而其他的一些配置文件,如 httpd.conf等,則是通過Include指令包含進來。

在apache2.conf裏有sites-enabled目錄,而在 /etc/apache2下還有一個sites-available目錄,其實,這裏面纔是真正的配置文件,而sites- enabled目錄存放的只是一些指向這裏的文件的符號鏈接,你可以用ls /etc/apache2/sites-enabled/來證實一下。

所以,如果apache上配置了多個虛擬主機,每個虛擬主機的配置文件都放在 sites-available下,那麼對於虛擬主機的停用、啓用就非常方便了:當在sites-enabled下建立一個指向某個虛擬主機配置文件的鏈 接時,就啓用了它;如果要關閉某個虛擬主機的話,只需刪除相應的鏈接即可,根本不用去改配置文件。

1. sudo cp /etc/apache2/sites-avaliable/000-default.conf , 命名爲 test.conf

2.修改配置文件:test.conf

<VirtualHost *:80>

# The ServerName directive sets the request scheme, hostname and port that

# the server uses to identify itself. This is used when creating

# redirection URLs. In the context of virtual hosts, the ServerName

# specifies what hostname must appear in the request's Host: header to

# match this virtual host. For the default virtual host (this file) this

# value is not decisive as it is used as a last resort host regardless.

# However, you must set it for any further virtual host explicitly.

ServerName www.test.com

ServerAdmin webmaster@localhost

DocumentRoot /var/www/html/test/

ErrorLog /var/www/html/test/error.log

CustomLog /var/www/html/test/access.log combined

<Directory "/var/www/html/test">

    Options FollowSymLinks

    DirectoryIndex index.php index.html index.htm

    AllowOverride All #注意這個地方的配置,會影響本地目錄下的.htaccess的啓用

    Order deny,allow

    Allow from All

</Directory>

# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,

# error, crit, alert, emerg.

# It is also possible to configure the loglevel for particular

# modules, e.g.

#LogLevel info ssl:warn

# For most configuration files from conf-available/, which are

# enabled or disabled at a global level, it is possible to

# include a line for only one particular virtual host. For example the

# following line enables the CGI configuration for this host only

# after it has been globally disabled with "a2disconf".

#Include conf-available/serve-cgi-bin.conf

</VirtualHost>

  

3. 建立鏈接文件:

sudo ln -s /etc/apache2/sites-available/test.conf /etc/apache2/sites-enabled/test.conf

或者: sudo a2ensite test.conf

4.重啓apache 服務器

sudo /etc/init.d/apache2 restart

5. 修改hosts(/etc/hosts)

添加127.0.0.1 www.test.com

到這裏基本就可正常訪問了!

附:如果這裏還需要對目錄級的URL重寫支持,繼續往下:

1. 終端運行

sudo a2enmod

程序提示可供激活的模塊名稱,輸入:rewrite

成功會提示 rewrite already load

 2. 修改/etc/apache2/sites-enabled/test.conf (該鏈接指向的是站點配置文件)
把下的AllowOverride 屬性改爲All,保存。(上面我們已經配置爲 All)

 3. 重新加載apache

sudo /etc/init.d/apache2 restart
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章