Trial Setup of Virtual Hosts. (One Web Server With Many Sites)
by Computothought in Circuits > Websites
2959 Views, 4 Favorites, 0 Comments
Trial Setup of Virtual Hosts. (One Web Server With Many Sites)
One nice thing about this project is that for home everyone can have not only their own web site, they can have several of them.
Note: This project assumes you already have all the Apache modules you need. If you run into a problem please get expert help if you are unsure as to what to do.
DNS.
$ sudo nano /etc/hosts
[code]
127.0.0.1 localhost oesrvr1
127.0.1.1 oesrvr1 www.softserv.com
192.168.1.31 oesrvr1 www.softserv.com
192.168.1.32 oesrvr3
192.168.1.99 printer1
192.168.1.98 printer2
192.168.1.30 nas
# The following lines are desirable for IPv6 capable hosts
...
[/code]
Enable Virtual Hosts.
$ sudo nano /etc/apache2/conf.d/virtual.conf
[code]
#
# enable multiple virtual hosts
#
NameVirtualHost *
[/code]
Modify the Default File.
$ sudo nano /etc/apache2/sites-enabled/000-default
[code]
NameVirtualHost *
<VirtualHost *>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
# This directive allows us to have apache2's default start page
# in /apache2-default/, but still have / go to the right place
#RedirectMatch ^/$ /apache2-default/
</Directory>
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>
ErrorLog /var/log/apache2/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog /var/log/apache2/access.log combined
ServerSignature On
Alias /doc/ "/usr/share/doc/"
<Directory "/usr/share/doc/">
Options Indexes MultiViews FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
Allow from 127.0.0.0/255.0.0.0 ::1/128
</Directory>
</VirtualHost>
<VirtualHost *>
ServerName oesrvr1
ServerAlias oesrvr1
ServerAdmin eddie@oesrvr1
DocumentRoot /var/www
<Directory /var/www >
Options FollowSymLinks
AllowOverride None
</Directory>
</VirtualHost>
<VirtualHost *>
ServerName www.softserv.com
ServerAlias www.softserv.com
ServerAdmin eddie@oesrvr1
DocumentRoot /var/www/www.softserv.com/html
<Directory /var/www/www.softserv.com/html >
Options FollowSymLinks
AllowOverride None
</Directory>
</VirtualHost>
[/code]
Repeat for Each Virtual Host
$ sudo mkdir -p /var/www/www.softserv.com/html
or
$ sudo mkdir /var/www/www.softserv.com
$ sudo mkdir /var/www/www.softserv.com/html
Create server detail file for each file in /etc/apache2/sites-available/
oesrvr1
[code]
<VirtualHost *>
ServerName oesrvr1
ServerAlias oesrvr1
ServerAdmin eddie@oesrvr1
DocumentRoot /var/www
<Directory /var/www >
Options FollowSymLinks
AllowOverride None
</Directory>
</VirtualHost>
[/code]
www.softserv.com
[code]
<VirtualHost *>
ServerName www.softserv.com
ServerAlias www.softserv.com
ServerAdmin eddie@oesrvr1
DocumentRoot /var/www/www.softserv.com/html
<Directory /var/www/www.softserv.com/html >
Options FollowSymLinks
AllowOverride None
</Directory>
</VirtualHost>
[/code]
Set a link for each file in /etc/apache2/sites-enabled
$ cd /etc/apache2/sites-enabled
$ sudo ln -s ../sites-available/oesrvr1
$ sudo ln -s ../sites-available/www.softserv.com
Enable sites;
$ sudo a2ensite oesrvr1
$ sudo a2ensite www.softserv.com
Prepare Web Pages.
Create index.html in each hosts DocumentRoot. My oesrvr1 web pages were already set up.
$ sudo nano /var/www/www.softserv.com/html/index.html
index.html
[code]
<html>
<body>
Hello world!
</body>
</html>
[/code]
Now that we have all the files edited, we need to reload and restart server
$ sudo /etc/init.d/apache2 reload
$ sudo /etc/init.d/apache2 restart
Do It to It.
$ sudo /etc/init.d/apache2 reload
$ sudo /etc/init.d/apache2 restart
Let's test the sites. You will have to use your site addresses that you set up. You will not be able to get to mine. Fire up your web browser and to to the two set up sites. If all has gone well you should have two different sites.
$ firefox oesrvr1 &
$ firefox www.softserv.com &
Good Luck.
Website Name Test.
$ nslookup www.testname.com
Server: 192.168.1.1
Address: 192.168.1.1#53
Non-authoritative answer:
Name: www.testname.com
Address: 208.87.35.104
Redo.
Figured out why I was having problems. One of the website names was an actual site on the net and the way the dns was set up. the outside site took priority. So be careful of your website names and you should be ok.
NameVirtualHost
With the default configuration you are only serving up one site, and that site is based on your IP address. What I’m setting up is name-based virtual hosting, meaning the Apache server will serve specific content based on the domain name requested. In this way a single server can host multiple sites, and serve up unique content based on the domain requested.
My preferred method of using name based virtual hosting is creating a seperate file for each domain. These can all be done within one file, but I’ll be creating a new file for each site.
First we need to define to Apache that we’re using name based virtual hosting instead of IP based. You can append the following line to your /etc/apache2/apache2.conf to define this:
NameVirtualHost ip.address:port
The above should be your public facing IP address (assuming you’re creating a public site), and port is generally port 80 by default. After this we’ll create the base configuration for your virtual hosts. Debian and Ubuntu use /etc/apache2/sites-available/ and /etc/apache2/sites-enabled/ directories for defining virtual hosting. One nice thing about this is that you can have more sites “available” than you have “enabled”, meaning not everything configured is actually live and listening. This is nice to quickly disable a site for whatever reason.
I like to create unique files for each of my domains within the /etc/apache2/sites-available/ folder. For example I have a file called “ubuntu-tutorials.com” in that directory, with the following contents:
<VirtualHost 67.207.131.28:80><br /> ServerName ubuntu-tutorials.com<br /> ServerAlias www.ubuntu-tutorials.com
/> ServerAdmin christer.edwards@ubuntu.com<br /> DocumentRoot /var/www/ubuntu-tutorials.com/html<br /> </VirtualHost>
What these settings do is as follows:
- ServerName listens for requests asking for a certain domain
- ServerAlias defines any additional domains that should match
- ServerAdmin is the contact for the site
- DocumentRoot is the path to the content for that site
Now that this file is created in the /etc/apache2/sites-available/ folder we’re just about ready to start, but we need to enable it. We can do that by creating a symbolic link from one folder to the next.
cd /etc/apache2/sites-enabled/<br /> ln -s ../sites-available/ubuntu-tutorials.com .
This site is now available (as in configured) and enabled (as in listening) once we restart the apache service:
sudo /etc/init.d/apache2 restart
Testing
To test your configuration you can, temporarily, configure your /etc/hosts file to point the domain to your IP address and see if your server loads up the correct site. This is only needed if the hostname or domain name does not already resolve to your IP address. Editing the /etc/hosts by adding the following line:
ip.address websitename
67.207.131.28
www.ubuntu-tutorials.com
Open your browser, try to access domain.tld and see if it loads the contents from your local DocumentRoot (from the configuration above). You might want to drop a file in the DocumentRoot to verify its pulling your local content.
cd /var/www/ubuntu-tutorials.com/html<br /> echo "Hello World" > index.html
----------------------------------------------------------------------------------
Tell apache where to find the files for www.myposgarage.com
$ sudo nano /etc/apache2/sites-available/www.myposgarage.com
<VirtualHost 192.168.1.50:80><br /> ServerName
myposgarge.com
ServerAlias
www.myposgarge.com
ServerAdmin me@there.com<br /> DocumentRoot /var/www/www.myposgarge.com/html<br /> </VirtualHost>
Set up the link
$ cd /etc/apache2/sites-enabled/<br /> $ ln -s ../sites-available/
www.myposgarge.com
If no local dns server then
$ sudo vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
192.168.1.50 oesrvr1 www.thesoftserv.com www.myposgarage.com
192.168.1.199 printer
192.168.1.51 nas lottie lottienas
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
Then just installed the files for the software (opensourcepos), I wanted to use instead of just using the index.html.
Update:
You probably updated your Ubuntu installation and one of the updates included the upgrade of Apache to version 2.4.x
In Apache 2.4.x the vhost configuration files, located in the /etc/apache2/sites-available directory, must have the .conf extension.
Using terminal (mv command), rename all your existing configuration files and add the .conf extension to all of them.
mv /etc/apache2/sites-available/cmsplus.dev /etc/apache2/sites-available/cmsplus.dev.conf
If you get a "Permission denied" error, then add "sudo " in front of your terminal commands.
You do not need to make any other changes to the configuration files.
Enable the vhost(s):
a2ensite cmsplus.dev.conf
And then reload Apache:
service apache2 reload
Your sites should be up and running now.
Checking on Multiple Sites.
[code]
<html>
<body>
<hr>
<center>
<h2>
Bookmarks
</h2>
</center>
<hr>
<table border="1" cellpadding="10">
<tr>
<td>
Ipadress
</td>
<td>
Site (and link)
</td>
<td>
Description
</td>
<td>
Server status
</td>
</tr>
<tr>
<td>
192.168.1.31
</td>
<td>
<a href="http://oesrvr1">Offshore Educators</a>
</td>
<td>
School site
</td>
<td>
<?php
if (fsockopen('oesrvr1', 80)){
echo('The server is online');
} else{
echo('The server is offline');
}
?>
</td>
</tr>
</table>
</body>
</html>
[/code]