Here is a simplification of my setup. I have a default VirtualHost with no ServerName or ServerAliases (000-default.conf in sites-enabled):
<VirtualHost *:80>
DocumentRoot /var/www/html
</VirtualHost>
Then I have another one (stuff.joe.conf):
<VirtualHost *:80>
DocumentRoot /var/www/stuff/
ServerName stuff.joe.com
</VirtualHost>
In my mind, I think I have a pretty good idea of how this should work.
Accessing http://joe.com/ properly takes me to the default virtual host and points in the right place (/var/www/html/).
Accessing http://stuff.joe.com/index.html finds the secondary virtual host config and takes me to where I would expect it to go (/var/www/stuff/index.html).
However... accessing http://stuff.joe.com/, contrary to what I would think, matches up with the default virtual host and takes me to /var/www/html/
This seems odd. Am I missing something here? I would expect it to either catch the stuff.joe.com config or not in both cases. Why does it act differently in the two cases?
I was missing the DirectoryIndex directive on the secondary virtual host config and so it was falling back to the default virtual host.
Related
I'm not quite sure what I'm trying to do is even possible.
Currently I'm using MAMP to host my projects myself. These projects are hosted and can be accessed by people who know my IP address when they type my IP address in their URL bar. When they access my IP, my they see a list of my project directories like this:
I am wondering if it is possible to make it so that when someone types charter.54.135.14.176 he sees the content of the charter folder and when he types LeagueOfLegendsFrontend.54.135.14.176 he is presented the content of League Of Legends Frontend folder.
I've been looking at tutorial videos about virtual hosts and I was left confused. I understand I have to do 2 things:
First, go to httpd-vhosts.conf and create a virtual host for each project. Something like this:
<VirtualHost *:80>
ServerName ???
ServerAlias ???
DocumentRoot "C:\MAMP\htdocs\Charter"
</VirtualHost>
<VirtualHost *:80>
ServerName ???
ServerAlias ???
DocumentRoot "C:\MAMP\htdocs\League Of Legends Frontend"
</VirtualHost>
I'm kind of clueless what am I meant to write on ServerName and ServerAlias
And secondly, I need to go to the hosts file and add this:
54.135.14.176 ???
And lastly I need to include virtual hosts by uncommenting Include conf/extra/httpd-vhosts.conf in httpd.conf file. Sadly I'm stuck on the rest.
You need to allocate two different IP addresses for your projects. So, the desired configuration may look like this:
<VirtualHost 127.0.0.2:80>
ServerName charter.zxc
DocumentRoot "C:\MAMP\htdocs\Charter"
</VirtualHost>
<VirtualHost 127.0.0.3:80>
ServerName lol-frontend.zxc
DocumentRoot "C:\MAMP\htdocs\League Of Legends Frontend"
</VirtualHost>
You, of course can add directory settings, logs and other things if necessary, but I only show the very basic setup. Then in your hosts file add:
127.0.0.2 charter.zxc
127.0.0.3 lol-frontend.zxc
Let assume that blah.com, blah2.com all point to the same server with IP=5.31.111.7.
I would like that:
accessing blah.com serves /var/www/site1
accessing blah2.com serves /var/www/site1
accessing 5.31.111.7 serves /var/www/site2
I tried
<VirtualHost *:80>
DocumentRoot /var/www/site1
</VirtualHost>
<VirtualHost 5.31.111.7:80>
DocumentRoot /var/www/site2
</VirtualHost>
but now everything goes to /var/www/site2, which is not what I wanted.
How to configure the VirtualHost, such that the served website depends on the URL ?
PS: why should I do this in /etc/apache2/sites-enabled/000-default instead of /etc/apache2/apache2.conf ? I don't understand this sites-enabled / sites-available/default naming... Why are there so many different config files by default on Debian, for such a simple thing?
What you want to do is called Name-Based Virtual Hosting, you'll need
NameVirtualHost *:80
to enable it on port 80, and for each VirtualHost, you need to give the name(s):
<VirtualHost *:80>
ServerName blah2.com
ServerAlias www.blah2.com
DocumentRoot /var/www/site1
</VirtualHost>
Note that there are limitations on SSL/TLS when doing name-based virtual hosting, but it's a bit of a moot point since post-POODLE, people start to require TLS anyway, so ancient browsers are out of luck anyway.
As to the config files, it's very very useful to have two classes of config files: the ones with defaults that a package update will overwrite, and your local ones that it will not touch, or better even, a directory full of the former and a directory full of the latter. (Because additional packages might want to make configuration settings, they'll all install in the former place, and you should only ever change/override config in the second place.)
I need a way to make all server names point to one document root without the need to manually type them all, maybe something like this:
<VirtualHost *:80>
DocumentRoot /www/example1
ServerName *
</VirtualHost>
Will that work?
No, you can't write ServerName *.
All you have to do is put the DocumentRoot in the first virtual host that gets read in your configuration. The Apache Name-Based Virtual Hosts guide explains that any server whose name doesn't match the ServerName in any virtual host configuration will be served by the default host, so it will get the DocumentRoot you want.
I've got a pretty straightforward issue with a linux based Apache 2.2 server I am setting up. I want to setup two totally different domains on the same server.
But it only serves content from the first tag! I've searched StackOverflow and read items at Apache.org but no luck.
I followed the directions on Apache.org and put these two sections at the bottom of my http.conf file.
<VirtualHost *:80>
DocumentRoot /var/www/mydomain1
ServerName sub1.mydomain1.com
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /var/www/mydomain2
ServerName sub2.mydomain2.com
</VirtualHost>
Now when I use a browser to go to: http://sub1.mydomain1.com it comes up fine. But if I go to http://sub2.mydomain2.com I still only get the content that located in /var/www/webfiles/mydomain1.
I did many of the obvious things such as:
- service httpd restart
- I changed the order of the two entries in my httpd.conf and once again, it only serves the first one in the list.
- One support doc I had Googled said to make sure to have the following entry point to a valid domain on your system. So I entered this (but it didn't change anything):
ServerName sub2.mydomain2.com:80
It must be something silly but I can't figure it out!
Ok, I figured it out. It was pretty silly. I just needed to uncomment this line so I would actually use all the virtual hosts:
NameVirtualHost *:80
You need to set up the two domains in two separate virtual hosts. Generally when I do this I like to split off an include directory full of virtual host files, with each file containing one virtual host.
<VirtualHost *:80>
ServerName site1.com
DocumentRoot "/var/www/site1"
</VirtualHost>
<VirtualHost *:80>
ServerName site2.com
DocumentRoot "/var/www/site2"
</VirtualHost>
I managed to set up virtual hots on my local machine, but I kinda run into a wall now.
Normally, when you type localhost/ into browser you will see what you are supposed to see. But after I have set my virtual hosts, anything I type goes to the vhost. I cant figure a way to have a virtual host AND the old functionality together.
Here is my vhost file (btw, I am using xampplite)
<VirtualHost domain.eu>
ServerName domain.eu
DocumentRoot /www/domain
ServerAlias *.domain.eu
</VirtualHost>
<VirtualHost domain.sk>
ServerName domain.sk
DocumentRoot /www/domain
</VirtualHost>
The second one gets redirected to sk.domain.eu via htaccess. When I add these 3 lines to vhosts, localhost starts working, but even the other vhosts go to /www/
<VirtualHost localhost>
DocumentRoot /www
</VirtualHost>
But to comment/uncomment these 3 lines everytime I need to localhost is stupid. Any advice how can I keep both of them working together?
Thanks for your time
You really shouldn't be using domain names in the VirtualHost declarations.
If these three virtual hosts have different IP address, you should be putting their respective IP addresses into the VirtualHost blocks, and never mention NameVirtualHost.
If they use the same IP address, you must be using NameVirtualHost, and then you must, in each virtual host, repeat the name in the very same spelling that you did in the NameVirtualHost declaration.
Try adding this before the VirtualHost containers:
NameVirtualHost localhost
NameVirtualHost domain.sk
NameVirtualHost domain.eu