Thursday, May 15, 2014

Installing WordPress locally on Mac OS

Some time ago I wrote the article "Installing WordPress locally on Windows", which, in a nutshell, described the whole process of installing, yourself, a full blown setup of PHP, MySQL and WordPress, without using any installers (Web Platform Installer, etc), which are great, but they don't really allow you the full control over each different component. Or we can just argue that it's for the sake of understanding each stage of the process.

In this article, I will describe how to Install WordPress locally on the Mac OS operating system, and all of the fitfalls I've bumped into, and how to resolve them.
The process is very similar to the Windows version, so I divided it into the same 3 major steps, while the commands themselves are different.

Before we begin

  • Changing secure files such as hosts, httpd.conf or httpd-vhosts.conf may be challenging. For once, refrain from using Mac's TextWriter, which will effectively break your configuration files, mainly because of wrong comma formats, but may cause other unexpected issues.
  • Because of security issues, you will not be allowed to edit these files directly. The best way I've found is to create a copy in an open directory of my own, and then copy-paste the files over to their original location, which will ask for credentials but eventually will allow the overwrite.
  • Every time you edit any of these three files, I recommend restarting the apache server, and checking for configuration errors.
    • sudo apachectl stop
    • sudo apachectl start
    • sudo apachectl



1) Setup Apache and PHP
In the Mac, Apache and PHP are shipped out-of-the-box, so all we need to do is configure them properly.

Open a terminal and type this to make all further commands run as admin (root)
sudo su -

Run this line to start apache
apachectl start

Try the link below. If everything works as expected, it will say "It Works!"
http://localhost

File locations:
  • Host entries are located at /etc/hosts (loopbacks)
  • Default website files are located at /Library/WebServer/Documents
  • Virtual Application URL entries are located at /etc/apache2/extra/httpd-vhosts.conf
  • Apache config is located at /etc/apache2/httpd.conf

Example hosts file entries (resolving special host headers)

127.0.0.1   tiagoduarte

127.0.0.1   wordpress

127.0.0.1   phpmyadmin



Example httpd-vhosts.conf (127.0.0.1 will go to the default root, then 3 overrides)

NameVirtualHost *:80 

# Virtual Hosts
#
# Required modules: mod_log_config

# If you want to maintain multiple domains/hostnames on your
# machine you can setup VirtualHost containers for them. Most configurations
# use only name-based virtual hosts so the server doesn't need to worry about
# IP addresses. This is indicated by the asterisks in the directives below.
#
# Please see the documentation at 
# <URL:http://httpd.apache.org/docs/2.4/vhosts/>
# for further details before you try to setup virtual hosts.
#
# You may use the command line option '-S' to verify your virtual host
# configuration.

#/Users/tiagoduarte/Sites/httpd-vhosts.conf
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for all requests that do not
# match a ServerName or ServerAlias in any <VirtualHost> block.
#

#NameVirtualHost *:80 
 
<directory "/Users/tiagoduarte/Sites">
    Options FollowSymLinks
    AllowOverride All#required for .htaccess support
    Order allow,deny
    Allow from all
    #Require all granted#only for apache 2.4
</directory>
 
<virtualhost localhost:80>
      DocumentRoot /Users/tiagoduarte/Sites
      DirectoryIndex index.php
</virtualhost>

<virtualhost wordpress:80>
      DocumentRoot /Users/tiagoduarte/Sites/WordPress
      DirectoryIndex index.php
</virtualhost>

<virtualhost phpmyadmin:80>
      DocumentRoot /Users/tiagoduarte/Sites/phpmyadmin
      DirectoryIndex index.php
</virtualhost>



Example partial httpd.conf (enable libraries and setup another working folder)
  • uncomment rewrite_module to support .htcaccess files
  • uncomment php5_module to support PHP5
  • update DocumentRoot to use Sites instead of /LibraryWebServer/Documents
  • update Directory to use Sites instead of /LibraryWebServer/Documents

DocumentRoot "/Users/tiagoduarte/Sites"
<Directory "/Users/tiagoduarte/Sites">

Remember to restart and test the apache server after each edit.


NOTE: If your Apache version is v2.4 or earlier, you will need to add Require all granted as a directive over the directory entry. For later versions, don't add it or it will cause problems.



2) Install MySQL
You will need to download MySQL for mac (DMG).
I personally always install free community edition versions.
There are different versions (older, latest, beta, 32 bits and 64 bits).
I prefer stable versions (the latest) and according to my CPU (x64).
You may notice that the latest version is older then newer version of the OS (e.g. 10.9.2) but that's okay, you can use a previous version of the MySQL package.

Install 3 files:
- .pkg xxx file
- startup item
- left pane

Start the database instance on MySQL settings page

Setup root password:
/usr/local/mysql/bin/mysqladmin -u root password ‘yourpassword’

Create WordPress DataBase:
/usr/local/mysql/bin/mysql -u root -pYOURPASSWORD “create database wordpress;”

Pro tip:
You can download phpMyAdmin, which will provide you a special website to manage the MySQL instance through a nice web user interface.
You'll need to rename config.sample.inc.php to config.inc.php and edit it with your instance details.

Note: In system preferences you will find a MySQL Panel which enables to start/stop the instance. I also had a strange error that wouldn't let me connect (Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)), which I solved by reinstalling MySQL and restarting the machine.

3) Install WordPress

Install WordPress using the command line utility to get the latest version to your current working directory.
If you have used a custom hosts entry, move it into your custom folder, otherwise, use /Library/WebServer/Documents.

Download command:
curl -O http://wordpress.org/latest.tar.gz

Extract the contents:
tar -xvzf latest.tar.gz

Rename wp-config-sample.php to wp-config.php

Get some keys
https://api.wordpress.org/secret-key/1.1/salt/

Configure the WordPress DataBase Name (e.g. wordpress), user (e.g. root), password and host (e.g. localhost).

 Open http://wordpress and start working with WordPress right away!


OS version used:
10.9.2

Useful Links:
http://coolestguidesontheplanet.com/fastest-way-to-install-wordpress-on-osx-10-6/

No comments:

Post a Comment