How to Configure Apache Web Server on Linux

July 26, 2009 by
Filed under: HowTo, linux, web development 

I've shown previously how to install a LAMP server in Ubuntu. If the purpose of your LAMP installation was to set up your own web development environment, then you may want to do some further configuration to your system. This Apache howto is not intended to cover Apache configuration in depth. This is just some basic configuration to help you set up a web development environment in Linux. For more information, see the official Apache documentation.

Why Change the Apache Configuration?

By default, Apache is set up for your web site's files to be in the /var/www directory. This is fine if you only want to work on one website and access it through http://localhost/. But what if you want to work on several websites at the same time? Well, one solution is to create different directories under /var/www like /var/www/site1 and then access it through http://localhost/site1/. I prefer a more elegant solution.

I prefer to build websites in a directory under my own ID. I can then configure the Apache http server to point to the site directories with URLs like http://site1/, http://site2/, etc.

Creating a Web Development Directory

Lets start off by creating a folder structure for the development environment. You can do this from your file manager. I prefer the terminal.

Affiliate Link
mkdir webdev
cd webdev
mkdir site1 site2

We now have a directory called webdev under our home directory. Within the webdev directory are two directories called site1 and site2.

Create Some Test Files

Now we're going to create some basic test files so that we know our Apache configuration worked. Again, I'm going to use the terminal to create these files, but feel free to use your favorite text editor.

cd ~/webdev/site1
echo 'Site1 works!' > index.html
cd ../site2
echo 'Site2 works!' > index.html

Enable the Sites in Apache

OK, we're now ready to do the actual Apache configuration. Go to /etc/apache2/sites-available.

cd /etc/apache2/sites-available

As root, copy the default file as site1.

sudo cp default site1

Repeat the process to create a site2 file.

As root, edit the site1 configuration file.

sudo nano site1

Edit the file to look like this (the changes are in bold).

<VirtualHost *:80>
	ServerAdmin webmaster@localhost
        ServerName site1

	DocumentRoot /home/yourID/webdev/site1/
	<Directory />
		Options FollowSymLinks
		AllowOverride None
	<Directory /home/yourID/webdev/site1/>
		Options Indexes FollowSymLinks MultiViews
		AllowOverride All
		Order allow,deny
		allow from all

	ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
	<Directory "/usr/lib/cgi-bin">
		AllowOverride None
		Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
		Order allow,deny
		Allow from all

	ErrorLog /var/log/apache2/error.log

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

	CustomLog /var/log/apache2/access.log combined

    Alias /doc/ "/usr/share/doc/"
    <Directory "/usr/share/doc/">
        Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Order deny,allow
        Deny from all
        Allow from ::1/128


Note: The line that changes AllowOverride None to AllowOverride All is required if you want to enable URL re-writes through a .htaccess file. You need this if you want to utilize pretty permalinks in WordPress.

You now need to enable your new site.

sudo a2ensite site1

You should get a message telling you that you need to reload Apache to activate the new configuration. But first you need to edit your /etc/hosts file.

sudo nano /etc/hosts

Edit the first line adding site1 to the end of the line so it looks something like this and save the file. localhost site1

You can now reload Apache.

sudo /etc/init.d/apache2 reload

Site1 is now enabled. Check it by browsing to http://site1/. If everything worked right, you should see a web page that says "Site1 works!"

Repeat the procedure to enable site2, etc.

Edit 12-Dec-09
If you get an error like “apache2: Could not determine the server’s fully qualified domain name, using for localhost,” you can fix it with this command.

echo "ServerName localhost" | sudo tee /etc/apache2/conf.d/fqdn

Then, reload Apache to eliminate the error.

sudo /etc/init.d/apache2 reload


208 Responses to “How to Configure Apache Web Server on Linux”

  1. Ajas K A says:

    yes, it worked....thank you so much.....

  2. Eras says:

    Great Tutorial. Had wrestled with Apache for a while without any luck.

  3. Josh says:

    What additional steps do I have to take to view the sites over my home network? I can view them on the machine that the server is running on, but that is not my main computer.

    • andy says:

      Are you trying to connect to the IP address of your server or if its this is know as a loopback address for your local machine and you wont be able to connect to another machine using this ip address. Try the IP for the server.

  4. L-Regbot says:

    This is a life-saver. Use it lots. But how can I remotely access my ~/webdev/site1 ? It functions just fine on my own computer, but on my home LAN gets me "It works!" but gets me "Not found." AFA I know I've got everything 755 permissions-wise.

  5. Pervez says:

    Thank you for this post .I want to know how can I inheriting HTML templates in APACHE2 Web-server as we can do in Django Framework . And how can I configure my My webpage with JS CSS IMAGE folders in webdev......?

  6. Bhavyesh says:

    hey dear, can u guide me for this,,,
    when i place any folder inside site1 folder then, i am unable to access that folder.. in my Ubuntu 12.04 via the web browser. The css placed under any sub folder or the java-script also under any sub folder is not considered by the browser.

    Example http://site1/ is working but http://site1/css gives forbidden message...

  7. Bhavyesh Gadhethariya says:

    Hey brother u are really sharing a nice stuff. I got really satisfied answer and technique of configuring apache.thanks once again. it is of great help to me :)

  8. Beardmancer says:

    Hi! This and your LAMP instructions are working great for me, way better than XAMPP for Linux. Thanks!

    One problem I'm encountering: on one of my sites, index.php (in the site's root directory) is not able to access main.css (in /css). The same is true for any resource in a subfolder that index.php tries to access. The specific error is "403 Forbidden."

    I have no experience configuring apache or php. Is there a setting somewhere that tells the site to include subfolders? The site's folder is located in my home directory, so I can't imagine that permissions would be an issue.

    Ubuntu 12.04, in case that matters.

    • Beardmancer says:

      Ah, I'm a dolt. Checking "Allow executing file as program" under folder properties did the trick. I should have ready into it a bit more.

      • Linerd says:

        I was just in the middle of a reply to look at the execute permissions on your folders when your second comment came in. I'm glad you got it figured out.

  9. leo says:

    hello, how can I access the site I created following your guide externally? I setup a wordpress, can I show it to someone over the internet? Thanks in advance

  10. Capri says:

    I am new bird in Linux world... I have created my own Linux Apache server in CentOS for my network monitoring with CACTI, I have configured each & everything & my apache is working fine. I have registered my web portal also but still i am not able to my portal(web-page) with other machine in network. please suggest me what can i do for resolution of my this error. Please help me & guide for succesful result. Thanks in advanced for my help.

  11. ric says:

    I don't know if you can answer me but I can't find /etc/apache2 directory in Mageia. I don't suppose you know where to look for this? :)

  12. K.narender says:

    hi, dis narender.k i want total informaion abt linux from basic to top level theory notes can u plz..............provide me the site u know/....................

  13. Versti says:

    When I browse to my http://site1/ I only get 'Could notcConnect' errors
    As far as I can tell I've followed the directions, including the permissions suggestion.
    Browsing localhost gives me the default 'It works!' page, but that's all I can load.
    Please Help!?!

  14. say says:

    The requested URL could not be retrieved
    what happen?? not working in my ubuntu

    • Linerd says:

      Did you replace yourID in the file above with your user ID? Otherwise, it could possibly be a permissions issue with your directories. Do you have execute permission on all of the directories in your path?

      • say says:

        yes, i replace "yourID"
        how i can execute permission? which directories?
        thanks for quick replay my question

        • Linerd says:

          You want execute permissions on your home directory, the webdev directory and site1 directory. You should be able to right click on the folders in your file manager to check the permissions. You can also set them from there.

          You can also set permissions from the command line:

          chmod ugo+x ~
          chmod ugo+x ~/webdev
          chmod ugo+x ~/webdev/site1
          chmod ugo+x ~/webdev/site2
  15. Hamidjon says:

    Thanks a lot, it is working :)

  16. Samir says:


    I got the solution. I was missing on some minor entry under my tools->options->Network->Connections->Settings.

    When on proxy network, then under firefox browser all I had to choose was manual proxy with my proxy settings. And I had to add my newly created document root folder with name for example site1, site2 etc. in No Proxy for entry in my firefox browser.

    Thanx anyway.

  17. Samir says:

    Also, Adding to above information, I am able to open my joomla websites by typing localhost in both cases when I put it in /var/www/ .

    So, Apache server is fine with or without proxy on vmware.

  18. Samir says:

    Thanks for the great tutorial. This really helped me. But I am facing some issues.

    I have Ubuntu-12.04LTS using vmware on Windows 7. I work on a internet with proxy configuration when I use my laptop in the office else I switch to no proxy. I configured the Apache as per your tutorial for multiple websites. It works great when I don't use proxy with my personnel internet connection at home.

    But in office when connected with internet using proxy, it does not work. When I enter the site in address bar of firefox, it gives me google search results. This is not the case when I dont use the proxy at home.

    For your information I have following configuration,
    1. on Vmware setting for network, I have chosen NAT.
    2. On Ubuntu under System Settings -> Network ,
    i use manual proxy with port 1080 when using office else I configure it to no proxy when at home.
    3. On Ubuntu, under Network Connections -> Wired I choose to configure my wired connection IPV4 as always Automatic(DHCP). This remains unchaged in both situations and I am able connect to internet.
    4. In firefox under Tools->Options->Network->Connections->Settings, i choose No proxy or Manual depending upon where I am accessing internet.

    Please help.

  19. Yatindra says:

    Super...I was struggling with an open source application installation for 3 weeks and the steps mentioned by you worked. Thanks a ton!!!!

  20. o2se3tak says:

    Thanks! Worked like a charm :)

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>