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,