Setting up a complete PHP development environment on ubuntu 12.04+

Written by on Thursday March 28, 2013 - Permalink -
ubuntu Last week, I had do reinstall my laptop and setup the complete php development environment. This includes the apache2 web server, php5, mysql, phpmyadmin, git, and a good php editor. Thought that this setup could be useful for other php developers who want to use Ubuntu, so I'm sharing a step by step guide here :) Before going any further, lets get something clear: this is for local / development environments, not for production servers! :)

First things first

Do you already have Ubuntu installed, right? I will not give advice on this, but you may find everything you need do install Ubuntu here: http://www.ubuntu.com/download I'm currently using the 12.04, the LTS (long-term support) version. I downgraded from 12.10 after getting a lot of weird problems with my audio, and It solved the problem. This instructions will work fine for other Ubuntu recent versions, though. I don't like Unity, so I installed Gnome 3 which is amazing for me. Of course everybody has its own tastes, so feel free to use whatever you want as a desktop environment. But if you want to try Gnome 3, installing it is a piece of cake:
sudo add-apt-repository ppa:gnome3-team/gnome3
sudo apt-get update
sudo apt-get install gnome-shell

Web Server

Update (2013/09/03) - To install PHP 5.4, add the ondrej ppa repository first :
sudo add-apt-repository ppa:ondrej/php5-oldstable
sudo apt-get update
And if you want to install PHP 5.5, use ppa:ondrej/php5 instead (recommended) . This 3 commands will install the local server, in this order: Apache 2, MySQL*, PHP 5.
sudo apt-get install apache2
sudo apt-get install mysql-server php5-mysql
sudo apt-get install php5 libapache2-mod-php5
Ps* When installing MySQL, you will be prompted to provide a password for the root mysql account. Now your webserver shall be installed and running. Point your browser to http://localhost and you will get this: Screenshot from 2013-03-27 20:46:33The Apache's Document Root, by default, is located at /var/www on Ubuntu. You can easily change this to a directory which best suits your needs. Just edit the file /etc/apache2/sites-enabled/000-default with an editor of your choice (just remember you need to use sudo). Change all "/var/www" to your desired path - there are 2 places (lines 4 and 9). Screenshot from 2013-03-27 22:57:56 You will probably want to have URL rewriting enabled, so I recommend you also edit line 11 and change "None" for "All" (AllowOverride All). After doing this, you need to restart Apache for your changes take effect.
sudo apache2ctl restart
Now, if you reload http://localhost you will get your new document root, which can be a directory structure (easy to navigate between different projects without any extra effort, but may be a problem to some project implementations). You can also create a VirtualHost for a specific project, so you can access it with a url like this: http://myfakedomain.local. This scenario makes your environment more similar to what you have at production servers, and it's not hard to set up.

Setting up an Apache VirtualHost

You just need to edit 2 files. First, create an entry inside your /etc/hosts file (use sudo to open the file otherwise you cannot save it). Add your desired fake local domain name, like this:
127.0.0.1        myfakedomain.local
Now, edit the same file you opened before - /etc/apache2/sites-enabled/000-default . You need to add a VirtualHost entry. As we are going to use name-based virtualhost, we also need to add this at the top of the file:
NameVirtualHost *:80
Then, add the VirtualHost section:
 ServerAdmin your@email.com
 DocumentRoot "/path/to/your/project"
 ServerName myfakedomain.local
Now, just restart Apache and try http://myfakedomain.local in your browser. We have our server running, and now let's get some extras.

PhpMyAdmin

Installing:
sudo apt-get install phpmyadmin
You will be prompted to choose the web server (apache) and to provide the mysql root password. After  this, PhpMyAdmin will be available in http://localhost/phpmyadmin , and you will be able to access it directly (without providing password).

GIT

Installing is quite straightforward:
sudo apt-get install git
Now configure your git user:
git config --global user.name "Your Name Here"
git config --global user.email "your_email@example.com
And run this to have a colored git (like code highlighting - way better to read).
git config --global color.ui true

Sublime Text 3

Sublime is probably the best complete (and lightweight... and awesome) editor available for free nowadays. They gentle ask that you buy a license, but there aren't any restrictions or limitations in the version we download for free. Sometimes it will show a silent popup to remember you about the license stuff, and that's all.
sudo add-apt-repository ppa:webupd8team/sublime-text-3
sudo apt-get update
sudo apt-get install sublime-text

Shell Swag with Oh-my-zsh

If you want to power up your shell, I would recommend Oh my zsh. Its beautiful and very handy, with a lot of plugins to enhance your shell experience. I specially like the git repository prompt, which is enough to make me love oh-my-zsh. Screenshot from 2013-09-03 13:25:55 I followed the steps described here, its quite easy. First, you need to have zsh and git-core packages as a requirement:
sudo apt-get install git-core
sudo apt-get install zsh
Then install oh-my-zsh and change your default shell:
wget https://github.com/robbyrussell/oh-my-zsh/raw/master/tools/install.sh -O - | zsh
chsh -s `which zsh`
You need to reboot so the changes take effect.

Did I forget something?

If you want to suggest something, please leave a comment! Keep in mind that this is a *very* generic starting point for a php development environment, really the basics for getting started.  
comments powered by Disqus