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
cd
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>
	<Directory /home/yourID/webdev/site1/>
		Options Indexes FollowSymLinks MultiViews
		AllowOverride All
		Order allow,deny
		allow from all
	</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 /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 127.0.0.0/255.0.0.0 ::1/128
    </Directory>

</VirtualHost>

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.

127.0.0.1 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 127.0.0.1 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

Comments

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

  1. gido505 says:

    I'm not really much on posting comments but I just have to thank you for this great tutorial, everything worked with no headaches.
    A thank you also for this tutorial on installing lamp on ubuntu 10.04
    http://tuxtweaks.com/2010/04/installing-lamp-on-ubuntu-10-04-lucid-lynx/

    Thank you very much. :)

  2. bart says:

    Thanks for the tutorial.
    I also liked the idea to have it all setup under the account that I use on the server (ubuntu).

    Now here is the issue that I have not been able to resolve and hope someone can help me with the optimal solution.
    I use Joomla and Joomla uses www-data (the account apache uses) to write files when you install extensions or add files to the CMS.
    Then I use an FTP account same as ubuntu user ID to copy an manipulate files remotely.

    Yes I can cmod the files and dir eachtime they are written to give access to Joomla or my account but there must be some way that both system account www-data and useraccount have full access to /home/useraccount/webdev/site1 site2 etc

    If not what is the use to set it up under the /home/useraccount

    sorry I am a noob but eager to learn what is the best way to do this.

  3. richarduie says:

    Thanks for this and the LAMP install how-to. Worked out of the box for me on Lucid - both laptop and workstation. I'm a mere developer, but the ease of this makes me feel frisky about the notion of doing it on my (internal) network print/file server as well. Refreshing to find simple, correct, complete how-tos.

    Having been "playing with" Linux since '95 (Slackware, RedHat, Mandrake, Mandriva) and using exclusively since '05 (Fedora, Kubuntu), I observe 'tis rare and wonderful to get perfect advice in Linux-land. Yours is the kind of effort that leads to more satisfied Linux-geeks and fewer Windoze and Mac based developers.

    richarduie

    P.S. Last addition for my purposes was cURL...Shi Chuan had that at:
    http://www.blog.highub.com/php/php-core/linux-ubuntu-install-setup-php-curl/

  4. Olwe says:

    I've followed the chmod instructions, but still get:

    forbidden
    You don't have permission to access / on this server.

    Any ideas on how to troubleshoot/fix?

  5. Stramash says:

    Thanks for the guide. I am getting a 403 when I try http://site1/ The file permissions are 644 and the directory is 755.

    • Linerd says:

      I think your index file needs to have execute permission. Have you tried setting the file permission to 755? Actually, 744 might work too.

      • Stramash says:

        I found my mistake. First I took out:

        Options FollowSymLinks
        AllowOverride None

        And that worked, then I realised I hadn't set All here:

        Options Indexes FollowSymLinks MultiViews
        AllowOverride All

        So I put the first bit back in, AllowOverride to All, and changed permissions back to 644 for files and 755 for dirs.
        Everything is fine now, though I am going to find out why each works and the difference they make.
        Cheers

  6. Al says:

    Hi,

    Great instructions, thanks. Very clear.

    I've got it all working on my Ubuntu virtual machine, running on a windows box but I've got a problem. I can't seem to access it via:
    http://192.168.0.11/MyApp
    I get a 'Not Found' response from apache.
    It works within the Ubuntu box i.e.
    http://localhost/MyApp
    ...but I'd rather just work with the browser if I can, from windows.
    Any ideas anyone?

    Thanks,

    Al.

    Oh, by the way... you'll need to set the 'i' immutable attribute on the hosts file or else Network Manager will screw it up for you. i.e.:

    sudo chattr +i /etc/hosts

  7. gadasadox says:

    thanks man, it helps me alot.

  8. Zoran says:

    Hi guys. Sorry but i still have problem. I followed all instructions but without success.
    Whenever i try to to start my website i get default page - not index.html page of site.

    Does anyone have suggestions how to fix this?

  9. Mav says:

    Hey, a little help please ... I set it up exactly as described, and followed the instructions exactly as you said; then I got a "Oops! Could not contact domain ... trying again." So then I added the fqdn file, then I followed community help on Ubuntu and disabled the default using a2dissite, and kept the enabled for site1, and now I am getting a Forbidden error about not having permission to access / on this server?

    I actually did all of this after following your previous post about setting up a dev environment with Ubuntu, so I'd really appreciate any troubleshooting help with this.

  10. Sam says:

    Great tutorial. I have done step by step uninstalling LAMP and reinstalling it again. I also installed phpmyadmin but http://localhost/phpmuadmin is giving me a 404 not found error.
    Can you please help get this phpmyadmin working

    Thank you

  11. Robert says:

    I get the following message where I use samd instead of site1.
    I have followed the instructions above exactly.

    While trying to retrieve the URL: http://samd/

    The following error was encountered:

    Unable to determine IP address from host name for samd

    The dnsserver returned:

    Name Error: The domain name does not exist.

    I can ping samd and get the correct response.

    Any help would be greatly appreciated.

  12. Safiur Rahman says:

    when i run
    sudo /etc/init.d/apache2 reload

    I get

    * Reloading web server config apache2
    Warning: DocumentRoot [/home/yourID/webdev/site1/] does not exist

    How can I fix?

    • JB says:

      You have to replace "yourID" with your login name, i.e. the name of your home directory. So if your login name is "safiur", then your home directory is probably /home/safiur.

      To change to your home directory in terminal, type:
      cd ~

      Then to display the current working directory, type:

      pwd

      It should display something like:

      /home/login_name

      where login_name is your actual login name.

  13. jimmidill says:

    Great site, great tutorial! I am a windows user but am using ubuntu 10,10 via VMWare (to teach myself PHP on a lamp stack). I like ubuntu but I still get lost everytime I step too far outside of my cosy familiar windows world, and if I'm honest most linux guides and tutorials have succeeded in baffling my poor pea-sized brain!

    This is one of the first linux sites that hasn't gone over my head, which I'm very grateful for :) I'll be following closely from now on. Thanks!

  14. alixky says:

    Great tut!! I have been following yours from installing LAMP and configuring Apache2 from this post and I have to say everything running without any hiccup. You're the man! Thank you very much! This site will be definitely one of my favorite spot on the net!

  15. Aaron says:

    Thanks for the great tutorials on getting LAMP up and running and setting it up for multiple sites! I love my new test environment.

    When my computer goes into suspend, I lose the changes I made /etc/hosts. Not a huge deal since I just sudo nano /etc/hosts and make the edits again. If you have any idea why this file "resets" I'd love to know what I can do about it.

    Thanks again for the fantastic tutorials!

    • Linerd says:

      I don't have time to dig into this right now, but it looks like there's a bug in Maverick. You may find a work-around if you read through the comments on the bug report.
      https://bugs.launchpad.net/ubuntu/+source/network-manager/+bug/659872

      • Aaron says:

        Thanks for the tip and link. I will check it out. It's not a huge deal to me since the workaround is quick and easy. When I figure it out, I will post it here. Thanks again!

        • Aaron says:

          Ubuntu 10.10 apparently released a fix in the network manager, because the "hosts" file is no longer overwritten, on suspend or reboot. Very nice! Thanks again, Linerd, for the great tutorial. I plan on reformatting and starting fresh with the final release of 11.04, so I will be using this tutorial again... Best, Aaron

  16. gjbois says:

    Just as a side note: When adding site2 I configured /etc/hosts like this:

    127.0.0.1 localhost.localdomain localhost site1 site2

    Now all three of the sites work, so I assume sites can be continually added to the list without affecting the others.

    Question: To enable the site I entered 'sudo a2ensite site'. Is there a command for disabling the site; say I no longer have the site.? I just don't want the list to grow and bog something down.

    • Linerd says:

      Yes, there is a command to disable a site. It is a2dissite. To remove site2 it would be like this:

      sudo a2dissite site2
  17. Hey man. I'm truly just a beginner, but through workin' with tutorials have kinda learned a few things through teaching myself. Here's all I want. I have an ubuntu 10. server with LAMP on it. I just want to be able to host multiple sites that are custom created by myself through Dreamweaver and such. I can see my site fine on my 'localhost' server, the computer I'm on right now, but cannot let's say through my laptop over the net. If I could just be told what has to be done, not necessarily step by step, then I can maybe figure it out. Then again, maybe not, but I'd like to know what's missing. Is it just a matter of setting a unique IP and configuring my domain name (which I bought through GoDaddy.com) to find that address? Or is it something else?

    I'm a beginner, REALLY JUST beginning... So, thank you very much, I really appreciate it!

  18. Adrian says:

    Hi, thanks for the tutorial it has gotten me further than any previous attempts, there is however a snag at the very end that I wonder if you can help me with:

    Where it says in you directions

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

    the file I see is different, it looks like this and I don't know where I should apply the edit:

    192.168.102.148 adrian-N80Vm # Added by NetworkManager
    127.0.0.1 localhost.localdomain localhost
    ::1 adrian-N80Vm localhost6.localdomain6 localhost6
    127.0.1.1 adrian-N80Vm

    # The following lines are desirable for IPv6 capable hosts
    ::1 localhost ip6-localhost ip6-loopback
    fe00::0 ip6-localnet
    ff00::0 ip6-mcastprefix
    ff02::1 ip6-allnodes
    ff02::2 ip6-allrouters
    ff02::3 ip6-allhosts

    When I run the final command

    "sudo /etc/init.d/apache2 reload"

    I get this:
    Warning: DocumentRoot [/var/www/home/adrian/webdev/site1/] does not exist

    Any ideas?

    • Linerd says:

      You should apply your edit to the end of the second line in your case: right after "localhost".

      For the warning you're getting. Make sure that the path you have in your site1 configuration file is just /home/adrian/webdev/site1/.

      • Adrian says:

        I've removed/reinstalled LAMP and followed instruction to the letter (copy-pasted) twice, still not working. Is it possible that you are using 10.04 and there is a bug in 10.10?

    • Adrian says:

      Thanks for your answer, just to clarify (I'm a total newbie:)

      Should it be:
      /home/adrian/webdev/site1/
      or
      /var/www/home/adrian/webdev/site1/
      ?

  19. Daniel says:

    Great tutorial, I have a special case in my setup I was wondering if you could help me out a little, I'm dualbooting Win7 & Ubuntu each on its partition, plus I have an extra big partition to store and share all my files betweem OS's, so in this partition I have setup a folder named "www" and from windows I'm accesing it using wamp by modifying in apache to link the server to "a:/dropbox/www" and on ubuntu I'm using lamp but I haven't been able to change from /var/www to /media/documentos/Dropbox/www since that is the path that appears in nautilus.

    Anyhelp would be appreciated.

    • Linerd says:

      You should be able to create a configuration file for your site similar to the one above for site1. Just replace the instances of /home/yourID/webdev/site1/ with /media/documentos/Dropbox/www/.

      • Daniel says:

        Hi linerd, I did, exactly what you said, after the last step, restart apache, I get the following error:

        403 Forbidden (In the title of the browser)

        Forbidden

        You don't have permission to access / on this server.
        Apache/2.2.16 (Ubuntu) Server at localhost Port 80

        I don't know what's going on.

        • JohnJ says:

          I have the same 403 error. I am working on a single boot Ubuntu 11.04 install.

          I came here after working through the tut on installing LAMP and running into problems in the last section: installing and testing phpmyadmin.

          Otherwise: tx for the great tuts.

          JohnJ

          • Charlie says:

            This is a cracking tutorial - I ran into the same 403 issue and after some digging sorted the solution.

            Firstly you can check in the apache logs to confirm /var/log/apache2/error.log to see if it is a permissions issue.

            The you can fix it by changing the permissions on the directory

            cdmod o+x /home/yourid/webdev/site1

            Hope this helps.

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>