Automating Drupal 6 installation by a Perl script
How can we efficiently deploy Drupal websites? Installing a Drupal website is rather straightforward. But it involves many small critical steps that always waste my time. One day, I came up to an idea that using a simple Perl script to (semi-)automate the procedures would save both time and accuracy resulting in higher productivity. Also, systematically generating new Drupal websites helps me manage them well later. In this post, I would like to share my approach to setting up new Drupal websites.
1. Overview
A Drupal installation roughly requires the following steps:
- Unpacking a Drupal tarball.
- Creating necessary files and setting permissions.
- Setting up an SQL database and a corresponding user account.
- Configuring Apache.
- On-site Drupal configuration through a browser.
I introduce a Perl script aimed at automating the steps 1,2 and 3.
2. Installing Drupal
2.1. Prerequisite
Drupal is based the LAMP server stack. If it is not set up yet, install related packages by
$ sudo tasksel install lamp-server
2.2. Getting resources
Download the latest Drupal distribution file from http://drupal.org/. As of writing this article, it is drupal-6.16.tar.gz. Move the archive file into /var/www/ (Apache's default root document directory).
yoi@pc:/var/www$ ls -l drupal-6.16.tar.gz -rw-r--r-- 1 yoi yoi 1090616 2010-04-22 13:23 drupal-6.16.tar.gz
2.3. Running the installation script
Download a Perl script drupal_install_helper.pl, put it under /var/www/, and give it an executable permission:
yoi@pc:/var/www$ chmod 775 drupal_install_helper.pl yoi@pc:/var/www$ ls -l drupal_install_helper.pl -rwxrwxr-x 1 yoi yoi 2944 2010-04-22 13:30 drupal_install_helper.pl
Executing the script with no arguments shows the usage of the script:
yoi@pc:/var/www$ ./drupal_install_helper.pl Usage: ./drupal_install_helper.pl <tarball_name> <project_name> <db_passwd> <bool_test>
Actually, the script requires four arguments:
- tarball_name: the path of a Drupal tarball, e.g. drupal-6.16.tar.gz
- project_name: the project name used as the name of your new drupal directory under /var/www/, e.g. myproject
- db_passwd: the password (e.g. 123xyz) of a new SQL database whose name is project_name prefixed by drupal_ (e.g. drupal_myproject)
- bool_test: 1 for a test (print all commands without executing them), 0 for a real installation.
To check what kind of commands will be actually executed, run the script in test mode:
yoi@pc:/var/www$ ./drupal_install_helper.pl drupal-6.16.tar.gz myproject 123xyz 1 **** THIS A TEST MODE (no commands executed) **** sudo tar xvfz drupal-6.16.tar.gz sudo mv drupal-6.16 myproject sudo cp myproject/sites/default/default.settings.php myproject/sites/default/settings.php sudo chown www-data:www-data myproject/sites/default/settings.php sudo mkdir myproject/sites/default/files sudo chown www-data:www-data myproject/sites/default/files sudo mkdir myproject/sites/all/modules sudo mkdir myproject/sites/all/themes mysql -u root -p -e "CREATE DATABASE drupal_myproject;" mysql -u root -p -e "GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER, CREATE TEMPORARY TABLES, LOCK TABLES ON drupal_myproject.* TO 'myproject'@'localhost' IDENTIFIED BY '123xyz'; FLUSH PRIVILEGES;" rm ~/.mysql_history A drupal tree and database initialized: --------------------------------------- drupal_dir_name: myproject drupal_db_name: drupal_myproject drupal_db_user_name: myproject --------------------------------------- Now, please visit the website and follow the in-site configurations. Bye!
Finally, run the script in real installation mode (enter the password of Unix root and MySQL root when asked):
yoi@pc:/var/www$ ./drupal_install_helper.pl drupal-6.16.tar.gz myproject 123xyz 0
At this moment, you have created and configured
- a new Drupal tree /var/www/myproject/
- an SQL database drupal_myproject
- an SQL user myproject with password 123xyz (who has an access to database drupal_myproject)
2.4. Configuring Apache
For the local development of your new Drupal site, it is nice to assign a local port number (e.g. 20001) mapped to the Drupal document root /var/www/myproject/. To do that, edit httpd.conf as
/etc/apache2/httpd.conf
Listen 20001 <VirtualHost _default_:20001> DocumentRoot /var/www/myproject </VirtualHost>
Then, restart Apache as
$ sudo /etc/init.d/apache2 restart
At this moment, you should be able to land on your new Drupal website at http://localhost:20001/.
2.5. Final configuration through a browser
Visit http://localhost:20001/ from a browser. Follow a fresh startup configuration by clicking on "Install Drupal in English".
![]() |
| Figure 1. Landing on a fresh Drupal site. |
Then, enter SQL database information as
- Database name: myproject
- Database username: myproject
- Database password: 123xyz
and proceed.
![]() |
| Figure 2. Setting database information. |
Your installation is successful if you see the following page. From this point, just continue a standard site-configuration procedures!
![]() |
| Figure 3. The site-configuration page. |


