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

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

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

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

  3. Olwe says:

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

    site1.conf:

    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>
      <Directory /home/user/websites/site1/>
      Options Indexes FollowSymLinks MultiViews
      AllowOverride All
      Require all granted
      </Directory>

      # 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
      </VirtualHost>

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

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

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

  6. 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:
    /home//websites/site1/
    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)

  7. Marsup says:

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

    - Marsup

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

  9. 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 1.1.1.1/drupal 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>