如何配置apache虚拟主机

  • windows下配置apache虚拟主机

首先确认下你的apache有没有启用虚拟主机。去掉LoadModule vhost_alias_module modules/mod_vhost_alias.so前的分号即是启用。并且确认是否包含虚拟主机的配置文件。虚拟配置文件默认是存在的,但是不启用的。可以在httpd.conf文件内搜索Virtual hosts,找到类似Include conf/extra/httpd-vhosts.conf这行(我的配置文件是这个),去掉前面的分号;就是引用虚拟主机配置文件了。

还有一点,重要的是,如果使用.htaccess做重写的话,需要加载rewrite模块,将LoadModule rewrite_module modules/mod_rewrite.so的分号去掉即可。

然后在你的virtual-host.conf的文件内添加你的配置。文件大概在confg/extra下面。不太记得了。找到就行了,呵呵……然后添加如下形式的配置信息:

<VirtualHost *:80>
   ServerAdmin webmaster@yourdomain.com
   DocumentRoot C:\your_webroot_directory
  
ServerName yourdomain.com
  
ServerAlias *.yourdomain.com
  
ErrorLog C:\your_webroot_directory \logs\error_log.txt
  
CustomLog C:\your_webroot_directory \logs\access_log.txt common
  
<Directory "C:\your_webroot_directory">
       Options Indexes FollowSymLinks Includes ExecCGI
       AllowOverride All
       Order allow,deny
       Allow from all
   </Directory>
</VirtualHost> 

以上粗体字体需要根据自己的实际情况填写即可。logs文件夹是用来记录apache的日志的。

然后,编辑hosts文件,路径:C:\WINDOWS\system32\drivers\etc\hosts。随便用什么编辑器打开,我是直接用文本打开的,在最后加入“ip地址    yourdomain”这样的格式。 如:192.168.26.32    yourdomain.com。
保存 退出。

然后测试下,在浏览器内输入yourdomain.com就可以看到你的网站了。

 

  • linux下配置apache虚拟主机

 

###################引用别人的记录-开始###################

假设VPS的IP是58.130.17.168,有两个域名指向该IP,分别是domain1.com, domain2.com,

修改/etc/httpd/conf/httpd.conf,在文件的最后加入:

ServerName58.130.17.168
 NameVirtualHost 58.130.17.168

<VirtualHost 58.130.17.168>
ServerName domain1.com
DocumentRoot /var/www/domain1.com
<Directory "/var/www/domain1.com">
Options Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
</VirtualHost>

<VirtualHost 58.130.17.168>
ServerName domain2.com
DocumentRoot /var/www/domain2.com
<Directory "/var/www/domain2.com">
Options Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>

</VirtualHost>

 

然后在/var/www/domain1.com和/var/www/domain2.com下创建不同的index.html文件:
echo "domain1">/var/www/domain1.com/index.html
echo "domain2">/var/www/domain2.com/index.html

在浏览器里输入http://domain1.comhttp://domain2.com即可看到效果了

 ###################引用别人的记录-结束###################

 

 我自己的是debian而且使用的是虚拟配置文件,参考如上的信息基本也配起来。我是在/etc/apache2/extra/vhost.conf(文件名记不清楚了,唉,记性太搓)

 

ServerName *:80
NameVirtualHost *:80
<VirtualHost *:80>
        ServerAdmin webmaster@localhost

        DocumentRoot /var/www/html/
        <Directory />
                Options FollowSymLinks
                AllowOverride All
        </Directory>
        <Directory /var/www/html/>
                Options Indexes FollowSymLinks
                AllowOverride All
                Order allow,deny
                allow from all
                # This directive allows us to have apache2's default start page
                # in /apache2-default/, but still have / go to the right place
                # RedirectMatch ^/$ /apache2-default/
        </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.log

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

        CustomLog /var/log/apache2/access.log combined
        ServerSignature On

    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>

<VirtualHost *:80>
        ServerAdmin webmaster@localhost
        ServerName naturemana.com
        #ServerAlias must write.
        ServerAlias *.naturemana.com
        DocumentRoot /var/www/html/www.naturemana.com/webroot
        <Directory />
                Options FollowSymLinks
                AllowOverride All
        </Directory>
        <Directory /var/www/html/www.naturemana.com/webroot>
                Options Indexes FollowSymLinks Includes ExecCGI
                AllowOverride All
                Order allow,deny
                allow from all
                # This directive allows us to have apache2's default start page
                # in /apache2-default/, but still have / go to the right place
                # RedirectMatch ^/$ /apache2-default/
        </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/www/html/www.naturemana.com/logs/error.log

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

        CustomLog /var/www/html/www.naturemana.com/logs/access.log common
        ServerSignature On

    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>

 

以上是我本地的配置。呵呵~~希望可以帮助到虚拟主机配置有问题的朋友

Total votes: 1462

评论

多谢!~

添加新评论