Drupal development setup on Ubuntu 12.04

I upgraded my local computer to Ubuntu 12.04 today and revisited my Drupal development environment. Here is a simple and fast guide how to configure Ubuntu for development. There are a number of installation documentation pages on this topic on drupal.org, but they suggest to use the packaged Drupal installation and to run Drupal in subfolders (e.g. http://localhost/drupal6). They also do not cover some other important development tools like Phpmyadmin or php.ini settings. During development we want to edit files and we want to do that somewhere in our home directory to avoid any permission problems. There is also Drubuntu, but it is outdated and does way too much magic in my opinion. So here is a more transparent tutorial that highlights the key development configuration settings for Drupal development.

Installation

To install a web server and MySQL and PHP and all the dependencies we simply install the default Ubuntu drupal package:

sudo aptitude install drupal7

The installation will probably prompt you for a few things: Set a password for the MySQL root user. For the Postfix configuration you can select the "Local only" profile, so that no outgoing mail will be sent by PHP from your computer. For the postfix system mail name you can just leave it at your default hostname. The installation will probably prompt you to configure a default drupal database, but we will do that manually afterwards, so skip it.

We don't want to expose any of our sites to the outside world, so we limit Apache to only listen on the localhost interface. Edit /etc/apache2/ports.conf and replace "Listen 80" with "Listen 127.0.0.1:80".

Listen 127.0.0.1:80

We want Apache to run as our own user account to avoid any permission problems or conflicts when running drush or editing config files. Edit /etc/apache2/envvars and change the user to your account name. Example:

export APACHE_RUN_USER=klausi
export APACHE_RUN_GROUP=klausi

Enable the Rewrite mod and restart Apache.

sudo a2enmod rewrite
sudo service apache2 restart

Now we would like to have Phpmyadmin to easily create, inspect and manipulate MySQL databases.

sudo aptitude install phpmyadmin

The installation will probably prompt you for a few things: Confirm that you want to automatically configure a database with dbconfig-common. Select Apache as web server to configure automatically.

Now your Phpmyadmin installation should be reachable at http://localhost/phpmyadmin/ .

We will need some tools for development:

sudo aptitude install git php5-xdebug php5-curl php-apc

php.ini

It is important to tune your /etc/php5/apache2/php.ini for development purposes. I changed the following entries:

memory_limit = 256M
error_reporting = E_ALL | E_STRICT
display_errors = On
display_startup_errors = On
track_errors = On
html_errors = On
session.gc_probability = 1

Folder setup

The next step is the setup of a workspace environment in my home folder where I will develop my Drupal projects. Example for my "drupal-8" development environment:

  • Drupal root: /home/klausi/workspace/drupal-8
  • Apache config: /etc/apache2/sites-available/drupal-8
  • Browser: http://drupal-8.localhost
  • Database: drupal-8

Checkout Drupal 8:

cd ~
mkdir workspace
cd workspace
git clone --recursive --branch 8.x http://git.drupal.org/project/drupal.git drupal-8

Apache config

Create an Apache configuration for your drupal-8 folder at /etc/apache2/site-available/drupal-8:

<VirtualHost *:80>
        ServerAlias drupal-8.localhost
        DocumentRoot /home/klausi/workspace/drupal-8
        <Directory "/home/klausi/workspace/drupal-8">
                Options FollowSymLinks
                AllowOverride All
        </Directory>
</VirtualHost>

Enable the configuration and restart Apache:

sudo a2ensite drupal-8
sudo service apache2 restart

You might wonder why I do not use an Apache wildcard/catchall configuration, here is the reason why: unfortunately the VirtualDocumentRoot directive requires that you modify your .htaccess in your Drupal installation (you have to enable the RewriteBase /). This is bad because then you always have local changes in that file if you are working with a Drupal core git checkout.

localhost DNS

Create the domain entry for drupal-8.localhost in /etc/hosts. Add "127.0.0.1 drupal-8.localhost". You will have to add each new project name in this file (you could also install Bind to automatically resolve this, but seems like an overkill to me).

I wrote a simple bash script that automatically creates the Apache site config and the site's hosts entry for me. Make sure to change the WORKDIR to your directory and run it like this:

sudo deploy-site.sh drupal-8

Database

Create the database in Phpmyadmin: Go to http://localhost/phpmyadmin , then "Privileges", then "Add a new User". Fill out the user name with "drupal-8", host localhost and generate a password (copy that password to use it later during the Drupal install process). Use the "Database for user" section and select "Create database with same name and grant all privileges".

Install Drupal

Now your Drupal installation should be reachable in your browser at http://drupal-8.localhost and you can start the installation. Done!

Development tips

Categories: 
AttachmentSize
deploy-site.sh.txt647 bytes

Comments

Rather than a custom script, I'd highly recommend using virtualhost.sh to create Apache site entries and local host records.

virualhost.sh does a bit more than creating Apache config and writing to /etc/hosts, it also creates stuff in the document root, which I do not want. I would have to remove that folder again after I ran the script to clone my Drupal there. So I prefer to stick with this simpler version.

You can specify where the folder of the site should be located and it doesn't have to create it for you. I do this all the time with an existing drupal7/8 checkout and alias 'another' site for the same root directory.

From the virtualhost.sh wiki page it says that it is for Mac OS X. See https://github.com/pgib/virtualhost.sh/wiki.

This is well and good, but I would advise taking advantage of the Drupal multisite feature to enable working with multiple seperate sites more easily.

The one change to this really is in apache config section.

<VirtualHost *:80>
ServerAlias drupal-8.localhost drupal-8.*.localhost
DocumentRoot /home/tnanek/workspace/drupal-8
<Directory "/home/tnanek/workspace/drupal-8">
Options FollowSymLinks
AllowOverride All
</Directory>
</VirtualHost>

This will make it so whenever using a url of drupal-8.<whatever you want>.localhost it will serve up the Drupal-8 directory. Now, I know in Drupal-7 from experience that you may need to have the full name in there (or drupal-7.example.localhost instead of example as Drupal 6 and Drupal 5 allowed -- haven't read through the default.settings.php yet to confirm this though) for the name of the site folder -- keep this in mind when using your drush site-install commands.

Likewise, I would advise installing Drush and setting up aliases to make development easier.

I have a drush commandfile which makes modifying the hosts file a simple command, see my sandbox, and the set up issue.

Though another thing to consider with multiple drupal installs on a single codebase is database management. I'd advise, for simplicity's sake, that you create one user per version of Drupal you are working with and always use that name as a prefix for the databases tied to that Drupal version. This way, if you created the user correctly, you won't need to add permissions to each new database you create.

Lastly, be sure to choose a good IDE and stick with it -- there are many variations in features of IDEs that can be tricky or impossible to find in others, so once you find one you like stay with it -- or if you prefer vim or emacs, then stick to them -- you will need to not only learn a new interface, but also "unlearn" the patterns that have developed over time... I myself still do a :w (from vim) when I want to save in an IDE at times.

In the text: sudo a2ensite workspace
Is this correct? Isn't it "sudo a2ensite drupal-8"? The command with workspace didn't work to me.

correct, thank you. I fixed it in the article.

I followed the steps (just changed to drupal 7) but I got the message 403 Forbidden when I tried to access the instalation. Did anybody got this error?

Ohh... I am using Ubuntu 12.04

The problem was permissions. The command "chmod a+x /home/username" solved the problem. More details http://ubuntuforums.org/showthread.php?t=797903&page=2

Yes, the webserver must be allowed to look into your home folder of course. But that should be possible with the default Ubuntu umask, maybe you restricted that?

Great writeup! Very similar to what I do.

Unless it has changed in recent versions of tasksel, I think you will find that you want to install php5-gd when you are installing git & xdebug to take advantage of the image tools & php5-mcrypt for phpmyadmin extra security.

Thanks for this great guide! Very good descriptions and easy to follow. Got my local Drupal development environment up and running in to time :)

This outline coupled with a few ideas yanked from robertjtownsend's ubuntu setup got me rolling quickly. Thanks for sharing! The only step left to do manually was the blank mysql database creation.

Certainly the few seconds it added a week called for scripting that also? (Randall's research might disagree xkcd.com/1205)

You can tack the creation of a mysql database with the same name onto the end of this script with these lines:

ROOTUSER="dbusername"
ROOTPASS="dbpassword"

mysql -u $ROOTUSER -h "localhost" -p$ROOTPASS -Bse "CREATE DATABASE $1;"

More complex examples including creating user and such can be found in the answers to this stackoverflow question.

Hi,

thank you for your article. It was really helpful to me but for some reason I'm unable to get it fully working on my Ubuntu 14.04 server. I know it is for Ubuntu 12.04 but aren't these the same thing except they use different software versions?

If so, can you update it to work with Ubuntu 14.04, please? In the mean time I used this article to set-up Drupal on my Ubuntu 14.04 vps.

thanks,