Ramlev.dk

I'm living in Copenhagen with my wife and Dogs.

Drupal developer for a living.

text

Vim and drupal code validation

I have been using the Syntastic plugin for vim for a long time. Syntastic does what it have to, in php files it validates the file by in short terms do a

$ php -l FILENAME

And present the error(s) in the statusbar, and with a little marker at the line(s).

If you install the PHP_CodeSniffer via the pear installer, Syntastic automatic validates the code against the default code standards setup in php_codesniffer.

I found DrupalCS module by Ericduran which is a PHP code sniffer addon, and tests your active code on-the-fly when saving.

The DrupalCS and phpcs (php code sniffer) is integrated into the Syntastic plugin.

The installation of PHP Code Sniffer and Drupal CS is quite simple, and it’s nicely documented on the DrupalCS module page.

After installing it, you can test if the DrupalCodingStandard work by test if it’s loaded in your terminal

$ phpcs -i

If you want to test a file from your terminal, you can fire the command

phpcs --standard=DrupalCodingStandard --extensions=php,module,inc,install,test,profile,theme FILENAME.module

The result will look something like this output

If you execute the phpcs without the Drupal specific validator

$ phpcs --extensions=php,module,inc,install,test,profile,theme FILENAME.module 

The result output is a bit more scary.

Vim configuration

The configuration for vim im using to get it working is.

let g:syntastic_enable_signs=1 " Enable markers on the line containing a problem.
let g:syntastic_auto_jump=0 " Dont jump to the first line with a problem
let g:syntastic_phpcs_conf='--standard=DrupalCodingStandard --extensions=php,module,inc,install,test,profile,theme' " Test php,module,inc,install ... files with the DrupalCodingStandard

The first days after using it on a daily basis, it was quite annoying, but it have made me more aware of how i code.

text

OSX Lion as the awësome php developing environment

Make your OSX Lion a dream of a php dev environment

This will be installed and configurered.

  • Homebrew + Homebrew alternatives
  • git
  • Iterm2
  • zsh + oh-my-zsh
  • Macvim
  • named
  • nginx
    • Configure nginx for wildcard local domains.
  • php 5.3.8
  • mariaDB
  • Real life example
    • Another site
  • Linux note

Homebrew

Homebrew - as told on the website, The missing package manager for OSX, it’s the easiest and most flexisble way to install various tools Apple didn’t include with OSX. The HomebrewAlt is an addon to the normal homebrew, with a lot of new apps to install. We’re using it to get php upgraded to 5.3.8.

There are a few requirements to be able to install Homebrew, and it’s:

  • OSX 10.5+ (which should be fully updated).
  • Xcode 4.x

It’s pretty simple to install Homebrew, it’s done with this oneliner.

/usr/bin/ruby -e "$(curl -fsSL https://raw.github.com/gist/323731)"

If you want to see the script.

Homebrew it now installed in /usr/local and it’s path is writeable by the user who have installed it, so you can acutally install with homebrew without sudo’ing.

Download and install Homebrew-alt into /usr/local/LibraryAlt

git clone https://github.com/adamv/homebrew-alt.git /usr/local/LibraryAlt

Add /usr/local/bin and /usr/local/sbin to your path.

vim ~/.zshrc 

Paste the following into the very buttom of the file and [esc]:wq

export PATH=$(brew --prefix)/bin:$(brew --prefix)/sbin:$PATH;

Git

Git is a free & open source, distributed version control system designed to handle everything from small to very large projects with speed and efficiency.

It’s simply installed with

brew install git

And you’re good to go.

Iterm2

iTerm2 is a replacement for Terminal. iTerm2 has a rich collection of features.

Go to www.iterm2.com and download and install the app.

ZSH & Oh-my-zsh

The ZSH is a shell designed for interactive use, although it is also a powerful scripting language, it will be installed along with [oh-my-zsh)[https://github.com/robbyrussell/oh-my-zsh] which is a framework for managing your ZSH configuration, theres a lot of plugins, which adds tab-completion for ex, git homebrew, and a lot of terminal themes to spice it all up with.

First we need to install wget and a never version of zsh, to download and execute the oh-my-zsg install script, it’s so easy done with homebrew.

brew install wget zsh

Fire this command to install oh-my-zsh

wget --no-check-certificate https://github.com/robbyrussell/oh-my-zsh/raw/master/tools/install.sh -O - | sh

MacVim

To get Macvim installed we again simply use Homebrew, we add a little something to homebrew, to learn a new sub command.

We want to search the homebrew library to see if MacVim is available to install.

brew search macvim

And we’re getting:

$ brew search macvim
macvim

Which means that it’s available, and ready for installation

brew install macvim

You can now start MacVim from your terminal with

mvim FILE

Named

named is a domain name daemon, which can act as a local dns server.

We need to set up named (bind), to handle wildcard DNS for our development TLF (top level domain) and using OpenDNS as secondary DNS lookup for all other requests.

Everything is set up in your terminal as root user.

sudo -i

To be sure, backup the original configuration

cp /etc/named.conf /etc/named.conf.orig

Create a keyfile which is read by named on startup.

rndc-confgen -a

Edit the named.conf file

vim /etc/named.conf

Somewhere inside options {} block, add OpenDNS as DNS forwarder.

forwarders {
    208.67.222.222 // OpenDNS primary
    208.67.220.220 // OpenDNS secondary
}

Add the following before the “0.0.127.in-addr.arpa” IN { zone

zone "dev" IN {
   type master;
   file "dev.zone";
};

Save your named.conf file [esc]:wq

Now we need to create the dev.zone file, so bind (named) could read the dns configuration for that tld.

vim /var/named/dev.zone

And paste this block into that file.

dev. 7200    IN       SOA     dev. root.dev. (
              2008031801 ;    Serial
              15      ; Refresh every 15 minutes
              3600    ; Retry every hour
              3000000 ; Expire after a month+
              86400 ) ; Minimum ttl of 1 day
              IN      NS      dev.
              IN      MX      10 dev.

              IN      A       127.0.0.1
*.dev.        IN      A       127.0.0.1

Save the file, agagin, with [esc]:wq

Run the following commands to ensure configuration is ok

named-checkconf /etc/named.conf

And

named-checkzone dev /var/named/dev.zone

Now set your computer dns to use localhost as primary dns handler, in System Preferences -> Network for both Wireless and Ethernet connections by clicking Advanced and selecting the DNS tab, and add 127.0.0.1.

Set Bind (named) to load when computer starts, and load it now.

launchctl load -w /System/Library/LaunchDaemons/org.isc.named.plist

Let’s flush the internal DNS cache first,

sudo dscacheutil -flushcache

Lets test that everything is working as planned.

dig test.dev

Should return something like

$ dig test.dev

; <<>> DiG 9.7.3-P3 <<>> test.dev
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 4384
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 1, ADDITIONAL: 1

;; QUESTION SECTION:
;test.dev.          IN  A

;; ANSWER SECTION:
test.dev.       7200    IN  A   127.0.0.1

;; AUTHORITY SECTION:
dev.            7200    IN  NS  dev.

;; ADDITIONAL SECTION:
dev.            7200    IN  A   127.0.0.1

;; Query time: 2 msec
;; SERVER: 127.0.0.1#53(127.0.0.1)
;; WHEN: Sun Dec 18 10:51:34 2011
;; MSG SIZE  rcvd: 72

And we can see it’s resolved as a local domain. All ok.

And a ping test, should return

$ ping test.dev                                                                                                  

PING test.dev (127.0.0.1): 56 data bytes
64 bytes from 127.0.0.1: icmp_seq=0 ttl=64 time=0.041 ms
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.110 ms

Nginx

Nginx is an awesome alternative to Apache Web Server, more lightweight, very customizable, and thats for good use with this setup.

This Nginx vhost configuration is mostly for Drupal installations, but works with other frameworks and homemade stuff, almost out-of-the-box, but changes to the vhost.conf file, depending on configration in your .htaccess file used in Apache, can appear.

First of all we need to ensure that Apache wont be started when booting.

sudo launchctl unload -w /System/Library/LaunchDaemons/org.apache.httpd.plist

Now we need nginx to be installed via Homebrew.

brew install nginx

When nginx is compiled, we need to backup the original nginx.conf file.

cp /usr/local/etc/nginx/nginx.conf /usr/local/etc/nginx/nginx.conf.orig

Open and configure your nginx.conf file to your needs.

vim /usr/local/etc/nginx/nginx.conf

Look for user in the file, and make it look like

user YOUR_NAME staff;

Save the file [esc]:wq

Create the nginx log directory, to make it show up in Console.app

sudo mkdir /var/log/nginx

Configure nginx for wildcard local domains.

Edit your nginx.conf

vim /usr/local/etc/nginx/nginx.conf

And add the following line in the http {} section just before the closing }

include /usr/local/etc/nginx/vhosts.conf

Save and exit the file [esc]:wq

Create the vhosts.conf file, in this example it’s confiugred for drupal development, but will work for the most.

vim /usr/local/etc/nginx/vhosts.conf

And paste the following into the file.

server {
    listen 80 default;
    server_name _;
    index  index.php;

    if ($host ~* "^(.+)+.dev$") {
        set $site $1;
    }

    root   /Users/[USERNAME]/www/$site;

    access_log  /var/log/nginx/$host.access.log combined;
    error_log   /var/log/nginx/$host.error.log crit;

    add_header X-Frame-Options SAMEORIGIN;

    location = /favicon.ico {
        log_not_found off;
        access_log off;
    }

    location /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
    }

    location = /backup {
        deny all;
    }

    location ~ \..*/.*\.php$ {
        return 403;
    }

    location / {
        try_files $uri $uri/ @rewrite;
    }

    location @rewrite {
        rewrite ^/(.*)$ /index.php?q=$1;
    }

    location ~ \.php$ {
        fastcgi_read_timeout 600;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_pass   127.0.0.1:9000;
    }

    # Fighting with ImageCache? This little gem is amazing.
    location ~ ^/sites/.*/files/styles/ {
        try_files $uri $uri/ @rewrite;
    }
}

Create a www folder in your homedir

mkdir ~/www

Restart your nginx

sudo nginx -s reload

And now you should be able to create new local sites without configurering and restarting nginx everytime.

PHP 5.3.8

The version of PHP in OSX Lion is upgraded to 5.3.6, but we will install PHP 5.3.8 with Homebrew, and with that it would be upgradeable, when new and awesome releases appear.

Now for the PHP 5.3.6 -> 5.3.8 upgrade. We will start by backing up the originally installed php.

sudo mv /usr/bin/php /usr/bin/php.orig

Run homebrew installer with the php brew file from the alternative brew directory.

brew install /usr/local/LibraryAlt/duplicates/php.rb --with-mysql --with-fpm

When php is compiled and ready, create your own php-fpm.conf file.

cp /usr/local/Cellar/php/5.3.8/etc/php-fpm.conf.default /usr/local/Cellar/php/5.3.8/etc/php-fpm.conf

Create symbolic link for it in /usr/local/etc/

sudo ln -s /usr/local/Cellar/php/5.3.8/etc/php-fpm.conf /usr/local/etc/php-fpm.conf

Edit the file

vim /usr/local/etc/php-fpm.conf

Add the following line below ;pid = run/php-fpm.pid

pid = /usr/local/var/run/php-fpm.pid

Update the user and group section as follows

user = _www
group = _www

Remove the ; from the start of the following lines then save using Ctrl+X then Y

pm.start_servers = 3
pm.min_spare_servers = 3
pm.max_spare_servers = 5
pm.max_requests = 500

Create directory for php-fpm log

mkdir /usr/local/Cellar/php/5.3.8/var/log/

and the file

touch /usr/local/Cellar/php/5.3.8/var/log/php-fpm.log

Make our log file visible in Console app

sudo ln -s /usr/local/Cellar/php/5.3.8/var/log/php-fpm.log /var/log/nginx/php-fpm.log

Set your timezone in php.ini

vim /usr/local/etc/php.ini

Look for ;date.timezone, and add following below

date.timezone = Europe/Copenhagen

And update the memory_limit

memory_limit = 256M

Save the file [esc]:wq

MariaDB

MariaDB is an alternative to MySQL, it’s created and maintained by some original authors of MySQL.

Install MariaDB from terminal and with homebrew

brew install mariadb

After installation is done, unset TMPDIR

unset TPMDIR

Then install database

mysql_install_db

Make all of the services (nginx, php-fpm and MariaDB run on boot)

Start iterm2 and become root.

sudo -i

Download the LaunchDaemon to load nginx on boot

curl http://realityloop.com/sites/realityloop.com/files/uploads/nginx.plist_.txt > /System/Library/LaunchDaemons/org.homebrew.nginx.plist

Download LaunchDaemon for php-fpm

curl http://realityloop.com/sites/realityloop.com/files/uploads/php-fpm.plist_.txt > /System/Library/LaunchDaemons/org.homebrew.php-fpm.plist

Copy the LaunchDaemon to load MariaDB on boot into place

cp /usr/local/Cellar/mariadb/5.2.8/com.mysql.mysqld.plist /System/Library/LaunchDaemons/com.mysql.mysqld.plist

Now it’s time to reboot your computer, to make sure everything is working.

After reboot, open iterm and run this command.

sudo /usr/local/Cellar/mariadb/5.2.8/bin/mysql_secure_installation

Answer the prompts as follows, replace [password] with a password of your own chosing

Enter current password for root (enter for none): [Enter]
Set root password? [Y/n] y
New password: [password]
Re-enter new password: [password]
Remove anonymous users? [Y/n] y
Disallow root login remotely? [Y/n] y
Remove test database and access to it? [Y/n] y
Reload privilege tables now? [Y/n] y

Real life example

Now for an example by installing a drupal site with a few tools.

Download drush which is a command line tool for Drupal, this will install drush_make as well.

brew install drush

Go to ~/www

cd ~/www

And download Drupal core.

drush dl drupal --drupal-project-rename=drupal7

Open your favorite browser and go to

http://drupal7.dev

You should see the installation page of Drupal. We could install drupal with a oneliner.

cd ~/www/drupal7

And we use drush to install with default settings.

drush site-install --db-url=mysql://root:PASSWORD@localhost/drupal7

And if you visit

http://drupal7.dev

you will see a default installed drupal site, and you will be able to login with user: admin, pass: admin.

Another site

We’ll just try another site

cd ~/www/

Install a drupal 6.x site

drush dl drupal-6.x --drupal-project-rename=drupal6

And again use drush site-install to install the site.

drush site-install --db-url=mysql://root:PASSWORD@localhost/drupal6

And visit

http://drupal6.dev

You will see an installed drupal 6 site.

** And everything without reloading Nginx ** - MAGIC

Linux note

As a little note, it would quite easy install on a unix based platform as well, skip the homebrew parts and install through your preferred install util (apt-get or whatever).

The paths would in most cases deviate a bit from the ones on OSX.

I just tested it on a Ubuntu 10.04, and took me around 10 mins to install from this guide.

Following