You are now on my Technical Blog. My other sites: Personal Blog - Genealogy Blog - Genealogy Site

DLMax Max Westen's ramblings on OSX and PHP

Blog Category: php


Easiest local development environment on OSX

Nov 19, 2012

Create a folder testsite.dev in your ~/Sites folder and immediately access it with your browser by using http://testsite.dev, without changing your apache config or adding sitenames to your DNS or /etc/hosts file. That’s what you can do after following the instructions in this post!

Background

Most of the time I’ve been using specific vhost files per webdevelopment project in a vhost folder of which all vhost files will be included. All development sites are added to the /etc/hosts file, so I could access them by a friendly-name lie http://dlmax.dev.

An article by Blain Smith on his local development setup made me rethink my own setup: "Can’t I just configure it to add the domainnames to the local DNS resolution too, so I only have to create a folder and immediately be able to access the site?"

Apache configuration

Current setup

I’m using the included Apache included in OSX Mountain Lion (10.8) and modified it, so it’s using 1 conf file per vhost in the /etc/apache2/vhosts folder. These get included at the end of the file /etc/apache2/httpd.config with the line:

Include /private/etc/apache2/vhosts/*.conf

in the vhosts folder I’ve created a file 1_default.conf that contains the following:

NameVirtualHost *

<VirtualHost *>
    ServerAdmin mwesten@localhost
    DocumentRoot "/Users/mwesten/Sites"
    ServerName max.local
    ServerAlias localhost
</VirtualHost>

An example of the other files, in this case dlmax.conf then contains:

<VirtualHost *>
    ServerAdmin mwesten@localhost
    ServerName dlmax.dev
    DocumentRoot "/Users/mwesten/Sites/dlmax"
    Options -Indexes +FollowSymLinks
</VirtualHost>

Changes to this setup:

In /etc/apache2/httpd.config change the line that calls all *.conf files in the vhosts folder, to only call the virtual.conf

Include /private/etc/apache2/vhosts/virtual.conf

now create the etc/apache2/vhosts/virtual.conf to use some wildcards:

NameVirtualHost *
UseCanonicalName Off

<VirtualHost *>
  VirtualDocumentRoot "/Users/mwesten/Sites/%0"

  <Directory "/Users/mwesten/Sites">
      Options Indexes FollowSymLinks MultiViews
      AllowOverride All
      Order allow,deny
      Allow from all
  </Directory>
 # If you are experiencing problems with the PHP DOCUMENT_ROOT setting, create a
 # PHP file containing the following lines:
 #   <?php
 #    $_SERVER['DOCUMENT_ROOT'] = str_replace($_SERVER['SCRIPT_NAME'], '', $_SERVER['SCRIPT_FILENAME']);
 # and uncomment the following line, pointing to the just created PHP file:
 #   php_admin_value auto_prepend_file /Users/mwesten/Sites/setdocroot.php

</VirtualHost>

DOCUMENT_ROOT problems with PHP

If you are experiencing problems with your DOCUMENT_ROOT server variable, uncomment the php_admin_value line in the virtual.conf file. Add the file setdocroot.php to the path specified above and put the following in, to fix the DOCUMENT_ROOT problem described in this apache bugreport

<?php
$_SERVER['DOCUMENT_ROOT'] = str_replace($_SERVER['SCRIPT_NAME'], '', $_SERVER['SCRIPT_FILENAME']);

Get the local DNS names working

Because the /etc/hosts file doesn’t parse wildcards, we cannot add something like: 127.0.0.1 *.dev.

The only option is to install a local DNS server like bind or a DNS proxy like DNSMasq.

I’m using DNSMasq, because it’s the easiest to configure and less prone to configuration errors.

Install and configure DNSMasq

I’m using Homebrew as my package manager, so if you don’t use one, or use another one, you should install brew(to keep it easy) or try to follow the next steps based on your package manager.

brew install dnsmasq

Then create a textfile /usr/local/etc/dnsmasq.conf and add the following line, to redirect all *.dev sites to the local ip 127.0.0.1:

address=/.dev/127.0.0.1

Now make dnsmasq start on system-startup and start it now:

sudo cp /usr/local/Cellar/dnsmasq/2.63/homebrew.mxcl.dnsmasq.plist /Library/LaunchDaemons
sudo launchctl load -w /Library/LaunchDaemons/homebrew.mxcl.dnsmasq.plist

Configure OSX to use your local system as first DNS server

Enter your network preferences and note the DNS servers named there. In my case I’m using my router, so 192.168.10.1. Now add your loopback IP (127.0.0.1) first and then your previous addres.

Now if you try to ping some address ending in .dev it should give a reply like this:

$ ping thisisatestdomain.dev
PING thisisatestdomain.dev (127.0.0.1): 56 data bytes
64 bytes from 127.0.0.1: icmp_seq=0 ttl=64 time=0.056 ms
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.079 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.101 ms
64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.075 ms
64 bytes from 127.0.0.1: icmp_seq=4 ttl=64 time=0.094 ms

--- thisisatestdomain.dev ping statistics ---
5 packets transmitted, 5 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 0.056/0.081/0.101/0.016 ms

Pinging to www.google.com should also work, so we know external name resolutions works well too.

Adding a new local site

Adding a site is now as easy as creating a new folder in the ~/Sites folder with the name of the local sitename, so for http://dlmax.dev you would create the dlmax.dev folder.

Setting ACL on webfolders when using GIT or SVN

Aug 27, 2012

When using a VCS(Version Control System) like SVN or GIT to deploy files to your webserver you are generally faced with some troubles with permissions.

If the website/webapplication needs permissions to write files in folders, you cannot easy chown these folders like:

sudo chown -R www-data:www-data content

When setting rights this way, the webapplication works like a charm, but updating with GIT or SVN give problems, because the user executing the update isn’t www-data.

A solution to this problem is to use ACL.

To install ACL on ubuntu

Then add the rwx right to the current running user and www-data on the folders content and config/user

sudo setfacl -R -m u:www-data:rwx -m u:`whoami`:rwx content config/user
sudo setfacl -dR -m u:www-data:rwx -m u:`whoami`:rwx content config/user

The move to Statamic

Jul 10, 2012

History:

Since running my own sites, I’ve been using several techniques to keep the site running.

In 1996 when I started having my own site just HTML with javascript, in 1997 I started toying around with some vanilla PHP sites and in 2001 I put up some sites with phpNuke. In 2003 I started using Drupal and in 2005 I settled with Wordpress.

Using Wordpress for a blog/simple site felt better than using something like Drupal. Wordpress was good at not getting (too much) in the way when writing content and setting up your site.

On the other hand: Every time I had to change something on the site(this happened mostly during one of the frequent security-updates) it felt like things could and should be easier…

No-DB CMS

I’ve been looking at a lot of different CMS’es, but most of them had the same problems and needed frequent database backups to prevent losing my blogging history. Then some No-DB CMS-es got my attention.

Jekyll and Octopress

I started looking at Jekyll and then stumbled upon Octopress. I converted my Wordpress posts to markdown with the ExitWP script and pulled them into Octopress. I styled the site, created and modified several plugins and all was ready to put live… But I didn’t.

It had a lot of stuff I liked:

  • Compass generation of stylesheets
  • Use of files-only system:
    • Easy versioning with Git
    • Easy deployment
    • Easy updating of the system software

Some other things held me back in eventually using it and pushing the system live:

  • It needed a ruby system(or had to use something as github pages) to generate the files (I do like Ruby, but I don’t like the extra step to generate the html files (Yes, I know you can automate this, but it needs to be a SIMPLE system that doesn’t get in the way))
  • I need access to a system that can access my server to update and add posts, because there is no interface to add or modify stuff. (This one is both positive(security) and negative (ease of use) and the negative won in this case.)

Laravel Kudos

I started thinking on what I’d like to see in a system to use it:

  • It had to run on my Linode VPS with Apache and PHP, without me adding some obscure unmanagable extra server
  • It had to be filebased and Markdown files are a must, so I can edit them with Mou, like I do with all notes/texts I write.

While looking at Lavarel, I came across the Kudos project by Ian Landsman. I really liked the way it worked and was still considering to rewrite the Octopress site to Kudos.

Statamic

Base system

While still thinking about picking this up sometime, I saw the release of Statamic in my timeline on the 20th of june. When looking at their site, I thought: "This could very well be what I was looking for!".

Within 10 minutes I bought a personal license( Hey, what’s $19,- for quality software!) for this blog and started tweaking the system to work how I want it to work.

Creating pages and pages with listings of items/posts is easy and the system works with you instead of against you. That’s quite a nice experience after using more complex systems (like Wordpress and Drupal) for websites comparable with the complexity of this blog!

Theming and styling the system is even more fun !

Start building off of the "Wild-London" theme that’s included, of just start with a framework like Zurb Foundation 3 or InuitCSS add your layouts, style your content with some templates and mix-in some partials and voila!

The building of the system is great, but the included admin interface that can be enabled eases creating and modifying your content in a way that you don’t get frustrated in writing or updating a post on your site.

Oh and the guys(Mubashar Iqbal & Jack McDade) creating the system have loads of things that will be added and are very swift in supporting their customers.

AddOns / Plugins

Because the system is just released, I ported and modified some of my plugins that I used for Octopress and added them to github for everyone to use.

FancyBox2 Image Lightbox

I like to be able to see bigger images in a lightbox on other sites and that’s exactly what I use on mine too. I’m using the nice FancyBox2; read the licensing when considering commercial use.

You can find the Fancybox addon for Statamic here.

Random Flickr badge images

I tend to place a lot of pictures on Flickr and like a way to display 9 random images in the sidebar. That’s exactly what this addon does. Just client-side with JS, so no cache is needed.

You can find the Random Flickr Badge addon for Statamic here.

Highlight.js Code Highlighting

Because most of the posts are technical and contain somesort of code or commands, I feel obliged to my readers to display the code in the best way: Color Coded. Highlight.js does a great job with that client-side, so I’ve added that in an addon.

You can find the Highlight.js Code Highlighting addon for Statamic here.

Tweets: displays latest tweets

I like to show the last tweets on my pages too. I rewrote some of the octopress stuff earlier and liked that better than the included twitter addon from Statamic, so I ported mine to Statamic.

You can find the Tweets addon for Statamic here.

Dynamic Sitemap generator

You can use a semi-dynamic way to generate the sitemap, like Eric Barnes explains in his post on "Statamic Sitemap".

I like it a bit more dynamic though; It needs to fetch all non-hidden pages and add a changefreq and priority to it that is a bit more flexible:

  • changefreq based on the last modification date; the longer untouched, the longer the changefreq
  • The priority based on folder/pagetype or even use a standard that can be overridden in the page YAML.

Because of my health, it’s a work in progress right now. I’ll add an extra post once it’s ready.

When it’s ready, You can find the Dynamic Sitemap addon for Statamic here.

Setting up MockSMTP on OSX 10.7(Lion) for local PHP development

Jun 01, 2012

** NOTE ** : This post was sitting in my drafts for way too long (over 7 months now), so I’ll just post it in the hopes someone likes it. There could be rough edges here and there, but hey, that’s what the contact page is for… ;)

Because I tend to do some PHP development on my MacBookPro runnin OSX 10.7(Lion) and want to be able to test the email-functionality, without the system actually sending email to people, I was on the lookout for an easy way to accomplish this in such a way, that I would be able to collect all mail and display both text and HTML versions of it. I found MockSMTP, and it works like a charm. In this post we’ll set it up and configure the system to route all mail to MockSMTP, even when it’s not running.

Install MockSMTP (via the AppStore or download it from their website)

Open the terminal and enter the following command to reroute all traffic on port 25 to port 1025 on the local system.

sudo ipfw add 1025 forward 127.0.0.1,1025 ip from any to any 25 in

Edit the Postfix main.cf file from the postfix configuration folder:

sudo /etc/postfix/nano main.cf

Add the following at the end of the file:

relayhost = localhost:1025

Make sure the line reading "mydomain_fallback = localhost" is witten as: "mydomain_fallback=localhost" so without the spaces, to prevent any runtime warnings.

In OSX Lion a new version of postfix is installed, so we need an extra file to deliver mail:

sudo touch /etc/postfix/submit.cred
sudo nano /etc/postfix/submit.cred

Add the following lines:

submitcred version 1
localhost|username|password

(Note: the username and password can be used literally here, they don’t need to match your account credentials!)

Check the maillog in a new terminal tab/window. This monitors new lines written to the logfile. This way you can troubleshoot and see if messages are sent correctly. (Ctrl+C to stop)

tail -f /var/log/mail.log

Start Postfix and make sure it will restart on system boot: enter in the terminal:


sudo launchctl load -w /System/Library/LaunchDaemons/org.postfix.master.plist

Afterwards to stop or start the service use:

sudo launchctl unload /System/Library/LaunchDaemons/org.postfix.master.plist
sudo launchctl unload /System/Library/LaunchDaemons/org.postfix.master.plist

Start the MockSMTP application and make sure it is listening on port 1025 (see the status in the bottom of the screen). MockSMTP Status

Now try to send a testmail:

echo "This is the body of a text test-mail" | mail -s "Email test" test@testdomain.dev

And all went well, we now have a testmail in our MockSMTP app. MockSMTP mailmessage

Because we used the IP Port Forward, mail sent by any application that tries to use the local SMTP server (localhost:25) or sendmail/postfix will be rerouted to MockSMTP. If the MockSMTP app is not running, mail will be spooled and will be delivered when you run the app again.

Have fun developing…

Older