Apache: using vhosts - apache

I've added a new entry to vhosts, d3test. When I go to d3test/ in Google Chrome, the page isn't found, Oops! Google Chrome could not find d3test.
All of my other entries work fine, for example graphgram/ shows the correct site.
Here is my vhosts:
#
# Use name-based virtual hosting.
#
NameVirtualHost 127.0.0.1:80
<VirtualHost 127.0.0.1:80>
DocumentRoot /Users/donald/Projects/graphgram
ServerName graphgram
</VirtualHost>
<VirtualHost 127.0.0.1:80>
DocumentRoot /Users/donald/Projects/lookgram
ServerName lookgram
</VirtualHost>
<VirtualHost 127.0.0.1:80>
DocumentRoot /Users/donald/Projects/d3test
ServerName d3test
</VirtualHost>
Why would all entries work except the last one?

have you added that entry to /etc/hosts aswell? just in case make sure. And restart your service after that.. should work, check what the log says...

Related

Apache ServerAlias not redirecting to correct ServerName

i'm try to setup multiple wordpress sites on my Amazon EC2 instance. Here's how my httpd.conf file looks like:
<VirtualHost *:80>
ServerName www.domain1.com
ServerAlias domain1.com
DocumentRoot /var/www/html/domain1
</VirtualHost>
<VirtualHost *:80>
ServerName www.domain2.co
ServerAlias domain2.co
DocumentRoot /var/www/html/domain2
</VirtualHost>
So, when i entered domain1.com or www.domain1.com in the browser, it redirects correctly to the content i wanted and so does www.domain2.co . However, when i entered domain2.co, it doesn't directs to the ServerName www.domain2.co but to the first VirtualHost settings www.domain1.com.
Anything i'm missing out here?
Try this. Apache will default to the 1st virtual host if it doesn't find a virtualhost match which means your second VirtualHost is being ignored. We use www. as an alias and the domain as the server name. See if this helps.
<VirtualHost *:80>
ServerName domain1.com
ServerAlias www.domain1.com
DocumentRoot /var/www/html/domain1
</VirtualHost>
<VirtualHost *:80>
ServerName domain2.com
ServerAlias www.domain2.com
DocumentRoot /var/www/html/domain2
</VirtualHost>
Figured it out guys, the browser caches previous data when domain2.co points to domain1.com. So even when i have set the virtualhost correctly for domain2.co, the browser will still load the previous cached data from domain1.com.
Solution will be to clear browser data.
Found out that another factor affecting this could be because of your ISP.
Read more here: https://sg.godaddy.com/help/what-factors-affect-dns-propagation-time-1746

Apache IP Virtual Hosts

Server has two IPs, fresh centos min install. Apache is working, both ips load Apache test page. both www.domain.com and domain.com resolve to second IP.
I'd like for the first IP (192.168.0.1) to load Apache test page, this is working fine
I want the second IP (192.168.0.2) to load a website in /home/site/www
Currently when we goto domain.com or www.domain.com or 2nd IP it loads apache test page instead of the site, here's our config. Also I have the IPs listed as 192 instead of the real ips. What am I missing? Why isn't 192.168.0.2 loading /home/site/www instead of the Apache test page?
ServerRoot "/etc/httpd"
Listen 80
ServerName 192.168.0.1:80
DocumentRoot "/var/www/html"
<Directory "/var/www/html">
NameVirtualHost 192.168.0.2:80
<VirtualHost 192.168.0.2:80>
DocumentRoot /home/site/www
ServerName mydomain.com
ServerAlias *.mydomain.com
ErrorLog logs/mydomain.com-error_log
CustomLog logs/mydomain.com-access_log common
</VirtualHost>
Update
The Fix
chcon -R --reference=/var/www /home/site/www
SELinux needed the correct permissions set on it, using the reference it copies the same permissions to my new folder
Try this:
ServerRoot "/etc/httpd"
Listen 80
ServerName 192.168.0.1:80
NameVirtualHost 192.168.0.1:80
NameVirtualHost 192.168.0.2:80
<VirtualHost 192.168.0.1:80>
DocumentRoot /var/www/html
ServerName mydomain.com #change accordingly
ServerAlias *.mydomain.com
ErrorLog logs/mydomain.com-error_log
CustomLog logs/mydomain.com-access_log common
</VirtualHost>
<VirtualHost 192.168.0.2:80>
DocumentRoot /home/site/www
ServerName mydomain2.com
ServerAlias *.mydomain2.com
ErrorLog logs/mydomain2.com-error_log
CustomLog logs/mydomain2.com-access_log common
</VirtualHost>
Don't forget to apply the changes on apache.
service httpd reload or similar command.
Also, make sure the directory /var/www/html has, at least, reading permissions for the apache user.
You are missing the NameVirtualHost directive.
NameVirtualHost 192.168.0.2:80
I would also highly suggest putting in Directory directives in as well.

Apache Multiply Sites again

OK. I have LAMP server on my local development machine. I need to develop few sites on it. So, I've created two files (configs of my sites) in $APACHE/sites-available and made symlinks for them for /sites-enabled. After that I've started sudo /etc/init.d/apache2 restart. No errors or warnings. When I try mysite.dev it gives it, and when foobar.dev - it gives mysite.dev!
So, apache gives one site for all virtual hosts. How to fix it?
http://pastebin.com/qjcx6RS3 (first site)
http://pastebin.com/FdVStJm8 (second site)
You'll need to set NameVirtualHost-directive like this in your httpd.conf:
NameVirtualHost *:80
Example with two vhosts (like in your case):
NameVirtualHost *:80
<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>
... it would of course also be possible to include the vhosts from seperate files into the httpd.conf.

Apache default VirtualHost

How can I set a default VirtualHost in Apache?
Preferably, I want the default host not to be the same as the IP address host. Now I have something like this:
NameVirtualHost *
<VirtualHost *>
ServerAdmin admin#example.com
DocumentRoot /someOtherDir/
ServerAlias ip.of.the.server
</VirtualHost>
<VirtualHost *>
ServerAdmin admin#example.com
DocumentRoot /someroot/
ServerAlias example.com *.example.com
</VirtualHost *>
If a domain is forwarded to my server, but isn't in this vhost.conf file, the files from /someOtherDir/ are loaded, as expected. But I want to be able to use a different root for the IP address itself and domains which aren't added to the vhost.conf file (yet). Is this possible?
I found the answer: I remembered that Apache uses the first block if no other matching block is found, so I've added a block without a serveralias at the top of the blocks:
NameVirtualHost *
<VirtualHost *>
DocumentRoot /defaultdir/
</VirtualHost>
<VirtualHost *>
ServerAdmin admin#example.com
DocumentRoot /someOtherDir/
ServerAlias ip.of.the.server
</VirtualHost>
<VirtualHost *>
ServerAdmin admin#example.com
DocumentRoot /someroot/
ServerAlias example.com *.example.com
</VirtualHost>
If you are using Debian style virtual host configuration (sites-available/sites-enabled), one way to set a Default VirtualHost is to include the specific configuration file first in httpd.conf or apache.conf (or what ever is your main configuration file).
# To set default VirtualHost, include it before anything else.
IncludeOptional sites-enabled/my.example.com.conf
# Load config files in the "/etc/httpd/conf.d" directory, if any.
IncludeOptional conf.d/*.conf
# Load virtual host config files from "/etc/httpd/sites-enabled/".
IncludeOptional sites-enabled/*.conf
The other answers here didn't work for me, but I found a pretty simple solution that did work.
I made the default one the last one listed, and I gave it ServerAlias *.
For example:
NameVirtualHost *:80
<VirtualHost *:80>
ServerName www.secondwebsite.example
ServerAlias secondwebsite.example *.secondwebsite.example
DocumentRoot /home/secondwebsite/web
</VirtualHost>
<VirtualHost *:80>
ServerName www.defaultwebsite.example
ServerAlias *
DocumentRoot /home/defaultwebsite/web
</VirtualHost>
If the visitor didn't explicitly choose to go to something ending in secondwebsite.example, they get the default website.
Actually, I'm using Virtual host configuration (sites-available / sites-enabled) on EC2 Linux AMI with Apache/2.4.39 (Amazon). So, I have 1 EC2 instance to serve many sites (domains).
Considering that you already have Virtual Host installed and working. In my folder /etc/httpd/sites-available, I have some files with domain names (suffix .conf), for example: example.com.conf. Create a new file like that.
sudo nano /etc/httpd/sites-available/example.com.conf
<VirtualHost *:80>
ServerName www.example.com
ServerAlias example.com
DocumentRoot /var/www/html/domain
</VirtualHost>
For each file.conf in sites-available, I create a symbolic link:
sudo ln -s /etc/httpd/sites-available/example.com.conf /etc/httpd/sites-enabled/example.com.conf
This is the default configuration, so, if access directly by IP of Server, you will be redirect to DocumentRoot of the first file (.conf) in sites-available folder, sorted by filename.
To have a default DocumentRoot folder when access by IP, you have to create a file named 0a.conf, then Apache will serve this site because this new file will be the first in sites-available folder.
You must create a symbolic link:
sudo ln -s /etc/httpd/sites-available/0a.conf /etc/httpd/sites-enabled/0a.conf
To check serving order, use it:
sudo apachectl -S
Now, restart Apache, and check out it.
Obligatory - none of the previous answers worked for me. I inherited a strange combination of IP address-based virtual hosts and * vhosts (not assigned/catch all IP addresses) based virtual hosts in this Apache configuration messed up by ISPConfig.
I wanted Apache to serve not configured hosts with the same page.
I had: not configured hosts went to the first vhost after 000-default.conf. No matter I had *:80 catch all defined as the first vhost, instead of default Apache would load first defined site:
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html
</VirtualHost>
Although it's not completely valid configuration, what finally worked was adding an IP address-based virtualhost without ServerName/ServerAlias defined:
<VirtualHost 192.168.10.10:80>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html
</VirtualHost>
<VirtualHost 192.168.10.10:443>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html
SSLEngine On
...
</VirtualHost>
$ apachectl -S outputs IP address-based vhosts first, and * based vhosts later, and finally my default site is loaded before real site:
AH00548: NameVirtualHost has no effect and will be removed in the next release /etc/apache2/sites-enabled/000-default.conf:50
192.168.10.10:80 is a NameVirtualHost
default server server.tld (/etc/apache2/sites-enabled/000-default.conf:34)
port 80 namevhost server.tld (/etc/apache2/sites-enabled/000-default.conf:34)
port 80 namevhost some-site.tld (/etc/apache2/sites-enabled/100-some-site.tld.vhost:7)
...
46.23.86.103:443 is a NameVirtualHost
default server server.tld (/etc/apache2/sites-enabled/000-default.conf:38)
port 443 namevhost server.tld (/etc/apache2/sites-enabled/000-default.conf:38)
port 443 namevhost some-site.tld (/etc/apache2/sites-enabled/100-some-site.tld.vhost:182)
...
*:80 is a NameVirtualHost
default server server.tld (/etc/apache2/sites-enabled/000-default.conf:1)
port 80 namevhost server.tld (/etc/apache2/sites-enabled/000-default.conf:1)
Word of notice - in a configuration like this, * vhosts won't work, so you need to apply IP addresses to all vhosts.
An alternative setting is to have the default virtual host at the end of the config file rather than the beginning. This way, all alternative virtual hosts will be checked before being matched by the default virtual host.
Example:
NameVirtualHost *:80
Listen 80
...
<VirtualHost *:80>
ServerName host1
DocumentRoot /someDir
</VirtualHost>
<VirtualHost *:80>
ServerName host2
DocumentRoot /someOtherDir
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /defaultDir
</VirtualHost>
I had the same issue. I could fix it by adding the following in httpd.conf itself before the IncludeOptional directives for virtual hosts. Now localhost and the IP 192.168.x.x both points to the default test page of Apache. All other virtual hosts are working as expected.
<VirtualHost *:80>
DocumentRoot /var/www/html
</VirtualHost>
Reference: https://httpd.apache.org/docs/2.4/vhosts/name-based.html#defaultvhost
Only supported and correct answer is:
<VirtualHost _default_:*>
DocumentRoot "/www/default"
</VirtualHost>
or my own version to return 403:
<VirtualHost _default_:*>
<Location />
Require all denied
</Location>
</VirtualHost>
The NameVirtualHost option would be a good option.
The solution is:
# apache2.conf
# #warning this is specific to apache 2.2
NameVirtualHost *:80
Listen 80
# ...
# aaaa.example.conf
<VirtualHost *:80>
ServerName aaaa.example
DocumentRoot /defaultDir
</VirtualHost>
# host1.example.conf
<VirtualHost *:80>
ServerName host1.example
DocumentRoot /someDir
</VirtualHost>
# host2.example.conf
<VirtualHost *:80>
ServerName host2.example
DocumentRoot /someOtherDir
</VirtualHost>
In my case, to work, I created a VirtualHost (n.e. VirtualHost per CNAME) called aaaa.example since I have different files for different VirtualHosts and knowing that Apache reads them in alphabetical order.

Why does http://localhost redirect to my default virtual host once I setup virtual hosts in Apache?

This is probably an easy question, but I want to understand better how Apache works with virtual hosts. I am setting up virtual hosts because I work on multiple websites at once and I don't want to use subdirectories. I was pretty much using the default Apache httpd.conf file with the DocumentRoot pointing to something like "/www". I uncommented the virtual hosts include and added the following:
NameVirtualHost *:80
<VirtualHost *:80>
ServerName site1.dev
DocumentRoot /www/site1
</VirtualHost>
<VirtualHost *:80>
ServerName site2.dev
DocumentRoot /www/site2
</VirtualHost>
Now when I go to http://localhost I get the default page for site1.
I'm sure there is a reason why this makes sense, but I don't quite understand it. I would've thought that only requests that were explicitly to http://site1.test would get routed through that directive and it wouldn't just become the default. Can someone explain why it becomes the default.
http://httpd.apache.org/docs/1.3/vhosts/name-based.html
(Should be true for 2.x also)
"If no matching virtual host is found, then the first listed virtual host that matches the IP address will be used.
As a consequence, the first listed virtual host is the default virtual host. The DocumentRoot from the main server will never be used when an IP address matches the NameVirtualHost directive. If you would like to have a special configuration for requests that do not match any particular virtual host, simply put that configuration in a container and list it first in the configuration file."
answer 1 is correct
and i'd add with namevirtualhosts as the first entry
essentially matches any not-named elsewhere virtualhost
it should ONLY be used to catch unintentional mal-formed and broken traffic
ie a machene with one ip called john.domain.com running www.domain.com and www.domain2.com as valid webservers on ip www.xxx.yyy.zzz might have an optimal config like thus
<VirtualHost *:80>
DocumentRoot /var/webserver/static-sites/unknown/
# a directory readable by apache with only a robots.txt denying everything
ServerName bogus
ErrorDocument 404 "/errordocuments/unknown-name.html"
#custom 404 describing how/what they might have done wrong try pointing a browser {with a hosts file at http://bogus/ on 193.120.238.109 to see mine#
ErrorLog /var/log/httpd/unknown-error.log
CustomLog /var/log/httpd/unknown-access.log combined
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /var/webserver/static-sites/unknown/
# a possibly different directory readable by apache with only a robots.txt denying everything
ServerName www.xxx.yyy.zzz
ServerAlias john.domain.com
ErrorDocument 404 "/errordocuments/ip-name.html"
ErrorDocument 403 "/errordocuments/ip-name.html"
#custom 404 telling them as a likely hacker/bot you wish to have nothing to do with them see mine at http://193.120.238.109/
ErrorLog /var/log/httpd/ip-error.log
CustomLog /var/log/httpd/ip-access.log combined
</VirtualHost>
<VirtualHost *:80>
ServerName domain.com
RedirectPermanent / http://www.domain.com/
ErrorLog logs/www.domain.com-error.log
CustomLog logs/www.domain.com-access.log combined
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /var/webserver/ftpusers/domain
ServerName www.domain.com
ServerPath /domain
ErrorLog logs/www.domain.com-error.log
CustomLog logs/www.domain.com-access.log combined
</VirtualHost>
<VirtualHost *:80>
ServerName domain2.com
RedirectPermanent / http://www.domain2.com/
ErrorLog logs/www.domain2.com-error.log
CustomLog logs/www.domain2.com-access.log combined
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /var/webserver/ftpusers/domain2
ServerName www.domain2.com
ServerPath /domain2
ErrorLog logs/www.domain2.com-error.log
CustomLog logs/www.domain2.com-access.log combined
</VirtualHost>
Confirming that for Apache 2.x, the first virtual host (with the same port number) will be used if a matching virtual host is not found.
http://httpd.apache.org/docs/2.2/vhosts/details.html
"If no matching vhost could be found the request is served from the first vhost with a matching port number that is on the list for the IP to which the client connected"
You can always add this code below, put it right below NameVirtualHost *:80 so that your default document root is served by default if no other virtual hosts found.
<VirtualHost *:80>
ServerName localhost
DocumentRoot /my/default/document/root
</VirtualHost>
Simply put this code at top in httpd-vhosts.conf
<VirtualHost localhost:80>
ServerName localhost
DocumentRoot d:/xampp/htdocs
<Directory "d:/xampp/htdocs/">
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Require local
</Directory>
</VirtualHost>
One way to do this is:
In your VirtualHosts configuration, enter the specific local site name you want to enable instead of using a wildcard:
<VirtualHost site1.dev:80> instead of <VirtualHost *:80>
Switch off NameVirtualHost *:80 which can be done by commenting it out in your vhosts.conf file
In your /etc/hosts file mention both aliases for the loopback IP:
127.0.0.1 localhost site1.dev
That's it. You should see that localhost goes to the default DocumentRoot as usual and the site1.dev goes to the site you've setup as virtual host.