Can I use virtual hosts AND localhost on MAMP? - macos

I've got my MAMP setup working with virtual hosts and it's been great. The one problem I have is that localhost doesn't work so long as virtual hosts is enabled. I haven't been able to find out if that's normal behavior or not. Can I make both localhost and virtual hosts work on MAMP?

Yes, this is possible. This will need to added to the vhosts, but it can't be added just like any other... it has to be configured so it's the default vhost. To accomplish this, you need to set up your apache config to allow vhosts, (i'm assuming you've already taken care of that).
Your Vhosts config needs a few things before you start listing Vhosts in order for localhost to work. first a NameVirtualHost directive, then a master vhost (your localhost), followed by your regular vhosts. Apache will attempt to match the provided host against all of your named vhosts, and when it fails, it will serve your default vhost (which is your first listed vhost) by default. In order to accomplish this, I recommend changing 3 files: etc/hosts, apache.conf and vhosts.conf.
In your hosts file, give your machine a name other than localhost on the first line where it sets 127.0.0.1 to localhost:
hosts
127.0.0.1 localhost puededev
then in your apache.conf change your ServerName directive to match the new hostname. This is usually set to 127.0.0.1 or localhost, and typically followed by a Directory directive block with some permissions settings
apache.conf
ServerName puededev
Now in your vhosts, you will set your NameVirtualHosts config, and your default virtual host, followed by your other vhosts:
vhosts.conf
NameVirtualHost puededev
Listen 80
Listen 8080
# list out any other ports you are using
<VirtualHost localhost:*>
ServerAdmin webmaster#example.com
ServerName localhost
DocumentRoot /home/usr/mamp/htdocs
<Directory /home/usr/mamphtdocs>
order deny,allow
Allow from 127.0.0.1
Allow from ::1
</Directory>
</VirtualHost>
It is also possible to use whatever configuration you use now, and create aliases to different application that will work off of virtual hosts. this is how phpmyadmin runs with MAMP, and you'll notice you can still use localhost/phpmyadmin as an accessible path, even though you are likely getting an forbidden/you do not have access error when you just use localhost/ at the moment.
Aliases will depend on whether your installation is set to manage aliases in external files, but you can always define an alias in the apache.conf. You would likely use this method if you want to do something like localhost/sampleapp instead of a vhost set to http://sampleapp/
Alias /sampleapp /path/to/sampleapp
<Directory /path/to/sampleapp>
Order allow,deny
Allow from all
</Directory>
now go to: http://localhost/sampleapp

Related

Apache 2.4 ignore httpd-vhosts.conf file

I have been following this steps setup apache virtualhost (windows) to create a Virtual Host in Windows but I don't know what is wrong because it doesn't work.
I want a virtual host with this url: http://local.shop.
httpd-vhosts.conf
<VirtualHost *:80>
DocumentRoot "C:/Apache24/htdocs/"
ServerName localhost
ServerAlias localhost
<Directory "C:/Apache24/htdocs/">
AllowOverride All
</Directory>
<VirtualHost *:80>
DocumentRoot "C:/Apache24/htdocs/shop/"
ServerName local.shop
ServerAlias local.shop
<Directory "C:/Apache24/htdocs/shop/">
AllowOverride All
</Directory>
ErrorLog "logs/localhost.html-error.log"
CustomLog "logs/localhost.html-access.log" common
</VirtualHost>
hosts
127.0.0.1 localhost
127.0.0.1 local.shop
If I try to access to http://localhost the page served is the page which is on C:/Apache24/htdocs/shop/ but if I try to access to http://local.shop I've got the next error ERR_NAME_NOT_RESOLVED
What am I doing wrong?
Edit 1:
I have removed ServerAlias directives and now If I try to access to http://localhost the page served is the right page, but if I try to acces to http://local.shop still doesn't work. I've got the same error ERR_NAME_NOT_RESOLVED
Edit 2:
I have used "ping" command from windows to try to reach to each host. And I get a response from localhost but not from local.shop
Edit 3:
I have made a change in my definition of localhost in httpd-vhosts.conf. I have changed DocumentRoot to "C:/Apache24/htdocs/shop"
<VirtualHost *:80>
DocumentRoot "C:/Apache24/htdocs/shop"
ServerName localhost
ServerAlias localhost
<Directory "C:/Apache24/htdocs/">
AllowOverride All
</Directory>
</VirtualHost>
Instead of getting the default page from shop directory, I still getting the default page from original localhost. It seems like Apache ignore httpd-vhosts.conf file.
Apache maybe listening (in case you restarted it after editing vhosts file), but nothing points to it. You need to edit your hosts file (located under C:\Windows\System32\drivers\etc\hosts) and point domain "local.shop" to IP address 127.0.0.1 so Apache can pickup from there. You may edit that file with Notepad, opened with admin rights.
It should looks something like this:
# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
# 102.54.94.97 rhino.acme.com # source server
# 38.25.63.10 x.acme.com # x client host
# localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost
127.0.0.1 local.shop
I encountered this same issue installing Apache 2.4 on Nov. Tuesday 2nd, 2021 at 2:37 pm.
Things you need to do if you are installing Apache without WAMP or LAMP:
If you do not want to do much configurations, unzip your downloaded files on c:\Apache 2.4 as many tutorials asked you to do.
If you want to use httpd command variable you need to add c:\Apache 2.4\bin into your Environment Variables path.
THIS IS WHAT MOST PEOPLE FAILED TO ADDRESS :: If you are going to create Virtual Hosts you need to open c:\Apache 2.4\bin\conf\httpd.conf in your favorite text editor or IDE and look for the line or statement #Include conf/extra/httpd-vhosts.com and uncomment it, which means removing the # (pound) sign at the beginning. At the time of this writing is line 510. It should look as follow:
509 #Virtual hosts
510 Include conf/extra/httpd-vhosts.conf
You need to open you text editor as admin and edit c:\windows\system32\drivers\etc\hosts file and add all your virtual hosts as so, you get the idea:
127.0.0.1 vhOne.com #your virtual host server name
127.0.0.1 vhTwo.com #your second virutal host server name
After doing that close any terminal and open that again, and you should be able to go to your terminal, without having to cd to the Apache directory all the time, just by typing:
> httpd -k install
> httpd -v
> httpd -k restart
You should also be able to ping your virtual host from the terminal and expect the response desired:
ping vhOne.com
And you should be able to visit your virtual host URL. Keep in mind that at first most browsers won't recognized 127.0.0.1 URL to be web addresses but search string so you have to type http://vhOne.com with the http:// in the URL. After that first visit you can go back by typing the server name without the http://

How to modify web address in localhost using wamp?

How to change localhost/sample/index.php to dev.sample.com? I make it in ubuntu using nginx I wonder how to make it in windows using wamp server.
First you need to add this line:
127.0.0.1 dev.sample.com
To C:\Windows\System32\drivers\etc\hosts (edit with notepad).
After that, uncomment the line (around line number 512):
# Virtual hosts
#Include conf/extra/httpd-vhosts.conf
Of C:\wamp\bin\apache\apacheX.Y.Z\conf\httpd.conf by removing the inital #, so you get:
# Virtual hosts
Include conf/extra/httpd-vhosts.conf
Finally, edit C:\wamp\bin\apache\apacheX.Y.Z\conf\extra\httpd-vhosts.conf by adding this to the end of the file:
<VirtualHost *:80>
DocumentRoot "c:/YOURPROJECTPATH/sample"
ServerName dev.sample.com
</VirtualHost>
The apacheX.Y.Z stands for the Apache version your WAMP is using.
You can check more examples on how to create virtual hosts in the Apache's docs examples (WAMP = Windows+Apache+MySQL+PHP).
Once you're done with these edits, left click the WAMP icon near the window's clock, go to Apache > Service > Restart Service and you can now use the new URL. A minor suggestion: you can choose more uncommon URLs, e.g. sample.developer so you don't end up on a real website when Apache service is down.
For keeping the http://localhost/ redirection
You can leave two lines in the C:\Windows\System32\drivers\etc\hosts file:
127.0.0.1 localhost
127.0.0.1 dev.sample.com
Or you can use the same line for this:
127.0.0.1 localhost dev.sample.com
However, I suggest that you don't pile up many references in one line (4 to 5 should be ok). It's preferable to have more lines to the same IP.
Along with your new site's virtual host, you can also have this in C:\wamp\bin\apache\apacheX.Y.Z\conf\extra\httpd-vhosts.conf (above or below the other):
<VirtualHost *:80>
DocumentRoot "c:/wamp/www"
ServerName localhost
</VirtualHost>

Virtualhost setup on localhost displays indexof

I'm attempting to set up a virtual host on Windows vista32. I keep getting a listing of the root folder htdocs instead of the site I've setup.
Here's my virtual host entry in Apache httpd-vhosts.conf:
<VirtualHost *:80>
ServerName site-ashback.co.uk
ServerAlias www.site-ashback.co.uk
DocumentRoot "C:/Program Files/Apache Software Foundation/Apache2.2/htdocs/site-ashback.co.uk"
DirectoryIndex index.php
</VirtualHost>
Hosts file:
127.0.0.1 site-ashback.co.uk
None of the examples I've been using have shown a few entries which are present in my hosts file thus:
127.0.0.1 localhost
::1 localhost
These two lines appear after all the commented examples and I've put my entry after these. Does this suggest I should use'localhost' instead of 127.0.0.1?
When I click my site in the listing it takes me to the site properly but the address then reads: site-ashback.co.uk/site-ashback.co.uk/. All I wanted to do is remove the word 'localhost' from local sites addresses when testing so that the path read like the real domain name when we go live.

HOW TO: Set up a Virtual Host on Apache 2.4.4 [ MAC / *NIX ]

I am posting this because I recently had a lot of trouble setting up a Virtual Host with a MAMP stack, due to Apple's throttling of the useable PHP version on Mac OS 10.8's default Apache2 installation.
This is a very quick guide on what to do and I owe the solution to this question on Stack Overflow:
Information Source
You can think of this as a compilation of what worked for me, as the accepted answers had no effect, but rather those with a significantly lower score.
Similarly, every guide I have seen fails to mention some points which users answered with on the sourced question.
Step 1: Install and configure Apache.
Make sure you specify what port you want to listen on, for me I specified 8080. This will be the case for this series of instructions.
Listen 8080 - Default is 80
Step 2: Edit your /etc/hosts file to spoof your loopback address, 127.0.0.1
127.0.0.1 localhost
127.0.0.1 some.example # domain-name.domain-TLD
127.0.0.1 www.some.example # The same as the above line, but with www. prefixed
You should really add a handle for subdomains on your web server, Apache or Nginx (or whatever else you use. Something that routes www to non-www.
Step 3: Enable the Virtual Hosts import on Apache.
Open your httpd.conf file located within Apache2's subdirectories. Usually within /conf
Uncomment the line that resembles this: Include conf/extra/httpd-vhosts.conf
Also uncomment this module import: LoadModule log_config_module modules/mod_log_config.so
Step 4: Configure your Virtual Hosts file
Find your Virtual Hosts config, httpd-vhosts.conf, you can comment out the two example Virtual Hosts in the file. Usually within /conf/extra
Copy your own Virtual Host into the file from this template:
<VirtualHost *:80> # Change the 80 to the number Apache2 "Listen"s on. In my case, 8080
ServerName SERVER-ADDRESS # E.g. mywebsite.local
ServerAlias WWW.SERVER-ADDRESS # E.g. www.mywebsite.local
DocumentRoot " SERVER-FILE-ROOT " # E.g. "Users/user-name/Sites"
<Directory /> # This should be a full path, though
Require all granted # Required for permission errors
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride none
</Directory>
</VirtualHost>
You're Done!
Once your Virtual Host has been edited to your liking you are done, just restart Apache and enjoy.
This guide already includes the fixes implemented, but in-case you still get permission errors:
You MUST make sure that your DocumentRoot is not inside any documents your user explicitly owns. If it needs to be, give "Read Only" access to "Everyone" on Mac for that particular folder, E.g. "Documents" or "Movies" etc....
Although the above answer is much explanatory, the following 2 things are most important when you are migrating virtual hosts to apache 2.4
Go to wamp/bin/apache/apache2.4.x/conf/httpd.conf find #Include conf/extra/httpd-vhosts.conf and uncomment to Include conf/extra/httpd-vhosts.conf
Add the virtual hosts in wamp/bin/apache/apache2.4.x/conf/extra/httpd-vhosts.conf as
<VirtualHost *:80>
ServerAdmin admin#localhost.com
DocumentRoot "H:/Wamp/www/mysite"
ServerName mysite
ServerAlias mysite
<Directory />
Require all granted
</Directory>
</VirtualHost>
Note: <Directory **/**> the / is important

Set up xampp server on office lan

Im looking to set up a server using xampp within a small office. I will ofcourse secure xampp but in order to make my webapp available to the other 4 PCs on the network do i just create a virtual host? Is there a way to ensure that access to the webapp is only available on the lan?
The current setup includes 4 pcs sharing an internet connection via a router. How can i set this up as a lan which i can provide access to my webapp?
Use an .htaccess file or set up permissions in the VirtualHost tags in httpd.conf. All you have to do is put this in a file named .htaccess inside of the folder holding your webpages:
order deny,allow
deny from all
allow from <ip you want>
allow from <ip you want>
allow from <ip you want>
etc...
Perhaps you can try.
open and make sure C:/xampp/apache/conf/httpd.conf
DocumentRoot “C:/xampp/htdocs”
Options Indexes FollowSymLinks
AllowOverride all
Order Deny,Allow
Allow from all
and open C:/xampp/apache/conf/extra/httpd-vhost.conf add ..
NameVirtualHost name_site:80
<VirtualHost name_site:80>
DocumentRoot C:/xampp/htdocs/folder_site/
ServerAdmin root#gmail.com
ServerName xxx.xxx.xxx.xxx
</VirtualHost>
note: xxx.xxx.xxx.xxx is xampp computer ip.
This work on win 7.
If still dosn't work. Try to disable youe windows firewall.

Resources