Apache settings to serve two sites in different directories - apache

I am not very good with apache, as I've been an IIS guy for years.
I have two websites in the following directories:
/htdocs/ (site 1)
/htdocs/site2 (site 2)
Doing searches on the web does not give any understandable info on what to do other than a vhost file, etc. I've tried a few things with no luck.
What file(s) do I need to update so that I can set:
www.site1.com - /htdocs/
www.site2.com - /htdocs/site2

There are two parts to creating the solution: setting up your virtual hosts and then creating the right document roots for each host.
This can all be done in httpd.conf, but more cleanly as separate files in directory conf.d
So in site-1.conf you'd have these statements:
<VirtualHost www.site1.com:80>
ServerName www.site1.com
ServerAdmin me#myemail.com
DocumentRoot /var/www/htdocs
ErrorLog /var/log/httpd/site1.error.log
</VirtualHost>
And in site-2.conf you'd have this:
<VirtualHost www.site2.com:80>
ServerName www.site2.com
ServerAdmin me#myemail.com
DocumentRoot /var/www/htdocs/site2
ErrorLog /var/log/httpd/site2.error.log
</VirtualHost>

Related

Virtual host points to global document root

I have a CentOS 7 server with apache. I have multiple virtual hosts running on the server. But one refuses to use the DocumentRoot that are specified for the virtual host in the http.conf file. It will only use the global DocumentRoot that are configured higher up in the httpd.conf file.
Have anyone else come across the same issue. I have been trying for a while now and i am all out good ideas.
Here is an example:
This works:
ServerAdmin uc#site1.com
DocumentRoot /var/www/site1.com/public_html
ServerName site1.com
ServerAlias *.site1.com
ErrorLog logs/site1.com-error_log
CustomLog logs/site1.com-access_log common
This doesn't work
ServerAdmin uc#site2.com
DocumentRoot /var/www/site2.com/public_html
ServerName site2.com
ServerAlias *.site2.com
ErrorLog logs/site2.com-error_log
CustomLog logs/site2.com-access_log common

Unsure of what to add to hosts file - Apache Subdomain

I'm trying to make a subdomain for my xampp/apache hosted website.
However, whenever I attempt to access the subdomain it gives me an "HTTPS insecure error" and redirects me to the main part of the site. Also, I can still access the main site without any issues at all.
In my 'httpd-vhosts.conf' file, I believe all the entries are correct and the issue lies with my hosts file.
To clarify, this is a website with a premium domain name, (i.e not just a website for local use). Most of the tutorials and guides I can find are only really designed for websites that are used locally only.
Here is my 'httpd-vhosts.conf' file (with my domain name edited out of course):
<VirtualHost *:80>
DocumentRoot "C:/xampp/htdocs/"
ServerName domain.xyz
ServerAlias https://domain.xyz
ErrorLog "logs/domain.xyz-error.log"
CustomLog "logs/domain.xyz-access.log" common
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "C:/xampp/htdocs/sub1"
ServerName sub1.domain.xyz
ServerAlias https://sub1.domain.xyz
ErrorLog "logs/sub1.domain.xyz-error.log"
CustomLog "logs/sub1.domain.xyz-access.log" common
</VirtualHost>
The entries I have in my hosts file are (which I'm fairly certain is completely wrong):
127.0.0.1 localhost
127.0.0.1 domain.xyz
127.0.0.1 sub1.domain.xyz

What is the procedure to add domain aliases to an existing linux apache installation?

I have a personal VPS hosted in * and an ubuntu installation. The ubuntu runs apache,php,mysql and is currently being used for 5 websites mapped by virtualhosts. I am writing the whole procedure in case someone needs it.
When I want to add a new domain, I create an 127.0.0.1 test.com *.test.com row in /etc/hosts, add a new file in /etc/apache2/sites-available and run a2ensite test.com - then restart apache. Each website has its own folder in /var/www and the virtualhost entry looks like this :
<Virtualhost *:80>
# Admin email, Server Name (domain name) and any aliases
ServerAdmin info#test.com
ServerName www.test.com
ServerAlias test.com *.test.com
# Index file and Document Root (where the public files are located)
DirectoryIndex index.html index.php
DocumentRoot "/var/www/test.com"
<Directory /var/www/test.com>
Options FollowSymLinks
AllowOverride All
Order allow,deny
allow from all
</Directory>
</Virtualhost>
I am planning to add some aliases like aaa.test.com, bbb.test.com, ccc.test.com etc. which will point/forward to different folders. The aaa.test.com one will point to /var/www/aaa/index.php and the bbb.test.com to /var/www/bbb/index.php. To summarize, different aliases - same domain - different folders all in apache. How do I achieve that ?
There can only exist one DocumentRoot per VirtualHost container. Since you have specified different DocumentRoot for each aaa.test.com, bbb.test.com etc, you need to setup a separate VirtualHost for each:
<VirtualHost *:80>
ServerName aaa.test.com
DocumentRoot /var/www/aaa
DirectoryIndex index.php index.html
...
</VirtualHost>
and so on.
As aaa.test.com and bbb.test.com should point to different directories, You are required to create separate Virtualhost entries manually. Before that you have to remove the _*.test.com_ from the ServerAlias of test.com Virtualhost entry. Then create a file at /etc/apache2/sites-available , say aaa.test.com and add the following and then save
<Virtualhost *:80>
ServerName aaa.test.com
DirectoryIndex index.html index.php
DocumentRoot "/var/www/aaa/"
</Virtualhost>
make sure to restart/reload the apache service.
Do the same for bbb.test.com.. That is all you required to do... All d best :)

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.

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.