Hello Drupal folk,

This one is aimed at those who have a mac and use a virtual host to make their drupal sites.

I have followed the steps in this tutorial: http://www.entityarts.net/blog/steve_krueger/07-19-2007/virtualhosts_mam...

And tried to do some research but it all pretty much follows the same set up.

I have also set MAMP to use port 80 (8889 for MySQL) so I don't have to type the port number as well.

What I want

My localhost root I want to be is: /User/imac/foo
I would like my site "root" (say www.domain.dev) to be: /User/imac/foo/site

follow the steps in the tutorial

My httpd.conf file

/Applications/MAMP/conf/apache/httpd.conf

Have added at the bottom of the file

<VirtualHost 127.0.0.1>
        DocumentRoot /Users/imac/foo
        ServerName localhost   
</VirtualHost>
    
<VirtualHost *>
        DocumentRoot /Users/imac/foo/site
        ServerName www.domain.dev
</VirtualHost>

My hosts file

(/private/etc/hosts)

127.0.0.1	localhost 
255.255.255.255	broadcasthost
::1             localhost
127.0.0.1	www.mydomain.dev

And it seems as if the addresses are reversed when I visit http://localhost/ it shows the hello word file (from the site folder). When I visit www.domain.dev I am shown the file structure from the foo folder.

Any suggests on what I may be missing?

Thanks in advance.

Comments

dnewkerk’s picture

See these posts of mine to maybe help with MAMP virtual hosts:
http://drupal.org/node/353266#comment-1178828
http://drupal.org/node/354895#comment-1185035

Let me know if that does the trick. Try out the Hosts widget I mention, as it has been the easiest way for me (never fails to flush DNS for me, etc, so it always works the moment I save a new entry). If you do manually edit /etc/hosts, at least the source I learned from a long time ago said you had to edit it using sudo (not just nano /etc/hosts).

I'm not sure if it's due to having migrated through several versions of MAMP, but my httpd.conf does not have the VirtualHost 127.0.0.1 that's mentioned in the link you posted.

-- David
davidnewkerk.com | absolutecross.com
View my Drupal lessons & guides

bdiddymc’s picture

I have added that hosts widget as I am not great with terminal.

I also deleted the virtualHost 127.0.0.1

Am I misunderstanding how the final result should work.

Say I want to have
www.domain.dev1
www.domain.dev2

I will add to the httpd.conf file

<VirtualHost *>
        DocumentRoot /Users/imac/foo/site/1
        ServerName www.domain.dev1
</VirtualHost>

<VirtualHost *>
        DocumentRoot /Users/imac/foo/site/2
        ServerName www.domain.dev2
</VirtualHost>

And add them to the hosts file using the Hosts Widget.

Does this mean I can then access localhost, www.domain.dev1 and www.domain.dev2 in the browser and it will show the individual index.php files for each respective location?

As with the current set up if I try localhost, www.domain.dev2
They are showing the dev1 site (Users/imace/foo/site/1)

Am I right in thinking that it shouldn't be doing this, or do you have to comment out info when you wish to change sites?

dnewkerk’s picture

The only thing I'm seeing right off that's hinting to me of a possible issue is the www ... try to set it up without using www, so that you enter http://site1.dev in your browser to pull it up. It's a local site and there's really no purpose for adding the www, even if the online version will have it.

Also make sure that you've properly setup your path to htdocs in MAMP's settings. In the "Apache" tab of MAMP, set the document root to exactly: /Users/imace/foo (where foo is htdocs). Inside that put folders for the actual sites. In my case I separate these one extra level as well with several initial folders for better organization, and sites inside this group of parent folders (e.g. one folder for my own sites, one for client sites, and one for cms testing/random stuff). So one example of a path for me (as entered in my VirtualHost) would be /Users/MyAccount/htdocs/cms/drupal6-test. The path for document root set in MAMP is /Users/MyAccount/htdocs. The drupal6-test folder is the folder containing Drupal (e.g I extracted the tar.gz and renamed the resulting folder to that name).

-- David
davidnewkerk.com | absolutecross.com
View my Drupal lessons & guides

cog.rusty’s picture

Your understanding is correct. It must be some small detail.

Do you have a NameVirtualHost * line before the vhosts?
Do you restart Apache every time you make a change?

bdiddymc’s picture

The NameVirtualHost * was commented out. but I removed the #.

Restarted my entire computer... but still the same issue.

Also removed the www from the addresses.

I will look into VirtualHostX, but also wouldn't mind figuring out how I'm missing this... may try reinstall MAMP...

aireworth’s picture

Apache always defaults to the first entry in the vhosts file if it can't find a match. You have "ServerName www.domain.dev" (NOT www.mydomain.dev) in the vhosts file and "www.mydomain.dev" in the hosts file. Those domain names don't match, so apache defaults to the first entry (as I understand it) and you get the directory structure of /foo - working as designed, I think.

I've had a whisky to many to sort your other problem but see if this sorts one of them :-)

Colin

bdiddymc’s picture

Thanks for the reply..

For some reason it is now working... I'm not sure what made it start to play ball.

Good spot, but in my follow up reply where I had two different domains, it was still having the same issue. And i didn't have a domain - mydomain discrepancy.

Final Code

NameVirtualHost *

<VirtualHost *>
        DocumentRoot /Users/imac/htpdocs
        ServerName localhost
</VirtualHost>

<VirtualHost *>
        DocumentRoot /Users/imac/htpdocs/los/c1
        ServerName loscam.c1
</VirtualHost>

<VirtualHost *>
        DocumentRoot /Users/imac/htpdocs/los/c2
        ServerName loscam.c2
</VirtualHost>

<VirtualHost *>
        DocumentRoot /Users/imac/htpdocs/los/c3
        ServerName loscam.c3
</VirtualHost>

I tried to test by deleting the httpd.conf file and replacing it with the back up, then adding all the details again. But still had the same issues.

I then started swapping around the code, so putting c2 before c1:

<VirtualHost *>
        DocumentRoot /Users/imac/htpdocs/los/c2
        ServerName loscam.c2
</VirtualHost>

<VirtualHost *>
        DocumentRoot /Users/imac/htpdocs/los/c1
        ServerName loscam.c1
</VirtualHost>

This started working, then when I swapped it back it would cause the issue again... was sort of going in circles for a while then it started working. So I added another one and an extra code for localhost so I can still have that option.

Sorry to anyone else looking for a solution to this problem... but I'm not quite sure...

NB: To follow up I can use www in the addresses but have decided to leave it out

babbage’s picture

Check out VirtualHostX. Spend c $9 and save yourself the hassle. I use MAMP Pro to do the same thing but this is a much more affordable option.

rajmataj’s picture

VirtualHostX, as you mentioned, is now $37.18.

In my opinion, use a decent code editor to edit the file as needed. This way, you will also get a better understanding of how the file is structured. TextWrangler is a decent free editor for basic editing.

ddanis’s picture

I know it seems a bit late, but after getting stuck in the same rut, I noticed one thing in the code examples that seemed different. In the first listing of the httpd.config file Sheedz shows us this.

<VirtualHost 127.0.0.1>
        DocumentRoot /Users/imac/foo
        ServerName localhost  
</VirtualHost>
   
<VirtualHost *>
        DocumentRoot /Users/imac/foo/site
        ServerName www.domain.dev
</VirtualHost>

By the time his configuration is working he shows us this.

NameVirtualHost *

<VirtualHost *>
        DocumentRoot /Users/imac/htpdocs
        ServerName localhost
</VirtualHost>

<VirtualHost *>
        DocumentRoot /Users/imac/htpdocs/los/c1
        ServerName loscam.c1
</VirtualHost>

<VirtualHost *>
        DocumentRoot /Users/imac/htpdocs/los/c2
        ServerName loscam.c2
</VirtualHost>

<VirtualHost *>
        DocumentRoot /Users/imac/htpdocs/los/c3
        ServerName loscam.c3
</VirtualHost>

The difference is in the Virtual Host used for the localhost. Initially it was this <VirtualHost 127.0.0.1>. Once it is set to <VirtualHost *> the whole thing seemed to work for me.

I hope this helps out someone else :)

chris_d2d’s picture

I too struggled with this very same issue for two days before I stumbled upon this post. Most tutorial forget to tell you to do two things:

  1. in your httpd.conf file, you have to remove the # from # NameVirtualHost 127.0.0.1
  2. you should change that from 127.0.0.1 to * and then make sure all of your entries below that are

This seems to have solved my issue.

Thanks to all who contributed to posting this solution!

batigolix’s picture

confirm that it works after changing the lines in MAMP's default httpd-vhosts.conf

from:
NameVirtualHost *:80

to:
NameVirtualHost *

nitin.k’s picture

Hi Folks,

If anyone is facing issues regarding setup the multi site in D8. Please follow the below steps.

Here I am going to create a new sub site in D8 named - domain1.com.

1. Just navigate to the Drupal 8 root dir.
2. Goto sites folder.
3. Create a new file sites.php or you can copy example.sites.php and rename it.
4. Put below code in sites.php.

$sites = array(
 'domain1.com' => 'domain1',
);

5. Now inside the sites folder create a folder named - domain1
6. Inside domain1 folder , you should have 3 folders.
A. modules
B. files
C. settings.php
7. if you already have db then link db from settings.php, if not then copy default.settings.php and paste it followed by rename to settings.php in domain1 folder and place the below code at the bottom of the file.

$databases['default']['default'] = array (
  'database' => 'whatever',
  'username' => 'whatever',
  'password' => 'whatever',
  'prefix' => '',
  'host' => '127.0.0.1',
  'port' => '3306',
  'namespace' => 'Drupal\\Core\\Database\\Driver\\mysql',
  'driver' => 'mysql',
);
$settings['install_profile'] = 'standard';
$config_directories['sync'] = 'profiles/sync';

8. Now if you are using mac or linux then fire below command.

sudo vi /private/etc/hosts

Add the following line at the end of the file.

127.0.0.1      domain1.com

9. Now the last step will be to map your hosts and drupal.

#
# Virtual Hosts
#
# If you want to maintain multiple domains/hostnames on your
# machine you can setup VirtualHost containers for them. Most configurations
# use only name-based virtual hosts so the server doesn't need to worry about
# IP addresses. This is indicated by the asterisks in the directives below.
#
# Please see the documentation at
# <URL:http://httpd.apache.org/docs/2.2/vhosts/>
# for further details before you try to setup virtual hosts.
#
# You may use the command line option '-S' to verify your virtual host
# configuration.

#
# Use name-based virtual hosting.
#

NameVirtualHost *:80

#
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for all requests that do not
# match a ServerName or ServerAlias in any <VirtualHost> block.
#
## <VirtualHost *:80>
    ##ServerAdmin webmaster@dummy-host.example.com
    ##DocumentRoot "/Applications/MAMP/Library/docs/dummy-host.example.com"
    ##ServerName dummy-host.example.com
    ##ServerAlias www.dummy-host.example.com
    ##ErrorLog "logs/dummy-host.example.com-error_log"
    ##CustomLog "logs/dummy-host.example.com-access_log" common
## </VirtualHost>

## <VirtualHost *:80>
    ##ServerAdmin webmaster@dummy-host2.example.com
    ##DocumentRoot "/Applications/MAMP/Library/docs/dummy-host2.example.com"
    ##ServerName dummy-host2.example.com
    ##ErrorLog "logs/dummy-host2.example.com-error_log"
    ##CustomLog "logs/dummy-host2.example.com-access_log" common
## </VirtualHost>

<VirtualHost *:80>
    DocumentRoot "/var/www/html"
    ServerName localhost
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot "/var/www/html/drupal-8.0.0/"
    ServerName domain1.com
</VirtualHost>

Please note you need to enable vhosts file include from httpd.conf file.

Uncomment below in httpd.conf file to include vhosts.

# Virtual hosts
# Include /... path ../httpd-vhosts.conf

to

<code>
# Virtual hosts
Include /... path ../httpd-vhosts.conf