Apache: 虚拟主机配置

3
当我尝试在Apache中配置虚拟主机时,我输入了以下内容:
NameVirtualHost *:80

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

在我的hosts文件中,我放置了以下内容:
127.0.0.1       localhost
127.0.0.1       gift.loc

我在浏览器上运行它,

http://gift.loc - is fine

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

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

我有没有忘记配置什么?有什么想法吗...

提前致谢,


感谢您的建议,我找到了解决方案。我在定义虚拟主机块之前放置了一些默认目录:<VirtualHost *:80> DocumentRoot /xampp/htdocs </VirtualHost> 这将呈现所有未匹配到已定义虚拟主机的请求。 - Trez
我写了这个答案,如果你想查看的话:https://stackoverflow.com/a/56535519/3980729 - Carlos Espinoza
4个回答

4
您需要为每个您想让Apache处理的主机设置一个VirtualHost条目。配置文件中的第一个条目将作为默认值使用,如果没有其他VirtualHost与请求匹配。
例如,如果我们有:
<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

您需要在vhosts.conf中添加localhost。

    NameVirtualHost *:80

    <VirtualHost *:80>
       DocumentRoot /xampp/htdocs/
       ServerName localhost
    </VirtualHost>

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

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

2

在Ubuntu上设置虚拟主机需要遵循以下几个步骤:
假设您的项目文件夹名为myProject。

步骤1:将您的文件夹放置在/var/www/html内。

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

步骤二:将项目文件夹的所有权转移给www-data用户。
sudo chown -R www-data:www-data /var/www/html/myProject

步骤三:在“可用站点”中创建新站点。
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>

步骤五:将你在 myProject.conf 中指定的虚拟服务器名称添加到 host 文件中。添加以下行:
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即可。


0
文档中看来,我们需要为每个不同的主机创建一个块以提供服务。
在同一文档中进一步说明:如果您要将虚拟主机添加到现有的 Web 服务器中,则还必须为现有主机创建一个块。

这个信息很有用,但我希望gift.loc能够通过虚拟主机提供服务,而“othersite”则通过“localhost/othersite”语法运行。 - Trez

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接