Ubuntu: Install Apache, PHP, MySQL, SASS, Bower and PhpStorm for local development

Whenever I need to set up a Linux machine or VM for local PHP development I either recall what I need to do from memory or cobble together what I need from the web, years ago it was very simple, Apache, MySQL and PHP, that is no longer the case. These days a simple setup could be Apache, MySQL, PHP, Git, Node, Bower, Ruby and SASS. Rather than have to recall from memory again I decided to document it when I installed Ubuntu on my laptop.

After the steps below the following will be installed and configured.

  • Apache, two sites, one for a Bootstrap HTML site, the other for a site using Zend Framework 1
  • MySQL
  • PHP
  • Git
  • Bower, via Node
  • SASS, via Ruby
  • And just for good measure PhpStorm

Apache

We need to install Apache and configure two websites, one will be a simple PHP website and the other will be include rewrites for Zend Framework 1. The sites are going to be created in /var/www/html but we will create symbolic links in our Documents folder that point to the directories.

Install Apache

$ sudo apt-get install apache2

Configure Apache

Create directories for sites and set permissions, replace site1 and site2 with whatever names you want to use for your sites, in my case dlayer and site.

$ sudo mkdir –p /var/www/html/site1
$ sudo mkdir –p /var/www/html/site2
$ sudo chown –R $USER:$USER /var/www/html/site1
$ sudo chown –R $USER:$USER /var/www/html/site2
$ sudo chown -R 755 /var/www/html

Create conf files for the new sites, as above replace site1 and site2 with whatever you used previously.

$ sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/site1.conf
$ sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/site2.conf

Edit the conf files

$ sudo gedit /etc/apache2/sites-available/site1.conf

You need to set ServerName and update DocumentRoot, in my case for site1 which is a Zend Framework site I have the below.

DocumentRoot /var/www/html/dlayer/public
ServerName dlayer.dev

Because this site is a Zend Framework base website we need to add in the rewrite rules, the below needs to be added, I add it just before the closing VirtualHost tag.

<Location />
   RewriteEngine On
   RewriteCond %{REQUEST_FILENAME} -s [OR]
   RewriteCond %{REQUEST_FILENAME} -l [OR]
   RewriteCond %{REQUEST_FILENAME} -d
   RewriteRule ^.*$ - [NC,L]
   RewriteRule ^.*$ /index.php [NC,L]
</Location>

Set the ServerName and update the DocumentRoot for the second site, my second site is a simple Bootstrap based site, the settings are as below.

$ sudo gedit /etc/apache2/sites-available/site1.conf
DocumentRoot /var/www/html/site
ServerName site.dev

We can now enable the sites and disable the default site

$ sudo a2dissite 000-default.conf
$ sudo a2ensite site1.conf
$ sudo a2ensite site2.conf

Enable the rewrite module and restart Apache

$ sudo a2enmod rewrite
$ sudo service apache2 restart

We can update our host files so we can reach the sites

$ sudo gedit /etc/hosts

Add the ServerNames you defined in the conf files, in my case I added the below

127.0.0.1    dlayer.dev
127.0.0.1    site.dev

Create the symbolic links to each of the sites, you can the point your IDE or editor at you projects folder rather than /var/www/html/…., replace site1 and site2 with whatever directory names you created in the first step and Site1 and Site2 with whatever you want to directory names to be in your projects directory.

$ sudo ln –s /var/www/html/site1 ~/Documents/Projects/Site1
$ sudo ln –s /var/www/html/site2 ~/Documents/Projects/Site2

MySQL

Install MySQL and MySQL Workbench, if you don’t want to use MySQL workbench simply doesn’t include it in the install command

$ sudo apt-get install mysql-server mysql-workbench

Follow all the prompts, make sure to set a password for the root user, if you have problems copying a password from your password manager into the password field just press return at the prompts setting the password to nothing and update the password afterwards following the below steps.

$ mysql –u root –p
$ use mysql;
$ UPDATE user SET Password=PASSWORD('PASSWORD you want to use') WHERE User='root';
$ flush privileges;
$ quit;

PHP

Install PHP, we are also going to include the GD library and the MySQL PDO driver.

$ sudo apt-get install php5 libapache2-mod-php5 php5-gd php5-mysql

Git

Install Git and set your name and email address

$ sudo apt-get install git
$ git config --global user.name "Your Name"
$ git config --global user.email "Your email"

If you need to generate a SSH key for GitHub check their website or execute the below.

$ ssh-keygen –t rsa –b 4096 –C "Your Email"
$ ssh-add ~/.ssh/id_rsa

To add the key to your account do the below

$ sudo apt-get install xclip
$ xclip -sel clip < ~/.ssh/id_rsa.pub
$ copy the contents of the clipboard to the relevant section in your GitHub profile settings

Bower

Install Bower

$ sudo apt-get install npm
$ sudo npm install –g bower
If the above doesn't work enter the below and try again
$ sudo ln –s /usr/bin/nodejs /usr/bin/node

SASS

Install SASS

$ sudo apt-get install ruby
$ gem install sass

PhpStorm

On my Linux machines I use PhpStorm, I have used PHPed on my Windows machines for the last 12 years but can see myself moving to PhpStorm on both platforms if only for consistency.

Download PhpStorm and unpack

$ tar –xvf [package name]

Move it install /opt and execute the shell script

$ sudo mv [Directory to move] /opt
$ cd /opt/[PhpStorm directory/bin
$ ./phpstorm.sh

You should now have a functional development server and IDE, checkout your sites into your Project directories and start coding,

Dlayer – Open Source

As of the 10th August Dlayer is now an Open Source project, I’ve been thinking through my options for a long time and it just made sense, below is an extract from the new Dlayer website with my two core reasons.

Development began in 2008, up until August 2015 Dlayer was a closed project that I worked on single handled, I took the decision to Open Source Dlayer for the reasons listed below:

I want Dlayer to succeed, to do that it needs to be out there, an app that never ships is no good to anyone.

I want to build up a community around Dlayer and now that I have a solid base in place I can let the community have a say in where the app goes next, I obviously have my own plans but would love to hear from others.

I’m still in the early stages of officially releasing Dlayer as an Open Source project, Dlayer was both an app and a website, I need to separate the two, I’m almost there with the new web site, once that is ready I can start on stripping the redundant parts out of Dlayer.

ZF1 Lib updates

I’ve updated my ZF1 lib site.

  • Added a simple pager view helper.
  • Updated the image resizer and image cropper classes including new examples.
  • Added a simple HTML table view helper.
  • Added a Bootstrap badge view helper.
  • Updated the Bootstrap nav view helper, one fix, one new feature.
  • Added a Bootstrap progress bar view helper.

GitHub profile and new sample site

I’ve been working on one of my public GitHub repos since Monday, I’ve still lots to do but there are a few worthwhile updates so I though I would mention them here.

  • I’ve renamed the repo, now much more obvious it is a collection of Zend framework 1 view helpers, action helpers and utility classes.
  • I’ve updated the design of the included sample site, now a simple Bootstrap based site.
  • I’ve added a Bootstrap glyphicon view helper
  • I’ve added a Bootstrap label view helper
  • I’ve added a Bootstrap nav component view helper
  • The sample site is available online at zf1.deanblackborough.com

There is still lots of work to do, I’m hoping to be done by Sunday but we will see.

  • The original view helper and utility classes haven’t been updated
  • I need to put in place a system to ensure I work on the site more often then twice a year
  • I’ve additional view helpers, action helpers and utility classes to add.
  • Need to create a ZF2 version of site.

Languishing repo

I’m going to take a short break from Dlayer on the 6th July to update my Zend repo on GitHub, I’m hoping it will be less than a week.

I will be adding a couple of extra utility classes, sorting out several issues with the current classes and adding a few bootstrap based view helpers.

A sub domain will be added to this site to host the example site.