81

I'm working with XAMPP on Mac OS X.

I'm trying to run a Symfony website properly for a client, and I really don't know Symfony (yet). I just want to install and launch it.

I've changed my /etc/hosts file this way:

127.0.0.1 www.mysite.local

And the httpd.conf file this way:

<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.

Where am I wrong?

13 Answers 13

132

This worked for me!

To run projects like http://localhost/projectName:

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

To run projects like http://somewebsite.com locally:

<VirtualHost somewebsite.com:80>
     ServerAdmin [email protected]
     DocumentRoot /path/to/htdocs/somewebsiteFolder
     ServerName www.somewebsite.com
     ServerAlias somewebsite.com
</VirtualHost>

The same for other websites:

<VirtualHost anothersite.local:80>
     ServerAdmin [email protected]
     DocumentRoot /path/to/htdocs/anotherSiteFolder
     ServerName www.anothersite.local
     ServerAlias anothersite.com
</VirtualHost>
9
  • 10
    I feel this should be the accepted answer. No port swaps, just how the OP imagined. Commented Feb 26, 2015 at 0:44
  • 8
    Thanks, great! That helped me. Also I would say that if one wants to run projects like 'projectname.com', he needs to add 'projectname.com' to hosts file (/etc/hosts on Ubuntu) Commented May 21, 2015 at 13:23
  • 2
    but this configurations cannot be use at the same time?
    – albanx
    Commented Oct 1, 2015 at 13:57
  • 1
    and how does /etc/hosts look? and does this work when document rot is outside /var/www?
    – axd
    Commented Mar 19, 2017 at 17:39
  • 3
    Why are you placing the servername in <VirtualHost> tag? Why not keep it *:80? Commented Jan 6, 2021 at 13:43
32

localhost will always redirect to 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. Make sure you also change the corresponding hosts file to implement this.

For example, my httpd-vhosts.conf looks like this:

<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.

To route the name intranet.dev properly, my hosts entry line is like this:

127.0.0.2 intranet.dev

This way, it will prevent you from creating another VirtualHost block for localhost, which is unnecessary.

3
  • Using other local loop-back address is a very neat trick. Kudos!
    – Terix
    Commented Jun 19, 2015 at 8:53
  • 3
    Pro tip: Don't forget to change the Listen parameter in your httpd.conf to match your configured loopback addresses.
    – Noir
    Commented Oct 9, 2015 at 9:29
  • 1
    Yes! this using 127.0.0.2 (or .3 or .4, etc.) did the trick for me for testing sites on my local machine (Win 10, Apache, PHP7.2) which also use .htaccess Rewrite rules. Thank you! Commented Aug 1, 2018 at 11:54
25

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.

But this is normal. I had this problem before, so don't worry!

4
  • 2
    Ok, i've added a VirtualHost for localhost also after the first directive, and now it works. Thank you :) Commented Oct 30, 2012 at 15:41
  • 1
    No Problem :) thats what im here for! Commented Nov 16, 2012 at 22:14
  • 1
    @AngelicaRosa Can You direct the step vy step answer, how you solved your problem. I'm unable to create virtualhost for localhost.
    – Siraj Alam
    Commented Mar 19, 2018 at 14:29
  • 1
    @Siraj just look at the answer with 74 upvotes, there's the virtualhost code you have to use. Commented Mar 20, 2018 at 15:28
15

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 other virtual hosts).

5
  • That fixed the issue for me on Mavericks Commented Feb 7, 2014 at 15:15
  • Worked for me on Ubuntu 14.04!
    – Konsole
    Commented Jun 17, 2014 at 6:59
  • Worked for me, you fantastic soul! Commented Nov 6, 2015 at 16:50
  • Worked for me too. Even though your answer is just like a repetition of other answers above. But I did exactly you did and it worked. Thanks a bunch
    – Siraj Alam
    Commented Mar 19, 2018 at 15:06
  • Mavericks = Mac OS X v10.9 Commented Oct 21, 2021 at 20:01
12

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. The ServerName and DocumentRoot included in this virtual host should be the same as the global ServerName and 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:

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

# Additional host
<VirtualHost *:80>
    # Other directives there
</VirtualHost>
2
  • Actually, I didn't use ServerName localhost, just copied (from httpd.conf) DocumentRoot and <Directory parts and it worked fine - same as previously with all hosts except the ServerNames and Aliases in the lower <VirtualHost *:80> tag.
    – Íhor Mé
    Commented Jun 3, 2017 at 23:18
  • that definitely helps, thank you :D :D now I can use both localhost and custom virtual host
    – CuongDC
    Commented Jul 24, 2019 at 11:55
10

I had the same issue of accessing localhost while working with virtualHost. I resolved it by adding the name in the virtualHost listen code like below:

In my hosts file, I have added the below code (C:\Windows\System32\drivers\etc\hosts) -

127.0.0.1   main_live

And in my httpd.conf I have added the below code:

<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.

3
  • Modifying hosts file is important here. Commented Aug 29, 2017 at 8:10
  • In my case, hitting main_live on browser did not work for the first time. then I tried main_live, it worked. then main_live (without http) started working. Very strange Commented Aug 29, 2017 at 8:12
  • @Vohidjon Karimjonov : I think there is browser access permissions needed to work this. So may be you have permitted browser to listen request for "main_live" and then only it starts working as a website, else it is considered as just a keyword. So when you inserted main_live it looked for the host file for the entry of 'main_live' and if found then only browser will ask you for permissions, and if permission granted then you will get your virtual host working and loads your homepage of web application. Let it be it worked for you. that's great!!!
    – Raj
    Commented Aug 31, 2017 at 4:47
5

For someone doing everything described here and still can't access:

XAMPP with Apache 2.4:

In file 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. Apache configures it on its own files. There isn't any need for 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 below). 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:

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.

If you were 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. You will see a list of the virtual hosts. Just check if your something.dev is there.

0
4

Just change <VirtualHost *:80> to <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.

3

According to this documentation: Name-based Virtual Host Support

You may be missing the following directive:

NameVirtualHost *:80
1
1

It may be because your web folder (as mentioned "/Applications/MAMP/htdocs/mysite/web") is empty.

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.

2
  • 1
    may is not a clear answer, help using exact solution
    – Mostafiz
    Commented Apr 27, 2016 at 7:53
  • Virtual drive will not be created unless u have INDEX.php in the folder, so I was saying that your folder may be empty (containing no index.php file)
    – Shahmir
    Commented May 1, 2016 at 5:19
0

I am running Ubuntu 16.04 (Xenial Xerus). This is what worked for me:

  1. Open up a terminal and cd to /etc/apache2/sites-available. There you will find a file called 000-default.conf.
  2. Copy that file: cp 000-default.conf example.local.conf
  3. Open that new file (I use Nano; use what you are comfortable with).
  4. You will see a lot of commented lines, which you can delete.
  5. Change <VirtualHost *:80> to <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
  8. Save the file and restart Apache: service Apache2 restart

Open a browser and navigate to example.local. You should see your website.

0

For anyone using Windows and the Bitnami WAMP Stack Manager Tool this virtual host configuration should go into 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 follows:

<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, sagits's answer is needed.

0

On Apache 2.4 on Windows, you also have to include the vhosts file from httpd.conf:

Include conf/extra/httpd-vhosts.conf

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.