How to Configure Apache Web Server on Linux
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.
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.
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.
As root, copy the default file as site1.
Repeat the process to create a site2 file.
As root, edit the site1 configuration file.
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.
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.
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.
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/fqdnThen, reload Apache to eliminate the error.
sudo /etc/init.d/apache2 reload


This and the LAMP tutorial are absolutely invaluable. Thank you Linerd.
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.
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:
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.
[...] /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 [...]
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
Thanks. I normally develop on Windows and this is my first attempt at ubuntu.
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.
@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:
You can then access site1 on your network at: http://ip.address/site1/
thanks for this tutorial, its simple and works
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
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.
@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.
Wonderful tutorial. Simple and concise.
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
@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.
Wow thanks alot, that worked like a charm, I use VMWare to test ie6-8 and for some reason it doesn't recognize the host machines site1 or site2 unless you change the host file. If anyone is wondering where this is located for windows XP: C:\windows\system32\drivers\etc\hosts
I suggest you assign a static address to your machine to make life easier.
I hate ie 6! Thanks a ton, this guide has been very useful.
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
Great tutorial!
However, I'm still not able to open my site1 page. It always points to the default page.
The error message when I reload apache2 is this...
apache2: Could not reliably determine the server's fully qualified domain
name, using 127.0.1.1 for ServerName
[warn] _default_ VirtualHost overlap on port 80, the first has precedence
[warn] NameVirtualHost 127.0.0.1:80 has no VirtualHosts
Thanks!
this tutorial, along with the tutorial on LAMP installation is great. thanks a lot.
with the two tutorials i managed to set up my own web development environment. with a lot of tail chasing though, and lots of help from you. again, thanks a lot.
i have some questions though. after i was done with the set up, i realised that i did not like the directory placements, directory names, and host names. i figured that maybe i don't have to do it over again.
so what i did was to instead, just (1) move the directories to where i wanted them to be, (2) rename the directories, (3) tinker on the configuration files (found in /etc/apache2/sites-available$) for the hosts i have made before and rename them according to my desire, (4) delete the symlinks i found in /etc/apache2/sites-enabled, (5)tinker on the file /etc/hosts and change the first line accordingly (i.e. 127.0.0.1 localhost localhost2 localhost3 localhost4), (6) execute sudo a2ensite for the sites, (7) reloaded apache, and finally (8) check on my browser. it worked!!!...
my only fear is, am did i do it right? did i not break anything with what i did? muchas gracias.
It sounds like you did it right. You can also disable a site with the sudo a2dissite command. It will remove the symlinks for you.
wow, i am excited. thanks a lot for the new command "sudo a2dissite".
buenas noches and again muchas gracias.
[...] later found them and worked almost flawless : 1.Installing LAMP on my version of Ubuntu (9.10). 2. Configuring Apache if your working on two or more sites. 3.In case you might need to start from scratch. Possibly related posts: (automatically [...]
I'am a (very) new user of Ubuntu. I used to work with Wamp server in Windows.
How can I create websites when I don't have permissions to write in the webdev forder ? How can I get permissions ?
If you created the webdev folder without using sudo, then you should have write permissions to that folder. If you don't own the folder, then you should be able to change the ownership with the chown command. If you want to change the permissions, then chmod is the command you want to use. Look at the man pages for these commands for more info.
If you follow this tutorial, you won't need permissions to the /var/www/ folder since you'll be putting the files in a folder under your home directory.
If you want to put your files in the /var/www/ directory, you can add your userID to the www-data group with this terminal command:
Correction: I just looked at my system and /var/www is owned by root.
Thank you for your help.
I tried the command :
sudo adduser myid www-data
I gave my password
and I got
Adding user myid to group www-data
but I still don't have permissions to modify the index.html file
You can change the ownership of index.html with:
You may want to change the ownership of /var/www if that's how you want to work. See my above correction about the ownership of that directory.
sudo chown yourid:yourid www
I still don't have permissions for /var/www
but everething works fine with site/ and site2
thank's for
127.0.0.1 localhost site1 site2
both sites work well now.
Thank again !
Merci beaucoup !