Extending on Chris Mallinson great tutorial here we have fine-tuned this guide for our WordPress setup and made some updates for the latest version of OSX which we will endeavour to keep up to date.
After toying with Vagrant boxes for a year or two we have gone back to the simplicity and “always on” speed of this native setup. The downside is it can be tricky to setup and easy to break.
To start your journey, you will need to set up Xcode, Apple’s own software development bundle. You can grab this from the App Store or (for all OS X Mavericks 10.9 and higher) run the below command in terminal.
Click install on the dialogue box and wait for it to complete. Once it’s installed, you will need to start up the app – the tools won’t work unless it has been started up at least once – then accept the licence agreement and shut it down.
Next, you will need to install Homebrew, a popular package manager for OS X, by running the below command in terminal.
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Run the below command in terminal to install MySQL.
brew install mysql
Finally, run the command below in terminal to install dnsmasq. This allows wildcard subdomain names.
brew install dnsmasq cd $(brew --prefix); mkdir etc; echo 'address=/.localhost/127.0.0.1' > etc/dnsmasq.conf sudo cp -v $(brew --prefix dnsmasq)/homebrew.mxcl.dnsmasq.plist /Library/LaunchDaemons sudo launchctl load -w /Library/LaunchDaemons/homebrew.mxcl.dnsmasq.plist sudo mkdir /etc/resolver sudo bash -c 'echo "nameserver 127.0.0.1" > /etc/resolver/localhost'
Now we have to set up Apache, a cross-platform web server, which is where we will store our web files. Get this started by running the below command in terminal.
sudo apachectl start
To test this is working, head over to http://localhost/ in your browser. You should see a page that says “It Works”.
Now we need to make a few quick changes to the configuration files. Run the following command in terminal.
sudo nano /private/etc/apache2/httpd.conf
Then use the terminal search feature (ctrl + w) to find and uncomment (remove the ‘#’) the following lines:
#LoadModule php5_module libexec/apache2/libphp5.so
or php7 for High Sierra uncomment
#LoadModule php7_module libexec/apache2/libphp7.so
#LoadModule vhost_alias_module libexec/apache2/mod_vhost_alias.so
Then, using the same search (ctrl + w) find and comment out (add a ‘#’ in front of each line) to the following:
<Directory /> AllowOverride none Require all denied </Directory>
This will give Apache full access to your file system and help direct all your files and virtual hosts to the right places.
Next, to give writing access to all your site directories, run the following command in terminal.
Search for your username and then ensure that group is changed to staff as seen below. User should be set your your user.
# # If you wish httpd to run as a different user or group, you must run # httpd as root initially and it will switch. # # User/Group: The name (or #number) of the user/group to run httpd as. # It is usually good practice to create a dedicated user and group for # running httpd, as with most system services. # User example_user Group staff
Next, run the below command in terminal to find the next file we need to edit.
sudo nano /private/etc/apache2/extra/httpd-vhosts.conf
Look for the following lines, and ensure that __user__ matches your username (hint: whoami).
And also /Users/__myfolder__/www/sites/%1/public matches you website locations!
Note: .dev no longer works as Chrome now forces to HTTPS via preloaded HSTS
<Directory "/Users/__user__/www"> Options Indexes MultiViews FollowSymLinks AllowOverride All Require all granted </Directory> <Virtualhost *:80> VirtualDocumentRoot "/Users/__myfolder__/www/sites/%1/public" ServerName sites.localhost ServerAlias *.localhost UseCanonicalName Off </Virtualhost>
Now restart Apache by running the following line in the terminal.
sudo apachectl restart
Now make a test website at the path you defined in httpd-vhosts.conf and you should be able to view it at your foldername.local !
Done. Please let us know if you have any more tips or feedback to help others.