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
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:
The 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).
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:
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:
Then, add the VirtualHost section:
<VirtualHost *:80> ServerAdmin firstname.lastname@example.org DocumentRoot "/path/to/your/project" ServerName myfakedomain.local </VirtualHost>
Now, just restart Apache and try http://myfakedomain.local in your browser.
We have our server running, and now let’s get some extras.
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).
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 "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 2
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-2 sudo apt-get install sublime-text
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.