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




Hello there
I have followed the tutorial and it seemed to work on the command line but when I tried to access http://site1/ i get 403 forbidden
You don't have permission to access / on this server.
where did I go wrong.
regards
roy
Check out the comments from Larry. He had the 403 error and it turned out to be a permissions problem. Do you have read permission open on your home directory?
Hello Linerd
Thank you very much that was the problem. Great tutorial.
Thanks again
Roy
Hey man! You are too great! What others cannot do, you have done. Now my long time desire has become possible because of your tutorial. And thanks to the almighty for showing me to this website.
Hi Linerd
Thanks for the tutorial. I'm a newbie trying to set up my own ubuntu linux server for a production enviroment to host some of my applications. Is the setup tutorial for lamp and apache suitable for production environments? Also if we want to keep Ubuntu lamp installation up to date is there a command that we use?
Thanks again for the great tutorial.
Hi Pragasan - I'm confident that the LAMP setup would be fine for a production environment. The Apache configuration, I'm not sure. I know little more than what I've documented on this website. I'm not sure that I've followed best practices for Apache, but the setup here works well for an offline development environment. I'd encourage you to consult the Apache documentation if you want to set up a production environment.
As far as keeping things up to date; if you're running Ubuntu Desktop, then the update manager should keep you up to date. Otherwise, you can update your system with
sudo apt-get upgrade
Hi Linerd
Thanks for all the help.
take car
I installed LAMP on a setup of Ubuntu 10.04 and added the site1 site2 environments exactly as you show. Both sites worked just as you described. I have started using site1 to follow a series of tutorials on php.
I have run into a problem and am baffled. Using firfox, I can got to localhost/site1 and call the index.html page and see the "Site 1 works!" in the browser
I have copied the testing.php into site1 and when I call the page it gives me the php page, so I know that apache is serving up php from site1 as well.
I have built the following page called index.php which calls for a MySQL database called gwdb and a table in the database called guitarwars. I have written the code exactly as it is written in the tutorial changing only the location of the database to site1 in the call to connect to the database; $dbc = mysqli_connect('site1','root', 'password', 'gwdb')
All I am getting in firefox is a blank page. In chromium web browser I am getting "Oops this link appears to be broken"
I am new to Linux and PHP and trying to learn and I can't see what is causing this break in the link. I have included the code below:
James - I edited your comments into one to capture the spirit of what I think you were trying to show.
I'm no php pro, so I doubt I'll be much help. I did try your file on my machine and it doesn't load. The file does work if I remove the php portion. Maybe another reader can offer some advice.
in your mysqli_connect statement you should still be using localhost not site1 because the mysql server is on localhost.
Linerd,
Thanks for cleaning up the comments. I fixed the issue. I had the database call parameters typed as strings ( ' ' ) by removing the single string quotes I was able to get the database to connect and the page would then load.
Thanks again for the tutorial to set up the multiple web application development environment for apache.
Awesome post! It worked great!
I NEEDED SOME MATERIAL TO FINISH MY REPORT ,I THINK I GOT IT THANK U
Fantastic post!!!!
I managed to get this working so if I VNC onto my ubuntu desktop server I can visit http://site1/ no problems but if i try from my laptop I get an error... should I be adding site1 to the second line of /etc/hosts @127.0.1.1 instead of 127.0.0.1 localhost?
Thanks a million
Just found the hack under the older posts...sorry should have checked there first
Is the configuration the same if using a no ip tool like NO-IP?
I cannot reach any of the 2 sites when using the NO-IP URL (.syte.net).
I tried adding the following entry in /etc/hosts:
192.168.1.100 site1 site2
(192.168.1.100 being the IP address of my Linux box)
Although, if using the URL .sytes.net, I do reach the 'It Works!' Apache test page from my webserver.
Any help would be greatly appreciated.
Thx!
Rudy, I haven't tried this setup with a dynamic DNS setup. I'm thinking you would most likely have to modify the Apache configuration. However, it might work like it does when accessing the host machine web server from a VirtualBox guest. Have you tried __.sytes.net/site1 or __.sytes.net/site2?
Linerd,
Thx for the quick reply.
Yes I did try __.sytes.net/site1 or /site2 without luck.
I actually figured it out. I had to disable the default config under /etc/apache2/sites-available by using the 'a2dissite' command (sudo a2dessite default).
Actually, I do have to add that it will only allow __.sytes.com to point to one of the site and it will not work in a __.sytes.net/site1 and __.sytes.net/site 2 way.
Still need to figure out this one.
is there a way for other machines on a local network to view these sites? I can view the /var/www/index.html page using either the ip address (http://192.168.1.100) or the name (http://linux-box) of my linux machine, but can't seem to find the custom sites. (Which work perfectly on the linux computer, thank you)
Check out the comment from ZogsterJack. He found a way by editing the /etc/hosts files on the client machines so he could see the custom sites. http://tuxtweaks.com/2009/07/how-to-configure-apache-linux/comment-page-1/#comment-2089
I haven't tried it myself, but it's been reported to work.