Apache multiple domains setup - apache

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>

Related

How to host 2 projects on 2 different subdomains using virtual hosts in MAMP

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

VirtualHost's not acting as expected with subdomain

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.

Running multiple domains on same server

I have a Ubuntu server running Apache and have 3 sites under /var/www/website/abc, /var/www/website/xyz and /var/www/website/lmn. I have 3 domains (www.abc.com, www.xyz.com, www.lmn.com) mapped to same machine (mapped same ip to 3 different domains on godaddy).
So I googled around and found this link - virtual host setup and made abc.com.conf in /etc/apache2/sites-available/ and correspondingly for other sites. Enabled the sites and then restarted apache but same site(/var/www/website/abc) appears on all 3 domains. I rechecked the paths but they seem to be correct. I can't figure out what is wrong. How can I route them to their corresponding sites?
It would be helpful in the future if you share your code (in this case the apache config files) to figure out what's wrong. In any case, this is roughly how the files can look (they don't have to look like this, there are other ways it can be configured).
First check /etc/apache2/apache2.conf and make sure you see the following code:
IncludeOptional sites-enabled/*.conf
The apache2.conf file is the primary configuration file. That line above includes all of the configuration files in the site-enabled folder. If you use a Red Hat derived OS you'll notice that the configuration file structure is different (Debian derivatives like Ubuntu like to split everything up into tons of configuration files, Red Hat derivatives keep it together)
Make sure that each of the files in the sites-enabled folder includes lines that look like this.
For abc.com.conf:
<VirtualHost *:80>
ServerName www.abc.com
DocumentRoot /var/www/website/abc
</VirtualHost>
If you also want "abc.com" to point to this virtual host enter "ServerAlias abc.com" underneath the ServerName line. What you're doing here is creating a VirtualHost block for any ip address (*) on port 80 (:80). You could replace the * in the opening VirtualHost line with your external ip address if you want to make sure that the VirtualHost is only matched to a particular ip (this is only potentially needed if there are multiple external ips pointing to your webserver). The ServerName line tells apache to match this VirtualHost whenever the Host HTTP header is www.abc.com. ServerAlias can be used to specify additional Hosts to match. Remember that www.abc.com and abc.com are treated as different Hosts. The DocumentRoot line sets the directory from which files are served.
Similarly for xyz.com.conf:
<VirtualHost *:80>
ServerName www.xyz.com
DocumentRoot /var/www/website/xyz
</VirtualHost>
If you also want "xyz.com" to point to this virtual host enter "ServerAlias xyz.com" underneath the ServerName line.
And finally for lmn.com.conf:
<VirtualHost *:80>
ServerName www.lmn.com
DocumentRoot /var/www/website/lmn
</VirtualHost>
If you also want "lmn.com" to point to this virtual host enter "ServerAlias lmn.com" underneath the ServerName line.

Running many websites on one server

This may be a duplicate question, but i have been thinking about it for long. I know, apache supports hosting many websites on a single server. But i want to know the implementation.
The server will have the single IP address. TCP is always port 80. Then how is it possible to run 10 different websites on a single machine. Also DNS, has one-to-one mapping.
I am thinking, probably some tweaking is done in HTTP protocol, but cant think of exact and best possible solution .
Thanks
You can add many VirtualHost entries in your Apache config as follows:
<VirtualHost *:80>
ServerName www.domain.tld
ServerAlias domain.tld *.domain.tld
DocumentRoot /www/domain
</VirtualHost>
<VirtualHost *:80>
ServerName www.otherdomain.tld
DocumentRoot /www/otherdomain
</VirtualHost>
This basically prompts Apache to respond differently, serving different documents based on which domain was requested.
More information can be found in the Apache docs: http://httpd.apache.org/docs/2.2/vhosts/name-based.html

Apache vhosts on localhost

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