:::: MENU ::::
Posts tagged with: Linux

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.


Lighting the LAMP in Ubuntu 14.04

If you are like me, who have recently switched from windows to Linux to enjoy more freedom, and experienced that freedom ain’t that cheap, given the wild goose chase that Linux welcomes with,for beginners, then you’re home. Relax and have a seat. Below I will share one of the easiest ways to light the LAMP in Ubuntu 14.04.

The easiest way to install LAMP stack in Ubuntu is by using tasksel.

Tasksel is a multiple related package installer for Ubuntu.

The procedure is pretty simple. First let’s break down the steps:

1. Update your application index by running update command
2. Install tasksel
3. Install LAMP Server using tasksel

That’s right folks! Only three simple commands and then you are good to go.

So, now lets have a look at the associated commands:

1. Running the program update command:

sudo apt-get update

First of all, what we need to understand is, what this command does. It just looks for any necessary updates, dependencies etc of your currently installed programs.

But, if you’ve recently ran this command then, you need not run it again. (Though it would do no ham if you do it).

2. Install tasksel:
sudo apt-get install tasksel

Simple. This command installs tasksel, which will help you install LAMP.

3. Install LAMP Stack:

sudo tasksel install lamp-server

This command installs the lamp stack. During the installation you will be asked to set the root password for mysql. Just that small attention seeking act and tadaaa, you have successfully installed LAMP.

Just a reminder, after tasksel installs, it shows an you options screen,where you can find lamp-server also listed. I tried installing LAMP from there, but it did not work. So I tried the above command and got the results without any hair pulling sessions caused due to shattering of expectations 🙂 .

It would also be good to install phpmyadmin right away, would it not? So,lets again get back to good old terminal and type in:

sudo apt-get install phpmyadmin

Now, the beautiful spontaneity of Linux takes over again and you can see it being installed. In between (again), you will be shown an option screen where you have to select Apache server,(since you are installing phpmyadmin to run with Apache server). After selecting Apache server, a again for the last time, you will be asked to set the password for phpmyadmin, which you have to reply as yes, then set the password,confirm it and then you’re done. PHPMYADMIN and LAMP both installed with a few painless commands.

But again, this will not run phpmyadmin when you hit
http://localhost/phpmyadmin

why? Because, poor Apache server doesnot know about phpmyadmin! So, we will tell Mr.Apache where to find our new guy.

Copy the following line and paste it in your apache2.conf file in /etc/apache2/apache2.conf

Include /etc/phpmyadmin/apache.conf

Then restart the Apache server by:

sudo service apache2 restart

or

sudo service apache2 reload

Now, you can access localhost/phpmyadmin

Login with the password that you created above during installation of phpmyadmin and …..enjoy, play around or whatever you want to do with it 😀 !

You can use the default Apache site to start with. Apart from that,Apache provides high flexibility to developers to configure virtual sites as per the needs. I will write about configuring new virtual sites in my next article.

This is my first article and so there might be some faults in presentation and a bit this and that, to improve which, I will be looking for your feedback. Be in touch!!