Apache 2 web server; install and config a virtual host

Good day; today we will install a web server using Apache 2. We will specifically use apache because there are many web servers out there, and apache is probably the most popular one even among windows users, when you install Wamp Server, or Xamp,

Apache Web Server

Apache Web Server

you are also installing apache 2.x. It has some other advantages or benefits like Openness, a lot of features, extensibility, portability, cost, simple administration, reliability, flexibility, etc. Check out this link for some explanations. It has also some downsides or disadvantages like for instance; it’s Written By Geeks, For Geeks. That does not mean is difficult or any thing like that, but it can be twisted for some specific situations.

The Linux philosophy to install a software like Apache 2 is different from Windows. Windows tries to solve all of your problems at once, Linux’s philosophy is to solve only one of your problems using the best practices. Each of this ways of thinking has it pros and cons, but that is not the objective of this class, so we move on.

To install the package you have to do:

aptitude install apache2

After the installer finishes, our apache server is fully operational, so we can access it directly from the browser by writing: “http://localhost” or “http://Our_IP_address”. Or if we already have our DNS pointing to our host. Then we can access directly from any computer by writing the host name on the address bar of the browser. Other wise we can start configuring the server, to be able to respond to our DNS requests (You are supposed to have a DNS configured to complete this class, you can follow the class number 5 about bind DNS). In all cases you should see the typical Debian message saying that “It Works!” before we continue.

Note: This post is part of a bigger Debian GNU-Linux material that you can access from here.

To make sure that your DNS is working you can ping web1.myco.com and your computer should respond. Otherwise do not proceed further than here till you have it working. After the DNS is working we can create our apache virtual host. For those who do not know:

“Virtual hosting is a method for hosting multiple domain names on a single server. This allows one server to share its resources, such as memory and processor cycles, without requiring all services provided to use the same host name…”

Taken from: http://en.wikipedia.org/wiki/Virtual_host on 2014-04-08.

In other words you are creating a name for a computer that does not really has to exist. Let’s think about it, do you really think that google.com is only one computer? yeah, yeah, what they should have is a lot of servers that work together to bring to us what we are asking for. they should have a strong DNS service in place and dynamically then they send you to the virtual host that they want you to go to open the search engine or the e-mail service or Youtube.

By default apache runs using the port 80, if you want to change this you can do it by editing the file:

nano /etc/apache2/ports.conf

Then the directive Listen is the one that defines the port to listen:

Listen 80

You can change it to the port you want and then you can access the server after restarting:

root@khs01sw09:~# service apache2 restart
Restarting web server: apache2 ... waiting .
root@khs01sw09:~#

To access the server you should add in the browser the port at the end of the url eg: http://localhost:8080

Now lets do our fist virtual host. As you might have already noticed Apche in Debian has the configuration files like most of the software in /etc, and it uses the directory called apache. So /etc/apache2 is where we modify to create a virtual host or any other behavior we ant to change in apache. In the other hand by default the www root is in /var/www, the www root folder is where the web server keeps the public material.

But going back to this fist virtual host we will call it “web1.myco.com”, and is going to redirect to our localhost, and we will redirect to a custom public folder. This directory will be in /var/wwwroot. and we will first create this folder an a web page inside.

mkdir /var/wwwroot
nano /var/wwwroot/index.html

Then we will add any web content to this index file so that we know we are accessing our own file and not any other.

<h1>My Website with a Virtual Host</h1>

A normal website has a lot more, but lets say that our website is complete and move on to create the actual virtual host. A virtual host for apache is a file that tells to go to a folder when Apache is asked about a resource. I our case we will ask about web1.myco.com and apache will respond letting us access the folder /var/wwwroot. In Apache 2  the virtual host or sites are in a directory called sites-available in /etc/apache2/ inside we should have atleast one file called default and another one called default-ssl. both of them are the template virtual hosts. The only difference is that default-ssl is a template for and https website and default is for a normal http, but they work the same. To create an other virtual host we just have to copy one of this files with a different name and change the relevant parameters.

cp /etc/apache2/sites-available/default.conf /etc/apache2/sites-available/web1.conf
nano /etc/apache2/sites-available/web1.conf

Note: you can use any name, but the .conf at the end is important in the newer versions of apache. So if you are using debian testing (8) you have to make the new file with .conf at the end. Yow are also supposed to leave the default configuration file and this configuration file is supposed to respond to a different name than your website.

When we open with nano it should show something like this:

<VirtualHost *:80>
        ServerAdmin webmaster@localhost

        DocumentRoot /var/www
        <Directory />
                Options FollowSymLinks
                AllowOverride None
        </Directory>
        <Directory /var/wwwroot/>
           Options Indexes FollowSymLinks Includes ExecCGI
           AllowOverride All
           Order allow,deny
           Allow from all

           #insert this line
           Require all granted
           #end insertion
        </Directory>

        ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
        <Directory /var/wwwroot/>
           Options Indexes FollowSymLinks Includes ExecCGI
           AllowOverride All
           Order allow,deny
           Allow from all

           #insert this line
           Require all granted
           #end insertion
        </Directory>
        ErrorLog ${APACHE_LOG_DIR}/error.log

        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn

        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

The truth is that you do not need all this to create a simple virtual host, but this one is also fine so we will use this file as base. And then we will change the parameters that we need to change and leave the rest unchanged. The only things we need to change are:

        DocumentRoot /var/wwwroot
        ServerName web1.myco.com
        ServerAlias web1.myco.com
        <Directory /var/wwwroot/>
           Options Indexes FollowSymLinks Includes ExecCGI
           AllowOverride All
           Order allow,deny
           Allow from all

           #insert this line
           Require all granted
           #end insertion
        </Directory>

To finally look like this:

<VirtualHost *:80>
        ServerAdmin webmaster@localhost        
        ServerName web1.myco.com
        DocumentRoot /var/wwwroot

        <Directory /var/wwwroot/>
           Options Indexes FollowSymLinks Includes ExecCGI
           AllowOverride All
           Order allow,deny
           Allow from all

           #insert this line
           Require all granted
           #end insertion
        </Directory>

        ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
        <Directory "/usr/lib/cgi-bin">
                AllowOverride None
                Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
                Order allow,deny
                Allow from all
        </Directory>
        ErrorLog ${APACHE_LOG_DIR}/error.log

        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn

        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Note:Apache 2 by default deny the access to all folder out of the default document root. so the directory directive is very important.

Now we have to say to apache2 enable this virtual host, for that we execute:

root@khs01sw09:~# cd /etc/apache2/sites-available/
root@khs01sw09:/etc/apache2/sites-available# a2ensite web1
Enabling site web1.
Run '/etc/init.d/apache2 reload' to activate new configuration!
root@khs01sw09:/etc/apache2/sites-available#

Then we restart the apache server again and we are done. Now we can write in our browser the address of the page web1.myco.com and it’s supposed to open.

Hope it helps, if you have any question please feel free to post it here.

The next class will be about proxy server using squid, hope to see you around.

 

Leave a Reply

Your email address will not be published. Required fields are marked *