How To Create A Localhost Web Site In Linux Using Apache Web Server


Read {count} times since 2020

Localhost is a hosting place in your own computer. There are a vast number of Localhost servers you can use. The most used and popular of them is Apache. Localhost enables you to test your work on your computer itself, instead of testing on a live server on the web. If you are creating a project, Localhost is the best place to test that project. In this tutorial I will show you how to create a localhost site running on Apache Server in any Linux Distribution.

This tutorial is for Ubuntu versions 10.10 and below. The latest versions from 11.04 should see this tutorial.

Apache Server Installation

We need to install Apache server first. To install Apache Web Server open Terminal and do the following command :

sudo apt-get install apache2

The Apache Server will be downloaded and installed automatically. After installation continue to the next part of this tutorial.

Site Creation

Open the Terminal CTRL + ALT + T. Make a copy of /etc/apache2/sites-available/default, let’s make the copy name mysite. (Name depends on the site you wish to create). The following command wil copy the file and name the new file as mysite.
sudo cp /etc/apache2/sites-available/default /etc/apache2/sites-available/<span style="color: red;">mysite</span>

Now edit the new file to configure :

sudo gedit /etc/apache2/sites-available/<span style="color: #ff0000;">mysite</span>
Then, Add the following contents in the file using gedit we just opened. Please read the 3 conditions below and then paste the content.
  1. You will need to change the codes ‘username’ shown below to your home folder name.
  2. You will need to change the names ‘mysite’ shown below to your desired site name.
  3. You can also change the web address of your site by renaming "mysite.com" to the site address you want.

Content that should be added to the file :

<span style="font-family: inherit;"><span class="tag"><VirtualHost</span><span class="pln"> *:80</span><span class="tag">></span></span>
 <span class="pln"> ServerAdmin webmaster@localhost</span>
 <span class="pln"> ServerName </span><span style="color: red;"><span class="pln">mysite.com</span></span>
 <span class="pln"> DocumentRoot <span style="color: red;">/home/username/mysite</span></span>
 <span class="pln"> </span><span class="tag"><Directory </span><span style="color: red;"><span class="pun">/</span><span class="atn">home</span><span class="pun">/</span><span class="atn">username</span><span class="pun">/</span><span class="atn">mysite</span></span><span class="tag">/></span>
 <span class="pln">  Options Indexes FollowSymLinks MultiViews</span>
 <span class="pln">  AllowOverride All</span>
 <span class="pln">  Order allow,deny</span>
 <span class="pln">  allow from all</span>
 <span class="pln"> </span><span class="tag"></Directory></span>
 <span class="pln"> ErrorLog /var/log/apache2/</span><span style="color: red;"><span class="pln">mysite</span></span><span class="pln">/error.log</span>
 <span class="pln"> # Possible values include: debug, info, notice, warn, error, crit,</span>
 <span class="pln"> # alert, emerg.</span>
 <span class="pln"> LogLevel warn</span>
 <span class="pln"> CustomLog /var/log/apache2/</span><span style="color: red;"><span class="pln">mysite</span></span><span class="pln">/access.log combined</span>
 <span class="tag"></VirtualHost></span>

Once you’ve saved these changes, you’ll need to enable the site. ( You will need to change the name ‘mysite’ shown below to the name of the file you create at “/etc/apache2/sites-available/” ).

cd /etc/apache2/sites-available && sudo a2ensite <span style="color: red;">mysite</span>

We specified a separate directory for storing log files, so we’ll need to create that log directory.( You have to change the name ‘mysite’ shown below to the name you given in the configuration file on “/etc/apache2/sites-available/” ).

sudo mkdir /var/log/apache2/<span style="color: red;">mysite</span>

Finally, we need to point the site address to localhost in your hosts file. For that, Edit /etc/hosts :

sudo gedit /etc/hosts

and add the following at the end of the file. ( You need to change the web address “mysite.com” shown below to your address given on the file in sites-available folder ).

127.0.0.1 <span style="color: red;">mysite.com</span>

Now we restart Apache Server using Terminal to apply all the changes we made.

sudo /etc/init.d/apache2 restart

You can now store all your work in /home/you home name/site name and navigate to it by pointing your browser to the address you gave.