How to Configure Apache Web Server on Linux

July 26, 2009 by Linerd
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.

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

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

  1. This and the LAMP tutorial are absolutely invaluable. Thank you Linerd.

    :o )

    One thing confused me. The two sites I created both ended up opening the same home page.

    I finally figured out that it was necessary to give each its own line AND its own IP address in the hosts file. Some clued up folk may have understood that without being told, but there may be other noobs like myself who need it spelled out.

    i.e.
    For my wordpress site: 127.0.0.1
    For my php_tutor site: 127.0.0.2

    It worked, so I guess that was the right thing to do.

    • Linerd says:

      Alan - Glad you got things working. I was able to get my system working with both sites on the same IP address. My hosts file looks like this:

      127.0.0.1 localhost site1 site2

      I'm no expert, but I don't think either way is necessarily right or wrong. There are usually several ways to get the job done. The key on this one is to make sure to run the apache2 reload command. I've also chased my tail a bit due to Firefox caching a page. The Web Developer Toolbar plugin provides an easy way to clear the browser cache.

      I just remembered that I also explicitly set up localhost to point to the /var/www directory, so that may have made a difference on my setup.

  2. [...] /var/www.  If you’ll be working on multiple sites you may want to consider some additional Apache configuration to keep things neat and clean on you [...]

  3. Raphael Yabut says:

    This is really great!

    I am finding my way through Linux, and tutorials are really educational.

    Thanks a lot for sharing.

    Got it right the first time!

    Best regards

  4. Rick says:

    Thanks. I normally develop on Windows and this is my first attempt at ubuntu.

  5. Vayu says:

    Thanks this was very helpful. I've now got multiple top level sites on one machine.

    Any idea how to access them on another machine on my local network?

    The "It works" page can be accessed by the other machines on my network by entering this machine's IP address, but I can't figure out how to access site1 and site2 from the other machines.

    • Linerd says:

      @Vayu - I haven't tried accessing the sites from other machines on my network. You may need to set up a local DNS (domain name server). It may be possible to remotely access site1 and site2 by adding a line to your /etc/hosts file on the client machines. I'll have to give that a try.

      Edit: As far as an easy solution, I've only come up with this cheap hack:

      sudo ln -s /home/userid/webdev/site1/ /var/www/

      You can then access site1 on your network at: http://ip.address/site1/

  6. bvsmohan says:

    thanks for this tutorial, its simple and works

  7. Larry says:

    Great Tutorial Linerd. Two questions.

    I am getting a 403 Forbidden permission error. I am new to running apache on my own machine (Ubuntu 9.10). Is the permissions error a problem with Ubuntu permissions or Apache permissions.

    Also, a random question. If I make the server name a .com (e.g. "ServerName site1.com"), in which order with the browser (Firefox) resolve the name conflict with the remote site?

    Do you know of a way to use Firefox to select which location to check. Someone may want to do this for testing purposes if they have static addresses all over there website (which a good coder would never do).

    Thanks

  8. Larry says:

    Hey Linerd.

    I figured it out. I although I had read permissions to all in ubuntu set to my "webdev" directory, I did not have read permissions set for the parent folder, which is my user in the home directory. It seems apache needs all folders in the virtual path to be readable by all. The only question I have is, does this make my entire user folder vulnerable in some way? Is there a way to restrict usability to only the home/user/webdev/ path, without having to change permissions of all other directories in the home/user/ directory individually?

    I figure that this must be possible since it is possible to have multiple users in the home directory, and there is a set up that restricts users from viewing each other's files.

    I guess the overall question is, is it possible to give read permissions to a sub directory without giving read permissions to all parent and sibling directories?

    Thanks.

    • Linerd says:

      @Larry - There should be no need for the read permission to be open on sibling directories. You could also make the rest of the files readable only by you. Having read and execute permission open on the directory would allow someone else to see the files, but as long as the read permission on the file itself is restricted, they won't be able to open the file.

      I'm sure there's another option that I can't think of right now too. You may be able to do something by creating a different group ID.

  9. gingerbreadman says:

    Wonderful tutorial. Simple and concise.

  10. ZogsterJack says:

    Many thanks for the excellent tutorial - all worked perfectly for me.

    One added point - I wanted to be able to access these multiple sites from other PCs on the local network.

    Spent ages looking for the answer, then it dawned on me. Update the hosts file in /etc on the client PCs with the fixed ip address of the server and the equivalent entries to those in the tutorial; for example 192.168.0.100 site1 site2 (where 192.168.0.100 is the server ip address)

    This does mean you'd have to setup each client PC and then amend for each new site you add in the future, but this works for me right now.

    Guess the better option is to set up a DNS server (thinking Bind9 for this) to centralise this effort, but I'll personally save that for another late night.

    Many thanks again

    • Linerd says:

      @ZogsterJack - Thanks for the comment. I know some other people have been wanting to figure out the same issue with the server on a home network, so thanks for posting a solution. I tend to just do my web work on my laptop with LAMP installed, so I haven't gotten into any real networking issues. I agree that a DNS server seems like the more "correct" solution, but I haven't found the motivation to give it a try.

  11. sanjay says:

    Many thanks for the excellent tutorial

    I have make changes exactly as you have metioned in your tutorial.
    I receive following error for testing site1

    Host not found
    name lookup for site1 failed

Leave a Reply