Virtualhost wildcard subdomain of wildcard subdomain in apache2.4 - apache

Currently, I have worked wildcard subdomain config like below:
<VirtualHost *:80>
ServerAlias *.domain
ErrorLog /tmp/error.log
CustomLog /tmp/access.log combined
VirtualDocumentRoot /var/www/%1/public
<Directory "/var/www">
DirectoryIndex index.php
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
but if I enter sub.sub.domain will return to the default vhost. How to configure to the main/root domain's path? I tried below conf but still not worked:
<VirtualHost *:80>
ServerAlias *.*.domain
ErrorLog /tmp/error.log
CustomLog /tmp/access.log combined
VirtualDocumentRoot /var/www/%2/public
<Directory "/var/www">
DirectoryIndex index.php
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
I changed %2 to %1 still doesn't work.
What is right syntax?

The above code has the right syntax but I just put it to the wrong placement order. So the correct answer is to put the second one to the top above the first one.
Below full config example :
<VirtualHost *:80>
ServerAlias *.*.domain
ErrorLog /tmp/error.log
CustomLog /tmp/access.log combined
VirtualDocumentRoot /var/www/%2/public
<Directory "/var/www">
DirectoryIndex index.php
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerAlias *.domain
ErrorLog /tmp/error.log
CustomLog /tmp/access.log combined
VirtualDocumentRoot /var/www/%1/public
<Directory "/var/www">
DirectoryIndex index.php
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
Apache is skipping/ignore the same subdomain/domain config by ascending order from the top. sub.sub.domain is also a part of *.domain , so if I want to configure it I must put the config in the top or if use different file config use lower number name.
Multiple ServerAlias or ServerName or NameVirtualHostlines will yield a "NameVirtualHost *:80 has no VirtualHosts" warning. Apache will ignore the second directive and use the first defined NameVirtualHost line, though. This seems to happen when one is using multiple virtual host configuration files and doesn't understand that you only need to define a particular NameVirtualHost line once
reference
Simple solution but I workaround for some hours and I hope it helps other people like me.

Related

Why isn't my apache httpd server serving content from my VirtualHost

I have an apache httpd server running on a linux server. The server's fqdn is www.example.com, but I have a dns alias of www.example2.com for the same server.
When I access the server using my browser and use http://www.example.com it serves the correct content, but if I use http://www.example2.com, I get the default content from my /var/www/html folder not the content from the /var/www/html2 folder.
A stripped-down copy of my httpd.conf file follows:
ServerName www.example.com:80
DocumentRoot "/var/www/html"
<Directory "/var/www/html">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
CustomLog "logs/access_log" combined
ErrorLog "logs/error_log"
LogLevel warn
<VirtualHost www.example2.com:80>
ServerName www.example2.com:80
DocumentRoot /var/www/html2
CustomLog "logs/access_log2" combined
ErrorLog "logs/error_log2"
<Directory "/var/www/html2">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerName www.example2.com
DocumentRoot /var/www/html2
CustomLog "logs/access_log2" combined
ErrorLog "logs/error_log2"
<Directory "/var/www/html2">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
The VirtualHost tag is for IP based filtering, to filter by host\server name use the ServerName directive.

My apache2 VirtualHost keep showing "It's working" instead of my site

I'm doing a project for school where I have to host 2 pages on a web server. I chose to do a VirtualHost using Apache2 but when it keeps showing me a "It works" page. The page is located here.
We are using a DNS server but the probleme seems to not be coming from this one.
Here is one of the .conf file :
<VirtualHost *:80>
ServerAdmin jongen.philemon#wt5.ephec-ti.be
ServerName wt5.ephec-ti.be
ServerAlias www.wt5.ephec-ti.be
DocumentRoot /var/www/monsite
<Directory /><br>
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/monsite>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
</VirtualHost>
I also changed the apache2.conf like this:
<VirtualHost *:80>
ServerAdmin jongen.philemon#wt5.ephec-ti.be
DocumentRoot /var/www/site
<Directory /var/www/site/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order deny,allow
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
You can also check our wiki on the GitHub to see all the steps I followed.
Thank's
I got to fix it by re-doing it using a Dockerfile. I uptated the files and the wiki on my github if you have the same issue !

Multiple Websites on Apache2

I have my apache configured to have 2 websites setup. I have the following in my apache2.conf
Include /opt/bitnami/apps/www.website1.com/conf/app.conf
Include /opt/bitnami/apps/www.website2.com/conf/app.conf
Here are the app.conf for the 2 websites
Website1
<VirtualHost *>
DocumentRoot /opt/bitnami/apps/www.website1.com/htdocs
ServerName www.website1.com:80
ServerAlias website1.com
ErrorLog /opt/bitnami/apps/www.website1.com/log/error.log
CustomLog /opt/bitnami/apps/www.website1.com/log/access.log common
<Directory "/opt/bitnami/apps/www.website1.com/htdocs">
Options Indexes MultiViews
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
<VirtualHost *>
DocumentRoot /opt/bitnami/apps/www.website1.com/htdocs
ServerName website1.uat.com:80
ServerAlias website1.uat.com
ErrorLog /opt/bitnami/apps/www.website1.com/log/error.log
CustomLog /opt/bitnami/apps/www.website1.com/log/access.log common
<Directory "/opt/bitnami/apps/www.website1.com/htdocs">
Options Indexes MultiViews
AllowOverride All
Order allow,deny
Allow from all
</Directory>
Website 2
<VirtualHost *>
DocumentRoot /opt/bitnami/apps/www.website2.com/htdocs
ServerName www.website2.com:80
ServerAlias www.website2.com
ErrorLog /opt/bitnami/apps/www.website2.com/log/error.log
CustomLog /opt/bitnami/apps/www.website2.com/log/access.log common
<Directory "/opt/bitnami/apps/www.website2.com/htdocs">
Options Indexes MultiViews
AllowOverride None
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
<VirtualHost *>
DocumentRoot /opt/bitnami/apps/www.website2.com/htdocs
ServerName website2.com:80
ServerAlias website2.com
ErrorLog /opt/bitnami/apps/www.website2.com/log/error.log
CustomLog /opt/bitnami/apps/www.website2.com/log/access.log common
<Directory "/opt/bitnami/apps/www.website2.com/htdocs">
Options Indexes MultiViews
AllowOverride None
Order allow,deny
Allow from all
</Directory>
Im testing these locally so i have my host setup below
xx.xxx.xx.xxx www.website1.com
xx.xxx.xx.xxx www.website2.com
When i go to www.website2.com, apache2 page pops up
When i go to www.website1.com, apache2 page pops up
When i go to www.website2.com/htdocs, i see the correct website and it works. When i got to www.website1.com/htdocs, i see website2. I dont understand why i am seeing website 2 here.
My first guess would be that you should remove the port :80 from ServerName and also change
<VirtualHost *>
to
<VirtualHost *:80>
Like so:
<VirtualHost *:80>
DocumentRoot /opt/bitnami/apps/www.website1.com/htdocs
ServerName www.website1.com
...
As prerik says use "VirtualHost *:80"
Also if it is Apache HTTPD 2.2.x, it needs "NamedVirtualHosts *:80" defined "once" in the config when several virtualhosts are present using the same ip:port scheme, if you don't add this all your requests will land in the first defined virtualhost.

Subdomain answer to specific folder in Apache

I want to configure an Apache vhost. Practically Basically I want by typing the address files.yyyyyy.ltd answer me the folder destination /var/www/html/alpha/files
This is apache configuration:
<VirtualHost *:80>
DocumentRoot /var/www/html/alpha/files
ServerName files.yyyyyy.ltd
ServerAlias files.yyyyyy.ltd
<Directory /var/www/html/alpha/files >
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Order allow,deny
allow from all
</Directory>
# Other directives here
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
DNS configuration:
yyyyyy.ltd. IN A XXX.XXX.XXX.XXX
www IN CNAME yyyyyy.ltd.
yyyyyy.ltd. IN CNAME files.yyyyyy.ltd.
What is wrong? Do you recommend some other method to achieve the same result?
Thk for help
DNS configuration
you need to change
yyyyyy.ltd. IN CNAME files.yyyyyy.ltd.
to
files IN CNAME yyyyyy.ltd.
you can also set all as A record like
yyyyyy.ltd. IN A XXX.XXX.XXX.XXX
www.yyyyyy.ltd. IN A XXX.XXX.XXX.XXX
files.yyyyyy.ltd. IN A XXX.XXX.XXX.XXX
What i would do to have a faster name resolution.
Apache configuration
Your Apache configuration is correct, but some stuffs are useless:
<VirtualHost *:80>
DocumentRoot /var/www/html/alpha/files
ServerName files.yyyyyy.ltd
<Directory /var/www/html/alpha/files >
Options Indexes FollowSymLinks MultiViews
AllowOverride all
</Directory>
# Other directives here
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
AllowOverride all is there to allow .htaccess, if you don't want .htaccess files, you can remove this command from your configuration.

Apache vhost not working for subdomains

I have this configuration but both url app.test.com & stage.test.com
redirect to same code/deployment
<VirtualHost *:80>
ServerName app.test.com
DocumentRoot /var/www/html/Test-Prod/web
<Directory "/var/www/html/Test-Prod/web">
Options Indexes FollowSymLinks
AllowOverride All
</Directory>
ErrorLog logs/test-prod__error_log
CustomLog logs/test-prod_access_log common
</VirtualHost>
<VirtualHost *:80>
ServerName stage.test.com
DocumentRoot /var/www/html/Test/web
<Directory "/var/www/html/Test/web">
Options Indexes FollowSymLinks
AllowOverride All
</Directory>
ErrorLog logs/test-website_error_log
CustomLog logs/test-website_access_log common
</VirtualHost>
The usual error for this is leaving out the NameVirtualHost directive if you're still using httpd 2.2
Add the following in your config file and it'll probably work
NameVirtualHost *.80
You might want to read the documentation for Named-based Virtual Host Support with httpd 2.2.
NameVirtualHost *.80
<VirtualHost localhost:80>
ServerName color
ServerAlias localhost
ServerPath "C:/wamp/www/subwww/color"
DocumentRoot "C:/wamp/www"
<Directory "C:/wamp/www/subwww/color">
Options Indexes FollowSymLinks
AllowOverride All
</Directory>
</VirtualHost>
in above code, subdomain name is color
and the url is http://color.localhost/
if the operating system is windows then add "127.0.0.1 color.localhost" in "C:/windows/system32/dirvers/etc/hosts" with notepad run as administration