example.com本地机器上的VirtualHost

时间:2012-09-13 03:01:36

标签: apache web virtualhost

我要做的是:将网站(example.com)添加到apache2/sites-available(我已指定其文档根目录)然后启用它并在该访问之后它在我的本地机器上(在浏览器中为example.com)。我有以下几个问题:

  1. 是否可以创建网站并为其指定特定域名(example.com)?
  2. 为了创建网站,提供域名并仅在我的本地计算机上访问它,我需要遵循哪些步骤?
  3. 注意:我有网站的内容。我想创建一个新网站,我不想更改Apache的默认网站(即apache2/sites-available/default)。

    更新

    我在重启Apache时遇到错误(我已按照描述添加了VirtualHost

    ERROR

    CustomLog takes two or three arguments, a file name, a custom log format string or format name, and an optional "env=" clause (see docs)
    Action 'configtest' failed.
    The Apache error log may have more information.
       ...fail!
    

    我的网站的文件

    <VirtualHost *:80>
    
            ServerAdmin webmaster@localhost
            ServerName dev.subhransu.com
    
            ScriptAlias /private /home/hg/repositories/private/hgweb.cgi
            <Directory /home/hg/repositories/private/>
                    Options ExecCGI FollowSymlinks
                    AddHandler cgi-script .cgi
                    DirectoryIndex hgweb.cgi
                    AuthType Basic
                    AuthName "Mercurial repositories"
                    AuthUserFile /home/hg/tools/hgusers
                    Require valid-user
            </Directory>
    
            ErrorLog ${APACHE_LOG_DIR}/dev.subhransu.com_error.log
    
            # Possible values include: debug, info, notice, warn, error, cr$
            # alert, emerg.
            LogLevel warn
    
            CustomLog ${APACHE_LOG_DIR}/dev.subhransu.com_ssl_access.lo$
    
            SSLEngine on
            SSLCertificateFile "/etc/apache2/ssl/dev.subhransu.com.crt"
            SSLCertificateKeyFile "/etc/apache2/ssl/dev.subhransu.com.k$
    
    </VirtualHost>
    

    httpd.conf文件

    <VirtualHost *:80>
    ServerName  dev.subhransu.com
    ServerAlias www.dev.subhransu.com
    </VirtualHost>
    

2 个答案:

答案 0 :(得分:1)

httpd.conf(或vhosts)中,ServerNameServerAlias设置是Apache将响应的域名

ServerName example.com
ServerAlias www.example.com

然后只需编辑您的主机文件,将example.com指向127.0.0.1

答案 1 :(得分:1)

我认为您可能正在寻找的功能是虚拟主机。使用vhosts,您可以创建任意数量的站点,每个站点都独立于另一个站点。对于每个vhost,您可以指定“ServerName”指令,该指令是您的域,可以是您想要的任何域。然后在您的计算机主机文件中,您可以将该域的所有呼叫路由到localhost。

如果您只需要设置一个站点,只需编辑默认的apache配置,单独保留vhost,然后如前所述更新计算机上的hosts文件。

参考文献: vhost examples How to edit your hosts file

相关问题