Sunday, May 29, 2016

Setting up a basic virtual host on Apache2 server

After installing Apache server on a virtual machine on VMWare for my experimental setup as explained in this previous post, my next move was to do the basic configurations of the HTTP server. After installation, most of the necessary configuration files of Apache is located in /etc/apache2 directory. The main apache configuration file is apache2.conf which is located in that directory. Let's open it and see.

cd /etc/apache2
vim apache2.conf


Going through this file contents will clearly show that there are references to various other configuration files from here. Few parameters are worth mentioning here. The Timeout parameter contains a value in seconds which is the maximum time the server can take to respond to a client request.

The KeepAlive parameter is used to decide whether we are going to support persistent connections. When a client requests an HTML page from the server, the client may have to send subsequent requests if the HTML page has references to various other files such as images. If the connection with the server is not persistent the client has to cleat a TCP connection for every subsequent request to the server. Using the KeepAlive parameter, we can ask the server to maintain persistent connections so that the same TCP connection will be maintained throughout the subsequent HTTP requests from that particular client to the server. This will improve the performance of the server significantly but it will use more server resources.

Apache server runs under a dedicated username and a group to isolate it from other things. apache2.conf file has entries to these user and group names which are actually defined in a separate file. Let's go ahead and look at some other directories and files associated with the server configurations.

vim envvars

This file contains some necessary environmental variables which will be set when we restart the apache server. One important parameter mentioned here is the exact user and group name the apache should running under. By default when we install apache server, a user name called www-data and a group with the same name is created. We can see this user by looking at the passwd file.

cat /etc/passwd

Setting up a virtual host:

(1) Setup the directory to hold the html files first with necessary permissions.

sudo mkdir -p /var/www/smallco.com/public_html
 
sudo chown -R $USER:$USER /var/www/smallco.com/public_html/
 
sudo chmod -R 755 /var/www/

(2) Let's create a simple html page for our sample website inside this created directory.

vim /var/www/smallco.com/public_html/index.html

(3) Time to create the apache configuration file for this new virtual host. We will copy the default file contents and edit where necessary.

sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/smallco.com.conf

sudo vim /etc/apache2/sites-available/smallco.com.conf

The contents I added to this file looks like the following.

<VirtualHost *:80>
    ServerAdmin admin@smallco.com
    ServerName smallco.com
    ServerAlias www.smallco.com
    DocumentRoot /var/www/smallco.com/public_html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>



(4) Enable this virtual host using the following command.

sudo a2ensite smallco.com.conf

(5) To view this new virtual host from our web browser we have to edit the hosts file since we don't own the domain name we have used and therefore we need to by-pass the DNS lookups going from the web browser to DNS services outside. (note that we can access this website using the host name only within this computer. To access from the outside world, we need to buy the domain name and DNS service mapping.)

sudo vim /etc/hosts

Inside this file, add an entry like this.

127.0.1.10  smallco.com

(6) Now we can see the web page from our web browser. First of all, restart the apache server and then access the page fro the web browser.

sudo service apache2 restart

w3m smallco.com

That's all folks! 

No comments:

Post a Comment