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.

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

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

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

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

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

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

  5. 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/[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?

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

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>