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
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 /home/yourID/webdev/site1/>
		Options Indexes FollowSymLinks MultiViews
		AllowOverride All
		Order allow,deny
		allow from all

	ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
	<Directory "/usr/lib/cgi-bin">
		AllowOverride None
		Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
		Order allow,deny
		Allow from all

	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 ::1/128


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


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

  1. amir says:

    thank for this post.
    very good.

  2. matt h says:

    I have followed this tutorial to a Tee but everytime I enter either site1 or site2 it just loads the default apache page and NOT my Site1 works! index page? Can anyone help me, I'm using ubuntu 12.10 with all the latest updates and upgrades, It is a fresh install and have ONLY installed the lamp stack following the tutorial at the top of the page. please help

    • Linerd says:

      If you're using Firefox as your web browser, try clearing the browser cache. I can't tell you the number of times that Firefox has driven me crazy because of that. Alternatively, try installing Chromium and check your pages.

      It could also be an issue with permissions on your directories. Try

      chmod 775 webdev webdev/site1 webdev/site2
  3. pat says:

    Sorry the line that says this is the only line that needs to changed in the apache2.conf file is

  4. pat says:

    I just clean installed ubuntu 14.04 and got the 404 errors, I had used this on 12.04 for about 2yrs.
    My solution is as follows dont know if it is correct but works fine.

    First change apache2.conf
    sudo nano /etc/apache2/apache2.conf

    AllowOverride None
    Require all granted

    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted

    # Options Indexes FollowSymLinks
    # AllowOverride None
    # Require all granted

    Follow Linerds instructions until sudo cp default site1 at this point make it
    sudo cp 000-default.conf site1.conf

    Next sudo nano site1.conf
    under ServerAdmin add
    ServerName site1

    then change
    DoucumentRoot /home/yourid/webdev/site1
    save changes

    sudo a2ensite site1.conf

    then finish out like Linerd says.

    Linerd thank you for the original tutoral. Like I said this works, if it works after update without having to make the change again I don't know time will tell.

  5. Roger says:

    Thanks a whole lot for this tutorial. I just did a fresh install of ubuntu and this is the first tweaking I've done on it. Worked like a charm! I stumbled around a little at first and couldn't get it to work right, because I wasn't understanding the steps correctly. After a few tries I finally got 'er done.

  6. Olwe says:

    How could this be modified to include access to /home/me/webdev/site1 over a home network? That is, I'd like not just http://site1 to work but work across my home network, i.e., http://192.168.0.x/site1 ?

  7. Olwe says:

    Got it to work with this tip I got off StackOverflow:


    ServerName site1

    ServerAdmin webmaster@localhost
    DocumentRoot /home/memyselfI/webdev/site1

    Options FollowSymLinks
    AllowOverride None

    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
    Require all granted

    . . .

    Don't know how much of this is truly good karma vesus kludge. . . .

    • Embinf says:

      Thanks, I got it to work too, on Linux Mint 16. The config file(s) in /etc/apache2/sites-available/ must end with the .conf extension, e.g. site1.conf. And here is what the contents of the file look like:

      <VirtualHost *:80>
      # The ServerName directive sets the request scheme, hostname and port that
      # the server uses to identify itself. This is used when creating
      # redirection URLs. In the context of virtual hosts, the ServerName
      # specifies what hostname must appear in the request's Host: header to
      # match this virtual host. For the default virtual host (this file) this
      # value is not decisive as it is used as a last resort host regardless.
      # However, you must set it for any further virtual host explicitly.
      ServerName site1

      ServerAdmin webmaster@localhost
      DocumentRoot /home/user/websites/site1/
      <Directory />
      Options FollowSymLinks
      AllowOverride None
      <Directory /home/user/websites/site1/>
      Options Indexes FollowSymLinks MultiViews
      AllowOverride All
      Require all granted

      # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
      # error, crit, alert, emerg.
      # It is also possible to configure the loglevel for particular
      # modules, e.g.
      #LogLevel info ssl:warn

      ErrorLog ${APACHE_LOG_DIR}/error.log
      CustomLog ${APACHE_LOG_DIR}/access.log combined

      # For most configuration files from conf-available/, which are
      # enabled or disabled at a global level, it is possible to
      # include a line for only one particular virtual host. For example the
      # following line enables the CGI configuration for this host only
      # after it has been globally disabled with "a2disconf".
      #Include conf-available/serve-cgi-bin.conf

      # vim: syntax=apache ts=4 sw=4 sts=4 sr noet

  8. Olwe says:

    Your sites-available config file is out of date for Apache 2.4.6 and my hacking around hasn't relieved a very difficult "403 Forbidden You don't have permission to access / on this server" I'm having. Any ideas?

  9. devk says:

    Hello! I also have a file access problem: site1 and site2 are in a /home/myid/webdev. Folders and sub-folders of site1 and site2 are not writable through php. I have chmod'ed folder 'webdev' recursively to 755, so what could be blocking there?

    • devk says:

      Solved! Apparently, some PHP needed to be executed by user www-data, so i chown'ed the respective folder accordingly (adding myId to the www-data group was not good enough).

  10. Embinf says:

    Thanks for the tutorial. I always have a file permission problem after setup and get the message: 403 Forbidden You don't have permission to access / on this server - when trying to access the newly setup websites.
    I solve this by using the file manager to change permissions of the files and folders. So for a website that is on the hard-drive at:
    I have to enable read only access for the files in the site1 folder for both group and others.
    Then for each sub-folder in the line I change folder access permission to Access files for both group and others ( so I do this for site1, websites, )

    My questions are - is there a better way to do this? Or did I miss something, because I do not see any other comments about file access problems? Is it possible to do this by adding yourself to a group so that everyone does not get access to the web development folders?

    • Embinf says:

      Sorry some things did not print properly in my comments above because I used angle brackets.
      /home//websites/site1/ should be /home/user name/websites/site1/
      and then
      ( so I do this for site1, websites, ) should be ( so I do this for site1, websites, user name)

  11. Marsup says:

    Wehre are the apache error logs located, and which file to bee seen for errors? Thanks.

    - Marsup

  12. [...] this to enabling sites in apache2. If you want you can also change port number for security [...]

  13. lenny b says:

    I'm not working locally, I have a server at an ip address and the partition for the site and files is on a separate disk than where the var/www directory is. Both virtual on the same server, but following the steps above, I get the "Index of/" page.

    I'm using Drupal and put but to no avail. (that's not the real IP of course, but the example)

    Is there anything else to reload/restart when doing this?

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>