【问题标题】:Apache: Virtual Host configurationApache:虚拟主机配置
【发布时间】:2011-05-04 06:39:41
【问题描述】:

当我尝试在 apache 中配置我的虚拟主机时。我放了这样的东西,

NameVirtualHost *:80

<VirtualHost *:80>
   DocumentRoot /xampp/htdocs/gift
   ServerName gift.loc  
</VirtualHost>

在我的主机文件中我放了这样的东西,

127.0.0.1       localhost
127.0.0.1       gift.loc

我在浏览器上运行它,

http://gift.loc - is fine

但是当我尝试使用这个时,

http://localhost/othersite - can't found

我错过了一些配置吗?任何想法...

提前致谢,

【问题讨论】:

  • 感谢您的建议,我找到了解决方案。我在定义虚拟主机块上方放置了一些默认目录: DocumentRoot /xampp/htdocs 这将呈现所有与定义的虚拟主机不匹配的请求。
  • 如果您想查看,我写了这个答案:stackoverflow.com/a/56535519/3980729

标签: apache virtual host


【解决方案1】:

docs 看来,我们需要为您想要服务的每个不同主机创建一个块

在同一个文档中,如果您要向现有的 Web 服务器添加虚拟主机,您还必须为现有的主机创建一个块。

【讨论】:

  • 很高兴知道,但我希望gift.loc由虚拟主机提供服务,而“其他站点”通过“本地主机/其他站点”语法运行。
【解决方案2】:

您需要为您希望 apache 处理的每个主机创建一个 VirtualHost 条目。如果没有其他 VirtualHosts 匹配请求,配置文件中的第一个将用作默认值。

例如,如果我们有:

<VirtualHost *:80>
   DocumentRoot /xampp/htdocs/gift
   ServerName gift.loc  
</VirtualHost>

<VirtualHost *:80>
   DocumentRoot /example/htdocs/gift
   ServerName example.com  
</VirtualHost>

对 foobar.org 的请求将由 gift.loc 虚拟主机处理。

【讨论】:

    【解决方案3】:

    你需要把 localhost 放在 vhosts.conf 中

        NameVirtualHost *:80
    
        <VirtualHost *:80>
           DocumentRoot /xampp/htdocs/
           ServerName localhost
        </VirtualHost>
    
        <VirtualHost *:80>
           DocumentRoot /xampp/htdocs/gift
           ServerName gift.loc  
        </VirtualHost>
    

    这很好用(确保你重新启动 apache)。如果您需要检查您的配置,您可以(至少在 linux 上)运行 httpd -S。

    【讨论】:

      【解决方案4】:

      您需要遵循几个步骤来设置 ubuntu 上的虚拟主机: 假设您的项目文件夹名称是 myProject

      第 1 步:将文件夹放在 /var/www/html 中

      sudo mv ~/myProject /var/www/html/
      

      第 2 步:将项目文件夹的所有权授予 www-data

      sudo chown -R www-data:www-data /var/www/html/myProject
      

      第 3 步:在可用站点中创建新站点:

      cd /etc/apache2/sites-available/ 
      ls
      

      在这里您将看到现有的 000-default.conf 和 default-ssl.conf 。将这两个文件的内容复制到一个文件中并替换您的文件夹名称或 将此复制到名为 myProject.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.example.com
      
          ServerAdmin webmaster@localhost
          DocumentRoot /var/www/html/myProject/
              ServerName project.com
              ServerAlias www.project.com
      
          ErrorLog ${APACHE_LOG_DIR}/error.log
          CustomLog ${APACHE_LOG_DIR}/access.log combined
      
      </VirtualHost>
      
      <VirtualHost *:443>
              ServerAdmin webmaster@localhost
      
              DocumentRoot /var/www/html/myProject/
              ServerName project.com
              ServerAlias www.project.com          
      
              ErrorLog ${APACHE_LOG_DIR}/error.log
              CustomLog ${APACHE_LOG_DIR}/access.log combined
              SSLEngine on
      
              SSLCertificateFile  /etc/ssl/certs/mobidev_cert.pem
              SSLCertificateKeyFile /etc/ssl/certs/mobidev_key.pem
      
      
              <FilesMatch "\.(cgi|shtml|phtml|php)$">
                      SSLOptions +StdEnvVars
              </FilesMatch>
              <Directory /usr/lib/cgi-bin>
                      SSLOptions +StdEnvVars
              </Directory>
      
      </VirtualHost>
      

      在此也包括自签名证书的路径,如图所示的 ssl 密钥和可以轻松下载的 ssl 证书。

      第 4 步:将您的项目添加到 apache 配置文件中。

      sudo vi /etc/apache2/apache2.conf
      

      将这行放入文件中:

      DocumentRoot "/var/www/html/myProject"
      <Directory /var/www/html/myProject/>
          Options Indexes FollowSymLinks
          AllowOverride All
          Require all granted
      </Directory>
      

      第 5 步:将您的虚拟服务器名称(在 myProject.conf 中指定)添加到主机文件中。添加该行:

      sudo gedit /etc/hosts
      127.0.1.1   project.com
      

      第6步:现在一切就绪,启用站点,重新启动apache

      sudo a2ensite /etc/apache2/sites-availabl/myProject.conf
      sudo systemctl reload apache2
      sudo update-rc.d apache2 defaults
      sudo update-rc.d mysql defaults
      sudo a2enmod ssl
      sudo a2ensite default-ssl
      

      只需在浏览器中点击 project.com。

      【讨论】:

        猜你喜欢
        • 2017-06-14
        • 2012-08-22
        • 2011-10-13
        • 1970-01-01
        • 2013-07-12
        • 2011-10-14
        • 2015-02-01
        • 1970-01-01
        相关资源
        最近更新 更多