It was one of those late nights that every student studying System Administration and Networking gets.
Out of necessity i was forced to finally break the brick wall between be ABLE to make an apache virtual hosts and NOT being able to create one.
It is from many blogs such as these and the long-lasting and extremely helpful *NIX community that i finally pieced together how to do it. I dont just write this to say i did it (Although i threw my hands up when i did) im writing this to contribute to the community of learners and teachers out the in the computer world. Hopefully i can simplify the steps down to their bare bones.
What you are trying to achieve:
- You want to serve multiple websites, but you only have one server....... CAN YOU? YES!
- Do something you thought you couldn't....... CAN YOU? YES!
Prerequisites:
- Linux Server (Debian or Ubuntu Server Edition Recommended - i use Debian)
- Apache web server 2.x
- Terminal or Shell access (Either through telnet, or SSH)
- Root or Sudo access Preferably (Not sure if needed, i will get back to you.)
- A domain. You need be able to create subdomains. Preferably from www.no-ip.com (They have free ones you can have 50 subdomains for any premium domain you buy, i own rudkerfern.net. I use it for testing. Its also my Minecraft IGN :p)
My Model System:
- I am using Debian GNU/Linux 6.0.6 Codename "Squeeze"
- I am on an Apple Network with IP adresses that build upon the 10.0.1.0 framework (no 192 for me :p)
- I will be using sub.example.com as the example domain (sub.* means any subdomain you created)
- I am doing most of my administration on a mac and ssh'ing in.
Note: "$" stands for the Linux Terminal Prompt
I did this on my home system so i have a dynamic IP (changing every few days) and premium domain registered at www.no-ip.com. You can still do this if you have a static IP. I will leave notes to tell you what to do if you have a static IP. I also have a firewall which mean i had to port forward the respective ports for anything i wanted to access on the inside from the outside world. You will need to do the same. I have another tutorial on port forwarding.
We will be using the standard http port 80
Nothing special.
Anything enclosed "< >" means you need you fill in YOUR respective value
WE WILL BE MAKING A NAME-BASED VIRTUAL HOST
I did this on my home system so i have a dynamic IP (changing every few days) and premium domain registered at www.no-ip.com. You can still do this if you have a static IP. I will leave notes to tell you what to do if you have a static IP. I also have a firewall which mean i had to port forward the respective ports for anything i wanted to access on the inside from the outside world. You will need to do the same. I have another tutorial on port forwarding.
We will be using the standard http port 80
Nothing special.
Anything enclosed "< >" means you need you fill in YOUR respective value
WE WILL BE MAKING A NAME-BASED VIRTUAL HOST
Let get started.
STEP 1, get shell access:
SSH or telnet into your server if you dont have physical access
Open up Terminal.app on Mac and enter the following
$ ssh <user>@<host/ip>
Example $ root@www.example.com
If you need to do this in Windows, you will need a telnet/ssh client called http://www.putty.org/
It works in a similar fashion.
STEP 2, get in the right directory:
On my Debian system the directory with all the configuration virtual hosts in under
/etc/apache2/sites-available
$ cd /etc/apache2/sites-available
STEP 3, write the configuration:
Now that you are in the right directory you can make the config file with my favorite text editor "nano"
$ nano sub.example.comHeres all i needed to get a virtual host working.
You can copy this over and edit it to your needs.
</VirtualHost><VirtualHost *:80>ServerName sub.example.comDocumentRoot /path/to/files/to/serve<Directory /path/to/files/to/serve>Options FollowSymLinksAllowOverride NoneOrder allow,denyAllow from all</Directory>
You will need to change "ServerName" to the subdomain you setup with your domain registrar.
And change "DocumentRoot" to the directory that you want to server files from
"<VirtualHost *:80>" just says that you are listening to all requests from all ip on port 80
You can of course add any other standard apache2 directives into this config file for this particular site.
You might be interested in the "AllowOverride" directive. This tells apache2 wether or not to use Authentication for people that are requesting anything from this site. None means NO authentication. The only option i use would be "AuthConfig" instead of "None" on the "AllowOverride" directive.
Now
ctrl+o and the <ReturnKey> to save
and ctrl+x to exit nano
STEP 4, enable the newly configured site:
You can use the apache2 tools "a2ensite" and "a2dissite" to enable and disable site configs respectivly
$ a2ensite sub.example.comIt should enable the site and give you the next step. For you guys out there that HAVE to know what apache just did for you, here it is: it created a Symbolic Link aka an "Alias" on desktop systems in
/etc/apache2/sites-enabled/sub.example.com
If you want to see more on that, cd that directory and try the command $ ls -la
You will fond some interesting things next the site config file ;)
STEP 5, reload apache2:
Yep, what the step says...
$ /etc/init.d/apache2 reload
Apache is very good at debugging config files, if something is wrong it will tell you what and where.
If you got no error then stand up and shout! You did it. No wait, never jump to soon, there will be plenty time for that, lets test it first!
Go to your browser and type in "sub.example.com" or whatever you domain and subdomain address is.
If you have nothing in the folder you wanted to serve from and it give you and "Index Of" page then you are in luck buddy. It worked, simply put in your content. E.g an index.html or index.php page.
STEP 6, post a comment if you have any problems:
Go ahead! Bring it.
Notes: to disable a site just use the "a2dissite <sitefile>" command then reload apache.