Why doesn't this Apache virtualhost entry work? - apache

I'm running Apache 2.4.6 on Ubuntu 13.
In sites-available, I have a conf file for my site that contains only this:
<VirtualHost *:80>
# Admin email, Server Name (domain name), and any aliases
ServerAdmin somebody#somewhere.com
ServerName www.ourco.me
ServerAlias ourco.me
# Index file and Document Root (where the public files are located)
DirectoryIndex index.html index.php
DocumentRoot /home/ouruser/public/ourco.me/public
# Log file locations
LogLevel warn
ErrorLog /home/ouruser/public/ourco.me/log/error.log
CustomLog /home/ouruser/public/ourco.me/log/access.log combined
</VirtualHost>
There are no other conf files enabled. When I have use apachectl -S to display the sites, it shows:
VirtualHost configuration:
*:80 is a NameVirtualHost
default server www.ourco.me (/etc/apache2/sites-enabled/ourco.me.conf:1)
port 80 namevhost www.ourco.me (/etc/apache2/sites-enabled/ourco.me.conf:1)
alias ourco.me
port 80 namevhost www.ourco.me (/etc/apache2/sites-enabled/ourco.me.conf:1)
alias ourco.me
ServerRoot: "/etc/apache2"
Main DocumentRoot: "/var/www"
Main ErrorLog: "/var/log/apache2/error.log"
Mutex watchdog-callback: using_defaults
Mutex default: dir="/var/lock/apache2" mechanism=fcntl
Mutex mpm-accept: using_defaults
PidFile: "/var/run/apache2/apache2.pid"
Define: DUMP_VHOSTS
Define: DUMP_RUN_CFG
User: name="www-data" id=33
Group: name="www-data" id=33
The first thing I notice is the duplicate entry for namevhost www.ourco.me. And when I visit the site in a browser, I get:
You don't have permission to access / on this server.
Apache/2.4.6 (Ubuntu) Server at www.ourco.me Port 80
All the directories and files specified in the conf file exist. a2ensite and a2dissite work as expected to add/remove a symlink for this file from sites-enabled, so it's looking at the right file. Does anyone know why its directives are being ignored? Thanks.

Put Allow from all
<VirtualHost *:80>
# Admin email, Server Name (domain name), and any aliases
ServerAdmin somebody#somewhere.com
ServerName www.ourco.me
ServerAlias ourco.me
# Index file and Document Root (where the public files are located)
DirectoryIndex index.html index.php
DocumentRoot /home/ouruser/public/ourco.me/public
# Log file locations
LogLevel warn
ErrorLog /home/ouruser/public/ourco.me/log/error.log
CustomLog /home/ouruser/public/ourco.me/log/access.log combined
Allow from all
</VirtualHost>

This appears to solve the problem:
<Directory />
Require all granted
</Directory>
when placed in the VirtualHost entry.
That snippet came from this post. I'm looking at this Apache doc to see why it works.

Related

Apache 2.4 installed on windows 11 and unable to get correct browser responses to requests for NamedVirtualHosts

I have Apache Lounge httpd-2.4.54-win64-VS17.zip installed on Windows 11.
I am following instructions at [1] to set up 2 vhosts. The vhosts are defined as:
# Ensure that Apache listens on port 80
#Listen *:80
<VirtualHost 192.168.0.1:80>
#/www/example1 directory is ralative to ${ServerRoot} which is "c:/apache24"
DocumentRoot "C:/Apache24/www/bletch"
ServerName bletch.net
ServerAlias www.bletch.net
</VirtualHost>
<VirtualHost 192.168.0.1:80>
DocumentRoot "C:/Apache24/www/dorfy"
ServerName dorfy.org
ServerAlias www.dorfy.org
</VirtualHost>
In httpd.conf, the Include conf/extra/httpd-vhosts.conf is uncommented.
The contents of /etc/hosts is:
127.0.0.0 localhost
::0 localhost
192.168.0.1 bletch.net
::1 bletch.net
192.168.0.1 dorfy.org
::1 dorfy.org
My httpd.conf contains the following:
Define SRVROOT "C:/Apache24"
ServerRoot "${SRVROOT}"
Listen 80
DocumentRoot "${SRVROOT}/htdocs"
<Directory "${SRVROOT}/htdocs">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
Include conf/extra/httpd-vhosts.conf
I have a unique index.html file in the htdocs, bletch, and dorfy directories so I will know exactly which one, if any, is being displayed in browser when requested. And I am using two browsers for testing, google chrome and ms edge.
I have cleared browser cache, set browswer to recognize and use /etc/hosts for local DNS lookup, and have run ipconfig /flushdns, and restarted httpd.
I have executed httpd -t -D DUMP_VHOSTS -D DUMP_RUN_CFG and got the following report:
C:\Apache24\bin>httpd -t -D DUMP_VHOSTS -D DUMP_RUN_CFG
VirtualHost configuration:
192.168.0.1:80 is a NameVirtualHost
default server bletch.net (C:/Apache24/conf/extra/httpd-vhosts.conf:44)
port 80 namevhost bletch.net (C:/Apache24/conf/extra/httpd-vhosts.conf:44)
alias www.bletch.net
port 80 namevhost dorfy.org (C:/Apache24/conf/extra/httpd-vhosts.conf:51)
alias www.dorfy.org
ServerRoot: "C:/Apache24"
Main DocumentRoot: "C:/Apache24/htdocs"
Main ErrorLog: "C:/Apache24/logs/error.log"
Mutex default: dir="C:/Apache24/logs/" mechanism=default
PidFile: "C:/Apache24/logs/httpd.pid"
Define: DUMP_VHOSTS
Define: DUMP_RUN_CFG
Define: SRVROOT=C:/Apache24
THE PROBLEM IS:
Request for localhost works correctly in both browers.
Request for www.bletch.net and www.dorfy.org in both browsers reslults in site can't be reached.
Request for bletch.net and dorfy.org both result in display of locoalhost index.html.
I have reviewed multiple posts on StackOverFlow, and Apache users list archive and found solutions to various problems (such as the need to set browsers to use /etc/hosts DNS lookup file and other mistakes I made) but have not found what is needed at the current state explained here.
Please, can anyone tell me what how to fix this? Thanks.
[1] [Troubleshooting Vhosts][1]

virtual host setup shows default apache2 page

I have setup a new virtual host on my ubuntu 16.04 machine but it redirects me to apache's default virtual host (which is disabled).
Below is the contents of the apache virtual hosts config file;
VirtualHost configuration:
*:80 roomba.dev (/etc/apache2/sites-enabled/roomba.dev.conf:2)
*:443 is a NameVirtualHost
default server roomba.dev (/etc/apache2/sites-enabled/default-ssl.conf:2)
port 443 namevhost roomba.dev (/etc/apache2/sites-enabled/default-ssl.conf:2)
port 443 namevhost roomba.dev (/etc/apache2/sites-enabled/roomba.dev.conf:10)
ServerRoot: "/etc/apache2"
Main DocumentRoot: "/var/www/html"
Main ErrorLog: "/var/log/apache2/error.log"
Mutex watchdog-callback: using_defaults
Mutex rewrite-map: using_defaults
Mutex ssl-stapling-refresh: using_defaults
Mutex ssl-stapling: using_defaults
Mutex proxy: using_defaults
Mutex ssl-cache: using_defaults
Mutex default: dir="/var/lock/apache2" mechanism=fcntl
PidFile: "/var/run/apache2/apache2.pid"
Define: DUMP_VHOSTS
Define: DUMP_RUN_CFG
User: name="www-data" id=33
Group: name="www-data" id=33
the domain is https://roomba.dev
You do not really have a question but based on the fact that I too am trying to setup a virtual host if not several, I have found a work around for the official Ubuntu virthost setup tutorial and the associated docs . They just do not work, see these comments and the confusion that ensued. I am assuming the were providing a clean way to add virtualhosts, similar to how you add APT lists in Linux.
Define your new virtual host in the default-ssl.conf file:
<IfModule mod_ssl.c>
<VirtualHost _default_:443>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html
...
</VirtualHost>
</IfModule>
I hope that you did not delete it. After that first virtual host closing tag, add a new host as described in the tutorial instead of creating a new configuration file. For example;
<IfModule mod_ssl.c>
<VirtualHost _default_:443>
...
</VirtualHost>
<VirtualHost *:80>
#ServerAdmin admin#roomba.dev
DocumentRoot /path/to/new/virtualhost
ServerName roomba.dev
ErrorLog ${APACHE_LOG_DIR}/roomba-error.log
CustomLog ${APACHE_LOG_DIR}/roomba-access.log combined
</VirtualHost>
</IfModule>
You can add as many as you want. I am still searching for documentation that could discourage this with explanations. As again, the Apache tutorial does not work but this does, yet they bothered to write it that way, several times. There must be a reason why.
NB: do not touch any permissions. Except the original virtual server directories, based on the http error codes.

VirtualHost with two domains doesn't work

I can't configure two domains with VirtualHost, because, always show me only one domain. I mean, if I visit domainB.com that redirect me to domainA.com and domainA.com works fine. I made this procedure in anothers hosts like: DigitalOcean or my own localhost, but with this instance I can't find the issue. If I disable one site the other works without problem.
I have a google instance with Ubuntu 17.10 and Apache2 with two domains (virtualhost). I disabled the default virtualhost. My folder structure is /var/www/domainA/ and /var/www/domainB/
Cloud DNS is configured with two zones domainA.com and domainB.com with A and CNAME records point to my server IP. I have too NS records pointed to googledomains.com and in my registar I added those DNS.
If I do a ping to both domains, them resolve correctly to my IP address without problem.
I have the two domains configured in sites-available:
#in /etc/apache/sites-available/domainA.com.conf
<VirtualHost *:443>
ServerAdmin webmaster#domainA.com
ServerName domainA.com
ServerAlias www.domainA.com
DocumentRoot /var/www/domainA
ErrorLog ${APACHE_LOG_DIR}/error-domainA.log
CustomLog ${APACHE_LOG_DIR}/access-domainA.log combined
SSLEngine on
SSLCertificateFile /etc/apache2/sslcert/certified-number.crt
SSLCertificateKeyFile /etc/apache2/sslcert/domainA.com.key
SSLCertificateChainFile /etc/apache2/sslcert/gd_bundle-g2-g1.crt
<FilesMatch "\.(cgi|shtml|phtml|php)$">
SSLOptions +StdEnvVars
</FilesMatch>
<Directory /var/www/domainA/>
Options FollowSymLinks
AllowOverride All
</Directory>
<Directory /usr/lib/cgi-bin>
SSLOptions +StdEnvVars
</Directory>
</VirtualHost>
#in /etc/apache/sites-available/domainB.com.conf
<VirtualHost *:80>
ServerAdmin webmaster#domainB.com
ServerName domainB.com
ServerAlias www.domainB.com
DocumentRoot /var/www/domainB
ErrorLog ${APACHE_LOG_DIR}/error-domainB.log
CustomLog ${APACHE_LOG_DIR}/access-domainB.log combined
<Directory /var/www/domainB/>
Options FollowSymLinks
AllowOverride All
</Directory>
</VirtualHost>
Result of apachectl -S command:
sudo apache2ctl -S
VirtualHost configuration:
*:80 domainB.com (/etc/apache2/sites-enabled/domainB.conf:1)
*:443 domainA.com (/etc/apache2/sites-enabled/domainA.conf:1)
ServerRoot: "/etc/apache2"
Main DocumentRoot: "/var/www/html"
Main ErrorLog: "/var/log/apache2/error.log"
Mutex rewrite-map: using_defaults
Mutex ssl-stapling-refresh: using_defaults
Mutex ssl-stapling: using_defaults
Mutex ssl-cache: using_defaults
Mutex default: dir="/var/run/apache2/" mechanism=default
Mutex mpm-accept: using_defaults
Mutex watchdog-callback: using_defaults
PidFile: "/var/run/apache2/apache2.pid"
Define: DUMP_VHOSTS
Define: DUMP_RUN_CFG
User: name="www-data" id=33
Group: name="www-data" id=33
What other setting I must to check?
UPDATE INFORMATION: If I try with www for the domainA, works fine.
www.domainA.com -> Works
domainA.com -> Doesn't work, redirect to domainB.com
www.domainB.com -> Works
domainB.com -> Works
I had only a VirtualHost configuration in the port 443 for domainA.com.
I added a configuration *:80 with a redirection to ssl and works. Like this:
<VirtualHost *:80>
ServerAdmin webmaster#domainA.com
ServerName domainA.com
ServerAlias www.domainA.com
Redirect / https://www.domainA.com/
</VirtualHost>
Thanks Matt Raines for his comment.

Wrong DocumentRoot being used in Apache Docker container

Even though I specified a DocumentRoot in my configuration, I need to specify the sub folder included in the DocumentRoot to load URLs.
Works: 192.168.2.35/default/index.html
Doesn't work, but is wanted: 192.168.2.35/index.html
Output of docker exec -it apache httpd -S showing virtual host loaded:
me#server:/var/www/html$ docker exec -it apache httpd -S
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
VirtualHost configuration:
192.168.2.35:80 localhost (/usr/local/apache2/conf/enabled-sites/default.conf:10)
ServerRoot: "/usr/local/apache2"
Main DocumentRoot: "/usr/local/apache2/htdocs"
Main ErrorLog: "/proc/self/fd/2"
Mutex default: dir="/usr/local/apache2/logs/" mechanism=default
PidFile: "/usr/local/apache2/logs/httpd.pid"
Define: DUMP_VHOSTS
Define: DUMP_RUN_CFG
User: name="www-data" id=33
Group: name="www-data" id=33
My volumes for the Docker container:
"Binds": [
"/var/www/html:/usr/local/apache2/htdocs:rw",
"/etc/apache2/conf:/usr/local/apache2/conf:ro",
"/var/log/apache2:/usr/local/apache2/logs:rw"
],
default.conf:
<Directory /usr/local/apache2/htdocs/default>
Options -Indexes
AllowOverride All
Require all granted
</Directory>
<VirtualHost 192.168.2.35:80>
ServerName localhost
ServerAdmin webmaster#localhost
DocumentRoot /usr/local/apache2/htdocs/default
LogLevel debug
ErrorLog logs/error.default.ca.log
CustomLog logs/access.default.ca.log combined
</VirtualHost>
The IP specified for the virtualhost doesn't match the IP used by Docker.
Using *:80 in the virtualhost instead fixes it.

www.Example.com works but not root (Example.com)

I am using Apache2 with UBUNTU14 LTS, all "standard" server.
The problem is simple, when using http://www.example.com it works fine, but when using http://example.com it doesn't, it goes to the wrong index.
Describing details
I have folders at ls /var/www/
example.com example2.example html
wiki.example.com wp.example2.example
Each with different and good index.htm working with http://www.example.com (goes to /var/www/example.com) or http://wiki.example.com, etc. But the index of http://example.com goes to /var/www/html/index.htm, not to /var/www/example.com (!).
At /etc/apache2/sites-available/example.com.conf we have,
<VirtualHost *:80>
ServerAdmin mymail#ggmail.com
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
<VirtualHost *:80>
ServerName wiki.example.com
DocumentRoot /var/www/wiki.example.com
</VirtualHost>
EDIT (notes)
We are using a workaround — and please ignore the "real world" examples in the comments, it is not valid as real testing —, an ugly piece of PHP code (not a solution) in the /var/www/html/index.php of the root (no subdomain), to redirect to www subdomain,
if ( preg_match( // if HTTP from other domain, redirects
'/(mydomain1|mydomain2|mydomain3)/',
strtolower($_SERVER['HTTP_HOST']),
$m
) ) switch($m[1]) {
case 'mydomain1':
header("Location: https://www.example.com"); die('');
case 'mydomain2':
header("Location: https://www.example2.example"); die('');
case '...':
header("Location: https://www...."); die('');
}
EDIT
As #covener comment suggest, apachectl -S, results in
VirtualHost configuration:
*:80 is a NameVirtualHost
default server localhost (/etc/apache2/sites-enabled/000-default.conf:1)
port 80 namevhost localhost (/etc/apache2/sites-enabled/000-default.conf:1)
port 80 namevhost atarica.com.br (/etc/apache2/sites-enabled/atarica.com.br.conf:1)
alias www.atarica.com.br
port 80 namevhost wiki.atarica.com.br (/etc/apache2/sites-enabled/atarica.com.br.conf:13)
port 80 namevhost atarica.com.br (/etc/apache2/sites-enabled/fabrincantes.com.conf:1)
alias www.fabrincantes.com
port 80 namevhost yellowtown.org (/etc/apache2/sites-enabled/yellowtown.org.conf:1)
alias www.yellowtown.org
port 80 namevhost wiki.yellowtown.org (/etc/apache2/sites-enabled/yellowtown.org.conf:35)
ServerRoot: "/etc/apache2"
Main DocumentRoot: "/var/www"
Main ErrorLog: "/var/log/apache2/error.log"
Mutex default: dir="/var/lock/apache2" mechanism=fcntl
Mutex mpm-accept: using_defaults
Mutex watchdog-callback: using_defaults
PidFile: "/var/run/apache2/apache2.pid"
Define: DUMP_VHOSTS
Define: DUMP_RUN_CFG
User: name="www-data" id=33 not_used
Group: name="www-data" id=33 not_used
Try this:
<VirtualHost *:80>
ServerName example.com
Redirect permanent / http://www.example.com/
</VirtualHost>
Then you just need to create another section with ServerName www.example.com for your real server configuration.
Also, wouldn't CNAME's records work has an alternative to this? (I'm not sure)
Sorry all, that was my "human error", as blind spot when reading file.conf scripts, and a general blindness here after my edit showing apachectl -S results, the error is there (! see fabrincantes.com lines), a wrong name "AAA" on the "BBB" configuration:
port 80 namevhost AAA (/etc/apache2/sites-enabled/BBB.conf:1)
alias BBB
so I fixed with simple name correction on my BBB.conf, that has an ServerName AAA instead "BBB",
<VirtualHost *:80>
...
ServerName BBB
...
</VirtualHost>
You would require to edit your /etc/hosts file so it points to correct links.
For example, configuration on my localhost would be
127.0.1.1 example.com www.example.com