Apache 2 on Ubuntu

Here are two methods. The first edits the main apache2.conf - which is typical if you have dedicated or single website/host. The second uses virtual hosts and is more typical of a shared setup, such as a development environment.

Method 1: apache2.conf

First thing to do is make sure you have apache up and running:
/etc/init.d/apache2 start

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 restart

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 you 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.***
hopefulley this helps some people and saves them the time that I spent trying to get it working.

Method 2: Virtual hosts

This method assumes a setup where you would have one (or more) virtual hosts config files in your sites-available directory.

Typical configuration:

  • Drupal 5.x / 6.x
  • Apache 2.2
  • mysql 5.0
  • debian/ubuntu server install

drupal path: /www/drupal/sites/site1...site2

I have root access to my server.

First, from the linux command line, I enabled the rewrite module for apache like this:

sudo a2enmod rewrite

Changed appropriate hosts file located at:

/etc/apache2/sites-available/

Now, depending on your setup do one of the following:

Option A - Multi-websites Setup

In the sites-available directory, you should see as many files as you have websites enabled. One is named default and the others should reflect the names of your additional sites if you are running virtual hosts. If you have only a single site, it will most likely be named default. You can open these files in an editor such as nano and make changes.

Change this directive in the file(s) found in sites-available:

Change From
AllowOverride None

Change To
AllowOverride All

Save this file and then restart apache.

sudo /etc/init.d/apache2 restart

Reload for good measure... (note: this step seems unnecessary. If someone could verify it is unneeded, please remove)
sudo /etc/init.d/apache2 reload

Option B - 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 restart / reload apache.

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.

It works!

GonZoo - July 11, 2007 - 07:02

Thanks for your help, just a comment:

It's not necesary to allow all sites to use mod_rewrite, just add the following lines in /etc/apache2/sites-available/000-default between labels:

<VirtualHost>
.
.
.
<Directory /var/www/drupal>
        AllowOverride All
</Directory>
.
.
.
</VirtualHost>

and change "drupal" for your drupal installation subdirectory.

And don't forget to restart your apache!!

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.

Thanks for the info. Got

Techinica - July 5, 2008 - 12:46

Thanks for the info. Got clean-urls working on both 5.2 and 6.2 installs by adding the AllowOverride All to my virtual hosts and a2dissite/a2ensite 'ing them.

Just one thing I found however, having AllowOverride All anywhere in the .htaccess file caused a Error 500 - Internal Server Error. Just one thing to look out for.

 
 

Drupal is a registered trademark of Dries Buytaert.