Virtual Hosting with Apache
Contents
Introduction
Apache Virtual Hosting in simple terms allows you to host more than one website on your server. Whether its just you or friends, family and business it can all be done, if its just you all you need to do is set up your configuration and use /var/www/website. For everything else then create new users on your server and point your virtual host configuration to there html folders in /home/username/.
In order to setup Virtual Hosting you will need Apache installed first. (if not use Installing httpd and PHP on Centos 5 for getting apache installed)
Before we get started, we’re going to be editing /etc/httpd/conf/httpd.conf. If you have not used a text editor for more information about text editors click here
Virtual Hosting Setup
Before we get going I just want to mention that # means comment, which will either be config information, or settings commented out usually by default.
Lets get going.
[root@vps ~]# vi /etc/httpd/conf/httpd.conf
Now go to the end of the file and find
# VirtualHost example: # Almost any Apache directive may go into a VirtualHost container. # The first VirtualHost section is used for requests without a known # server name. # #<VirtualHost *:80> # ServerAdmin webmaster@dummy-host.example.com # DocumentRoot /www/docs/dummy-host.example.com # ServerName dummy-host.example.com # ErrorLog logs/dummy-host.example.com-error_log # CustomLog logs/dummy-host.example.com-access_log common #</VirtualHost>
This is an example of how to set up a Virtual Host, as you can see it is commented out and won’t affect the Configuration.
The *
Now is a good time to also mention that if you want a site to run on another port such as 8080 or 1001 then that’s where you would change it but note it wont work if there is something running on the server using that port already, such as port 22 - SSH Server uses that port, Mail servers use port 25 is used by an SMTP server such as Postfix, Sendmail.
A more complete list can be found at http://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers
ServerAdmin is basically the webmasters email address, which can be any valid email address; it doesn't have to be the same domain as the virtual host.
DocumentRoot is the path to the folder the website is located. This can be a folder you create on the server, an existing folder, or pointing to the public_html folder within /home/username of a user you have created.
ServerName is the address of your website.
ErrorLog and CustomLog is where the error/custom log will be located, again this can be a folder you have created, an existing folder or a logs folder created in /home/user. However this log should not be in a folder that you have assigned as the DocumentRoot, otherwise anyone can access the log and it could compromise your server security.
ServerAlias this is not in the example, by adding this to the directive it allows you to add aliases to the virtual host, it allows you to point more than one domain or sub domains to the same virtual host.
Examples of Virtual Hosting.
# mywebsite <VirtualHost www.mytld.com:80> ServerAdmin webmaster@www.mytld.com DocumentRoot /www/mytld/public_html ServerName www.www.mytld.com ErrorLog /www/mytld/logs/error.log CustomLog /www/mytld/logs/access.log common </VirtualHost>
# Website Virtual Hosting
# One Host per username.
# Johnny's Site
<VirtualHost 1.2.3.4:80>
ServerAdmin johnny@mytld.tds
DocumentRoot /home/johnny/public_html
ServerName www.mytld.tds
ServerAlias www.mytld.tds mywebsite.mytld.tds sub.mytld.tds
ErrorLog /home/johnny/logs/error.log
CustomLog /home/johnny/logs/access.log common
</VirtualHost>
# Quantact
<VirtualHost 1.2.3.4:80>
ServerAdmin support@quantact.com
DocumentRoot /home/quantact/public_html
ServerName www.quantact.com
ErrorLog /home/quantact/logs/error.log
CustomLog /home/quantact/logs/access.log common
</VirtualHost>
#Adam's site.
<VirtualHost 1.2.3.4:80>
ServerAdmin emailme@mywebsite.tds
DocumentRoot /home/adam/public_html
ServerName www.mywebsite.tds
ErrorLog /home/adam/logs/error.log
CustomLog /home/adam/logs/access.log common
</VirtualHost>
Once you have created your Virtual Hosts and saved the config file, all you need to do is restart apache and it's done.
[root@vps ~]# service httpd restart Stopping httpd: [ OK ] Starting httpd: [ OK ]
In order to test to make sure it works, all you need to do is put a simple html file in to the directory specified by DocumentRoot and then go to the domain. As long as the domain is setup and pointing at your server ip and there are no errors in the Virtual Hosting config then you should see that page come up in your browser.
- Login to post comments
