This week I’ve spent quite a few hours trying to set up a local development environment on my new Mac. Although I’ve used the built in version of Apache 2, much of the other software that comes pre-installed with OS X is not ideal and needs to be replaced or tweaked. It is also not a bad idea to have your server software in /usr/local to avoid it being potentially broken by system updates. This is a brief record of the steps necessary to create a solid server setup that suits me, mainly for running WordPress sites (PHP and MySQL) and Ruby on Rails. It’s really more of a record for myself if I ever have to do it again – although no doubt next time it will be on Snow Leopard and everything will be slightly different!
- We’ve going to use the built-in version of the Apache web server. To start/stop the web server, go to System Preferences in OS X and select Sharing > Web Sharing.
- Move the Apache document root to a more convenient location (my personal ‘Sites’ directory): open /etc/apache2/httpd.conf and change the ‘DocumentRoot’ variable in 2 places (note: the PHP module should be left commented out, as it is by default). Next enable .htaccess by editing /etc/apache2/users/<username>/<username>.conf and specifying
AllowOverride All. Restart Apache.
- Download and install a more recent version of PHP with more capability than the standard Apple one (GD library, Mcrypt, etc). This installs PHP into the directory /usr/local/php5.
- Download and install a version of MySQL server that correlates with that version of PHP (so the PHP MySQL library matches). MySQL is installed into /usr/local/mysql-5.0.77-osx10.5-x86 with a symbolic link from /usr/local/mysql.
- Create or edit ~/.bash_login and add this line to the end:
export PATH="/usr/local/bin:/usr/local/sbin:/usr/local/mysql/bin:$PATH". This makes it more convenient to interact with MySQL from the command line, as well as pointing our system to our custom software installations in /usr/local.
- To start MySQL from the command line type
sudo mysqld_safe(To stop MySQL type
mysqladmin -u root -p shutdown)
- Set password for MySQL root user:
mysqladmin -u root -p 'mypassword'(Note that the MySQL user is different from the Unix user that the MySQL is running under – usually _mysql).
- To check current users in MySQL, use:
SELECT Host, User, Password FROM mysql.user;. Set passwords for all as necessary. Alternatively, to limit access to your personal machine only, create the file /etc/my.cnf as detailed on this page (‘A Note About Security’).
- Set passwords for other users/hosts as required – see instructions half way down this page. Create an ‘admin’ user for MySQL so we’re not using root in the various config files etc. Will need to grant privileges.
- Download and install PHPMyAdmin. Unzip and rename directory, then place in web document root. Make a file called config.inc.php to put in your blowfish password (any random phrase will do). You can copy libraries/config.default.php if you like.
- Make sure you’ve installed Xcode from the OSX install disk.
- Add in MySQL C bindings for Ruby, to make Rails faster – instructions near the bottom of this page (you will need to have Xcode Tools installed in OS X to do this – use your OS X installation disk if needed)
- Follow the instructions on HiveLogic for installing Ruby and Ruby on Rails into /usr/local.