How to ignore system files in PhpStorm and Git

One of my excellent team members showed me a neat little tip a few weeks ago on how to avoid pushing and pulling all those irritating little system and application generated hidden files in my Git repos.

Most of us here (I say “us” but I’ve hardly touched a line of code at the office!) use JetBrains awesome PhpStorm IDE these days, for the super quick text editor and wonderful integrations. So finding out there’s a nice little way to get rid of those pesky files once and for all, I was all too happy to hear!

Gitignore usage with PhpStorm and Git
Gitignore usage with PhpStorm and Git

Simply head over to gitignore.io, enter your operating system, preferred IDE and any tools you might you might use (Composer, Vagrant, etc.), and click the drop-down to “Download”.

Then copy that file to the root of your project, the same location you did “git init”.

This is not just for PhpStorm however, it’s a Git feature that I only found out about by using PhpStorm.

How to setup Mac OSX 10.9 Mavericks for Apache Multiple VirtualHosts

At the beginning of the month and after 8 years in the financial services sector, I started at a new company. Day one was a surprise and a refreshing change, everything was ready for me and worked! No red-tape, no policy documents to fill in, just a MacBook Pro sat ready and waiting for me to do whatever I liked with it. The new Mac was pre-installed with 10.9 so I thought I’d write down the process for either new Mac users/owners or anyone interested in setting up their local machine for development purposes.

Ideally for this you’ll already have your preferred stack installed. I personally do all this using Homebrew, I previously posted a guide to do this, “How to setup an AMP environment running Laravel And MariaDB“.

The process is easier done in Terminal, so familiarity with that is helpful but not essential here, I’ll try to add helpful tips as we go along. To edit files, my preferred choice is nano, but you may want to use vi or another GUI visual editor. So, pop open Terminal and let’s begin..

Editing the Apache configuration file

Firstly we need to allow the usage of the httpd-vhosts.conf file from the main Apache configuration file httpd.conf.

sudo nano /etc/apache2/httpd.conf

In nano, using the “Where Is” function ctrl+W, search for “httpd-vhosts.conf”, you should find this directly under “# Virtual hosts”. Simply remove the hash at the beginning of the line to uncomment the include and your entry should then look something like this:

# Virtual hosts
Include /private/etc/apache2/extra/httpd-vhosts.conf

Save this (ctrl+X, then Y), now we need to edit that virtual hosts file and define our own domains:

sudo nano /etc/apache2/extra/httpd-vhosts.conf

The default file comes with some basic information and a couple of dummy configs, but for the purpose of learning, we’ll remove all of this and create our own from scratch. Use ctrl+K (Cut Text) to remove the existing lines. Enter the following in order:

# Force Apache to use port 80
Listen 80

Firstly we make sure that Apache is using the correct port, the usual being port 80 but you can use anything else that is unreserved if you prefer.

# Use a wildcard to listen for requests on all IP addresses
NameVirtualHost *:80

We then set the virtual host requests to listen for anything using port 80, if a domain “www.mydomain.com” for instance, does not match one of the ServerName directives below, it will default to using the VirtualHost.

<VirtualHost *:80>
    ServerName lee-mcintosh.com
    ServerAlias www.lee-mcintosh.com
    DocumentRoot "/Users/username/Sites/lee-mcintosh.com"
    ErrorLog "/private/var/log/apache2/error_logs/lee-mcintosh.com"
    CustomLog "/private/var/log/apache2/access_logs/lee-mcintosh.com" common
    ServerAdmin admin@lee-mcintosh.com
    <Directory "/Users/username/Sites/lee-mcintosh.com">
        Options Indexes FollowSymLinks
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>

The main parameter in the above block is the “DocumentRoot”. This defines where your files can be found. As a new PhpStorm fan (previously a SublimeText lover!), I use the default /Sites folder to store my files, you can of course choose whatever you prefer, just make sure you update the “<Directory>” value too.

I also added in an alias for “www” to allow this full sub-domain to also reach the same files, and also some directory permissions, I won’t cover those here, but maybe in a future post.

Next we need to trick your machine into believing that the internet address used “lee-mcintosh.com” is actually on your local machine, to do this we simply update our hosts file:

sudo nano /etc/hosts

Add the domain or domains you’ve created above:

127.0.0.1 www.lee-mcintosh.com

Note that I’ve used the alias version instead of the base domain.

All that is left to do now is restart Apache:

sudo apachectl restart

In your browser, simply navigate to your chosen domain, in this example: http://www.lee-mcintosh.com

How to setup an AMP environment running Laravel And MariaDB locally

I personally find the nicest repo of Laravel to be Chris Borgia’s Radiate package. It nicely pulls together some of the best bits of the web including version 4 of the excellent PHP framework and all the basics you’ll need with tastiest front-end kit like HTML5 Boilerplate, Font Awesome, Sass, Twitter Bootstrap and simple register form.

Note: As you’ll notice with most of my posts, I assume you’re using a Mac. If not, buy one! You’ll also need sudo permissions

So, to get started, let’s prepare the environment…

You’ll need Xcode install, so if you haven’t got it, go grab it from the Apple Store, then install the command line tools:

$ xcode-select --install

If you don’t already have it, install Homebrew (I prefer to curl it)

$ ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)"
$ brew doctor

If you do, simply update

$ brew update

Then grab the tap where the dependancies are

$ brew tap homebrew/dupes
$ brew tap homebrew/versions
$ brew tap homebrew/homebrew-php

Brew up your preferred flavour of PHP (I’m going with 5.4 here as 5.5 isn’t stable apparently!)

$ brew install php54

Some extra libraries are required to get this to work…

$ brew install php54-mysqlnd_ms php54-memcached memcached
$ ln -sfv /usr/local/opt/php54/*.plist ~/Library/LaunchAgents
$ launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.php54.plist

Now that you have your own custom compiled version of PHP installed, we need to update OSX’s Apache configuration to use the new PHP module as opposed to the native OSX version.

$ brew info php54

Take note of the LoadModule line in the ‘Caveats’ section

$ nano /etc/apache2/httpd.conf

Search for ‘LoadModule php5_module’. Change to use the new php54 library by adding # on the old one and adding new one on the next line

#LoadModule php5_module libexec/apache2/libphp5.so
LoadModule php5_module /usr/local/opt/php54/libexec/apache2/libphp5.so

Save and close the file, then you need to install mcrypt

$ brew install mcrypt php54-mcrypt

Restart the Apache

$ sudo apachectl restart

Now create a new folder for your project (I use PhpStorm to set this up)

$ cd /Users/leemcintosh/Sites/
$ mkdir myproject

Now let’s configure Apache to run your website…

To make things easier where permissions are concerned, I would recommend running Apache as your local user. This will allow you to set your VirtualHost to point to your local project without encountering permission issues).

$ nano /etc/apache2/httpd.conf

Search for “User _www” and replace ‘_www’ with your Mac username (mine being “leemcintosh”.

Then search for “vhosts” and uncomment the line so it looks like this

# Virtual hosts
Include /private/etc/apache2/extra/httpd-vhosts.conf

VirtualHosts make it easier to run your projects locally and there are a number of ways to set up a VirtualHost environment in Apache, I’ll show you how to add multiple websites using the “sites-available” folder…

At the end of the file, add the following

Include /private/etc/apache2/other/*.conf
Include /private/etc/apache2/sites-enabled/*

Save the file

$ cd /etc/apache2/
$ mkdir sites-available

We’ll add “sites-enabled” shortly, once we’ve configured your new site first

$ cd sites-available

Create a new site in here using the name of your new project

$ nano dev.myproject.com

Now we need to enter our VirtualHost configuration information

NameVirtualHost *:80

<VirtualHost *:80>
    ServerName dev.myproject.com
    ServerAdmin webmaster@localhost
    DocumentRoot /Users/leemcintosh/Sites/myproject
    <Directory /Users/leemcintosh/Sites/myproject/>
          Options Indexes FollowSymLinks MultiViews
          AllowOverride None
          Order allow,deny
          allow from all
    </Directory>
    ErrorLog /var/log/apache2/myproject-com.error.log
    CustomLog /var/log/apache2/myproject-com.access.log combined
</VirtualHost>

Save the file (ctrl+x then Y), and now copy that to the “sites-enabled” folder, on Linux distributions you can use the a2ensite command, but on a Mac you have to do things manually using a symbolic link (symlink)…

$ ln -s sites-available/ sites-enabled

Now that we have our site configured, we must trick our browser into believing that the website is on the Internet, we do that by forcing an entry in our hosts file…

$ nano /etc/hosts

At the bottom of this file, add your new project

127.0.0.1     dev.myproject.com

Save the file, clear the dns cache and then restart Apache

$ dscacheutil -flushcache
$ apachectl restart

You should now be able to visit http://dev.myproject.com (obviously replace myproject and name with your own) and it will load the web application under /Users/leemcintosh/Sites/myproject/.

If you don’t see the website, check Apache

$ apachectl configtest

From that you should be able to see any errors that have occurred. Simply rectify and restart Apache. If problems still persist, either tail the logs as defined in the VirtualHost configuration, or contact me!

Download the Laravel framework and components…

Clone Radiate into your new project (you’ll need an ssh key mapped to github for this part)

$ cd /Users/leemcintosh/Sites
$ git clone git@github.com:cborgia/radiate.git myproject

Inside your project root (myproject/), install composer

$ composer install

Start Artisan server, and create a new key

$ php artisan serve
$ php artisan key:generate

Install MariaDB (MySQL variant)…

$ brew install mariadb

Start MySQL

$ mysql.server start

For additional MySQL configuration, check back for new posts! Have fun :)