Set up local development environment


Please follow this guide to ready your computer for programming on Ubuntu Linux with Apache, PHP and PostgreSQL. Some tips:

Linux: Ubuntu 14.04 LTS Desktop

This is our desktop operating system. Use the guides here to install it on your computer.

When you have logged into your new Ubuntu desktop:

  1. Get the internet working
  2. Software updates will automatically start, let them download and install
  3. Install your preferred browser using Software Center.
  4. Run your browser from Search (the top left icon on the launcher bar), then drag it onto the launcher bar for easy access
  5. Get your email working since it will be needed for service accounts such as Dropbox, if you use that to back up your files during local development, and also for viewing system mails later on


This will be used to store our passwords.

  1. Install KeePass2 using Software Center
  2. Run KeePass2 from Search and follow setup instructions
  3. Choose "Use a master password" and store the file somewhere safe. Obviously, the master password should be both strong and easily memorable


This is the famous Linux command line and will be used to configure the computer further. It is already installed, but since we will be staring at this window a lot, let's make it look nicer:

  1. Run Terminal from Search, drag it onto launcher bar
  2. From the menu, choose Edit - Profile Preferences, then click the Colors tab, then under Built-in schemes, select Black on light yellow

Sublime Text

This is the text editor, it is used to edit text files in the normal environment outside the terminal. Most of our coding will be done with this program.

  1. Open the terminal and enter the following:
  2. sudo add-apt-repository ppa:webupd8team/sublime-text-3
  3. sudo apt-get update
  4. sudo apt-get install sublime-text
  5. Run Sublime Text from Search, drag it onto launcher bar
  6. From the menu, choose Preferences - Color Scheme - Lazy

File associations: later when we are working with text files with various endings (.php, .inc), it will be a pain to keep selecting Open With - Sublime Text when opening the file using the File Browser (Nautilus). To associate a file type with Sublime Text, right click on a text file of that type in Nautilus, then from the menu, choose Properties - Open with - Sublime Text - Set as default

Developer Tools

Now we move onto the developer tools. Most of this will be done in the terminal. Where I have written {username} below, I mean your Ubuntu username, for example if I have written "/home/{username}" and your Ubuntu username is "max", then you would write "/home/max".


This is used to edit files within the terminal. Unfortunately, the commands are quite bizarre, but you only need to know a few in order to get by.

  1. Run Terminal from Search
  2. sudo apt-get install vim

Important Vim commands in order to complete the tasks below:

Task Command
Open a file sudo vi /path/to/file
Navigate up and down the open file Up and down arrow keys, PageUp, PageDown.
Note that there is a percentage in the bottom right to tell you how far down you are in a large file. I will refer to this number below.
Navigate along lines Left and right arrow keys, Home, End
Move into edit mode in order to make changes SHIFT-i
Copy and paste text Use mouse right-click options. CTRL-C and CTRL-V don't work.
Exit edit mode ESC
Save the file :x then ENTER
Close file without saving :q! then ENTER


This is the web server.

  1. sudo apt-get install apache2
  2. In your browser, go to http://localhost, ensure you see a page saying "It works!"

Change the default file location and prevent server restart warning. Remember to use the Vim commands above.

  1. sudo vi /etc/apache2/sites-enabled/000-default.conf
  2. Find DocumentRoot and change its value from /var/www to /home/{username}
  3. Close and save
  4. sudo vi /etc/apache2/apache2.conf
  5. Find ServerRoot about 30% of the way down, and add a new line below it. On the new line, type ServerName localhost
  6. Nagivate down to about 80% and find <Directory /var/www/>. Replace this line with <Directory /home/{username}/>
  7. Close and save
  8. sudo apache2ctl restart

Test the changes

  1. In your text editor, make a new file, type test1 and save it as "index.html" in your home directory: /home/{username}/
  2. In your browser, go to http://localhost, now it should read the file above and just say "test1".


This is the language we will use to make our pages dynamic.

  1. sudo add-apt-repository ppa:ondrej/php5-oldstable
  2. sudo apt-get update
  3. sudo apt-get install php5

Use the default PHP development settings.

  1. sudo cp /usr/share/php5/php.ini-development /etc/php5/apache2/php.ini
  2. sudo cp /usr/share/php5/php.ini-development /etc/php5/cli/php.ini

Set the correct timezone for time functions.

  1. sudo vi /etc/php5/apache2/php.ini
  2. About 45% down, comment in the "date.timezone" line by removing the preceding semi-colon, then change the value to whichever timezone you are in according to this list. I change mine to Europe/Berlin
  3. sudo vi /etc/php5/cli/php.ini
  4. Same as point 2

Restart Apache, which also refreshes the PHP settings.

  1. sudo apache2ctl restart

Test that PHP code is now recognised:

  1. In your text editor, make a new file, type <?php phpinfo(); ?> and save it as "index.php" in your home directory: /home/{username}/
  2. In your browser, go to http://localhost/index.php, ensure you see a long page full of tables describing your PHP installation


PEAR is a set of classes that provides additional functionality to PHP, and we will use the Mail class to send emails, e.g. to email the user a new password.

We will be sending mail programmatically via a Gmail account so we are not dependent on the mail functionality of any specific machine. If you have a private Gmail account, I recommend NOT using it for this purpose. So please do the following:

  1. Create a new Gmail account which will be used a mail dispatcher. The email address should not include your name, and the password should be generated using KeePass2
  2. When we try to send mail programmatically, Google will detect the attempt and block it unless you change the account settings to allow this to be done. This is done in the Gmail screen by going into Settings - Security - Account permissions - Access for less secure apps - change to Enabled

Install PEAR and PEAR Mail.

  1. sudo apt-get install php-pear php-net-smtp
  2. sudo pear install mail
  3. Verify installation using pear list-files Mail. The last entry should be "php /usr/share/php/Mail.php"

At the time of writing, using the Mail class generates numerous PHP warnings because the library is not up to date. Make the following changes to update the library to prevent warnings.

First Mail.php:

  1. sudo vi /usr/share/php/Mail.php
  2. On line 74, change function &factory($driver, $params = array())
    to static function &factory($driver, $params = array())
  3. On line 253 change function $addresses = Mail_RFC822::parseAddressList($recipients, 'localhost', false);
    to $Mail_RFC822 = new Mail_RFC822();
    $addresses = $Mail_RFC822->parseAddressList($recipients, 'localhost', false);

Now PEAR.php:

  1. sudo vi /usr/share/php/PEAR.php
  2. On line 250 change function isError($data, $code = null)
    to static function isError($data, $code = null)
  3. On line 472 change function &raiseError($message = null,
    to static function &raiseError($message = null,


PostgreSQL is the database and pgAdmin is a front-end for the database showing tables, views etc.

  1. sudo apt-get install postgresql postgresql-contrib php5-pgsql pgadmin3

Change the default password for the superuser "postgres".

  1. Open KeePass2 and add a new entry for "local postgres". The username is "postgres". We will use the generated password in a moment.
  2. sudo -u postgres psql. The prompt will now look a bit different, but don't worry.
  3. \password postgres
  4. Copy the new password from Keepass2 and paste it, then again when prompted
  5. CTRL-d. The prompt should now return to normal.

Create a test database and table:

  1. Run pgAdmin from Search and drag it to the launcher bar
  2. Expand the Object browser on the left until you see "localhost", then dbl-click it
  3. Right click on Databases, choose "New Database..". Under Name type "testdb" then click OK
  4. Click on "testdb", then click the expand button next to it, then the expand button next to "Schemas", then the expand button next to "public"
  5. Right on Tables, choose "New Table.."
  6. Under Name, type "testtable", then click the "Columns" tab, then the Add button
  7. Under Name, type "testcolumn", from the Data type dropdown, select "text", then click OK
  8. Click the Constraints tab, then click Add. Under name enter "testpk"
  9. Click the Columns tab, from the Column dropdown select testcolumn, then click Add, then OK, then OK again

You should now have a test database with a test table with a single column. Now we enter some values:

  1. Right click on testtable, choose "View Data", then "View Top 100 Rows"
  2. Click in the cell, then enter "Jim", then click the asterisk
  3. In the new cell below, enter "Bob", then press TAB
  4. Close the View Data window

Test that the PHP connector to Postgres is working:

  1. In your text editor, make a new file, copy the following into it. Remember to change the password to your own postgres password:
    $db = pg_connect('host=localhost port=5432 dbname=testdb user=postgres password={password}');
    $qry = pg_query($db, "select * from testtable");
    $arr = pg_fetch_all($qry);
  2. Save the file as "index2.php" in your home directory: /home/{username}/
  3. In your browser, go to http://localhost/index2.php, ensure you see Jim and Bob

comments powered by Disqus