I wrote this post as a quick reminder about how to install Drupal 7 on a fresh install of Ubuntu Server. For the purpose of this article I’m doing the installation on Ubuntu Linux, Server edition, version 12.04. I am assuming a default server installation of Ubuntu 12.04 with no extra software installed (e.g. do not install LAMP option).
Step #1 – install apache2, mysql-server, php5, php5-mysql and php5-gd
sudo apt-get install apache2 mysql-server php5 php5-mysql php5-gd
This first step installs the Apache2 web server, the mysql database server, the PHP version 5 programming language support, PHP support for the MySQL database and a graphics library of functions for PHP (php5-gd). During the install you’ll be asked for a password for the mysql root user. Use a secure password that you don’t use on other sites or databases. This is the database account with the power to create other database users and modify any part of any database, so it’s almost as important as the root user password on a Linux system. And much like we do on a Linux system we’ll create a non-administrator database account to use our databases. (in another step).
Step #2 – enable mod_rewrite so we can use clean URLs.
sudo a2enmod rewrite
This command is one I either forget or bungle (thinking it a2enmod mod_rewrite, it’s just rewrite). I’m including it here before we install Drupal because we want to make sure it’s done before we enable clean URLs on the web site.
Step #3 – download drupal, unpack it and movie it to the proper directory.
As the normal user on the server system download drupal (in this case 7.14) and unpack it:
tar -zxvf drupal-7.14.tar.gz
Next we need to move the important files to /var/www :
sudo mv drupal-7.14/* drupal-7.14/.htaccess /var/www
Note that there is a space between the * and the next drupal-7.14/.htaccess and the /var/www. We need sudo since we’re moving it to a protected area of the system. Of course if you’re using a different version of drupal you’ll have to substitute the version numbers (e.g. 7.14).
Step #4 – make the Drupal settings directory writable and copy the sample configuration file so Drupal can overwrite it.
Drupal has a nice script that will create the necessary database tables for us. In order to properly use that script we have to copy a sample configuration file to the proper directory and allow Drupal to write to that directory.
sudo cp ./sites/default/default.settings.php ./sites/default/settings.php
sudo mkdir ./sites/default/files
sudo chmod o+w ./sites/default/files
sudo chmod o+w ./sites/default/settings.php
Step #5 – create the database, create a database user to access the Drupal database and assign the database user appropriate permissions.
Now is the time to create a database. The database is used by Drupal during the web install.
mysql -u root -p
This first command launches the authentication for the root mysql user to access mysql. Enter the mysql root user password (note: not the Linux root password, the password we created when we first installed mysql). Once in we’ll see a mysql > prompt. At this prompt type:
create database drupal7;
drupal7 is the name we’re using for our database. I like to use a database name that reflects the purpose of the database. For example: repairdb. (repair database). The semicolon at the end is important to get mysql to understand that this is the end of the instruction.
Now we need to create a database user and assign that user proper permissions to access the database. There was a time I used to just give the user total permission on the database. From a security standpoint it’s a bad idea and drupal actually includes the proper permissions you need to set in the INSTALL.mysql.txt file, so they’re easy enough to copy (no need to remember the exact permissions. Thank you drupal docs team!):
grant SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER on drupal7.* to ‘localuser’@'localhost’ identified by ‘gr3atl0ngpassw0rd’;
This one mysql command has multiple parts to it. First, the permissions: SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER.The permissions are acting on the drupal7 database we created earlier, but we need to assign those permissions to a user. In this case a user named localuser. The ticks (normally the same key as ” on most US keyboards) are necessary, leave them off or put them in the wrong spot and it won’t work. That localuser is on the localhost machine. As with the user ‘localhost’ gets put in ticks. We also have to assign the user a password to access the database. When we do the web portion of the drupal 7 install we’ll need this password. It too gets put inside ticks ‘gr3atl0ngpassw0rd’. We need to remember this password (and the user name) for later. Don’t forget the semicolon to complete the mysql command. Note: you do not have to have a corresponding Linux user account for the mysql username.
Now let’s quite out of mysql:
Step #6 – Take care of the “apache2: Could not reliably determine the server’s fully qualified domain name, using 127.0.1.1 for ServerName” message:
Even if we’ve assigned our computer a name in /etc/hostname we will still get this message. To fix the problem we need to tell apache2 the name. This is done by adding the ServerName directive to the /etc/apache2/httpd.conf file. I like to use vi, but many people I know use gedit. sudo will need to be used since this file is in a protected area of the system:
sudo vi /etc/apache2/httpd.conf
Next we enter the ServerName directive along with our server/computer name:
Note that there is no semicolon (we’re not in mysql anymore). Save and restart apache2:
sudo service apache2 restart
Now we get a proper message when apache2 starts:
* Restarting web server apache2 … waiting [ OK ]
Step #7: Install Drupal 7 – the fun part!
I have a dirty little admission to make – one of the steps I normally do right after installing Ubuntu server is to install a SSH server: sudo apt-get install ssh. Then I ssh into the computer using my laptop (or another computer) and run the above commands. I don’t like installing a desktop installation of Ubuntu on any servers, but I like using a GUI. To get the best of both worlds I ssh in from another box.
In some cases (at work for instance) when I set up a server I can’t always have a static I.P. address. I use ifconfig to find out the current dynamic address of the server I’m ssh’d into:
When using Firefox to set up Drupal for the first time I use the hostname rather than the I.P. address. The problem with configuring Drupal to use the I.P. address is if you’re on a network where you can only have DHCP addresses your server I.P. address will change at some point (and your server will break). So when setting up Drupal be sure to use the http://rather than I.P. address.
If you see the screen above, don’t panic (Hitchhiker’s Guide to the Galaxy)… this is the default Ubuntu apache2 web page. At this point you have 2 options: delete /var/www/index.html or just append the URL with index.php. We should delete the index.html file to avoid further problems:
sudo rm /var/www/index.html
When we visit the site next we should see a screen that looks like the following:
On this screen we have two options: Standard or Minimal installation. I typically choose Standard installation. The screen that follows is the language choice screen. The default install selected is English:
If we’ve done everything correctly up to this step the next screen will ask us to enter details about the database and username and password we’ll use to connect to that database. If something is missing, php5-gd for example, Drupal instructs us to fix the problem. In this case we successfully completed all the previous steps and can move on to the database information entry screen. Note that this is the user we set up earlier to connect to a particular database in mysql. As with all Linux/UNIX, don’t use root.
If you enter a wrong detail here Drupal will inform you it cannot connect to the database. Note that you do NOT need to enter ‘username’@'localhost’ here, just the username. No ticks necessary.
Once you’ve entered the details about the database Drupal will start to insert database tables. A progress bar displays Drupal’s progress.
With this step complete Drupal now asks for information about your site. Note that once this step completes Drupal instructs us to remove the write permission on the settings.php file (so no one else can change/connect to your site). Before jumping to the next screen you should change the permission on the settings.php file:
sudo chmod ugo-w ./sites/default/settings.php
With this done it’s time to move on to the next screen. The next screen is a general site information setup screen. Enter your pertinent details here.
At this point we’re done with the web side of our quick Drupal installation. Drupal rewards us with one last screen before we get to the web site index page:
Click on the link Drupal displays to visit your new basic Drupal installation or use http://<hostname> (where you substitute your host name for <hostname>) to display your new Drupal 7 site.
One last step I like to do on my installations, set the user and group for all of the data to www-data. You can do this by changing to /var directory and running chown recursively on the directory:
sudo chown -R www-data.www-data www/
This last step gives the www-data user and group permission to access the www folder, all files, and sub-folders. Why www-data you might ask? Doing a ps aux | grep www-data reveals the answer:
www-data 31244 0.0 1.4 45548 14864 ? S 09:45 0:00 /usr/sbin/apache2 -k start
www-data 31245 0.0 0.4 38192 5048 ? S 09:45 0:00 /usr/sbin/apache2 -k start
www-data 31248 0.0 1.8 48600 18480 ? S 09:45 0:02 /usr/sbin/apache2 -k start
This list is edited for brevity, but I just wanted to show that apache2 is running as the www-data user.
From here the vast world of the Drupal content management system awaits you! There is a lot of good Drupal 7 information available on Youtube, but a word of warning to make sure you’re following Drupal 7 information because there are significant differences between Drupal 6 and 7.
All the best with your Drupal site install and please feel free to comment about your Drupal experiences here.