How to Configure Apache Web Server on Linux

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

Affiliate Link
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

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

  1. ljuper says:

    Why can i surf to the page http://192.168.1.*/site1/
    but not http://site1 from my other computers on the same network?

    Using ubuntu headless server. 12.04

    P.s i´ve used the sleazy hack sudo ln -s /home/user/webdev/site1/ /var/www/ to make it work

    • Linerd says:

      I think you would need to edit your /etc/hosts file on each machine of your network so that site1 resolves as 192.168.1.1/site1.

      • ljuper says:

        Hi!

        Thanks, i understand i can do that on each machine i have in the network.
        But that is not an option since i have a couple of machines. It works if i edit the hosts on my OSX of course, but i can´t run around. And is it even possible to reach and edit the host files on a iPhone by the way?

        Do i have to edit the DNS Server running on the server? And if, is there a good tutorial available?

        • Linerd says:

          Editing the DNS would be the "right" way to fix it for every client. Sorry, I don't know how to do this. BIND is the typical DNS server on Linux, so I suggest you start your research there. Good luck! :)

  2. Noddy says:

    Thank you for this. I have just tested it on Xubuntu 12.04 and everything works fine.

    I used gksudo leafpad instead of sudo nano

  3. Ed says:

    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!

  4. ans says:

    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

    • Linerd says:

      Check you /etc/hosts file. Sometimes Network Manager likes to overwrite this file, so you might need 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
      • ans says:

        Linerd,
        My problem was much simpler. Apache just stop running and I didn't know. sudo service apache2 start was enough! Thanks again.

  5. ans says:

    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

    • Linerd says:

      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.

      • ans says:

        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

  6. bifs says:

    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!

  7. Pawan says:

    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?

    • Linerd says:

      It sounds like mod_rewrite isn't working. Did you change AllowOverride to All under the <directory /home/yourID/webdev/site1/> section?

  8. Lah says:

    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

  9. Larsen says:

    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?

    • Ines says:

      I have the same problem :(

      • o2se3tak says:

        check permissions for your home directory (under which you have created site1 and site2). you need to try: chmod 750 /home/yourid or chmod 710 /home/ or chmod 755 /home.yourid (depending on how much access you want to give - the least that is required for the sites to be visible is execute permission for the group)
        do the same with the webdev folder and its contents chmod 750 -R /home/yourid/webdev (or 710 or 755)

  10. zaldy says:

    followed the instructions here and everything came out as expected. thanks!

  11. John Erck says:

    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.

  12. Jeoffrey Stiernon says:

    Great tutorial ! Worked like a charm !

  13. sugiono says:

    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

  14. Sachith says:

    Thank you very much. Instructions were perfect

  15. Stanislav says:

    MANY MANY THANKS. 5:00 was trying to make a virtual host on a pile of manuals, only your fully working. Thank you!

  16. Sugiono says:

    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/Tux Tweaks ,

    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?

    • Linerd says:

      What is install.php? What is is supposed to do?

      • sugiono says:

        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

        • sugiono says:

          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.

          • sugiono says:

            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:

            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:

          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

  17. Rodrigo says:

    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

  18. Nipun says:

    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

  19. Paul says:

    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?

    • Matt says:

      "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").

  20. Nipun says:

    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)

    • Linerd says:

      Your virtual host file should refer to *:80 for port 80, not *:0.

      • raj says:

        U have to /etc/apache2/httpd.conf ----------- servername localhost . U won't get the error

        • raj says:

          U need to do this in /etc/apache2/site-available/site1 -------- DocumentRoot which u have create a directory site folder

          • raj says:

            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

Leave a Reply

Your email address will not be published. Required fields are marked *

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>