Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

SUBSCRIBE

COMMENTS

Tux Tweaks
Linux Tweaks, HowTo's and Reviews
Home

About

Search this website...

Privacy Policy

How to Configure Apache Web Server on Linux

SUBSCRIBE BY EMAIL

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.

RECENT POSTS

Distro Review: Linux


Mint 13 Maya
Cinnamon Edition
Fixing Common
Problems with LAMP
on Ubuntu
Installing LAMP on
Ubuntu 12.04 Precise
Pangolin
Ubuntu 12.04 is Here:
List of download
mirrors
View Images With The
Proper EXIF
Orientation In Chrome
or Chromium
TAGS

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.

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.

android Apache Apple

audio avast bash

command
line dell Distro Review
Chromium

fedora firefox flac FreeDOS


gadgets

cd
mkdir webdev
cd webdev
mkdir site1 site2

cd ~/webdev/site1
echo 'Site1 works!' > index.html
cd ../site2
echo 'Site2 works!' > index.html

gnome google

sudo cp default site1


Repeat the process to create a site2 file.
As root, edit the site1 configuration file.

sudo nano site1

google chrome GStreamer

HowTo HP humor

Edit the file to look like this (the changes are in bold).

ImageMagick jaunty
karmic koala LAMP

<VirtualHost *:80>

GO

Enter your email


address:

Subscribe
Delivered by
FeedBurner

TOP CONTENT

Distro Review: Linux


Mint 13 "Maya"
Cinnamon Edition
Installing LAMP on
Ubuntu 10.04 and
10.10 (Lucid Lynx,
Maverick Meerkat)
Install LAMP and
phpMyAdmin on
Ubuntu 11.10
Installing LAMP on
Ubuntu 12.04 Precise
Pangolin
How to Configure
Apache Web Server
on Linux
Remove Old Kernels In
Ubuntu With One
Command
How To Control Fan
Speeds in Ubuntu
Fade Out Images with
GIMP
How To Rotate The
Screen in Ubuntu With
The Nvidia Driver
How To Uninstall LAMP
in Ubuntu 9.10 Karmic
Koala
CATEGORIES

Select Category
ARCHIVES

2012 (10)
May (2)
Distro Review:
Linux Mint 13
"Maya" Cinnamon
Edition
Fixing Common
Problems with
LAMP on Ubuntu
Apr (4)
Mar (3)
Jan (1)
2011 (26)
2010 (60)
2009 (89)

converted by Web2PDFConvert.com

karmic koala LAMP

linux Lucid Lynx


mp3 MSI Wind Nautilus
netbook network news
ogg open source
opinion phpMyAdmin
review security

Ubuntu usb web


development
windows WordPress

<VirtualHost *:80>
ServerAdmin webmaster@localhost
ServerName site1

2009 (89)
2008 (37)
FAVORITE SITES

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>

Ithaca Presbyterian
Church
Tux Machines
Ubuntu Forums

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 servers 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.

converted by Web2PDFConvert.com

sudo /etc/init.d/apache2 reload

Tags: Apache, HowTo, linux, web, web development, WordPress

Comments
149 Responses to How to Configure Apache Web Server on Linux
Older Comments
Nipun says:
November 26, 2011 at 9:16 AM
HI.Thanks for the great tutorial.
Whenever i reload apache2 i get the following:
* Reloading web server config apache2 [Sat Nov 26 19:42:10 2011] [warn] NameVirtualHost *:0 has no
VirtualHosts
[Sat Nov 26 19:42:10 2011] [warn] NameVirtualHost *:0 has no VirtualHosts
Also i am not able to send "PUT" requests via any REST client(for eg Google Chrome has an extension for the
same)
Reply

Linerd says:
November 26, 2011 at 11:21 AM
Your virtual host file should refer to *:80 for port 80, not *:0.
Reply

raj says:
April 5, 2012 at 4:56 AM
U have to /etc/apache2/httpd.conf ----------- servername localhost . U won't get the error
Reply

raj says:
April 5, 2012 at 5:00 AM
U need to do this in /etc/apache2/site-available/site1 -------- DocumentRoot which u have
create a directory site folder
Reply

raj says:
April 5, 2012 at 5:59 AM
Try it...........
/etc/apache2/site-available/site1.............
DocumentRoot
a2dissite default
a2ensite
service apache2 reload
service apache2 restart
u can see that error won't get u..............[warn] NameVirtualHost *:0 has no VirtualHosts

Paul says:
December 6, 2011 at 11:43 AM
Great tutorial. I'm new to this stuff. Did the same thing for site2 as suggested but it didn't work! It
generated the index.html output from /var/www/ instead. I think the tutorial needed to be a little more concise
with regards to site2. Checked my scripts to ensure that there were no typo's. Should the first line in /etc/hosts
read "127.0.0.1 localhost site1 site2" for instance?

converted by Web2PDFConvert.com

Reply

Matt says:
January 10, 2012 at 8:33 AM
"Should the first line in /etc/hosts read "127.0.0.1 localhost site1 site2" for instance?"
No, each site requires a new line so instead of "127.0.0.1 localhost site1 site2" it should be:
127.0.0.1 localhost site1
127.0.0.1 localhost site2
You just repeat the steps you did to create site1 in order to create site2, site3, etc. except you add a line in
/etc/hosts for each new site. Also, your site name can be pretty much any name you want (it doesn't have
to be "site1").
Reply

Linerd says:
May 15, 2012 at 7:57 AM
I use

127.0.0.1 localhost site1 site2


with no problems.
Reply

Nipun says:
December 13, 2011 at 5:48 AM
Hi,Thanks for the wonderful tutorial.If i wish to view say index.html under site1 from another computer
connected over LAN,how can i do that.
So from my other computer if i fire http://site1/index.html it should render the contents
Reply

Rodrigo says:
December 20, 2011 at 2:26 PM
Hi there,
I did it exactly the way you explained, and everything has been set up right. The only problem happened in the
end ..
phpmyadmin could not be found ... i've got an 404 error, even with the "success" message at the end of
installation "ldconfig deferred processing now taking place"
what could be the error?
Thanks that much ..
hugs from Brazil
Reply

Linerd says:
December 20, 2011 at 2:31 PM
Look at this post: http://tuxtweaks.com/2011/10/install-lamp-and-phpmyadmin-on-ubuntu-11-10/
and find the section near the bottom that refers to Fixing phpMyAdmin.
Reply

Sugiono says:
January 12, 2012 at 10:06 PM
Hello again,
I checked each step , and everything looks good. testing.php and phpmyadmin run smoothly. the only thing
when I try to execute install.php, the browser stop and blank.
Can you please advise? I did change the /etc/apache2/sites-available/[sitename] ,
Options Indexes FollowSymLinks MultiViews
AllowOverride All ( and also to AuthConfig>
Order allow,deny
allow from all
restart the apache, and still not working. pls anyone can help?
Reply

Linerd says:
January 13, 2012 at 8:36 AM
What is install.php? What is is supposed to do?
Reply

converted by Web2PDFConvert.com

sugiono says:
February 11, 2012 at 6:59 AM
install.php is an executable file for my php package / module, but no worry about that first since
i am still figuring out how to enable site 1 and site 2 since everytime i reload apache,
result
error line 13
unable to resolve host 0.0.0.0:80
thus i can never access index.html i have stored in that directory, instead it shows the index.html from
/var/www
i do not know what happend, i edit /etc/hosts and erase the 127.0.1.1 ubuntu , since i thought that is
the confusing culprit everytime the apache2 want to restart,
can you please help me straight this out, thanks in advance
Reply

sugiono says:
February 11, 2012 at 7:01 AM
the exact message is
sugiono@ubuntu:~$ service apache2 reload
* Reloading web server config apache2 ulimit: 88: error setting limit (Operation not permitted)
apache2: apr_sockaddr_info_get() failed for ubuntu
apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for
ServerName
httpd not running, trying to start
(13)Permission denied: make_sock: could not bind to address 0.0.0.0:80
no listening sockets available, shutting down
Unable to open logs
Action 'graceful' failed.
The Apache error log may have more information.
Reply

sugiono says:
February 11, 2012 at 7:22 AM
this problem arise since I install ubuntu server on my pc.
the creating a web development directory was fine, but then i was unable to execute
install.php ( see my top posting), so i clean install everything and start with ubuntu server,
ubuntu desktop and LAMP.
I forward my server ( port 80 ) so that I can have it alive. it went great for /var/www web,
but when i tried to have the second hosting, web development, fail with the above message (
see my 3rd posting) although the name site1 can be accessible ( direct to /var/www, where it
should be /home/webdev/site1 )

Linerd says:
February 11, 2012 at 1:06 PM
Do you get the same errors if you use sudo with the above command?

sudo service apache2 reload


You can fix the fully qualified domain name by doing this:

echo "ServerName localhost" | sudo tee /etc/apache2/conf.d/fqdn

Linerd says:
February 11, 2012 at 1:12 PM
Don't remove that line from /etc/hosts. It should work with it in there and removing it may
mess up something else.
I think you need to find the file that is referencing IP address 0.0.0.0:80. Since you're working with
your local machine, it should probably reference 127.0.0.1:80
Reply

Stanislav says:
January 15, 2012 at 11:05 AM
MANY MANY THANKS. 5:00 was trying to make a virtual host on a pile of manuals, only your fully
working. Thank you!
Reply

Sachith says:
February 8, 2012 at 7:58 AM

converted by Web2PDFConvert.com

Thank you very much. Instructions were perfect


Reply

sugiono says:
February 11, 2012 at 7:39 AM
maybe below can give more information ( note that site1 name is mysite here )
sugiono@ubuntu:~$ apachectl -S
ulimit: 88: error setting limit (Operation not permitted)
VirtualHost configuration:
wildcard NameVirtualHosts and _default_ servers:
*:80 is a NameVirtualHost
default server localhost (/etc/apache2/sites-enabled/000-default:1)
port 80 namevhost localhost (/etc/apache2/sites-enabled/000-default:1)
port 80 namevhost mysite (/etc/apache2/sites-enabled/mysite:1)
Syntax OK
Reply

Jeoffrey Stiernon says:


March 1, 2012 at 12:10 AM
Great tutorial ! Worked like a charm !
Reply

John Erck says:


March 1, 2012 at 6:16 PM
You rock. Thanks so much for posting this. I'm moving off a shared host and onto a barebones Linux box
with no support... was totally lost as to how to emulate my old environment and this did the trick.
Reply

zaldy says:
March 11, 2012 at 6:51 AM
followed the instructions here and everything came out as expected. thanks!
Reply

Larsen says:
April 3, 2012 at 2:19 PM
I keep getting the same error
Forbidden
You don't have permission to access / on this server.
Apache/2.2.20 (Ubuntu) Server at site1 Port 80
I tried to rerun the tutorial, but still the same : / any idea what went wrong?
Reply

Lah says:
April 8, 2012 at 8:41 PM
Thank you for this tutorial. Could you give a bit more explanation on Apache directives in more simple as
this configuration. Also add some comments in the files. thank you
Reply

Linerd says:
April 8, 2012 at 10:14 PM
After reading this post, you pretty know everything I know about Apache
I know it's not the easiest reading, but I suggest you check out the official Apache documentation.
Reply

Pawan says:
April 15, 2012 at 1:37 PM
hey! thanks for this!
I was planning to have a local wordpress development environment setup. so i followed your tutorial and
everything worked.
except one. when i create a new post or page and publish it, and try to open its permalink, i get the 404 error. it
says that the link can not be found on Apache2(ubuntu) server at port 80.
moreover, it displays the url as "/permalink" rather than "site1/permalink".
what is going wrong?
Reply

Linerd says:
April 15, 2012 at 11:23 PM
It sounds like mod_rewrite isn't working. Did you change AllowOverride to All under the <directory
/home/yourID/webdev/site1/> section?
Reply

converted by Web2PDFConvert.com

bifs says:
April 17, 2012 at 12:50 PM
Excelent tutorial but I have a more especifc question regarding permissions:
Supposing I don't want do change from /var/www main directory, and that I am programming from an external
computer with Netbeans updating files directly on the server, what should be /var/www permissions? And owner
of the directory?
With only one site I managed it, but now I have other projects and I am trying to setup a SVN subvservion
repository and everything goes right until it's time to write inside /var/www/whatever...
Could you please, give me some tips?
Thanks in advance!
Reply
ans says:
May 14, 2012 at 8:36 PM
Hey there,
Thank you for the tutorial. I follow all the steps on ubuntu 12.04, but when I enter site1 or site2 on the address
bar it is always the content of the default.html which is displayed. I check that site1 and site2 was enabled in the
sites-enabled folder and the links are there. Can you help me figure out what is wrong?
Thank you
Reply

Linerd says:
May 14, 2012 at 9:07 PM
What is the content of your /etc/hosts file? You should have a line that looks like:

127.0.0.1 localhost site1 site2


If you edit that file, make sure to then reload Apache

sudo service apache2 reload


You might also want to clear your browser cache for good measure too.
Reply

ans says:
May 15, 2012 at 7:01 AM
hey sorry, I have been waiting and didn't even think about just refreshing the page to see your
reply.
this is the content of my hosts:
127.0.0.1 localhost
127.0.0.1 localhost site1
127.0.0.1 localhost site2
I did that after reading this comment here:
" Matt says:
January 10, 2012 at 8:33 AM

"Should the first line in /etc/hosts read "127.0.0.1 localhost site1 site2" for instance?"
No, each site requires a new line so instead of "127.0.0.1 localhost site1 site2" it should be:
127.0.0.1 localhost site1
127.0.0.1 localhost site2
You just repeat the steps you did to create site1 in order to create site2, site3, etc. except you add a
line in /etc/hosts for each new site. Also, your site name can be pretty much any name you want (it
doesn't have to be "site1"). "
I change the configuration to localhost site1 site2 and it it working! I have been on ths simple
line for the hole night!!! crasy !
Thank you
Reply

ans says:
May 16, 2012 at 10:41 AM
Hey Linerd,
It looks like things are getting worse for me. This solution I posted worked fine Yesterday, now my browsers
can't display any local pages or phpMyAdmin any more... what's going on? thx
Reply

Linerd says:
May 16, 2012 at 11:26 AM
Check you /etc/hosts file. Sometimes Network Manager likes to overwrite this file, so you might need
converted by Web2PDFConvert.com

to make the changes again. If that is the case, you could try making the file immutable so it can't be
overwritten by the system. So make your changes to the file, then

sudo chattr +i /etc/hosts


If you later need to reverse this later (is mutable a word?)

sudo chatter -i /etc/hosts


Reply

ans says:
May 17, 2012 at 6:10 PM
Linerd,
My problem was much simpler. Apache just stop running and I didn't know. sudo service
apache2 start was enough! Thanks again.
Reply

Ed says:
May 16, 2012 at 11:38 AM
WONDERFUL!
I had so many issues doing this on Natty 11.04, however on Pp 12.04 and your tutorial it works fine.
BIG BIG thank you!
Reply
Older CommentsLeave

a Reply

Your email address will not be published. Required fields are marked *
Name *
Email *
Website
Comment

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>
Post Comment
COPYRIGHT 2009 - 2011 TUX TWEAKS.

GET A BLOG WORDPRESS LOG IN

converted by Web2PDFConvert.com

You might also like