Apache:虚拟主机配置

Apache:虚拟主机配置,apache,virtual,host,Apache,Virtual,Host,当我试图在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 但是当我

当我试图在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
我是否遗漏了要配置的内容?任何想法

提前感谢,

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


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

对于希望apache处理的每台主机,都需要一个VirtualHost条目。如果没有其他VirtualHost与请求匹配,则配置文件中的第一个将用作默认值

例如,如果我们有:

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

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

DocumentRoot/xampp/htdocs/gift
ServerName gift.loc
DocumentRoot/example/htdocs/gift
ServerName example.com

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

您需要将localhost放在vhosts.conf中

    NameVirtualHost *:80

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

    <VirtualHost *:80>
       DocumentRoot /xampp/htdocs/gift
       ServerName gift.loc  
    </VirtualHost>
NameVirtualHost*:80
DocumentRoot/xampp/htdocs/
服务器名本地主机
DocumentRoot/xampp/htdocs/gift
ServerName gift.loc

这工作正常(请确保重新启动apache)。如果您需要检查您的配置,您可以(至少在linux上)运行httpd-S。

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

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

sudo mv ~/myProject /var/www/html/
步骤2:将项目文件夹的所有权授予www数据

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>
将以下行放入文件:

DocumentRoot "/var/www/html/myProject"
<Directory /var/www/html/myProject/>
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>
步骤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。

很高兴知道,但我希望gift.loc由虚拟主机提供,而“其他站点”则通过“localhost/othersite”语法运行。感谢您的建议,我找到了解决方案。我在define virtual host块上方放置了一些默认目录:DocumentRoot/xampp/htdocs这将呈现所有与定义的虚拟主机不匹配的请求。如果您想签出它,我写了这个答案:
sudo gedit /etc/hosts
127.0.1.1   project.com
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