Saturday, March 16, 2013

Setting up a Moodle Development Environment on a Mac

Having been an Ubuntu user for years, I am switching my main computer to a new Macbook Pro. This means that I now need to change the way I do things, and learn how to set up a Mac to be a developer station. So, here I go...

To begin with, I consulted with my colleagues +Justin Filip and +Amy Groshek, both Moodle developers and both power Mac users. They both pointed me to MacPorts as a starting point. Amy further advised me that although OSX comes preinstalled with PHP and Apache, I probably want to replace them both with MacPorts versions of the software. An opinion shared by a blog post Amy shared with me on setting PHP up using MacPorts. I'm going to use that blog post, and the MacPorts installation page to start my setup.

Now, one thing I need to be careful with is that this blog post is for up to version Lion. I'm running version Mountain Lion; so there may be some differences.

...and the first thing I run into is those differences...

The blog post tells me to turn off Apache:
"go to System Preferences, type Apache in it’s spotlight. It will highlight Sharing, but you just need to press return. Just make sure Web Sharing is disabled."
Unfortunately, "Apache" turns up nothing. And accessing "Sharing" direct doesn't offer "Web Sharing". I do a search for "Mountain Lion" and "Apache", and turn up several articles describing how to enable Apache in the absence of the "Web Sharing" option. It turns out that Apache is disabled by default with OS-X Mountain Lion. I'm guessing, I can skip this step.

The next thing I am asked to do is to install Xcode. This should be available in the Mac App Store, so I'm going to check there first. Searching for 'xcode' in the App Store I immediately find it. It defines itself as "everything developers need to create great applications for Mac...". Checking the "Installing MacPorts" page on the MacPorts site, the "Xcode" requirement is confirmed. So, I install it from the App Store.

Next, the MacPorts site is telling me I need to have the Command Line Developer Tools. The instructions say they can be installed from within Xcode 4, which I just installed. I find Xcode in my Applications folder and run it. It takes me through more installation steps, and then allows me to start it. According to the information on that page, simply running and accepting the license should be enough. But I find another page that points me to the "Preferences -> Downloads" section where I need to install the "Command Line Tools".

Next, both sites tell me to install MacPorts by downloading the dmg from the site. I think I'll follow that advice. The download runs me through a standard installation process, and I choose the standard method. It all seems to install just fine.

Having done that, I move on to installing MySQL. The blog post I'm reading recommends installing the MySQL server installation using MacPorts. After reading others, I decide to do just that. The recommended commands are:
sudo port install mysql5-server -- installs all necessary software
sudo port load mysql5-server -- configures MySQL to start at bootup
sudo -u _mysql mysql_install_db5 -- sets up necessary database tables
/opt/local/lib/mysql5/bin/mysqladmin -u root password 'new-password' -- sets a root password 
Next, I will install PHP, Apache and some required libraries.
sudo port install php5 +apache2 +pear +fastcgi php5-mysql +mysqlnd -- installs all necessary software
sudo port load apache2 -- configures PHP to start at bootup 
Then I will configure PHP with development settings:
cd /opt/local/etc/php5/
sudo cp php.ini-development php.ini
 
And enable it in Apache:
cd /opt/local/apache2/modules
sudo /opt/local/apache2/bin/apxs -a -e -n "php5" libphp5.so 
Next, I can change where my Apache sites will run from. By default, they are in "/opt/local/apache2/htdocs", but I want them to be in subdirectory off my home. To do this, I edit the "/opt/local/apache2/conf/httpd.conf", and change the two lines below as follows:
DocumentRoot "/opt/local/apache2/htdocs"
to
DocumentRoot "/Users/mikechurchward/www"
and
<Directory  "/opt/local/apache2/htdocs">
and
<Directory "/Users/mikechurchward/www">
This allows me too store my web, and PHP files in my home directory. Some other changes in that file:
<IfModule dir_module>    DirectoryIndex index.html index.php</IfModule>
AddType application/x-httpd-php .phpAddType application/x-httpd-php-source .phps
This should give me everything to run web and PHP.

Now, I reboot my computer and try to access a web page on my machine. It works.

Next, I need git. This looks like an easy task for MacPorts. A quick search concludes that "git-core" is what I should need, so I issue the command:
sudo port install git-core +bash_completion
This installs git-core as well as a variant known as bash_completion, which will allow the "tab" key to help complete git commands.

Now I use git to checkout a Moodle installation, and it works!

So, I will now navigate to my new Moodle codebase, and:
Moodle requires the iconv PHP extension. Please install or enable the iconv extension.
Okay. So I need to add more PHP extensions. Luckily, Moodle documents what I need.

To add the iconv extension, I enter:
sudo port install php5-iconv
then restart Apache:
sudo /opt/local/apache2/bin/apachectl restart
Once more to the site and I actually move on the installation page!

To make my life easier, I think I will add phpMyAdmin before moving on.
sudo port install phpmyadmin
Then, since I have moved my webroot, I move it:
sudo mv /opt/local/www/phpmyadmin ~/www/
Then reconfigure it with:
cd /opt/local/www/phpmyadmin/sudo
cp config.sample.inc.php config.inc.php
 
$cfg['Servers'][$i]['auth_type']     = 'config';    // Authentication method (config, http or cookie based)?
$cfg['Servers'][$i]['user']          = 'root';      // MySQL user
$cfg['Servers'][$i]['password']      = '';          // MySQL password (only needed with 'config' auth_type)
When I try to run it, I get an error about socket connections not working. After a bit of research, I come up with this:
Create /opt/local/etc/mysql5/my.cnf, add the following to it and save    [mysqld_safe] socket = /tmp/mysql.sock
...and...
Also to remain compatible with other programs that may have been coded to look for the socket file in its original location then add this symbolic link:
    sudo ln -s /opt/local/var/run/mysql5/mysqld.sock /tmp/mysql.sock
That gets phpMyAdmin running. Now onto install Moodle.

I step through the initial steps of Moodle until I hit the "Server checks" page. This page tells me exactly what other PHP extensions I'm missing.
sudo port install php5-curl
sudo port install php5-openssl
sudo port install php5-xmlrpc
sudo port install php5-soap
sudo port install php5-intl
And reload the page. Success. The installation continues, and runs successfully.

I now have most everything I need... But I still need an IDE.

I've been an Eclipse IDE user for a very long time, but both +Amy Groshek and +Justin Filip are telling me that Sublime Text 2 is the way to go, so I'm going to see what its like. Now, to install and use it, I'm just going to use Stuart Herbert's page. No need to recreate that here.

So, at this point, I have a fully working development environment for Moodle working on my Macbook Pro. I'm sure I'll need more as I go along, but this is a good starting point.