简体   繁体   English

Apache VirtualHost 和本地主机

[英]Apache VirtualHost and localhost

I'm working with XAMPP on Mac OS X.我正在 Mac OS X 上使用 XAMPP。

I'm trying to run a Symfony website properly for a client, and I really don't know Symfony (yet).我正在尝试为客户正确运行Symfony网站,但我真的不了解 Symfony(还)。 I just want to install and launch it.我只想安装并启动它。

I've changed my /etc/hosts file this way:我以这种方式更改了我的/etc/hosts文件:

127.0.0.1 www.mysite.local

And the httpd.conf file this way:httpd.conf文件这样:

<VirtualHost *:80>
  ServerName www.mysite.local
  DocumentRoot /Applications/MAMP/htdocs/mysite/web
  DirectoryIndex index.php
  <Directory /Applications/MAMP/htdocs/mysite/web>
    AllowOverride All
    Allow from All
  </Directory>
  Alias /sf /Applications/MAMP/htdocs/mysite/lib/vendor/symfony/data/web/sf
  <Directory "/Applications/MAMP/htdocs/mysite/lib/vendor/symfony/data/web/sf">
    AllowOverride All
    Allow from All
  </Directory>
</VirtualHost>

Now, the site is working (yay!), but I can't access any more any of my other local sites because localhost is rendered as www.mysite.local .现在,该站点正在运行(是的!),但我无法再访问任何其他本地站点,因为localhost呈现为www.mysite.local

Where am I wrong?我哪里错了? Thank you!谢谢!

This worked for me!这对我有用!

To run projects like http://localhost/projectName运行像http://localhost/projectName

<VirtualHost localhost:80>
   ServerAdmin localhost
    DocumentRoot path/to/htdocs/
    ServerName localhost
</VirtualHost>

To run projects like http://somewebsite.com locally在本地运行像http://somewebsite.com这样的项目

<VirtualHost somewebsite.com:80>
     ServerAdmin webmaster@example.com
     DocumentRoot /path/to/htdocs/somewebsiteFolder
     ServerName www.somewebsite.com
     ServerAlias somewebsite.com
</VirtualHost>

Same for other websites其他网站也一样

<VirtualHost anothersite.local:80>
     ServerAdmin webmaster@example.com
     DocumentRoot /path/to/htdocs/anotherSiteFolder
     ServerName www.anothersite.local
     ServerAlias anothersite.com
</VirtualHost>

localhost will always redirect to 127.0.0.1 . localhost将始终重定向到127.0.0.1 You can trick this by naming your other VirtualHost to other local loop-back address, such as 127.0.0.2 .您可以通过将其他 VirtualHost 命名为其他本地环回地址(例如127.0.0.2来欺骗这一点。 Make sure you also change the corresponding hosts file to implement this.确保您还更改了相应的hosts文件以实现此功能。

For example, my httpd-vhosts.conf looks like this:例如,我的httpd-vhosts.conf如下所示:

<VirtualHost 127.0.0.2:80>
    DocumentRoot "D:/6. App Data/XAMPP Shared/htdocs/intranet"
    ServerName intranet.dev
    ServerAlias www.intranet.dev
    ErrorLog "logs/intranet.dev-error.log"
    CustomLog "logs/intranet.dec-access.log" combined

    <Directory "D:/6. App Data/XAMPP Shared/htdocs/intranet">
        Options Indexes FollowSymLinks ExecCGI Includes
        Order allow,deny
        Allow from all
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

(Notice that in <VirtualHost> section I typed 127.0.0.2:80 . It means that this block of VirtualHost will only affects requests to IP address 127.0.0.2 port 80 , which is the default port for HTTP. (请注意,在<VirtualHost>部分中,我输入了127.0.0.2:80 。这意味着 VirtualHost 的这个块只会影响对 IP 地址127.0.0.2端口80请求,这是 HTTP 的默认端口。

To route the name intranet.dev properly, my hosts entry line is like this:为了正确路由名称intranet.dev ,我的hosts输入行是这样的:

127.0.0.2 intranet.dev

This way, it will prevent you from creating another VirtualHost block for localhost , which is unnecessary.这样,它将阻止您为localhost创建另一个 VirtualHost 块,这是不必要的。

This is normal if you see it.如果你看到它,这是正常的。 Since it is the first virtual host entry, it will show local host.由于它是第一个虚拟主机条目,它将显示本地主机。

Let's say for example you didn't want that page to show.例如,假设您不希望显示该页面。 All you want to show is the "Apache, it works" page, so you would make a vhost entry before mysite.local as local host and point it to the "it works" page.您想要显示的只是“Apache,它可以工作”页面,因此您可以在 mysite.local 之前创建一个vhost条目作为本地主机,并将其指向“它可以工作”页面。

But this is normal.但这是正常的。 I had this problem before, so don't worry!我以前也遇到过这个问题,所以不用担心!

You may want to use this:你可能想使用这个:

<VirtualHost *:80>
    DocumentRoot "somepath\Apache2.2\htdocs"
    ServerName localhost
</VirtualHost>
<VirtualHost *:80>

as your first virtual host (place it before another virtual hosts).作为您的第一个虚拟主机(将它放在另一个虚拟主机之前)。

I had the same issue of accessing localhost while working with virtualHost.在使用 virtualHost 时,我遇到了访问 localhost 的相同问题。 I resolved it by adding the name in the virtualHost listen code like below:我通过在 virtualHost 监听代码中添加名称来解决它,如下所示:

In my hosts file, I have added the below code ( C:\\Windows\\System32\\drivers\\etc\\hosts ) -在我的主机文件中,我添加了以下代码( C:\\Windows\\System32\\drivers\\etc\\hosts ) -

127.0.0.1   main_live

And in my httpd.conf I have added the below code:在我的httpd.conf 中,我添加了以下代码:

<VirtualHost main_live:80>
    DocumentRoot H:/wamp/www/raj/main_live/
    ServerName main_live
</VirtualHost>

That's it.而已。 It works, and I can use both localhost , phpmyadmin , as well as main_live (my virtual project) simultaneously.它有效,我可以同时使用localhostphpmyadmin以及main_live (我的虚拟项目)。

Additional description for John Smith's answer from the official documentation .官方文档中约翰·史密斯的回答的附加说明。 To understand why it is.要了解为什么会这样。

Main host goes away主要主持人离开

If you are adding virtual hosts to an existing web server, you must also create a block for the existing host .如果要将虚拟主机添加到现有 Web 服务器,则还必须为现有主机创建块 The ServerName and DocumentRoot included in this virtual host should be the same as the global ServerName and DocumentRoot.此虚拟主机中包含的 ServerName 和 DocumentRoot 应与全局 ServerName 和 DocumentRoot 相同。 List this virtual host first in the configuration file so that it will act as the default host.首先在配置文件中列出此虚拟主机,以便它充当默认主机。

For example, to work properly with XAMPP, to prevent VirtualHost overriding the main host, add the follow lines into file httpd-vhosts.conf :例如,要与 XAMPP 一起正常工作,以防止 VirtualHost 覆盖主主机,请将以下行添加到文件httpd-vhosts.conf 中

# Main host
<VirtualHost *:80>
    ServerName localhost
    DocumentRoot "/xampp/htdocs"
</VirtualHost>

# Additional host
<VirtualHost *:80>
    # Over directives there
</VirtualHost>

For someone doing everything described here and still can't access:对于执行此处描述的所有操作但仍然无法访问的人:

XAMPP with Apache HTTP Server 2.4: XAMPP 与 Apache HTTP Server 2.4:

In file httpd-vhost.conf :在文件httpd-vhost.conf 中

<VirtualHost *>
    DocumentRoot "D:/xampp/htdocs/dir"
    ServerName something.dev
   <Directory "D:/xampp/htdocs/dir">
    Require all granted #apache v 2.4.4 uses just this
   </Directory>
</VirtualHost>

There isn't any need for a port, or an IP address here.这里不需要端口或 IP 地址。 Apache configures it on its own files. Apache 在它自己的文件上配置它。 There isn't any need for NameVirtualHost *:80;不需要 NameVirtualHost *:80; it's deprecated.它已被弃用。 You can use it, but it doesn't make any difference.你可以使用它,但它没有任何区别。

Then to edit hosts, you must run Notepad as administrator (described bellow).然后要编辑主机,您必须以管理员身份运行记事本(如下所述)。 If you were editing the file without doing this, you are editing a pseudo file, not the original (yes, it saves, etc., but it's not the real file)如果您在没有执行此操作的情况下编辑文件,则您正在编辑一个伪文件,而不是原始文件(是的,它会保存等,但它不是真实文件)

In Windows:在 Windows 中:

Find the Notepad icon, right click, run as administrator, open file, go to C:/WINDOWS/system32/driver/etc/hosts , check "See all files" , and open hosts.找到记事本图标,右击,以管理员身份运行,打开文件,进入C:/WINDOWS/system32/driver/etc/hosts ,勾选“查看所有文件” ,打开主机。

If you where editing it before, probably you will see it's not the file you were previously editing when not running as administrator.如果您之前编辑过它,可能您会看到它不是您之前未以管理员身份运行时编辑的文件。

Then to check if Apache is reading your httpd-vhost.conf , go to folder xampFolder/apache/bin , Shift + right click, open a terminal command here, open XAMPP (as you usually do), start Apache, and then on the command line, type httpd -S .然后检查 Apache 是否正在读取您的 httpd-vhost.conf ,转到文件夹xampFolder/apache/binShift + 右键单击​​,在此处打开终端命令,打开 XAMPP(如您通常所做的那样),启动 Apache,然后在命令行,输入httpd -S You will see a list of the virtual hosts.您将看到虚拟主机列表。 Just check if your something.dev is there.只需检查您的something.dev是否在那里。

According to this documentation: Name-based Virtual Host Support根据此文档:基于名称的虚拟主机支持

You may be missing the following directive:您可能缺少以下指令:

NameVirtualHost *:80

Just change <VirtualHost *:80> to <VirtualHost 127.0.0.1:80> .只需将<VirtualHost *:80>更改为<VirtualHost 127.0.0.1:80>

Then the default DocumentRoot will serve for all domains or IP addresses that point to your server and specified VirtualHost will work.然后默认的DocumentRoot将为指向您的服务器的所有域或 IP 地址提供服务,并且指定的VirtualHost将起作用。

It may be because your web folder (as mentioned "/Applications/MAMP/htdocs/mysite/ web ") is empty.这可能是因为您的 web 文件夹(如提到的“/Applications/MAMP/htdocs/mysite/ web ”)是空的。

My suggestion is first to make your project and then work on making the virtual host.我的建议是先做你的项目,然后再做虚拟主机。

I went with a similar situation.我去了类似的情况。 I was using an empty folder in the DocumentRoot in httpd-vhosts.confiz and I couldn't access my shahg101.com site.我在httpd-vhosts.confizDocumentRoot中使用了一个空文件夹,但无法访问我的shahg101.com站点。

I am running Ubuntu 16.04 (Xenial Xerus).我正在运行Ubuntu 16.04 (Xenial Xerus)。 This is what worked for me:这对我有用:

  1. Open up a terminal and cd to /etc/apache2/sites-available .打开终端并cd/etc/apache2/sites-available There you will find a file called 000-default.conf .在那里你会找到一个名为000-default.conf的文件。
  2. Copy that file: cp 000-default.conf example.local.conf复制该文件: cp 000-default.conf example.local.conf
  3. Open that new file (I use Nano ; use what you are comfortable with).打开那个新文件(我使用Nano ;使用你喜欢的)。
  4. You will see a lot of commented lines, which you can delete.您会看到很多注释行,您可以将其删除。
  5. Change <VirtualHost *:80> to <VirtualHost example.local:80><VirtualHost *:80>更改为<VirtualHost example.local:80>
  6. Change the document root to reflect the location of your files.更改文档根目录以反映文件的位置。
  7. Add the following line: ServerName example.local And if you need to, add this line: ServerAlias www.example.local添加以下行: ServerName example.local如果需要,添加此行: ServerAlias www.example.local
  8. Save the file and restart Apache: service Apache2 restart保存文件并重启Apache: service Apache2 restart

Open a browser and navigate to example.local .打开浏览器并导航到example.local You should see your website.您应该会看到您的网站。

For any one using Windows and Bitnami WAMP Stack Manager Tool this virtual host configuration should go into Bitnami\\apache2\\conf\\bitnami\\bitnami.conf对于使用WindowsBitnami WAMP 堆栈管理器工具的任何人,此虚拟主机配置应进入Bitnami\\apache2\\conf\\bitnami\\bitnami.conf

Note: Some settings in Directory section is not a must.注意:目录部分中的某些设置不是必须的。

For example my virtual host configuration for site.com would be as follow例如,我的site.com虚拟主机配置如下

<VirtualHost site.com:80>
  DocumentRoot "C:/Bitnami/apache2/htdocs/site/docroot"
  
  <Directory "C:/Bitnami/apache2/htdocs/site/docroot">
    Options Indexes FollowSymLinks
    AllowOverride All
    <IfVersion < 2.3 >
      Order allow,deny                          
      Allow from all
    </IfVersion>
    <IfVersion >= 2.3 >
      Require all granted
    </IfVersion>
  </Directory>
</VirtualHost>

Remember that configuration for vhost as mentioned, by other friends, above is needed.请记住,需要上面其他朋友提到的 vhost 配置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM