How to setup a Drupal LAMP development environment on Linux (Ubuntu)

Here are my notes for setting up a brand new Linux/Ubuntu server for Drupal development. If you are cool with the command line, then you might find this handy.

1. Install Apache/MySQL/PHP via tasksel

Reference: https://help.ubuntu.com/community/ApacheMySQLPHP

> sudo apt-get update
> sudo apt-get install tasksel
> sudo tasksel install lamp-server

You will be asked to provide a mysql root password.

Test installation worked:

> apache2 -v
> mysql --version
> php -v



2. Install gd

Reference: http://www.cyberciti.biz/faq/ubuntu-linux-install-or-add-php-gd-support-...

> sudo apt-get install php5-gd
> sudo service apache2 restart

Test installation worked:

> php5 -m | grep -i gd



3. Install mail server via tasksel

Reference: http://askubuntu.com/questions/54960/how-do-i-set-up-an-email-server

> sudo tasksel install mail-server

You will be required to provide the main/default domain for your server, e.g. example.org

Test installation worked:

> echo "This is a test." | mail -s Testing someone@somedomain.com



4. Enable Apache mod_rewrite for clean URLs

Reference: https://www.drupal.org/node/1572984

> sudo a2enmod rewrite

This will create a link from /etc/apache2/sites-enabled/rewrite.load to /etc/apache2/sites-available/rewrite.load.

> cd /etc/apache2/sites-available
> sudo cp default default.orig
> sudo vi default

Search for 'AllowOverride None' and change 'None' to 'All' and save.

Repeat for default-ssl.

> sudo service apache2 restart

This will be tested once Drupal is installed.


5. Install Drush

Reference: https://www.drupal.org/project/drush

> sudo apt-get install php-pear
> sudo pear channel-discover pear.drush.org
> sudo pear install drush/drush

Test drush:

> drush

If you get a message that Drush needs to download a library, do:

> sudo drush
> sudo chown -R [youruser]:[youruser] ~/.drush/cache

or download/install library yourself

Test again:

> drush



6. (optional) Install phpmyadmin

Reference: http://www.allaboutlinux.eu/how-to-install-drupal-7-on-ubuntu/3/

I don't use this, but you can install with:

> sudo apt-get install phpmyadmin



7. Install Drupal via drush

Reference: http://drush.ws/help/5#site-install

Make it so admin users can create directories in /var/www:

> cd /var
> sudo chgrp [admin-group] www
> sudo chmod 775 www

Install Drupal:

> cd www
> drush dl drupal
> mv [drupaldir] [sitedir] (e.g. mv drupal-7.14 examplesite)
> cd [sitedir]
> drush site-install standard --db-url='mysql://root:pass@localhost:port/dbname' --site-mail=[site-email] --site-name=[site-name] --account-mail=[account-email] --account-name=[account-name] --account-pass=[account-password]



8. (Alternative to drush Drupal install) Install Drupal manually

Reference: https://www.drupal.org/documentation/install/download

  1. Grab Drupal zip file from https://www.drupal.org/project/drupal.
  2. Unzip and rename/move directory to /var/www/[sitename]
  3. Create database in mysql, e.g.

    > mysql -u [user] -p[password]
    > create database [database-name];
    > exit;



  4. Copy /var/www/[sitename]/default.settings.php to /var/www/[sitename]/settings.php and make writable with:

    > cd /var/www/[sitename]/sites/default
    > cp default.settings.php settings.php
    > chmod a+w settings.php



  5. Create files directory with:

    > cd /var/www/[sitename]/sites/default
    > sudo mkdir files
    > sudo chown [admin-user]:www-data files
    > sudo chmod 775 files

    I used to use 'chown www-data:www-data files' but the ownership shown above for files works best with drush: https://www.drupal.org/node/759970



  6. From browser, go to: http://[yourdomain]/[sitename]/install.php
  7. Run through installation process



9. Check out your new site!

Go to http://[yourdomain]/[sitedir] in a browser to see your new Drupal site. Login using the account name and account password used above.

To see if clean URLs are working, go to http://[yourdomain]/[sitedir]/user or any other page that should be accessible.


10. (optional) Install git

Reference: http://docs.oseems.com/operatingsystem/ubuntu/install-git-client

Since I use git for development, I'll add that too:

> sudo apt-get install git-core

Check installation worked:

> sudo git --version

I use git on the command line but there are a number of GUI-based git clients including gitk, giggle, git-cola, git-gui, and gitg.

When setting up git repositories, use:


git config --global branch.autosetuprebase always

from Randy Fay's article on automatic git rebasing.



11. (optional) Install debugging, testing, and performance tools of your choice

Here are some from: https://klau.si/dev


> sudo apt-get install php5-xdebug php5-curl php-apc

Add a comment with your favorite debugging and testing tools...



12. (optional) Create public ssh key

I use a public ssh key so that I can login to other machines easily and access git repositories that require keys.

> ssh-keygen -t rsa

Use a passphrase if desired.

It will create a file .ssh/id_rsa.pub. Copy the .ssh/id_rsa.pub contents to the .ssh/authorized_keys file of the machine you want to connect to or to any repository host services.

Also copy any .ssh/id_rsa.pub contents from other machines to this new server's .ssh/authorized_keys so that connecting will be easy.


13. (optional) Copy bash files from other machine

I have a lot of bash settings and aliases configured that I like to use on all my development machines.

Copy .bash_profile, .bashrc, .bash_logout, and .bash_aliases from an older machine to the new one.

It is easy to get confused which machine you are on, so I usually update the .bash_profile file so that the prompt starts with a custom string to indicate this, e.g.

PS1="[mydev:\u:\W]\$ "

This will show the user for the \u and the leaf directory for the \W.


14. (optional) Copy vim settings from other machine

I'm using vim for editing and need to copy the .vimrc file and follow the directions to install the Drupal vimrc project .vim files:


> cd
> wget https://ftp.drupal.org/files/projects/vimrc-7.x-1.x-dev.tar.gz
> cd .vim
> tar xzvf ~/vimrc-7.x-1.x-dev.tar.gz --strip-components 1 --exclude=README.txt

To use ctags in vim:

> sudo apt-get install exuberant-ctags

Check installation worked:

> ctags --version



15. (optional) Copy existing Drupal site to new server

Often I need to pull over one or more Drupal sites to my new machine like the following. I have scripts that do many of these steps, but this is a manual way to do it:

On old machine:

  1. Create a dump of site database:

    > cd [path-to-drupal-site]
    > drush cc all
    > mysqldump -u[user] -p[password] [database-name] > mydbdump.mysql
    > gzip mysqldump.mysql
    > mv mysqldump.mysql.gz ~
  2. Create a tar ball of site files:

    > cd
    > tar czvf myfiles.tgz [path-to-drupal-site]/sites/default/files
  3. Copy database .gz file and files .tgz file to new server

On new machine:

  1. Set up code:

    > cd /var/www
    > git clone [user]@[domain]:[project] [sitedir]
  2. Set up database:

    > cd
    > mysql -u [user] -p[password]
    > create database [database-name];
    > exit;
    > gunzip mydbdump.mysql.gz
    > mysql -u [user] -p[password] [database-name] < mydbdump.mysql
    > mysql -u [user] -p[password] [database-name]
    > show tables;
    > exit;
  3. Set up files:

    > cd /var/www/[sitedir]/sites/default
    > mv ~/myfiles.tgz .
    > tar xzvf myfiles.tgz
    > sudo chown -R [admin-user]:www-data files
    > sudo chmod 775 files
    > sudo chmod -R g+w files
  4. Set up settings.php:

    > cd /var/www/[sitedir]/sites/default
    > cp default.settings.php settings.php
    > vi settings.php
    [edit mysql user, password, and database]
  5. Clear cache and try it:

    > drush cc all

    Go to http://[yourdomain]/[sitename]



16. (optional) php.ini settings

Reference: https://klau.si/dev

If this is for a development-only machine, the php.ini file can be adjusted to help with debugging, etc. Don't make these changes for non-development servers.


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

17. (optional) Update settings.php with development settings

If you don't want to mess with your global php.ini settings (#16 above), then you can set these php settings within each Drupal site's settings.php file. You can also set variables such as turning off caching, e.g.


// display all errors and set high memory!
error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
ini_set('track_errors', TRUE);
ini_set('html_errors', TRUE);
ini_set('memory_limit', '256M');

$conf['securepages_enable'] = 0; // don't use secure pages
$conf['preprocess_js'] = 0; // don't zip js files
$conf['preprocess_css'] = 0; // don't zip css files
$conf['cache'] = 0; // no page caching
$conf['block_cache'] = 0; // no block caching
$conf['googleanalytics_account'] = "UA-XXXXXXX-X"; // don't track in GA

18. (optional) Create additional users

If you need more user accounts on your development server, first create the account and set the password:


> sudo useradd -d /home/[username] -m [username]
> sudo passwd [username]

Then add the user to the desired groups and verify the groups:


> sudo usermod -a -G [group] [username]
> id [username]


If anyone knows how to do any of these steps in an easier or different way or there are steps that you think are missing, feel free to leave a comment.

Comments

Drush cache

Great tutorial but I think some of it has now changed. On CrunchBang (Debian) in any case but I think Ubuntu also.

Instead of changing the ownership of the Drush cache, you now need to create it with "drush cache-clear drush" and no need to become root.

Thanks

Thanks for the tip :)

404 Not Found

Hi. Thanks for all the great advice! However, following the instructions above, I installed a Drupal instance via drush. Then, when I go to localhost/[mysitename], I get the standard Drupal post-install home page, which is good. But...when I try to go anywhere else on the site, localhost/[mysitename]/user for example, I get a "The requested url....was not found on this server" error. When this happens on my shared hosting account, I know to go to .htaccess and uncomment RewriteBase. However, this isn't fixing the problem on my local instance. Any advice? Thx in advance!

Clean URLs?

I get this issue when mod_rewrite isn't installed/enabled on Apache. Try going to:

http://localhost/[sitename]?q=user

If that works, then clean URLs / mod_rewrite isn't set up. You can also check your apache logs at something like:


> tail /var/log/apache2/error.log

to see what error you get when you go to other URLs.

Good luck!
Kristen

Great post!

It will be fantastic if someone shares a post ofhow to install some ide and some debugging tools.

Drush

$ sudo apt-get install drush

That'll give you Drush 4.5. It's also possible to check it out via git and make an alias to it as well if you need 5.

drush 5.4

I installed with:


pear install drush/drush

which is the method listed on the project page and it installed drush 5.4. You can install older versions if desired like:


pear install drush/drush-4.6.0

What about git and gitolite,

What about git and gitolite, xdebug, apc dvel-php, phpunit, selenium?

Yep!

The steps above will just do the basics. I use git but I haven't set up the other tools you mention. Most people have their favorite testing/IDE tools, so I'll leave those out, but perhaps I should add git in the list since that's what I use and is very common in Drupal-land.

If you have any tutorials for installing any of those tools that you'd like to point to, feel free to add another comment.

Xdebug and apc are SOP.

Xdebug and apc are SOP. Especially APC.

Also, https://www.drupal.org/project/drush_extras has a nice little utility to facilitate generating and pushing the keys.

Generating dummy content quickly

And, to generate some quick dummy content, check out Jacob Singh's post on using devel generate via drush.

I'm a Speaker at DrupalCon Portland