Apache 2 on Ubuntu

Last modified: September 3, 2009 - 21:14

There are two methods for setting up Drupal 5.x/6.x with Apache on Ubuntu. The first (preferred) method edits the 'Virtual Host configuration, which is the default setup on Ubuntu (even for a single-site webserver). The second edits the main apache2.conf, which is typical for an older setup.

Method 1: 'Virtual Host' setup

First, from the linux command line, enable the rewrite module for apache with this command:

sudo a2enmod rewrite

Next, use an editor (such as nano) to edit the appropriate Apache configuration file for your Drupal site in the /etc/apache2/sites-available/ directory. For a single site, the file is /etc/apache2/sites-available/default; if you have multiple sites, the file names should reflect the names of the sites to which they refer. Thus, to edit the default site configuration, use

sudo nano /etc/apache2/sites-available/default

Look for the Directory section referring to the folder where your Drupal site lives (in /etc/apache2/sites-available/default, this is typically <Directory /var/www>), and change the line:

AllowOverride None

to

AllowOverride All

(See https://help.ubuntu.com/community/EnablingUseOfApacheHtaccessFiles for more information).

Save this file and then reload apache.

sudo /etc/init.d/apache2 reload

Sub Domain Setup

An alternative to multiple virtual hosts files is to use subdomains. This allows you to use a wild-card in the Server Alias. This allows for both a simple multi-site Drupal setup, as well as multi Drupal versions. Instead of multiple virtual hosts files, you define a subdomain for each drupal profile.

Consider the following and modify your configuration file to fit your needs.

  1. http://myproject.dr5.example/
  2. http://myproject.dr6.example/
  3. http://myproject2.dr6.example/

Here is an example of a partial listing of a virtual host configuration file that would support the last two lines in the above example. Note this is not intended to be a COMPLETE configuration file, but rather provide guidance for your development setup.

<VirtualHost *>
DocumentRoot "/www/Dr6"
ServerName example
ServerAlias *.dr6.example

<Directory "/www/Dr6">
AllowOverride All
</Directory>

Edit & save your config file to suit your development needs. Assuming the site is already enabled, then reload Apache.

Method 2: apache2.conf

Then you have to enabled the rewrite module(mod_rewrite). You no longer have to do the:
LoadModule rewrite_module modules/mod_rewrite.so
AddModule mod_rewrite.c

It's now as easy as:
sudo a2enmod rewrite

To disable this module it's just:
sudo a2dismod rewrite

with Apache version 2, the httpd.conf has been deprecated and the new file is located at:
/etc/apache2/apache2.conf

in this file you need to add your directory and the allow override to give access to your drupal site.
so look for a section in your apache2.conf that has Directory tags and just add another section:

<Directory /var/www/drupal_website_install>
    AllowOverride all
</Directory>

*keep in mind that my website is in a subdirectory (drupal_website_install) and so you may need to edit the above to reflect this. By this I mean if i go into my webbrowser I need to go to http://localhost/drupal_website_install/

After you edit you apache2.conf as listed above you need to restart the server by:
/etc/init.d/apache2 reload

Problems with Rewrite

If you are having problems with getting your rewrite to work you can always use logging. To do that add this to the end of /etc/apache2/apache2.conf:

RewriteLog "/var/log/apache2/rewrite.log"
RewriteLogLevel 3

Level 0 is no logging
Level 9 is log everything
You can pick the level to determine the amount of output you need.
***Security Warning: Make sure to take the log code out, disable it, or put the log file in a directory that can't be read by normal users (as shown above) otherwise it can result in a security breach.***
hopefully this helps some people and saves them the time that I spent trying to get it working.

Finish - Enable Clean URLs

Now browse to your site, login, click administer, find "Clean URLs" and browse to that page, run the test for "Clean URLs" (In Drupal 4.6 - 5.x this is buried in the paragraph explaining "clean Urls").

If your setup is like mine, you should now have Clean URLs in drupal.

solution for ubuntu gutsy

suoko - February 18, 2008 - 08:50

Here is what I did to have clean urls with ubuntu gutsy:
a2enmod rewrite

and then added the following lines before line:
<Directory /var/www/>
in '/etc/apache2/sites-available/default':

<Directory /var/www/drupal>
AllowOverride all
RewriteEngine on
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.+\..+)$ [NC]
RewriteCond %{HTTP_HOST} !^.+\..+\..+$ [NC]
RewriteRule ^(.*)$ http://www.%1/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^(.*)$ [NC]
RewriteRule ^files/(.*)$ sites/%1/files/$1 [L]
RewriteRule ^files/(.*)$ sites/%1/files/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
</IfModule>
</Directory>

ADVICE: I'm having problems with apache dir mod using this configuration since now index.php has priority over index.html.
Hope to solve it soon

rewrite.conf file did not exist

abolinder - February 26, 2008 - 22:41

In trying to puzzle out why this wasn't working for me on Ubuntu Server 7.10, I discovered that the file /etc/apache2/modules-available/rewrite.load was there but the file /etc/apache2/modules-available/rewrite.conf did not exist. I could use the command 'sudo a2enmod rewrite' and the rewrite mod would 'look' enabled, but clean-urls would still not function because there was no .conf file for the rewrite mod.

I created one with nano, added the text...

RewriteEngine On

...to it, and saved it to /etc/apache2/modules-available/rewrite.conf.

Then, I went to /etc/apache2/sites/sites-available/default and changed both the 'Directory /' and 'Directory /var/www/' AllowOverride values from 'None' to 'All', used a2enmod to enable the rewrite mod, restarted Apache2, and I was then able to turn on Clean URLs in my Drupal install.

Cannot get it to enable in Ubuntu 8.10

Miggle - April 21, 2009 - 17:08

I'm running Ubuntu 8.10 on Virtual box and use Drupal 6.10. I followed the directions from all posts but I couldn't enable clear URLs.

1) Ran sudo a2enmod rewrite. Module now found in mods-enabled directory. Also, the mod shows when I run apache2ctl -M

2) Updated .../sites-enabled/000-default

  <VirtualHost *:80>
        ServerAdmin webmaster@localhost
        ServerName ubuntusrv
        ServerAlias ubuntusrv

        DocumentRoot /var/www/
        <Directory />
                Options FollowSymLinks
                AllowOverride All
        </Directory>
        <Directory /var/www/>
                RewriteEngine on
                RewriteBase /
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                Order allow,deny
                allow from all
        </Directory>

3) Created rewrite.conf file in ../mods-available as stated in previous comment.

I'm out of ideas on how to get clean url to enable. The radio button is still greyed out.

Assistance will be appreciated :) Thanks

UPDATE:
Err, got it to work finally. Modified as follows:

        <Directory /var/www/>
        <IfModule mod_rewrite.c>
                RewriteEngine on
                RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
        </IfModule>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                Order allow,deny
                allow from all
        </Directory>

Your fix worked for me too,

ericinwisconsin - May 10, 2009 - 06:03

Your fix worked for me too, Miggle. I'm running Ubuntu Server 9.0.4.

Thanks!

Miggle, thank you, I was

wplate - July 20, 2009 - 22:37

Miggle, thank you, I was looking everywhere for how to get Clean URLs to work in Ubuntu 9.0.4, finally adding your suggested working text to apache2.conf did the trick.

-- Wes Plate

PHP

aangel - September 4, 2009 - 15:25

I inherited an Ubuntu box with PHP installed but missing the rewrite.conf (as noted above) and also the line:

AddType application/x-httpd-php .php3 .php

which I added to httpd.conf. Without it, apache wouldn't interpret php files as php files.

Andre'

Ubuntu 8.04 - Apache2

boonjackmedia - November 9, 2009 - 04:42

Method #2 works perfectly!

Here's a little visual helper from my /etc/apache2/apache2.conf file, notice the added line:
snippet start...
# The internationalized error documents require mod_alias, mod_include
# and mod_negotiation. To activate them, uncomment the following 30 lines.

# Alias /error/ "/usr/share/apache2/error/"
#
#
# AllowOverride None
# Options IncludesNoExec
# AddOutputFilter Includes html
# AddHandler type-map var
# Order allow,deny
# Allow from all
# LanguagePriority en cs de es fr it nl sv pt-br ro
# ForceLanguagePriority Prefer Fallback
#

<Directory /var/www/drupal_website_install> 
    AllowOverride all                 
</Directory>

#
# ErrorDocument 400 /error/HTTP_BAD_REQUEST.html.var
# ErrorDocument 401 /error/HTTP_UNAUTHORIZED.html.var
# ErrorDocument 403 /error/HTTP_FORBIDDEN.html.var
# ErrorDocument 404 /error/HTTP_NOT_FOUND.html.var
# ErrorDocument 405 /error/HTTP_METHOD_NOT_ALLOWED.html.var
# ErrorDocument 408 /error/HTTP_REQUEST_TIME_OUT.html.var
# ErrorDocument 410 /error/HTTP_GONE.html.var
# ErrorDocument 411 /error/HTTP_LENGTH_REQUIRED.html.var
# ErrorDocument 412 /error/HTTP_PRECONDITION_FAILED.html.var
# ErrorDocument 413 /error/HTTP_REQUEST_ENTITY_TOO_LARGE.html.var
# ErrorDocument 414 /error/HTTP_REQUEST_URI_TOO_LARGE.html.var
# ErrorDocument 415 /error/HTTP_UNSUPPORTED_MEDIA_TYPE.html.var
# ErrorDocument 500 /error/HTTP_INTERNAL_SERVER_ERROR.html.var
# ErrorDocument 501 /error/HTTP_NOT_IMPLEMENTED.html.var
# ErrorDocument 502 /error/HTTP_BAD_GATEWAY.html.var
# ErrorDocument 503 /error/HTTP_SERVICE_UNAVAILABLE.html.var
# ErrorDocument 506 /error/HTTP_VARIANT_ALSO_VARIES.html.var

# Include of directories ignores editors' and dpkg's backup files,
# see README.Debian for details.

# Include generic snippets of statements
Include /etc/apache2/conf.d/

# Include the virtual host configurations:
Include /etc/apache2/sites-enabled/
...snippet end.

Mike Sayre
BoonJack Media

 
 

Drupal is a registered trademark of Dries Buytaert.