Tuesday, January 22, 2008

Host|Host Multiple Domains using Apache (VirtualHost) - ossPro

Linux: Host Multiple Domains using Apache (VirtualHost) - ossPro

Linux: Host Multiple Domains using Apache (VirtualHost)

If you host multiple domains you don't always want or have a need to host them all on different machines. One brilliant solution to this is to use Apache's Virtual Hosts feature.


The first step to understanding how this works is to understand what goes on when someone visits a website. When a user types in "http://osspro.com?p=8″ in the address bar the browser then in turn looks up the IP address of that domain, opens a connection with that IP address, and then asks for data that is associated with the entered url by sending text along the lines of "GET http://osspro.com?p=8″. You can test this yourself with any telnet program by connecting to a website's ip address on port 80 and just typing the request. If the server you are connected to has information about the url you requested it will give you that data else it will give you an error message.

With that knowledge you can see how multiple domain names can be hosted on a single server. You should also be able to understand how sub-domains can be treated as different websites entirely. In this way, you can point multiple domain names to the same IP address and have Apache host them from different directories on your server.

For our examples, we will be using syntax for Redhat based distributions such as Redhat, Fedora, CentOS. The syntax of the configuration files will be the same on other distros but the command line syntax may be somewhat different.

Installing Apache

First up we will install apache, start it, and make sure it comes up during the boot process.

# yum install httpd
# service httpd start
# chkconfig httpd on


Apache Configuration

On Redhat-based systems the main configuration file for Apache is /etc/httpd/conf/httpd.conf. There is a lot going on inside that file but we are only going to pay attention to the following lines for now:

include conf.d/*.conf
This means that any file ending in .conf and is located in the /etc/httpd/conf.d directory will be loaded into the Apache configuration. We can use this to our advantage by having a separate configuration file for each of our domains.
User apache Group apache
These two lines mean that the httpd process will run with the same permissions as user apache and group apache. This will become important when setting up files/directories that the web server will need to access.
NameVirtualHost *:80
This line is commented out by default. It tells apache that we want to use named-based virtual hosts.


Our domains

Let's say we own the domain example.com and want the following configuration:

/home/example/logs/ Log files for *.example.com
/home/example/example.com/ DocumentRoot for http://example.com and http://www.example.com
/home/example/subdomain.example.com/ DocumentRoot for http://subdomain.example.com



We will start by creating and setting the password for a user with the home directory /home/example.

# adduser -d /home/example user0001
# passwd user0001

This will create the directory /home/example and assign us a user we can use for updating our website.



Now we will create our directory structure and give it permissions that allow our user and the group apache to access the files as they need.

# mkdir /home/example/example.com
# mkdir /home/example/subdomain.example.com
# mkdir /home/example/logs

# chown -R user0001:apache /home/example
# chmod -R 750 /home/example/subdomain.example.com/

# chmod 770 /home/example/logs/



If you have a distribution based on version 5 or greater of Redhat then you might have SELinux in enforcing mode. If this is the case then you will need to set the context of the files and directories to those proper for Apache.

# chcon -R system_u:object_r:httpd_sys_content_t /home/example/*



Alternatively, you could put SELinux in permissive mode with the following:

# setenforce 0

You will have to set SELINUX=permissive in /etc/selinux/config to make sure the setting sticks after a reboot.



Now we create /etc/httpd/conf.d/example.com.conf    Download this File

<VirtualHost *:80>     ServerAdmin user0001@example.com     DocumentRoot /home/example/example.com     ServerName example.com     ServerAlias www.example.com     ErrorLog /home/example/logs/error_log     CustomLog /home/example/logs/access.log common </VirtualHost>   <VirtualHost *:80>     ServerAdmin user0001@example.com     DocumentRoot /home/example/subdomain.example.com     ServerName subdomain.example.com     ErrorLog /home/example/logs/error_log     CustomLog /home/example/logs/access.log common </VirtualHost>



If you have SELinux in enforcing you will need to set the context for the file.

# chcon system_u:object_r:httpd_config_t /etc/httpd/conf.d/example.com.conf



Let's load our changes into apache.

# service httpd reload



Finally, let's test our configuration by configuring two files (one for each of our domains).

/home/example/example.com/index.html

<html> <body> This is http://example.com </body> </html>



/home/example/subdomain.example.com/index.html

<html> <body> This is http://subdomain.example.com </body> </html>



Finally, let's test to make sure Apache is configured correctly.

# telnet localhost 80
Trying 127.0.0.1…
Connected to localhost.localdomain (127.0.0.1).
Escape character is '^]'.
GET http://example.com
<html>
<body>
This is http://example.com
</body>
</html>
Connection closed by foreign host.


# telnet localhost 80
Trying 127.0.0.1…
Connected to localhost.localdomain (127.0.0.1).
Escape character is '^]'.
GET http://subdomain.example.com
<html>
<body>
This is http://subdomain.example.com
</body>
</html>
Connection closed by foreign host.

bold text is what you type.

No comments: