基于名字的(通过域名来区分)的虚拟主机

基于端口的虚拟主机

基于IP的虚拟主机

 

 

基于端口的虚拟主机

 

1、建立虚拟主机使用的目录、设置好权限

2、修改/etc/apache2/ports.conf,增加需要监听的端口

ports.conf:

NameVirtualHost *:80 
Listen 80 
Listen 8080 

3、到/etc/apache2/sites-enabled目录下,拷贝一份000-default命名为mysite,使用文本编辑器打开mysite,修改端口、DocumentRoot和Directory。最好也修改一下日志文件,方便查看。详见下边的例子,红色为需要修改的地方。

mysite:

<VirtualHost *:8080
        ServerAdmin webmaster@localhost

       DocumentRoot /var/www/mysite 
        <Directory /> 
                Options FollowSymLinks 
                AllowOverride None 
        </Directory> 
       <Directory /var/www/mysite/> 
                Options Indexes FollowSymLinks MultiViews 
                AllowOverride None 
                Order allow,deny 
                allow from all 
        </Directory>

        ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/ 
        <Directory "/usr/lib/cgi-bin"> 
                AllowOverride None 
                Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch 
                Order allow,deny 
                Allow from all 
        </Directory>

        ErrorLog /var/log/apache2/error_mysite.log

        # Possible values include: debug, info, notice, warn, error, crit, 
        # alert, emerg. 
        LogLevel warn

        CustomLog /var/log/apache2/access_mysite.log combined

    Alias /doc/ "/usr/share/doc/" 
    <Directory "/usr/share/doc/"> 
        Options Indexes MultiViews FollowSymLinks 
        AllowOverride None 
        Order deny,allow 
        Deny from all 
        Allow from 127.0.0.0/255.0.0.0 ::1/128 
    </Directory>

</VirtualHost>

4、重启apache服务

sudo /etc/init.d/apache2 restart