Posts Tagged ‘macport’

Installing MySQL, Apache2, PHP, and WordPress with MacPorts

November 17th, 2009

I’ve recently started working on a website project using WordPress.  I’ve got a production environment where I run this blog on my ubuntu server (VPS) hosted by slicehost.  But because I’m going to muck around with themes and plugins I decided that I’m better off doing that locally on my Mac.  So this is the first of a series of blog posts for getting WordPress up and running on my local machine.

I have macports already installed and I prefer its packages over the packages shipped with OSX.  Mostly because they are more up to date and there is more variety.  Plus it feels more native to working with my VPS.  Before we get started, lets ensure that our MacPorts is up to date:

$ sudo port selfupdate
--->  Updating the ports tree
--->  Updating MacPorts base sources using rsync
MacPorts base version 1.8.1 installed,
MacPorts base version 1.8.1 downloaded.
--->  MacPorts base is already the latest version

There will be four parts to this series:

  1. Installing MySQL
  2. Installing Apache2
  3. Installing PHP along with MySQL support
  4. Installing WordPress

I like to chunk these up rather than merge them all into one article as the temptation is to mix information together and then its harder to see where one step stops and the next one starts.  Also it means that you can look at the entries in isolation and they should still make sense.

Installing MySQL

I had a binary distribution of MySQL on machine which I needed to remove before installing the macports distro, this thread was useful as it isn’t particularly clear what to do:

http://forums.mysql.com/read.php?11,75256,255851#msg-255851

The following article was my main source of inspiration, I’ve pretty much followed these steps:

http://2tbsp.com/content/install_and_configure_mysql_5_macports

Install the port and setup the mysql system database

$ sudo port install mysql5-server
$ sudo -u mysql mysql_install_db5

Setup some handy aliases in your shell

$ mate ~/.profile
alias mysqlstart='sudo /opt/local/bin/mysqld_safe5 &'
alias mysqlstop='/opt/local/bin/mysqladmin5 -u root -p shutdown'
$ source ~/.profile

Start the server and set the root password

$ mysqlstart
$ /opt/local/lib/mysql5/bin/mysqladmin -u root password 'root'
$ /opt/local/lib/mysql5/bin/mysqladmin -u root -p -h localhost password 'root'

(Not sure what this second step is for!)

Now we can use the mysqlstop alias because the password has been set.

And we’re all done!

Installing Apache2

Source of inspiration for this entry is from here:

http://2tbsp.com/content/install_apache_2_and_php_5_macports

$ sudo port install apache2

Enabled virtual hosts in main config

$ sudo mate /opt/loca/apache2/conf/httpd.conf
# Virtual hosts
Include conf/extra/httpd-vhosts.conf

Updated virtual hosts config

$ sudo mate /opt/local/apache2/conf/extra/httpd-vhosts.conf

The directory part is important as Apache 2.2 directories are by default not browsable so ensure that you enable them!

<VirtualHost *:80>
    ServerAdmin webmaster@myapp.local
    DocumentRoot "/Users/tgmcclen/workspace/myapp/htdocs/site"
    ServerName myapp.local
    ServerAlias www.myapp.local
    ErrorLog "/Users/tgmcclen/workspace/myapp/logs/error_log"
    CustomLog "/Users/tgmcclen/workspace/myapp/logs/access_log" common
 
    <Directory "/Users/tgmcclen/workspace/myapp/htdocs/site">
        Options Indexes FollowSymLinks
        AllowOverride None
        Order allow,deny
        Allow from all
    </Directory>    
</VirtualHost>

Setup an alias for controlling apache and start

$ mate ~/.profile
alias apache2ctl='sudo /opt/local/apache2/bin/apachectl'
$ source ~/.profile
$ apache2ctl start

Add an entry in the hosts file for the virtual host

$ mate /etc/hosts
127.0.0.1 myapp.local

Now place files in the htdocs directory and browse to them in a browser:

http://myapp.local/hello.html

Installing PHP along with MySQL support

$ sudo port install php5 +apache2 +pear
...
Note: php5 installs files outside the common directory structure.
--->  Installing php5 @5.3.0_3+apache2+darwin_10+macosx+mysql5+pear+sqlite
--->  Activating php5 @5.3.0_3+apache2+darwin_10+macosx+mysql5+pear+sqlite
To customize php, copy
/opt/local/etc/php5/php.ini-development (if this is a development server) or
/opt/local/etc/php5/php.ini-production (if this is a production server) to
/opt/local/etc/php5/php.ini and then make changes.
$ sudo cp /opt/local/etc/php5/php.ini-development /opt/local/etc/php5/php.ini

Now enable PHP module with apache2

$ cd /opt/local/apache2/modules
$ sudo /opt/local/apache2/bin/apxs -a -e -n "php5" libphp5.so

and add directives for mime_module and dir_module in the virtual host entry:

<VirtualHost *:80>
    ServerAdmin webmaster@myapp.local
    DocumentRoot "/Users/tgmcclen/workspace/myapp/htdocs"
    ServerName myapp.local
    ServerAlias www.myapp.local
    ErrorLog "/Users/tgmcclen/workspace/myapp/logs/error_log"
    CustomLog "/Users/tgmcclen/workspace/myapp/logs/access_log" common
 
    <Directory "/Users/tgmcclen/workspace/myapp/htdocs">
        Options Indexes FollowSymLinks
        AllowOverride None
        Order allow,deny
        Allow from all
    </Directory>
 
    <IfModule mime_module>
      AddType application/x-httpd-php .php
      AddType application/x-httpd-php-source .phps
    </IfModule>
 
    <IfModule dir_module>
        DirectoryIndex index.html index.php
    </IfModule>
</VirtualHost>

And then restart Apache to get going…

$ apache2ctl restart

Installing MySQL support for PHP

$ sudo port install php5-mysql
--->  Computing dependencies for php5-mysql
--->  Fetching php5-mysql
--->  Verifying checksum(s) for php5-mysql
--->  Extracting php5-mysql
--->  Configuring php5-mysql
--->  Building php5-mysql
--->  Staging php5-mysql into destroot
--->  Installing php5-mysql @5.3.0_0+mysqlnd
To use mysqlnd with a local MySQL server, edit /opt/local/etc/php5/php.ini and set
mysql.default_socket, mysqli.default_socket and pdo_mysql.default_socket
to /opt/local/var/run/mysql5/mysqld.sock
--->  Activating php5-mysql @5.3.0_0+mysqlnd
--->  Cleaning php5-mysql
$ sudo mate /opt/local/etc/php5/php.ini
; Default socket name for local MySQL connects.  If empty, uses the built-in
; MySQL defaults.
; http://php.net/pdo_mysql.default-socket
pdo_mysql.default_socket=/opt/local/var/run/mysql5/mysqld.sock
 
; Default socket name for local MySQL connects.  If empty, uses the built-in
; MySQL defaults.
; http://php.net/mysql.default-socket
mysql.default_socket = /opt/local/var/run/mysql5/mysqld.sock
 
; Default socket name for local MySQL connects.  If empty, uses the built-in
; MySQL defaults.
; http://php.net/mysqli.default-socket
mysqli.default_socket = /opt/local/var/run/mysql5/mysqld.sock

Installing WordPress

Inspiration for instructions from here:

http://codex.wordpress.org/Installing/Updating_WordPress_with_Subversion
http://codex.wordpress.org/Installing_WordPress

Inside your htdocs directory:

$ svn co http://core.svn.wordpress.org/tags/2.8.6 .

(Latest tag will in URL on page, otherwise just browse to tags dir in folder to determine)

Create the database in MySQL

$ /opt/local/bin/mysql5 -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.1.40 Source distribution
 
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
 
mysql&gt; CREATE DATABASE mydb;
Query OK, 1 row affected (0.05 sec)
 
mysql&gt; GRANT ALL PRIVILEGES ON myname.* TO "mydb"@"localhost" IDENTIFIED BY "mypassword";
Query OK, 0 rows affected (0.58 sec)
 
mysql&gt; FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
 
mysql&gt; EXIT
Bye

Now setup configuration

$ cp wp-config-sample.php wp-config.php
// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define('DB_NAME', 'mydb');
 
/** MySQL database username */
define('DB_USER', 'myname');
 
/** MySQL database password */
define('DB_PASSWORD', 'mypassword');
 
/** MySQL hostname */
define('DB_HOST', 'localhost');
 
/** Database Charset to use in creating database tables. */
define('DB_CHARSET', 'utf8');
 
/** The Database Collate type. Don't change this if in doubt. */
define('DB_COLLATE', '');

Open a browser and continue the install:

http://myapp.local/wp-admin/install.php

You’ll most likely notice that you’re getting lots of errors and warnings on the pages, these can be turned off:

$ sudo mate /opt/local/etc/php5/php.ini
; Default Value: On
; Development Value: On
; Production Value: Off
; http://php.net/display-errors
display_errors = Off
$ apache2ctl restart

Now you’re good to WordPress away!