Name based virtual hosts - apache

I'm having trouble figuring out how virtual hosting works. For example lets say in my 'hosts' file I have:
127.0.0.1 localhost
127.0.0.1 mysite1.com mysite1.com mysite3.com
Does this mean that whenever I type in localhost, mysite1.com, mysite2.com, or mysite3.com into my browser URL, the page loaded will be the same for all of them?

The /etc/hosts file only has limited connection to virtual hosting of Apache. The only thing you do with it is give your host (or rather the loop back interace lo) several names. If you haven't set up anything about virtual hosting yet (which I assume) typing
http://localhost/
http://mysite1.com/
http://mysite2.com/
http://mysite3.com/
as URL in your browser will all render the same welcome page (provided you have at least set up your Apache) because in all cases the browser will try to access the web server at 127.0.0.1 which your Apache usually listens to.
To create a true virtual hosting you now need to activate this feature in the configuration file of Apache using the tags
<VirtualHost mysite1.com:80>
...
</VirtualHost>
<VirtualHost mysite2.com:80>
...
</VirtualHost>
<VirtualHost mysite3.com:80>
...
</VirtualHost>
The simplest version of a virtual hosting would be that you define a seperate document root for each host and share all other configuration items. The details of this (represented by the ...), of course, would definitely be out of scope of this answer. A good start for reading would be http://httpd.apache.org/docs/2.2/vhosts/examples.html.

Related

Apache httpd domain and ip separation

I have been trying this for hours and am getting pretty frustrated. I am trying to use a ReadyNAS Pro as a webserver. It uses Apache for its web interface. I would like to use that interface when I address the device via IP address, but I also have my domain name pointing to it. When I use my domain name I want it to display my website (with wildcards for the website's pages). I have tried using mod-rewrite without much success. It seems to break the ReadyNAS web interface. I have tried adding a named virtual host, but I don't have much experience with that and I don't know what I should use as a default virtual host to address my machine by IP. I have tried moving the ReadyNAS web interface to a sub-folder of the Apache Root, but that seems to break the web interface also.
It should be noted, that while I could create a subdomain for the web interface, I would rather address it by my machine's local IP only so it isn't visible to the outside world. While there are some simple tutorials of setting up a website on the ReadyNAS they use the ReadyNAS share system that appends a folder name to your url i.e. domain.com/www/. This is just no good.
Long story short, I want to host my website using my domain on my ReadyNAS (no sub-folders). I want to continue using the ReadyNAS web interface, but only from my local network. I do NOT want to run a second web server. I do NOT want to wipe the original OS (proprietary hardware/drivers are used for intelligent power management)
For reference it is a ReadyNAS Ultra 4+ with ReadyNAS default OS (Debian Etch).
P.S. Don't tell me this isn't what a NAS is for, I am aware and I don't care nor want to hear you cry about it.
If I did understand correctly you would like to access your own site with address like mydomain.home not mydomain.home/www I have achieved this with following config by using ReadyNas Duo
First modify Virtual.conf file from
<VirtualHost _default_:80>
to
NameVirtualHost *:80
<VirtualHost *:80>
Now you can add new virtual host config file under the addons directory ie. mydomain.conf
<VirtualHost *:80>
ServerName mydomain.home
DocumentRoot /var/www/mydomain.home
<Directory /var/www/mydomain.home>
Options Indexed
Order allow,deny
Allow from all
</Directory>
SSLEngine off
</VirtualHost>
Here is link for the original post that I did follow https://liewdaryl.wordpress.com/page/2/
I did add NameVirtualHost definition so apache would not warn about overlapping virtualhost definitions

One virtual host not responding to subdomains while all others work

I have a server at work that is running windows server 2008. I have installed wamp on it and use it to host all my sites. For multiple sites, I include the following into the httpd.conf file:
<VirtualHost *:80>
ServerName www.NewURL.com
ServerAlias NewURL.com *.NewUrl.com
DocumentRoot "X:/www/NewURL/"
</VirtualHost>
After saving the httpd.conf file, I open up my DNS Manager, add the domain, and add 2 a records. The "Name" of the first is set to * (Making the FQDN = *.NewURL.com). I change the IP address to my servers local address (Let's say, 192.158.5.5).
This process has worked great for about 40-50 URLs, even allowing me to customerName.OldURL.com and still correctly retrieving the index.php file.
I have a new site I added to my httpd.conf file as well as my DNS manager. The website is available 100% while within my work ip address range, but when I test the site at home, only the main domain (www.NewURL.com) is reachable, not clientName.NewURL.com. clientName.NewURL.com times out and is unreachable according to www.isup.me. However, NewURL.com works just fine.
I've set up NewURL.com exactly as I have all the other URLs I'm running of this server. I've event gone through and started again from scratch... with the same result.
Anybody know of anything else I can check other than the virtual host settings and the DNS manager?

Define local site in Apache - Use port other than 80

I'm wondering if anyone can help me define a new local VirtualHost using Apache.
The goal is to have a site that is viewable only from the local network (hidden behind a firewall). Right now I have Webmin installed and it runs on transaction:10000. I am trying to get the other site running on transaction:7000.
The following does not work:
DocumentRoot /var/path
Is there a better way to do this? I'm new to Apache and trying to figure this out.
Summary: Need local /var/path directory to run on a port other than 80 for local access only.
I'm assuming that you don't know to Apache Virtual Host Code. I will show you how to use it. If this didn't help you please don't hesitate to reply to me.
You must place code in your HTTPD.CONF which can be found in the conf folder. If your file is called wwwroot instead just change the name in the code below BUT DO NOT CHANGE THE NAME OF THE FOLDER!!!
NameVirtualHost 111.22.33.44
<VirtualHost *:7000>
ServerName www.domain.tld
ServerPath /domain
DocumentRoot /htdocs/domain
</VirtualHost>
What does this mean? It means that a request for any URI beginning with "/domain" will be served from the virtual host www.domain.tld. This means that the pages can be accessed as domain.tld/domain/ for all clients, although clients sending a Host: header can also access it as domain.tld/.
In order to make this work, put a link on your primary virtual host's page to http://www.domain.tld/domain/. Then, in the virtual host's pages, be sure to use either purely relative links (e.g., "file.html" or "../icons/image.gif") or links containing the prefacing /domain/ (e.g., "http://www.domain.tld/domain/misc/file.html" or "/domain/misc/file.html").
So all domains will be pointed to your IP address and based on the domain name if it is "www.domain.tld" it will take you to the folder "/htdocs/domain" or "/wwwroot/domain"
EDIT:
<Directory "/var/path/">
Order Deny,Allow
Deny from all
Allow from 127.0.0.1
</Directory>
This lets access to the "/var/path/" folder from only localhost which is where Apache is configured which is on your computer!
Good Luck!!!

404 with VirtualHost and Subdirectories with Apache (httpd)

Note: apparently there's a collision on the ServerName in my virtualhost entries and I need to make some changes to my /etc/hosts files. Looking into this but any insight is appreciated. Thanks
=======================================
(note I replaced http:// with hxxp:// to allow stackoverflow to let me post this)
I have a server, 10.1.1.1 that I am going to host multiple WordPress installations on in subdirectories. Specs on the server:
OS: RHEL 6.2 Santiago
Apache version: Apache/2.2.15 (Unix)
PHP version: 5.3.9
MySQL version 5.5.20
The first WordPress Installation I want to run from the virtual directory site_one (i.e. hxxp://10.1.1.1/site_one) -- the code for this site is to reside in /var/www/sites/site_one
The second WordPress Installation I want to run from the virtual directory site_two (i.e. hxxp://10.1.1.1/site_two) -- the code for this site is to reside in /var/www/sites/site_two
I have also dropped a dummy hello world index.html file in /var/www/sites
--
I have made the following changes to my httpd.conf:
DocumentRoot for the entire site is set to /var/www/sites i.e.:
DocumentRoot "/var/www/sites"
at the very bottom of httpd.conf I have the following VirtualHost directives (I replaced < and > with [ ] because I couldn't get stack overflow to output < blocks >:
NameVirtualHost *:80
[VirtualHost *:80]
ServerName 10.1.1.1
DocumentRoot /var/www/sites/site_one
Alias /site_one /var/www/sites/site_one/
[/VirtualHost]
[VirtualHost *:80]
ServerName 10.1.1.1
DocumentRoot /var/www/sites/site_two
Alias /site_two /var/www/sites/site_two/
[/VirtualHost]
I check the syntax of httpd (httpd -t) and the sytax is OK
I restart httpd
--
If I try to access hxxp://10.1.1.1/site_two it gives me a 404 and the error_log reports that it is looking in /var/www/sites/sites_one/site_two/ for the file... Obviously I want it to not look there but in /var/www/sites/site_two/ ... what am I doing wrong? Thanks for any and all help!
See the Apache documentation. http://httpd.apache.org/docs/2.2/vhosts/name-based.html
You are trying to use Name Based Virtual Host because you only have one IP address. This is fine but you are missing the DNS part of how this works. From the documentation: "you need only configure your DNS server to map each hostname to the correct IP address and then configure the Apache HTTP Server to recognize the different hostnames."
The first listed virtual host is always the default host. That is why your site_one will behave the way you "think" it is supposed to, however when you try to access site_two, it falls under the default virtual host.
You have the wrong idea about how Virtual Hosts work. What you are trying to do can be accomplished using aliases, however if anywhere in your code you refer to document root, you have to follow it up with which site you are referring to.
E.g. in PHP code something like this:
require($_SERVER['DOCUMENT_ROOT']."/site_one/directory/to/wherever

Multiple domains on apache server

First a quick disclaimer, I'm not a 'server guy' or a 'unix pro' or anything like that, I'm a web programmer who got stuck doing server works since I ran linux (ubuntu) on my netbook.
I'm trying to set up an apache server running on Debian to automagically serve multiple domains, each domain needs to have its own directory in /var/www.
Since this is the last thing I do for this company I really need it to be easy for my successor (who is even more a beginner at servers than I am), to create more domains without having to muck around with ssh or /etc/apache2/sites-available, so what I'm looking for is basically any magic mumbo-jumbo in default (or apt-get, or conf.d) that makes the server start serving any domain that has a matching folder in /var/www they will ofcourse have to initiate domain transfers the usual way.
I have no problem setting up domains individually.
Ick... hope the above makes sense to someone.
To serve multiple domains from Apache, you'll need Apache Virtual Hosts. You can start serving any domain that has a matching folder in /var/www" with Apache Virtual Hosts using mod_vhost_alias.
The following configuration will take the fully-qualified domain name (like www.example.org) and use it as the variable '%0'. So, to serve out 'www.example.org', you create a directory at /var/www/www.example.org/docs , and place your HTML content there. Your Cgi script will go in /var/www/www.example.org/cgi-bin/
<VirtualHost 192.168.1.100:80>
# get the server name from the Host: header
UseCanonicalName Off
VirtualDocumentRoot /var/www/%0/docs
VirtualScriptAlias /var/www/%0/cgi-bin
</VirtualHost>
Then, point 'www.example.org' to '192.168.1.100', and Apache will happily serve that Virtual Host.
Untested Code with flavor of Ubuntu
sudo a2enmod rewrite
vi /etc/apache/sites-enabled/000-default
NameVirtualHost *
<VirtualHost *>
DocumentRoot /var/www/
RewriteEngine On
RewriteRule ^(.*)$ %{HTTP_HOST}/$1
</VirtualHost>
sudo /etc/inid.d/apache2 restart