Create a Drupal site on an Ubuntu Server

This will help you quickly install a Drupal site on an Ubuntu server.

This is my script for this purpose. Use it as you will. This procedure assumes that you have already installed Apache, MySQL, phpMyAdmin and PHP. It also assumes that you are familiar with everything. This is probably not a beginner’s guide, but an aid to someone more experienced that wants to install Drupal quickly.

Create and run a shell script to create your site

Use this template to create a shell script that will create your site. Create a text file from the following template called “” (in your home directory is fine). Modify lines 7 to 17 for your installation. Then use “chmod +x” and execute the script with “sudo ./”.

# make drupal project

OWNER="andrew" 			# change to your username
GROUP="staff"			# change to your username or the group that will work on the site
DRUPAL_URL="" # update to the current version, if there is one
SITESROOT="/usr/local/var/www"  # change to the directory where you put website directories
URL=""		# change to your site's address

# database.php parameters
DBUSER="sitename"		# change to something meaningful
DBPWD="passwordgoeshere"		# change to a long password
DBHOST="localhost"		# leave alone
DBDATABASE=$DBUSER		# leave alone
# make sure this is run as root
if [[ $UID -ne 0 ]]; then
    echo "Not running as root"
if [ ! -d $SITESROOT ]; then
	echo "$SITESROOT directory does not exist"
# check for existence of the specified user owner
/bin/egrep  -i "^$OWNER" /etc/passwd > /dev/null
if [ ! $? -eq 0 ]; then
   echo "User $OWNER, which is specified as the owner, does not exist in /etc/passwd"
# check for existence of the specified group
/bin/egrep -i "^$GROUP" /etc/group > /dev/null
if [ ! $? -eq 0 ]; then
   echo "$GROUP, which is specified as the group to use, does not exist in /etc/group"
# change to the siteroot directory
if [ -d $URL ]; then
	echo "$SITESROOT/$URL directory already exists"
mkdir $URL
cd $URL
# get Drupal
echo 'get Drupal'
wget -O drupal.tgz $DRUPAL_URL
tar -xzvf drupal.tgz
find . -maxdepth 1 -type d -name 'drupal*' -exec mv {} public \;
sudo chown -R $OWNER:$GROUP public
find . -type d -exec chmod g+ws {} \;
# temporarily allow write to settings
cp public/sites/default/default.settings.php public/sites/default/settings.php
chmod a+w public/sites/default/settings.php
chmod a+w public/sites/default
# logs
echo "set up logs"
mkdir logs
sudo chown $OWNER:www-data logs
chmod g+ws logs
touch logs/error.log
touch logs/combined.log
# create Apache virtual site
echo creating Apache virtual site file in /etc/apache2/sites-available
echo "
	ServerName $URL
	DocumentRoot $SITESROOT/$URL/public
	DirectoryIndex index.php
	LogLevel warn
	ErrorLog $SITESROOT/$URL/logs/error.log
	CustomLog $SITESROOT/$URL/logs/combined.log combined

" > /etc/apache2/sites-available/$URL
# finish up
echo -e "Drupal website was created in: $SITESROOT/$URL\n"
echo -e "To finish, do the following:\n"
echo "1. Enable site with: sudo a2ensite $URL"
echo "2. Restart Apache with: sudo /etc/init.d/apache2 restart"
echo "3. Make a crontab entry ('sudo crontab -e') like: 0 * * * * wget -O - -q -t 1 http://$URL/cron.php"
echo -e "4. Create a MySQL database with:\n"
echo -e "\t     user: $DBUSER"
echo -e "\t      pwd: $DBPWD"
echo -e "\t     host: $DBHOST"
echo -e "\t database: $DBDATABASE"
echo "5. Open the site in your browser and finish the set-up."
echo "6. Remove write privileges with: sudo chmod og-w public/sites/default/settings.php"
echo "   ..and: sudo chmod og-w public/sites/default"

Create the database and user with phpMyAdmin

  • click the “Privileges” tab and add a new user
  • in the “User name” field, enter the sitename that you entered for DBUSER in the script
  • in the Host field, select “Local”
  • enter or generate a password – make it a long one, like 20 characters
  • in the Database for User list, select “Create database with same name and grant all privileges”
  • click Go

Enable the new Drupal website

This will tell Apache to enable the virtual site that was made in the script and then restart Apache.

sudo a2ensite
sudo /etc/init.d/apache2 restart

Configure the site

Open site in browser and configure it

Remove temporary write privileges

During site creation, write privileges were needed, after site set-up, remove them. In a shell, while in the sites directory (where the public directory is run these commands:

sudo chmod og-w public/sites/default/settings.php
sudo chmod og-w public/sites/default
Be Sociable, Share!