2 domains pointing to 1 server (2 different subfolders) - apache

OK, so, I think the title is rather self-explanatory.
I've set up my own Apache server (on Debian) and hosting 2 different sites (let's say mysite1.com at /home/www/mysite1, and mysite2.com at /home/www/mysite2).
In my domain name registrar setup page (Dynadot actually), I created A records (still not sure what this is exactly...) for both of them, pointing to my server's ip...
Now, on the server's side, here'e what my .conf file looks like (/etc/apache2/sites-enabled/mysite actually)
<VirtualHost *:80>
DocumentRoot /home/www/mysite1.com
ServerName www.mysite1.com
ServerAdmin drkameleon#gmail.com
<Directory />
Options FollowSymLinks
AllowOverride All
</Directory>
<Directory /home/www/mysite1.com>
Options Indexes FollowSymLinks Multiviews
AllowOverride All
Order allow,deny
allow from all
</Directory>
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin
<Directory "/home/www/mysite1.com/cgi-bin">
AllowOverride All
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
</VirtualHost>
<VirtualHost *:8080>
DocumentRoot /home/www/phpmyadmin
ServerName www.mysite1.com
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /home/www/mysite2.com
ServerName www.mysite2.com
ServerAdmin drkameleon#gmail.com
<Directory />
Options FollowSymLinks
AllowOverride All
</Directory>
<Directory /home/www/mysite2.com>
Options Indexes FollowSymLinks Multiviews
AllowOverride All
Order allow,deny
allow from all
</Directory>
</VirtualHost>
Now, the thing is. Now matter which site I visit (1 or 2), I always get mysite1's contents.
What am I doing wrong? Any ideas?

There are multiple possibilities of what could go wrong.
Do you have
#
# Use name-based virtual hosting.
#
NameVirtualHost *:80
somewhere in the config? (http://httpd.apache.org/docs/2.2/mod/core.html#namevirtualhost)
Without that config entry Apache is not sensitive to domain names in dispatching requests to virtual hosts.
It's just a guess.
Make sure Apache has been reloaded after any changes to the config!

Related

Virtualhost wildcard subdomain of wildcard subdomain in apache2.4

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.

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 Virtual Hosts not working

I am trying to set up two virtual hosts for one IP address say xxx.yy.xxx.yyy
I have my files on /var/www/html/folder1 and /var/www/html/folder2
I am adding VH like :
DocumentRoot /var/www/html/folder1
ServerName xxx.yy.xxx.yyy/folder1
AllowOverride All
DocumentRoot /var/www/html/folder12
ServerName xxx.yy.xxx.yyy/folder2
AllowOverride All
As I am new on CentOS7 Apache, can't figure out how to solve it.
I do not know if I can be helpful but I locally configure so apace to have multiple domains
<VirtualHost q4y.aurecchia.dev:80>
DocumentRoot "C:\xampp\htdocs\QuestManager\AurQuest4You\public"
ServerAdmin q4y.aurecchia.dev
<Directory "C:\xampp\htdocs\QuestManager\AurQuest4You">
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
<VirtualHost csm.aurecchia.loc:80>
DocumentRoot "C:\xampp\htdocs\CSM TEST\Aur_CSM\public"
ServerAdmin csm.aurecchia.loc
<Directory "C:\xampp\htdocs\CSM TEST\Aur_CSM">
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>

Apache2 subdomain redirects to main domain

I have an EC2 instance setup running Ubuntu 14.04 and Apache. I have a single elastic IP serving multiple domains and subdomains all of which point to individual folders on the server. The problem I am having is unless the subdomain is explicitly set in my .conf it will redirect to the main domain. I can't seem to find a definitive answer here or in google.
I have a single .conf file residing in /etc/apache2/sites-enabled/ serving all of the domains and subdomains like so:
<VirtualHost *:80>
ServerAdmin me#mydomain.com
ServerName mydomain.com
ServerAlias www.mydomain.com
DocumentRoot /var/www/html/mydomain.com
<Directory /var/www/html/mydomain.com>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerAdmin me#mydomain.com
ServerName sub1.mydomian.com
ServerAlias www.sub1.mydomain.com
DocumentRoot /var/www/html/sub1.com
<Directory /var/www/html/sub1.com>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerAdmin me#mydomain.com
ServerName sub2.mydomian.com
ServerAlias www.sub2.mydomain.com
DocumentRoot /var/www/html/sub2.com
<Directory /var/www/html/sub2.com>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerAdmin me#mydomain.com
ServerName mydomain2.com
ServerAlias www.mydomain2.com
DocumentRoot /var/www/html/mydomain2.com
<Directory /var/www/html/mydomain2.com>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
</VirtualHost>
So if I go to sub1.mydomain.com or sub2.mydomain.com it gets properly routed. But if I type sub3.mydomain.com which does not exist in my .conf file it gets redirected to mydomain.com. I do not want this behavior. How do I resolve this?
You need to enable name based virtualhosts. Add below line before your first virtual host and restart apache.
NameVirtualHost :80

Multiple Virtual Sites with Wampserver

I've having a problem creating multiple virtual hosts, I've edit C:\wamp\alias\web.local to look like this:
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin webmaster#website1.local
DocumentRoot "C:/Documents and Settings/username/workspace/www.website1.com"
ServerName website1.local
<Directory "C:/Documents and Settings/username/workspace/www.website1.com">
DirectoryIndex index.php index.htm index.html
Options Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin webmaster#website2.local
DocumentRoot "C:/Documents and Settings/username/workspace/www.website2.com"
ServerName website2.local
<Directory "C:/Documents and Settings/username/workspace/www.website2.com">
DirectoryIndex index.php index.htm index.html
Options Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin webmaster#website3.local
DocumentRoot "C:/Documents and Settings/username/workspace/www.website3.com"
ServerName website3.local
<Directory "C:/Documents and Settings/username/workspace/www.website3.com">
DirectoryIndex index.php index.htm index.html
Options Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin webmaster#website4.local
DocumentRoot "C:/Documents and Settings/username/workspace/www.website4.com"
ServerName website4.local
<Directory "C:/Documents and Settings/username/workspace/www.website4.com">
DirectoryIndex index.php index.htm index.html
Options Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
and my hosts file looks like this:
127.0.0.1 localhost
127.0.0.1 website1.local
127.0.0.1 website2.local
127.0.0.1 website3.local
127.0.0.1 website4.local
I've restarted Wampserver but I don't seem to be able to access website1.local, website2.local, website3.local or website4.local. I get a 'Network Error (dns_unresolved_hostname)' error message.
Please note localhost is the only one that seems to work.
Any help appreciated.
Regards,
Stephen
EDIT
I'm running Wampserver 2.2 on Windows XP.
Couple of problems, You dont create virtual hosts by editing that file.
You use this file c:\wamp\bin\apache\apache.2.x.y\conf\extra\httpd-vhosts.conf
then you uncomment the 'Include conf/extra/chris-vhosts.conf' from within the https.conf file, its near the bottom of the file.
Now the VHOST definitions you quote above look pretty good so you can probably just move then to the correct place and you a pretty good to go.
Except for
Is is a Very Very bad idea to use directory structures that contain spaces eg
DocumentRoot "C:/Documents and Settings/username/workspace/www.website3.com"
It is much safer to place your site code in a structure something like this
C:\websites\www\website3
C:\websites\www\website4
or even
D:\websites\www\website4
Oh and one more thing, you only need one reference to
NameVirtualHost *:80
Normally the first line of the vhost definition file.