## Index → Cookbook

## Cookbook: Setting up Drupal

Setting up Drupal with Cherokee is really easy. We will assume that our `Document Root` directory is `/var/www/drupal` of the server we are working on. This recipe uses Drupal 6.6, which is the latest release at the time of writing.

You will need PHP support correctly configured in Cherokee, and PHP with the MySQL module installed. The default configuration already provides a valid PHP configuration for Cherokee if you have php-cgi installed, but you can follow the appropriate recipe about setting up PHP in case you don't have it available for some reason.

Under these conditions, you could start Drupal's installation and you would already be able to have your site up and running.

However, we can add several refinements to Cherokee's settings. Mainly:

1. Forward all requests for www.example.net (or whatever domain is resolved to our machine) to example.net

2. Set up an appropriate url rewriting configuration for Drupal.

3. Serve directly the static content speeding avoiding the dynamic-processing bottle-neck.

4. Use the regex from Drupal's .htaccess for denying access to certain paths.

With this we should be able to do everything Drupal's supposed to do, and it should work with Imagecache's dynamic thumbnail generation.

### Setting up Cherokee

#### Default virtual server

We'll begin by cloning the default virtual server, just to keep the default PHP configuration. Create a clone named `example.net`.

Then, we'll delete every erasable rule in the default virtual server since we are going to use it to redirect every petition not matched by the example.net virtual server. We will set the remaining one to be managed by the `Redirection` handler, like this:

Type | Regular Expression | Redirection
--- | --- | ---
External | (.*)$ | http://example.net/$1

After that, this is how the list of rules for this server should look like.

This clears the first milestone. The remaining three will be accomplished by tweaking the `example.net` virtual server.

#### example.net

##### First step

Remember to set up the `Document root` to `/var/www/drupal`.

Delete all the rules except `php` and `Default`.

As previously, we will manage the `Default` rule with the redirection handler.

Type | Regular Expression | Redirection
--- | --- | ---
Internal | ^(.*)$ | /index.php?q=$1

##### Second step

Remember to set up Drupal as custom error handler for the virtual server. Do so in the `Error Handler` tab, selecting the `Custom redirections` option and sending 404 errors to Drupal.

Error | URL
--- | ---
404 Not Found | /index.php

##### Third step

Next, we need to address the clean URLs matter. To do so, create another redirection rule.

Type | Regular Expression | Redirection
--- | --- | ---
Internal | ^/(.)\?(.)$ | /index.php?q=$1&$2

##### Fourth step

After this we will go straight to another milestone: directly serving static files, which is an easy task to accomplish.

Just set up an `File Exists`-type rule. Check the `Match any` checkbox, and manage it with the `Static file` handler. Remember to activate the `IO Cache` option and to specify whatever expiration period you see fit for these files. If you ever edit the configuration just remember that this rule should be located after the PHP rule. Otherwise you will end up statically serving them instead of processing them via PHP. In fact it is a good idea to keep your rules for dynamic contents in a high position on your list of rules.

##### Fifth step

Now to block bad paths, as specified by the htaccess file provided with Drupal. For this we will use an internal `Regular expression`-type rule matching the following expression:

    (\.(engine|inc|info|install|module|profile|po|sh|.*sql|theme|tpl(\.php)?|xtmpl)|^(code-style\.pl|Entries.*|Repository|Root|Tag|Template))(\?.*)?$

Handle this with the `HTTP error` handler:

Error
403 Forbidden

##### Sixth step

The last thing to do on Cherokee's side is to specify a sixth rule that fixes an issue with how requests for the root are managed having the `File exists` handler in place. Simply define yet another redirection rule for this regular expression:

Type | Regular Expression | Redirection
--- | --- | ---
Internal | ^/$ | /index.php

All done. After this you should have six rules in your list. This configuration does work. Reorder your rules accordingly if you seem to have any trouble.

Figure: List of rules

Now, to install Drupal!

### Setting up Drupal

First download and uncompress the distributed Drupal release into `/var/www/drupal`, and create a database suitable for the installation.

Log in to MySQL:

    mysql -u root -p

And create the database for Drupal. We will be using the name drupal, the user drupaluser and the password drupalpassword, but you should set up your own.

    CREATE DATABASE drupal;
    GRANT ALL PRIVILEGES ON drupal.* TO drupaluser@localhost IDENTIFIED BY 'drupalpassword';
    GRANT ALL PRIVILEGES ON drupal.* TO drupaluser@localhost.localdomain IDENTIFIED BY 'drupalpassword';
    FLUSH PRIVILEGES;
    quit;

Then point your web browser to `http://localhost` and follow the instructions provided by the installer.

You will need to copy the config file and change the permissions manually to proceed:

    cd /var/www/drupal/sites/default
    cp default.settings.php settings.php
    chmod 644 settings.php

And the installation will be almost automatic. Just fill up the requested values and you will obtain the following results once your are through.