Create Your Own Web Server With BIND And Apache On CentOS 5 (Simplified) - Page 2

Want to support HowtoForge? Become a subscriber!
 
Submitted by c0mrade (Contact Author) (Forums) on Mon, 2008-06-30 15:33. ::

Jumping again :) This time a bit further to line 1005 there you should see:

NameVirtualHost *:80

The NameVirtualHost directive tells Apache that we want to use name-based virtual hosting, or in simpler terms, a bunch of web sites all using the same IP address. Add a couple blank lines underneath '#NameVirtualHost *:80' and on the line right below put NameVirtualHost and your hostname or IP address followed by a :80. The ':80' means we're using name-based virtual hosting for the HTTP protocol.

So you should produce something like this:

NameVirtualHost host.website.com:80

Now we are almoust done configuring our httpd.conf file. The only thing left for us to configure is if someone visits our server using the IP rather then domain name we will set our default hostname to be visible by those users. We can insert this at the end of configuration file

<VirtualHost host.website.com:80>
    ServerAdmin admin@website.com
    DocumentRoot /www
    ServerName 77.77.197.69
    ErrorLog logs/error_log
    CustomLog logs/access_log common
</VirtualHost>
In order to keep our httpd.conf easier to manage we should add this line at the bottom of the file:
Include conf/vhosts/*.conf

Meaning that we will include all .conf entries from /etc/httpd/conf/vhosts where we will store virtual hosts for our websites as we add them. It will be easier to understand as we progress further. Ok we are almoust done, save httpd.conf and go on.

Creating web site directories

We should now create web site directory structure in order to manage our websites properly. Firstly let's create the main directory:

mkdir /www

We are now making root the owner of /www:

chown root.root /www

Before going any further we need to make one adjustment, we must add a zone for host.website.com into our named.conf because we used it as a hostname in httpd.conf and we will get error if we don't do it. So we should add this to named.conf

zone "host.website.com" IN {
        type master;
        file "/var/named/host.website.com.db";
};

And of course below is the content of the file:

/var/named/chroot/var/named/host.website.com.db
$TTL 14400
@      86400    IN      SOA     ns1.website.com. admin@website.com. (
                2008021501      ; serial, todays date+todays
                86400           ; refresh, seconds
                7200            ; retry, seconds
                3600000         ; expire, seconds
                86400 )         ; minimum, seconds
host.website.com. 86400 IN NS ns1.website.com.
host.website.com. 86400 IN NS ns2.website.com.
host.website.com. IN A 77.77.197.69
localhost.host.website.com. IN A 127.0.0.1

You can add as many records as you wish. Before restarting named make a link:

ln -s/var/named/chroot/var/named/host.website.com.db /var/named/host.website.com.db

service named restart

Now we go back to the directory structure. So we have added a zone for our website.com now we need a virtual directory for it.

cd /www
mkdir website.com
cd website.com
mkdir html
mkdir html/cgi-bin
mkdir databases
mkdir logs

Since we have made a directory for our website, all we need is a user which will be legible to manage this domain name. We can do that with:

useradd -d /www/your-domain/ domain_user

or if you want to restrict shell access to the user use this:

useradd -d /www/your-domain/ -s /sbin/nologin domain_user

After the user has been added we can modify its password with:

passwd

This user still doesn't have his "home" :) place which he owns, now we add this:

cd /www && chown domain_user:domain_user website.com

If you create a "databases" directory then you'll need to change the owner to the mysql user otherwise MySQL won't be able to write to the database files. All directories, subdirectories, and files dealing with databases should be owned by the mysql user and we can do that with this command:

cd website.com && chown -R mysql:mysql databases

With this we completed the creation of the website directory structure, now the only thing left to do is to tell Apache where our website is and we are all done.

Now let's track back a bit when we were configuring httpd.conf we inserted a line there:

Include conf/vhosts/*.conf

Now you will figure out why we did that if you didn't already. Since the folder doesn't yet exist we should create it:

cd /etc/httpd/conf && mkdir vhosts

Now we go inside the vhosts directory:

cd vhosts

and we create a new file for our domain:

nano website.com.80.conf

We should insert the following into this file:

<VirtualHost host.website.com:80>
    ServerAdmin admin@website.com
    ServerName website.com
    ServerAlias www.website.com
    DocumentRoot /www/website.com/html
    ScriptAlias /cgi-bin/ /www/website.com/html/cgi-bin/
    ErrorLog /www/website.com/logs/error_log
    CustomLog /www/website.com/logs/access_log combined
</VirtualHost>

We finally reach the end point now when we told apache where ithe directory of our website is, now every request from browser for www.website.com will be read from /www/website.com/html; in there you should put your files ex: index.html. Now we can turn our httpd on, or restart if it was on already.

service httpd start

or

service httpd restart

If you are experiencing any problems with this tutorial or with the configuration of either Apache or BIND don't hesitate to contact me. I'm sure I had those and even worse problems - I'll be glad to help in my spare time.

Author : Emir Ibrahimbegovic
E: admin@green.ba
W: www.green.ba
Thanks to: Tony Bhimani, Sébastien Wains, Nayyar Ahmad

Please do not use the comment function to ask for help! If you need help, please use our forum.
Comments will be published after administrator approval.