:::: MENU ::::
Posts tagged with: Virtual host

Apache 2.4.7 and its configuration in Ubuntu 14.04

What is Apache Server?

Apache is a free, open-source web server software developed by a group of 20 programmers, famously know as The Apache Group. It is the most popular web server software used today. Initiation of works on Apache dates back to 1995. Apache helps to deliver content over the internet to the requesting clients. It became the first web server to server more than a 100 million websites in 2009 and surpassed its own record by serving 54.2% of all the websites.

Apache and PHP

Consider an organization. There are 3 parts- The Customers, The Management and The workers or employees. Now when it comes to web, these roles are played respectively by Browser, Apache and PHP. Like a customer who requests for something from the organization, the browser initiates a content request. The management then, receives the request (note the ‘receives’ word) and passes it to the workers. The workers do all the work, process the request, produce the results and then make it ready so that the management can deliver it to the requesting customer. In the same fashion, Apache receives request from the clients / browsers & calls the PHP interpreter which interprets the request, looks for the appropriate requests, generates the html and then Apache sends it back to the customer browser. PHP is one of the most popular language for server side scripting due to its simplicity, open-source nature and “not too steep” learning curve. PHP Apache and MySQL is the one of the most popular web environment since some years.

Configuring Apache (writer is using Ubuntu 14.04):

Apache, probably is the most flexible and easily configurable web sever software. A developer can configure it as per their needs without much hassles.Below I will explain how an Apache server can be configured to create virtual sites.

What are Virtual Sites?

By default, Apache runs based on a set of rules defined in its config files. For example, most of us (specially windows users using wamp or xampp) use http://localhost/<project name> to access projects that are in our local directory, once the Apache server starts running.

By creating virtual sites, you define your set of rules like the ServerName (localhost in above examples), server ip address, port number, allowing or disallowing redirection, email of server administrator and also the directory where Apache should look for your projects.

The Process:

By default, Apache default site configuration resides at:

/etc/apache2/sites-available/000-default.conf

First of all, I will write down the steps in plain language, so that it becomes easy to comprehend and keep track of what actually is happening. Actually, these are the broken down steps for the sake of simplicity. So, here we go:

1. Copy the default configuration file and rename the copy to <name-you-prefer>.conf

2. Edit the ServerName,ServerAlias,ServerAdmin

3. Define the DocumentRoot  where you want Apache to look for your projects.

4. Define access rules to your virtual site.

5. Define the error logs and access logs (if only you want to change the default error log location)

STEP 1:

We do it in the Linux way. In your terminal, type:

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

A copy of the configuration file is created with the name projects.conf . This actually eases our work because we just have to edit things instead of writing each of them.

STEP 2:

a. ServerName: It is the name that you enter in the url.for example: localhost is a server name. I would prefer using, “projects”. So that I can put all my projects in this new site and it kinda looks pretty cool (Well, for me at-least 😀 ).

So, in the projects.conf file, put ServerName projects

b. ServerAlias: It is an alias / alternative name of server name. This means that if I enter alias name instead of server name the effect should be the same as I entered ServerName. That’s why its called ServerAlias, since it is a alias of our server name.

for our example, we will use ServerAlias Projects

c. ServerAdmin: This is the email address that will receive an email if something goes wrong or some issue arise. ServerAdmin shall contain your email address.

ServerAdmin admin@manish-lamichhane.com.np

STEP 3:

DocumentRoot defines the directory where your project resides. For instance its in a folder called projects inside www. Then we have to define it as:

DocumentRoot /var/www/projects/

Note: Make sure you have a folder with projects name that has write permission and your /var/www/ have read permission.

(If you have not done that yet, read further to closing parenthesis.(Use you LISP compiler between your two ears, if you know what I mean ):

1. sudo mkdir -p /var/www/projects/test
(-p creates parents directory if they do not exist)

2. sudo chmod -R 755 /var/www/
( check the properties by looking at permissions of these folders by right clicking them. They shall be accessible to all). For security reasons, they should be made readable only and not writeable.

3. sudo chown -R $USER:$USER /var/www/projects

(-R means recursive, so -R after chown means recursively own all the folders and sub folders inside projects folder)

That’s it. We have a project folder that has write permission and /var/www/ allows read access.

)

Step 4:

Now comes the <Directory>. This will be meaningful to you if you understand the use of .htaccess file. The .htaccess file apply rules to a particular directory. Apache gives precedes to the .htaccess files first. That means the changes or modifications in the .htaccess files override the behaviour of the particular directory that they reside in. If no .htaccess file is present then the rules in this container (i.e. <Directive>) are applied. Rules like removing index.php from url are defined in .htaccess files, and if you want to apply changes through all projects inside the projects/ directory, you have to make changes in this container.

for now keep it as:

<Directory /var/www/projects/ >
    
    Require all granted

</Directory>

This means that projects/ can be accessed from all the requesting clients.However, You can restrict access to this site as well.
for example:

Require not host rougecontent.com

prohibits rougecontent.com from accessing this directory.

Detailed information regarding this section can be found here.

STEP 5: Defining the location of error and access logs.

You can use it as it is if you don’t want a separate configuration file for your site. Or you can create an error and access log inside your projects directory (or anywhere you want) and then give the location in the ErrorLog and Custom log. I have, for this example, created /var/www/projects/logs/error.log and /var/www/projects/logs/access.log

So, summing things up, after following all the above steps, your projects.conf file should look like this:

<VirtualHost *:80>

ServerAdmin webmaster@localhost
ServerName  projects
ServerAlias Projects
DocumentRoot /var/www/projects/public_html/

<Directory /var/www/projects/public_html/ >

Require all granted

</Directory>

ErrorLog /var/www/projects/logs/error.log
CustomLog /var/www/projects/logs/access.log combined

</VirtualHost>

The number 80 means that the virtual host listens to all the requests from port 80.

Now two simple things are remaining.

First: Editing the host file to define projects

gksudo gedit /etc/hosts/

Then, add:

127.0.0.1 projects

The ip that you are defining is called a “Loop-back IP” which tells Apache to look for the host in this machine or in other words, redirects the request to the machine itself whenever you enter http://projects in your browser.

Second: Enable this new site. Type the below command in terminal and then restart apache2 server.

sudo a2ensite projects.conf ( a2 means apache2 ensite means enable site)

sudo service apache2 restart

Note: If there are some errors in your configuration file, they will be shown in the terminal when you attempt to restart the Apache server.Also, by documentation of Apache, you need not disable your default configuration file (000-default.conf) because the virtual site (project.conf) will always take precedence over default site. However, there are cases when the virtual site works only when the default site is disabled. Disabling of a site can be done using the following command:

sudo a2dissite 000-default.conf (or any configuration that you wan t to disable)

As mentioned above, a2 means apache2 dissite means disable site

You may now create any html or php file inside projects/ directory and then access using http://projects/<filename.php>

Hope this article is helpful, specially to the beginners.