Apache virtualHost ignored or pointing to the default one - macos

I'm trying to setup my Apache on OSX, but I'm not able to make my local url 'awr.local' point to the correct path. I mean whether I type http://localhost or http://awr.local, it always shows me the index.html page in my 'localhost' vhost path.
I've restarted my httpd service countless times, with or without sudo.
Any help would be greatly appreciated, thanks :'|
I've been following a tutorial (https://getgrav.org/blog/macos-sierra-apache-multiple-php-versions), here are the steps about Apache :
Disabling the bundled Apache and installing the one from homebrew :
sudo apachectl stop
sudo launchctl unload -w /System/Library/LaunchDaemons/org.apache.httpd.plist 2>/dev/null
brew install httpd
Then, some httpd.conf modifications :
# changed Listen 8080 to :
Listen 80
[...]
# changed DocumentRoot "/usr/local/var/www" to :
DocumentRoot "/Users/wallace/dev/default"
[...]
# changed <Directory "/usr/local/var/www"> to :
<Directory "/Users/wallace/dev/default">
[...]
# changed AllowOverride None to :
AllowOverride All
# uncommented :
LoadModule rewrite_module lib/httpd/modules/mod_rewrite.so
[...]
# changed
# User _www
# Group _www
# to :
User wallace
Group staff
[...]
# added the missing line :
ServerName localhost
Until this point everything seemed to be working fine, I've installed PHP and MariaDB without any problem.
Then came the virtual hosts part :
Some other httpd.conf modifications :
# uncommenting these lines
LoadModule vhost_alias_module lib/httpd/modules/mod_vhost_alias.so
[...]
Include /usr/local/etc/httpd/extra/httpd-vhosts.conf
Editing the file /usr/local/etc/httpd/extra/httpd-vhosts.conf :
<VirtualHost *:80>
DocumentRoot "/Users/wallace/dev/default"
ServerName localhost
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/Users/wallace/dev/awr"
ServerName awr.local
</VirtualHost>
The /etc/hosts file :
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
127.0.0.1 awr.local
::1 awr.local
The output of 'httpd -S' :
VirtualHost configuration:
*:80 is a NameVirtualHost
default server localhost (/usr/local/etc/httpd/extra/httpd-vhosts.conf:25)
port 80 namevhost localhost (/usr/local/etc/httpd/extra/httpd-vhosts.conf:25)
port 80 namevhost awr.local (/usr/local/etc/httpd/extra/httpd-vhosts.conf:30)
ServerRoot: "/usr/local/opt/httpd"
Main DocumentRoot: "/Users/wallace/dev/default"
Main ErrorLog: "/usr/local/var/log/httpd/error_log"
Mutex rewrite-map: using_defaults
Mutex default: dir="/usr/local/var/run/httpd/" mechanism=default
Mutex mpm-accept: using_defaults
PidFile: "/usr/local/var/run/httpd/httpd.pid"
Define: DUMP_VHOSTS
Define: DUMP_RUN_CFG
User: name="wallace" id=501 not_used
Group: name="staff" id=20 not_used

Well, today when I was preparing to try the answer of Juan, I had something new : instead of serving me the localhost, the awr.local url showed me a 403 forbidden. After a new research & doc reading, I managed to make it work like this :
My /usr/local/etc/httpd/extra/httpd-vhosts.conf file :
The Require all granted is the most important part.
<VirtualHost *:80>
DocumentRoot "/Users/wallace/dev/default"
ServerName localhost
<Directory "/Users/wallace/dev/default">
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/Users/wallace/dev/awr"
ServerName awr.local
<Directory "/Users/wallace/dev/awr">
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
I had to restart Apache with sudo for it to work :
sudo brew services stop httpd && sudo brew services start httpd

I had the same problem and couldn't get it to work no matter what I tried. Stopping, starting or restarting httpd did not work. Even though httpd -S always reported the correct settings, all browsers loaded the default docroot, regardless of the virtualhost settings. Then I noticed something strange. After stopping httpd with brew services stop httpd I reloaded localhost in the browser and it still loaded without a problem. This gave me the idea to sudo killall httpd then start it again with brew services start httpd. After this, all was fine.

Considering how much time I spent on this, I will be dropping my own fix too.
First of all I noticed that all server names using the 127.0.0.1 IP always points to the default directory. I edited my custom domain to use 127.0.1.1 in /etc/hosts.
In /usr/local/etc/httpd/extra/httpd-vhosts.conf, edited it according to Alfred's answer as I was initially getting a 403 too.
Files looks something like this;
/etc/hosts:
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 localhost
127.0.1.1 customdomain.local # note: 127.0.1.1
127.0.1.2 customdomain2.local # note: 127.0.1.2 (will increment for each new domain)
255.255.255.255 broadcasthost
::1 localhost
::1 customdomain.local
::1 customdomain2.local # this part is equally important though I am not sure why
/usr/local/etc/httpd/extra/httpd-vhosts.conf:
<VirtualHost customdomain.local:80>
ServerAdmin webmaster#customdomain.local
DocumentRoot "/Users/user/Projects/customdomain/public"
ServerName customdomain.local
<Directory "/Users/user/Projects/customdomain/public">
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
Confirm that your Listen value in /usr/local/etc/httpd/httpd.confis 80.
This might not be the best solution but should solve the issue.

This is how I have it setup (I've made changes to match your configuration).
Besides awr.local must be in your /etc/hosts file pointing to 127.0.0.1 or any other network interface you want to use.
<VirtualHost awr.local:80>
DocumentRoot "/Users/wallace/dev/awr"
ServerName awr.local
<Directory "/Users/wallace/dev/awr">
AllowOverride All
Order allow,deny
allow from all
Require all granted
</Directory>
</VirtualHost>

Related

Getting: Forbidden - You don't have permission to access this resource

Mac OS Big Sur - 11.4
I am trying to set up some local vhosts. I did the following:
Modified the httpd.conf file, to include the vhosts folder:
Include /usr/local/etc/httpd/extra/proxy-html.conf
And in my /usr/local/etc/httpd/extra/proxy-html.conf, I have:
<VirtualHost *:80>
ServerAdmin myemailaddress.com
DocumentRoot "~/Dropbox/cms/drupal_local"
ServerName drupal.local
ErrorLog "/usr/local/var/log/httpd/drupal.local.error.log"
CustomLog "/usr/local/var/log/httpd/drupal.local.access_log" common
</VirtualHost>
I then modified the /etc/hosts file as follows:
27.0.0.1 localhost drupal.local
255.255.255.255 broadcasthost
::1 localhost
I made sure I had a folder ~/Dropbox/cms/drupal_local
And in it, I created a simple index.html, that says:
<h1>Testing local drupal vhost</h1>
I then did:
brew services restart httpd
And the result was:
Stopping `httpd`... (might take a while)
==> Successfully stopped `httpd` (label: homebrew.mxcl.httpd)
==> Successfully started `httpd` (label: homebrew.mxcl.httpd)
I then went to my browser, and typed:
http://drupal.local
And the response was:
Forbidden
You don't have permission to access this resource.
What am I missing?
Here's what ended up working:
I modified my /usr/local/etc/httpd/extra/proxy-html.conf as follows:
<VirtualHost *:80>
ServerAdmin myemailaddress.com
DocumentRoot "~/Dropbox/cms/drupal_local"
ServerName drupal.local
<directory "~/Dropbox/cms/drupal_local/">
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</directory>
ErrorLog "/usr/local/var/log/httpd/drupal.local.error.log"
CustomLog "/usr/local/var/log/httpd/drupal.local.access_log" common
</VirtualHost>
and my /etc/hosts file as follows:
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
127.0.0.1 drupal.local
Then:
brew services restart httpd
I just had the same issue on MacOS Monterey. Based on the logs, it was caused by an autoindex error.
I fixed it by adding index.php to DirectoryIndex in httpd.conf (path /usr/local/etc/httpd/httpd.conf in my case):
<IfModule dir_module>
DirectoryIndex index.html index.php
</IfModule>
(Then of course, restart the service with brew services restart httpd for changes to take effect)
For anyone still having this issue in MacOS Monteray, after days of searching I finally found the solution in the apple forums article:
https://discussions.apple.com/docs/DOC-250004361

localhost is showing index of when I added a virtual host in /usr/local/etc/httpd/extra/httpd-vhosts.conf

I installed apache with brew and changed port 8080 to 80. when I accessed localhost this shown me a page "it works!" and that page is located in /usr/local/var/www directory with file name index.html but when I added a virtual host in /usr/local/etc/httpd/extra/httpd-vhosts.conf file.
<VirtualHost *:80>
DocumentRoot "/Users/ranaamir/projects/sample/yii2/backend/web"
ServerName yii2.example.be
ServerAlias yii2.example.be
<directory "/Users/ranaamir/projects/sample/yii2/backend/web">
Options Indexes FollowSymLinks
AllowOverride all
Order Deny,Allow
Allow from all
Require all granted
</directory>
ErrorLog "/usr/local/var/log/httpd/vjfBackenderror.log"
CustomLog "/usr/local/var/log/httpd/vjfBackendaccess.log" common
</VirtualHost>
and restart the service with sudo apachectl -k restart command and this host also added in /etc/hosts.
##
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
127.0.0.1 yii2.example.be
Then I access localhost or yii2.example.be both are showing a page where only one line mentioned Index of /.
if I remove code of block <VirtualHost *:80> from httpd-vhosts.conf then again localhost host showing correct index.html.
why my project not running on the virtual-host url instead of index of /?
Any help would be so appreciated!!
I have resolved this issue, I made little change in httpd.conf file where we have to mentioned which type of file they will be process first so I just added index.php in the code block of dir_module.
<IfModule dir_module>
DirectoryIndex index.php index.html
</IfModule>
I hope this will save your time and minimize your stress. :)

How to create virtual hosts in MAMP?

I am new to Mac but used Ubuntu for development for a long time. I know how to create virtual hosts in Ubuntu but have no idea about Mac. I have created a hosts entry like below :
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 localhost mysite.loc
255.255.255.255 broadcasthost
::1 localhost
But what to do next?
While googling, I found these steps to easily create virtual hosts on MAMP:
Open your console in mac and edit your hosts file like this
sudo vim /etc/hosts
This opens a system file that contains the following line:
127.0.0.1 localhost
add your desired host name after local host:
127.0.0.1 localhost mysite.loc
press ESC, then :wq! to overwrite and close the file.
Now go to your MAMP directory and open apache config file located at /Applications/MAMP/conf/apache/httpd.conf in any text editor and locate the following lines:
# Virtual Hosts
# Include /Applications/MAMP/conf/apache/extra/httpd-vhosts.conf
Remove the hash (pound) sign from the beginning of the line that begins with Include
# Virtual Hosts
Include /Applications/MAMP/conf/apache/extra/httpd-vhosts.conf
Save the file, and then open Applications/MAMP/conf/apache/extra/httpd-vhosts.conf. This is where you define the virtual hosts.
At the bottom of the page are two examples of how to define virtual hosts in Apache. They look like this:
<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>
Edit both examples. Virtual hosts override the existing localhost, so the first one needs to re-establish localhost. Edit the second one for the virtual host you want to add. Only the DocumentRoot and ServerName directives are required. To add a virtual host for mysite, the edited definitions should look like this:
<VirtualHost *:80>
DocumentRoot /Applications/MAMP/htdocs
ServerName localhost
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/Users/username/Sites/mysite"
ServerName mysite.loc
</VirtualHost>
This assumes that you want to locate the files for mysite in your Sites folder. Replace "username" in the second definition with your own Mac username. If you want to store the files in a different location, adjust the value of DocumentRoot accordingly.
If you want to create more than one virtual host, copy one of the definitions, and edit it accordingly.
Save all the files you have edited, and restart the servers in the MAMP control panel. You should now be able to access the virtual host with the following URL: http://mysite.loc/.
Enjoy..!!
Allow virtual hosts
Go to Applications > MAMP > conf > apache > httpd.conf
Find this line:
# Virtual hosts
#Include /Applications/MAMP/conf/apache/extra/httpd-vhosts.conf
Uncomment the code by removing the hash symbol.
# Virtual hosts
Include /Applications/MAMP/conf/apache/extra/httpd-vhosts.conf
Allow SymLink Override
Find this line in that same httpd.conf file.
<Directory />
Options Indexes FollowSymLinks
AllowOverride None
</Directory>
change None to All.
<Directory />
Options Indexes FollowSymLinks
AllowOverride All
</Directory>
Add the virtual host path
Go to Applications > MAMP > conf > apache > extra > httpd-vhosts.conf
add the virtual host with servname and document root like the below code
<VirtualHost *:80>
ServerName example.dev
DocumentRoot "/path/to/directory"
</VirtualHost>
Allow your computer to recognize your local domain
Open terminal and type
sudo pico /etc/hosts
then add your domain
127.0.0.1 example.dev
Restart your server.
If the url is showing error in chrome try safari
In my config in MAMP, only the first virtual host was responding.
After hours of search I founded the instruction for solving the problem (before listing virtual hosts definitions) :
NameVirtualHost *:80
Now, my 3 virtual hosts are working !
I followed this post, as recommended by szatti1489, and it worked for me: https://www.taniarascia.com/setting-up-virtual-hosts/
A couple of points are worth mentioning though:
This line didn't already exist in my httpd.conf file, I had to add it: Include /Applications/MAMP/conf/apache/extra/httpd-vhosts.conf
I had to use the .test domain ending, not .dev for my VirtualHost ServerName. The post mentions this, but then continues using.dev. Apparently, Chrome didn't support the .dev domain ending after 2017, although it didn't work in Firefox or Safari for me either.
Recently I changed from XAMP to MAMP on MAC. I tried to set up my last virtual hosts, but MAMP's 8888 port number was avoid the regular work.
Finally I found the solution. You could change the Listen port and the ServerName in httpd.conf as you could find in the following post:
https://www.taniarascia.com/setting-up-virtual-hosts/
Adding to the answer of Ritesh
You probably also want to add a directory configuration in your httpd.conf similar to the one that is already there, but for your the document root of your new server.
For Example:
<Directory "/Users/username/Sites/mysite">
Options All
AllowOverride All
Order allow,deny
Allow from all
XSendFilePath "/Users/username/Sites/mysite"
</Directory>
Building off of Srinivasan's answer. This is what I did in order to have 2 virtual hosts set up
myapp-local.local:8888/
myapp-local2.local:8888/
/Applications/MAMP/conf/apache/extra/httpd-vhosts.conf
<VirtualHost *:80>
DocumentRoot /Applications/MAMP/htdocs
ServerName localhost
</VirtualHost>
<VirtualHost *:8888>
DocumentRoot /Applications/MAMP/htdocs/my_app
ServerName myapp-local.local
<Directory "/Applications/MAMP/htdocs/my_app">
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
<VirtualHost *:8888>
DocumentRoot /Applications/MAMP/htdocs/instance-2/my_app
ServerName myapp-local2.local
<Directory "/Applications/MAMP/htdocs/instance-2/my_app">
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
sudo vi /etc/hosts
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 localhost myapp-local.local
127.0.0.1 localhost myapp-local2.local
255.255.255.255 broadcasthost
::1 localhost
Drupal specific:
sites/default/settings.php
$settings['trusted_host_patterns'] = [
'^localhost$',
'^myapp-local.local$'
'^myapp-local2.local$'
];
*/
restart MAMP server

Virtual Hosts not working XAMPP 1.8.3

Im trying to set up a virtual host using my local xampp stack but after following many tutorials i simply can't work out where im going wrong.
Here is what i added to my httpd-vhost.conf file:
NameVirtualHost *:80
<VirtualHost *:80>
ServerName localhost
DocumentRoot E:\Programs\xampp\htdocs
</VirtualHost>
<VirtualHost *:80>
DocumentRoot E:\Programs\xampp\htdocs\CVCMS
ServerName cvcms.dev
<Directory "E:\Programs\xampp\htdocs\CVCMS">
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
And here is my hosts file
# localhost name resolution is handled within DNS itself.
127.0.0.1 localhost
# ::1 localhost
127.0.0.1 lmlicenses.wip4.adobe.com
127.0.0.1 lm.licenses.adobe.com
127.0.0.1 cvcms.dev
I have restarted apache after every change, i have made sure http.conf is linked to the httpd-vhosts file.
The issue is when i type in the domain cvcms.dev it shows the xampp splash page instead of the index.php in my cvcms folder.
Any ideas why?
Thanks
please remember to add the quotes to your virtual host document root path. At the same time what you need to to do enable them is to find httpd.conf , in this version it should be in your etc folder within your XAMPP's root folder.
Look out for :
# Virtual hosts
#Include etc/extra/httpd-vhosts.conf
Since '#' is a comment prefix, you have to uncomment "#Include etc/extra/httpd-vhosts.conf"
See if this works. Remember to restart your XAMPP's apache server to read the new settings you've just set. Cheers !
You can read more on this here.

MAC OS X: MAMP Environment & Virtual Hosts

I'm not sure what's happening here, but I'm running MAMP 1.9 on MAC OS X. In my httpd.conf file, the DocumentRoot is set to the default htdocs path. Within the htdocs folder, I've created a number of "subsites" in an effort to set them up as Virtual Hosts.... I can get to the paths specified, but they all seem to default to the first one I set up.
Essentially, the first VirtualHost I configured was for site1.localhost.com. I've since configured site2.localhost.com and site3.localhost.com. However, going to either of the last two addresses, always seems to redirect me to site1.localhost.com. Additionally, going to just localhost.com:8888 also just pulls up site1.localhost.com. I'm not sure where I made a mis-step, but hopefully someone here will be able to help me figure out the problem... Oh, and I have restarted apache etc., after any changes made to either the /etc/hosts or httpd.conf file.
In my httpd.conf file (the relevant parts, anyway...):
#
# DocumentRoot: The directory out of which you will serve your
# documents. By default, all requests are taken from this directory, but
# symbolic links and aliases may be used to point to other locations.
#
# MAMP DOCUMENT_ROOT !! Don't remove this line !!
DocumentRoot "/Applications/MAMP/htdocs"
#
# Note that from this point forward you must specifically allow
# particular features to be enabled - so if something's not working as
# you might expect, make sure that you have specifically enabled it
# below.
#
#
# This should be changed to whatever you set DocumentRoot to.
#
<Directory "/Applications/MAMP/htdocs">
<VirtualHost *>
DocumentRoot "/Applications/MAMP/htdocs/site1/"
ServerName site1.localhost.com
</VirtualHost>
<VirtualHost *>
DocumentRoot "/Applications/MAMP/htdocs/sub/site2/"
ServerName site2.localhost.com
</VirtualHost>
<VirtualHost *>
DocumentRoot "/Applications/MAMP/htdocs/sub/site3/"
ServerName site3.localhost.com
</VirtualHost>
In my /etc/hosts file:
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 localhost
127.0.0.1 site1.localhost.com
127.0.0.1 site2.localhost.com
127.0.0.1 site3.localhost.com
255.255.255.255 broadcasthost
::1 localhost
fe80::1%lo0 localhost
In your httpd.conf file find the following line and remove the comment (#):
# NameVirtualHost *
In addition to MattLeff's answer, you should also add a server alias, just to be safe:
ServerAlias www.website.dev
If you don't and your browser automagically adds "http://www" (and hides it, rawr!) then your environment will automatically default to the first virtual host.
I can see you got your answer. It wasn't sufficient in my case. I was able to get the localhost + many other vhosts (demo.client.com) working on my MAMP when I added Chords's suggestion. It only worked when I added the localhost as a virtual host, on the top of the list.
NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot "/Applications/MAMP/htdocs"
ServerName localhost
</VirtualHost>
<VirtualHost *:80>
ServerAdmin me#email.com
DocumentRoot "/Applications/MAMP/htdocs/clientA/"
ServerName clientA.local
ErrorLog "logs/clientA-local-error_log"
CustomLog "logs/clientA-local-access_log" common
</VirtualHost>

Resources