Thursday, 23 May 2013

Local Drupal7 on Mac OSX Lion

Having set up a website on a live server using Drupal I wanted to set up a local copy for development without risking stuffing up the live version. This post describes how I set up Drupal 7 on my Mac OSX Lion/10.7.

Why?

I wanted to work on the site offline
I wanted to test code safely without risking the production site
Can use better text editors such as Sublime

Assumptions?

I am using Mac OSX Lion aka 10.7
Drupal 7.22
I will also be focussing on the Zen Drupal theme (version 7.x-5.1) although this may not be relevant to what I'm doing here.

How?

The steps to create a functional local version of a Drupal based website are:
Part 1 (you're here!)
Have an AMP stack (Apache, MySQL and PHP)
Create the Drupal database
Download and configure Drupal 7.22

In Part 2 I set up a multi-site drupal installation - so that my web server doesn't just use the default drupal website.

In Part 3 I create a local copy of a live site from a server.
Make new local copy of my server site
Decide workflow for dev (local) and live (server) sites.
Synchronise live to dev, change dev, synchronise dev to live

Out of the box php is not enabled. This is quickly rectified by editing the apache config file in Terminal. This requires root user access to your Mac rather than the default admin user - see instructions for turning root on from Apple. Having done that change the apache config file as follows:

sudo nano /etc/apache2/httpd.conf

ctrl-w opens the Find box so type php and press enter

Uncomment the LoadModule php5_module line by removing the #
ctrl-o and enter to save and ctrl-x to exit

Install MySQL

MySQL is an open source application available for all platforms. You can download the latest version of MySQL for your platform in a number of formats (including source code!) The easiest way for Mac is to download Mac OS X ver. 10.7 (x86, 32-bit), DMG Archive. NB This download is around 150MB. When you click on download you will be asked to login/sign up. You don't have to do this as there is a 'No Thanks' link at the bottom of that page. Clicking the downloaded .dmg file opens a window containing the packages to install by double clicking them and following the wizard:

Once installed open up System Preferences and then the MySQL Preferences to turn on the server.

Now to make using mysql in a Terminal's command line I added export PATH=$PATH:/usr/local/mysql/bin to my path similarly to how I got Android's ndk-build command onto path by creating a .bashrc file in my username folder ('cd ~' to get there). This allows the mysql command to be run from any directory without having to type out the whole path. To make sure it works type mysql into Terminal and you should see a paragraph of text starting with Welcome to the MySQL monitor. To exit type \q.

Now it's installed there is a little config to do.
Setting password: mysql_secure_installation and answering yes to all questions
Finally starting the Web Sharing in System Preferences -> Sharing and clicking on the button to create a personal website folder.

Testing the AMP

To make sure it's all working, open a browser and go to localhost and in another window localhost/~<username>, you should see the following

Next return to a browser and go to the URL something like localhost/~workingmatt/drupal where workingmatt must be replaced by your own username. This will take you to the beginning of the browser based stage of the installation process as shown below.

Choose your language...

Fill in database information as set in the previous step of creating the Drupal database. At this point I had an error communicating with the server - a 2002 error. The solution thanks again to Neil was

<root>$ mkdir /var/mysql

<root>$ cd /var/mysql/

<root>$ ln -s /tmp/mysql.sock /var/mysql/mysql.sock

Add some information about the site, the site maintenance account is important as it sets what will be 'User 1' who has some special abilities useful later on.

All should be good and you should pat yourself on your back, you've installed Drupal.

When you visit your new site at localhost/~<username/drupal/ you will see the default site

In Part 2 I will make new local copy of my server site. In Part 3 I decide workflow for dev (local) and live (server) sites, synchronise live to dev, change dev, synchronise dev to live.