One Apache virtual host resolves to index of/, while the others resolve correctly - apache

I'm having an issues trying to resolve a third virtual host. The third virtual host always resolves to index of/.
Localhost and eshop.lh both correctly resolve their own individual index files.
UPDATE 1 I've added another test vHost, this also does not resolve correctly and serves index of/. Removing eshop and localhost does not change this behaviour.
UPDATE 2
Changed loglevel config to debug.
The errors i'm getting is no matching ServerName but it seems to be looking in SSL 433 VHost configurations. The names and paths are correct, no typos.
[ssl:debug] [pid 14388:tid 1924] ssl_engine_kernel.c(2364): [client 127.0.0.1:54023] AH02044: No matching SSL virtual host for servername engine.lh found (using default/first virtual host)
It then defaults the connection to (server www.example.com:443) which is the first configures (default) SSL VHost in the httpd-ssl.conf file.
As a test, i spoofed the ServerName for eshop.lh to see if it would also serve me the SSL Vhost default, but it doesn't. It serves me localhost which is the first Vhost configuration for port 80.
Vhost configuration: Showing spoofed eshop.lh and including 433 config too, that i left out previously.
VirtualHost configuration:
*:80 is a NameVirtualHost
default server localhost (C:/xampp/apache/conf/extra/httpd-vhosts.conf:46)
port 80 namevhost localhost (C:/xampp/apache/conf/extra/httpd-vhosts.conf:46)
port 80 namevhost engine.lh (C:/xampp/apache/conf/extra/httpd-vhosts.conf:53)
port 80 namevhost test.lh (C:/xampp/apache/conf/extra/httpd-vhosts.conf:60)
port 80 namevhost eshot.lh (C:/xampp/apache/conf/extra/httpd-vhosts.conf:67)
*:443 www.example.com (C:/xampp/apache/conf/extra/httpd-ssl.conf:121)
UPDATE 3
Re-installed xampp and it appears to be working smoothly now.
My configuration:
NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot "C:/xampp/htdocs/localhost"
ServerName localhost
ErrorLog "logs/error.log"
CustomLog "logs/access.log" common
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "C:/xampp/htdocs/eshop.lh/"
ServerName eshop.lh
ErrorLog "logs/error.log"
CustomLog "logs/access.log" common
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "C:/xampp/htdocs/engine.lh/"
ServerName engine.lh
ErrorLog "logs/error.log"
CustomLog "logs/access.log" common
</VirtualHost>
127.0.0.1 localhost
::1 localhost
127.0.0.1 eshop.lh
127.0.0.1 engine.lh
The paths are correct on my machine.
It's engine.lh that won't resolve correctly. eshop.lh works fine.
The file inside the engine.lh directory are simply:
read.me
index.html
Calling httpd -t -D DUMP_VHOSTS produces:
*:80 is a NameVirtualHost
default server localhost (C:/xampp/apache/conf/extra/httpd-vhosts.conf:44)
port 80 namevhost localhost (C:/xampp/apache/conf/extra/httpd-vhosts.conf:44)
port 80 namevhost eshop.lh (C:/xampp/apache/conf/extra/httpd-vhosts.conf:52)
port 80 namevhost engine.lh (C:/xampp/apache/conf/extra/httpd-vhosts.conf:60)
I restart apache with: hpptd -k restart or via the control panel.
In the apache docs: Name-based Virtual Host support, it gives this example to serve up different vhosts:
<VirtualHost *:80>
# This first-listed virtual host is also the default for *:80
ServerName www.example.com
ServerAlias example.com
DocumentRoot "/www/domain"
</VirtualHost>
<VirtualHost *:80>
ServerName other.example.com
DocumentRoot "/www/otherdomain"
</VirtualHost>

Related

Multiple Virtual host with Port 443 pointing to wrong directory

I have created two virtual host files on ubuntu apache2
www.example1.com.conf
www.example2.com.conf
which is working fine with port 80. without https.
I have added following code for 443
VirtualHost *:443>
ServerAdmin info#example1
ServerName example1.com
DocumentRoot /var/www/www.example1.com/
#SSLEngine on
#SSLCertificateFile /etc/ssl/example1.crt
#SSLCertificateKeyFile /etc/ssl/example1.key
#SSLCertificateChainFile /etc/ssl/SectigoRSADomainValidationSecureServerCA.crt
</VirtualHost>
and
<VirtualHost *:443>
ServerAdmin info#example2
ServerName example2.com
DocumentRoot /var/www/www.example2.com/
</VirtualHost>
Now, when i am opening https://www.example2.com/, i am seeing content of example1.com.
but if i do http://www.example2.com/ then content is ok.
Can you please suggest what will be the issue ?

Virtualhosts not working, defaulting to html folder

I can't get virtualhosts to work, the server environment is RHEL
whenever I navigate to test.com it defaults to the index.html page in /www/html/ instead of the virtual server document root /var/www/test.com/public_html
below is an excerpt from the httpd.conf file:
NameVirtualHost *:80
IncludeOptional "/etc/httpd/sites-enabled/*.conf"
in /etc/httpd/sites-enabled/test.com.conf :
<VirtualHost *:80>
ServerName www.test.com
ServerAlias test.com www.test.com
DocumentRoot /var/www/test.com/public_html
</VirtualHost>
When I run httpd -S
...
port 80 namevhost www.test.com (/etc/httpd/sites-enabled/test.com.conf: 1)
alias test.com
alias www.test.com
port 80 namevhost default-http (/etc/httpd/conf.d/25-default-http.conf: 6)
...
I'm pretty new at this, any help appreciated thanks. I've also modified the hosts file on the client to point test.com to the server
When testing this on a browser it was defaulting to https which listens on port 443. I changed the listening port from 80 to 443: VirtualHost *:443 and it resolved. Alternatively I could also use http://www.test.com to view the page.

What is my mistake in configure port based virtualhost in Apache server?

I tried to configure port no based virtual host in APACHE server, i am getting below error
[root#ram conf]# service httpd restart
Stopping httpd: [ OK ]
Starting httpd: httpd: apr_sockaddr_info_get() failed for ram
httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName
[Wed Oct 14 15:11:37 2015] [warn] VirtualHost 192.168.2.3:80 overlaps with VirtualHost 192.168.2.3:80, the first has precedence, perhaps you need a NameVirtualHost directive
[Wed Oct 14 15:11:37 2015] [warn] NameVirtualHost 192.168.2.3:2233 has no VirtualHosts
(13)Permission denied: make_sock: could not bind to address [::]:2233
(13)Permission denied: make_sock: could not bind to address 0.0.0.0:2233
no listening sockets available, shutting down
Unable to open logs
[FAILED]
[root#ram conf]#
In this case my configuration file is
<VirtualHost 192.168.2.3:80>
ServerAdmin root#lopcalhost
DocumentRoot "/opt/sample"
DirectoryIndex "welcome.html"
ServerName "host.admin.com"
Listen 2233
NameVirtualHost 192.168.2.3:2233
<VirtualHost 192.168.2.3:80>
ServerAdmin root#lopcalhost
DocumentRoot "/opt/web1"
DirectoryIndex "123.html"
ServerName "host1.admin.com"
Your question is confusing. Are you sure this is not what you meant, or intended?
<VirtualHost 192.168.2.3:2233>
ServerAdmin root#lopcalhost
DocumentRoot "/opt/sample"
DirectoryIndex "welcome.html"
ServerName "host.admin.com"
Listen 2233
NameVirtualHost 192.168.2.3:2233
<VirtualHost 192.168.2.3:2233>
ServerAdmin root#lopcalhost
DocumentRoot "/opt/web1"
DirectoryIndex "123.html"
ServerName "host1.admin.com"
How are you accessing these sites? Is this a development server on a LAN, that is not your dev machine? Or a LAN server that only the LAN accesses?
Also, try commenting out this line:
# NameVirtualHost 192.168.2.3:2233
then restart apache server and see if that corrects it. Do you want apache to be listening only to port 2233 or to port 2233 and port 80?
if both, you want, I think:
Listen 2233
Listen 80
ADDED, based on comments:
Try this then:
Listen 2233
Listen 80
NameVirtualHost *
<VirtualHost 192.168.2.3:80>
ServerAdmin root#lopcalhost
DocumentRoot "/opt/sample"
DirectoryIndex "welcome.html"
ServerName "host.admin.com"
...
</VirtualHost>
<VirtualHost 192.168.2.3:2233>
ServerAdmin root#lopcalhost
DocumentRoot "/opt/web1"
DirectoryIndex "123.html"
ServerName "host1.admin.com"
...
</VirtualHost>
Does that work?
Why are you not using 127.0.01 for the local machine? The only time you need the static machine IP is if you are accessing the sites from outside your dev server, ie, a machine elsewhere in your network, or outside it, assuming port forwarding etc in the router.
By the way, I realized it's also possible you are simply unaware that you can use your local machine etc/hosts file to send requests based on the site name.
You didn't say what operating system you are using, in OSX and Unix/Linux, it's /etc/hosts (not positive about osx since I never use it, but I assume that's the same)
https://superuser.com/questions/525688/whats-the-windows-equivalent-of-etc-hosts
Sample windows path: if it's missing, just create the hosts file there and save it (I don't use windows so I don't know if this has changed).
\WINDOWS\system32\drivers\etc
Sample using /etc/hosts and 127.0.0.1:
127.0.0.1 localhost site1 site2
Then sample apache config:
ADDED, based on further comments:
With /etc/hosts:
Listen 80
NameVirtualHost *
<VirtualHost 127.0.0.1:80>
ServerAdmin root#lopcalhost
DocumentRoot "/opt/sample"
DirectoryIndex "welcome.html"
ServerName "site2"
...
</VirtualHost>
<VirtualHost 127.0.0.1:80>
ServerAdmin root#lopcalhost
DocumentRoot "/opt/web1"
DirectoryIndex "123.html"
ServerName "site1"
...
</VirtualHost>
This will work and create two sites that can be accessed separately, in your browser, like: http://site1/ http://site2/

Apache 404 with specific port

I have a LAMP server and I have to use a port other than 80.
I set "Listen 3689" and I didn't remove "Listen 80".
When I use the port 80 everything works fine but when I try the other one I got 404.
Any ideas?
you should add a virtual host with the port you want
usually there is
<VirtualHost *:80>
DocumentRoot /www/example1
ServerName www.example.com
# Other directives here
</VirtualHost>
Now you need to add
<VirtualHost *:3689>
DocumentRoot /www/example1
ServerName www.example.com
# Other directives here
</VirtualHost>

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.