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



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.
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/.
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.
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
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.
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?
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/.
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?
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/
?
It should be:
/home/adrian/webdev/site1/
I'm doing something wrong, think it's best to start over, Do I need to delete something so it won't clash with new configuration?
Never mind, I've removed and installed LAMP anew, should take care of it.
If it still does not work I'll be back nagging
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!
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.
Yes, there is a command to disable a site. It is a2dissite. To remove site2 it would be like this:
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!
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
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!
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
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!
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!
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?
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.
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.
Solved this. The proxy server was intercepting the url. I just added samd to the exception list and it worked.
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
Follow the instructions here to fix phpmyadmin: http://tuxtweaks.com/2010/04/installing-lamp-on-ubuntu-10-04-lucid-lynx/comment-page-1/#comment-2952
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.
It may be a permission issue with the directory where have your files. Try this:
If you're using Firefox, you may also want to try clearing the browser cache after you change permissions.
Just to check -- by cdmod you mean chmod, right?
Oops. Yes, I mean chmod. I'll fix my previous comment.
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?
Make sure you added ServerName site1 to the site1 file in the sites-available directory.
thanks man, it helps me alot.
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
Check out this comment thread: http://tuxtweaks.com/2009/07/how-to-configure-apache-linux/comment-page-1/#comment-2089
It may be of some help, but I haven't tried it myself. Good luck!
Oh, and thanks for the tip about chattr on the /etc/hosts file. I know this has been getting overwritten for some people.
Thanks for the guide. I am getting a 403 when I try http://site1/ The file permissions are 644 and the directory is 755.
I think your index file needs to have execute permission. Have you tried setting the file permission to 755? Actually, 744 might work too.
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
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?
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/
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.
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.
Thanks lol....
Below mentioned url is very easy to understand,
http://www.redhatlinux.info/2011/11/different-types-of-configure-httpd.html