Setting Vagrant with Yii PHP

vagrant

As everyone is talking about start using Vagrant, I’m writing this post about how you can use it for Yii and customize it for other projects written in PHP.

I’m not going to tell you why are we using Vagrant, bit far point less, if some of you is interested why we need it than please look at their offical site,
Here we just want a standard development environment every team developer can use and work with it.

Have a look at what we are going to do today! it will just take few minutes 🙂

1. Installing Vagrant
2. Installing VirtualBox
3. Setting up Vagrant with my favorite box
4. Provisioning with Vagrant
5. Setting up Apache, PHP 5.X, Mysql 5.X and othe things

last but not least we need a working copy of yii framework, where you are developing your app!

1. Installing Vagrant
I’m using Ubuntu 12, I’m not going to run through how to do this in every operating system because it is out of the scope of this tutorial.
So the first thing to do is to head over to the downloads page of Vagrant and install the version fits for your operating system.
Once fully installed, the command vagrant is now available to use in your favorite terminal.

2. Installing VirtualBox
Another requirement to use Vagrant is that you have a Virtual Machine provider. This is basically just software that runs the virtual machine. I’m going to be using VirtualBox because it’s free and easy to set up. After installation you don’t need to check as much. Lets proceed!

3. Setting up Vagrant with my favorite box
Now the fun part has arrived! Vagrant is basically just a configuration file (Vagrantfile) that tells the VirtualMachine what to set up and in the end what to run. The Vagrantfile should be kept in the root of your project and should be added to Git so that anyone else who is working on the project can use the same configuration file.
Lets open our favorite terminal and move to the working directory where your project is setup.

cd /var/www/project/stowitt/front-panel/

now do:

vagrant init

Now if you open your project in a text editor, you will see the new Vagrantfile into your project.

Next thing is your need is box. When you are setting up a Virtual Machine, you need the operating system and all of that junk before you even begin. Operating system is the Vagrant box here, Vagrant provides base images of those operating system.
To download an Ubuntu box, run the following command:

vagrant box add precise32 http://files.vagrantup.com/precise32.box

Once done add the following lines to your Vagrantfile:

config.vm.box = "base"
config.vm.box = "precise32"

Now it knows which box to use and your machine is just ready?? wanna try! run the command in terminal:

vagrant up

You should see a load of output to the terminal. Vagrant doesn’t actually have a user interface so you will see only the output of every command. Now to see what is going on you can do SSH, similar to your production environment.
While it’s up do:

vagrant ssh

You are not into the virtual machine! You can do anything you want but lets type “exit” and return for now.

After ended up working on machine, we can shut it down by using the following command:

vagrant destroy

Now lets do this back:

$ vagrant up
$ vagrant ssh
$ cd /vagrant
$ ls -l

You should see a list of all the files from your project. Vagrant will automatically sync all of the files from the root of the project. Pretty cool!

4. Provisioning with Vagrant

If you have ever set up a fresh installation of Ubuntu, you will know that you have to install a load of things before you are good to go. Now that you can SSH into your machine, you can happily go about installing whatever you want to set up the machine exactly how you want it.

To set up provisioning, all we have to do is to create a shell script that will automatically be loaded when we boot up Vagrant.

Create a new file called bootstrap.sh in the root of your project (the same location where you saved Vagrantfile).

Next we need to tell Vagrant to run this file when it starts up. Open your Vagrantfile and add the following line under where you set the box:

config.vm.box = "precise32"
config.vm.provision :shell, :path => "bootstrap.sh"

Now that we have provisioning set up, we can start adding a list of instructions for what should be provisioned whenever Vagrant is set up.

5. Setting up Apache, PHP 5.X, Mysql 5.X and othe things

Now we are ready to install anything we want on our virtual development machine. To be specific about PHP and specially Yii we are going to install these items one by one.

Open up your boostrap.sh file and add the following:

#!/usr/bin/env bash

Now lets update Ubuntu:

apt-get update

You need to add these lines in bootstrap.sh as we go!!!

Install Vim:

apt-get install -y vim

Set up Apache:

apt-get install -y apache2
# Remove /var/www default
rm -rf /var/www
# Symlink /vagrant to /var/www
ln -fs /vagrant /var/www
# Add ServerName to httpd.conf
echo "ServerName localhost" > /etc/apache2/httpd.conf
# Setup hosts file
VHOST=$(cat <<EOF
<VirtualHost *:80>
  DocumentRoot "/vagrant/public"
  ServerName localhost
  <Directory "/vagrant/public">
    AllowOverride All
  </Directory>
</VirtualHost>
EOF
)
echo "${VHOST}" > /etc/apache2/sites-enabled/000-default
# Enable mod_rewrite
a2enmod rewrite
# Restart apache
service apache2 restart

Install PHP 5.X:

apt-get install -y libapache2-mod-php5
apt-get install -y python-software-properties
add-apt-repository ppa:ondrej/php5
apt-get update

Set up other PHP stuff:

# Need sudo to run the following, adding root password as "Crazy007Fox"
sudo passwd
Crazy007Fox
sudo
Crazy007Fox
apt-get install -y php5-cli
apt-get install -y php5-mysql
apt-get install -y php5-curl
apt-get install -y php5-mcrypt

Install cURL:

apt-get install -y curl

Install MySQL:

export DEBIAN_FRONTEND=noninteractive
apt-get -q -y install mysql-server-5.5

Install Git:

apt-get install git-core

Set up Yii:

# setup database:
echo "CREATE DATABASE IF NOT EXISTS yii_app" | mysql
echo "CREATE USER 'root'@'localhost' IDENTIFIED BY ''" | mysql
echo "GRANT ALL PRIVILEGES ON yii_app.* TO 'yii_app'@'localhost' IDENTIFIED BY ''" | mysql
# run migration
cd /var/www/
yiic migrate
# give write permission to some directories
sudo chmod -R 777 /var/www/assets/
sudo chmod -R 777 /var/www/protected/runtime/

 

Final Run

Now everything got setup, but hey! how do I run the project from virtual machine. Well Vagrant makes this really easy with it’s port forwarding feature. This simply allows you to specify ports on the guest machine to share via a port on the host machine.

Add the following line to your Vagrantfile:

config.vm.network :forwarded_port, host: 8090, guest: 80

now start your machine with vagrant up
vagrant up

Once done, on your local machine try:

http://localhost:8090 in your browser to see your project.

Checkmate you are done with your virtual machine setup and your site is running 🙂

Advertisements

PHP IDEs compared!

I’ve talked with many PHP programmers, and one thing that surprises me most is how few use IDEs. Most use text editors, such as Notepad++, dreamweaver.

The text editors I mentioned (and others I didn’t) are great — I don’t want to start a pointless war over which editor is better. However, I have ranked them.

1. Codelobster PHP Edition

Codelobster PHP Edition streamlines and simplifies php development process. You don’t need to keep in mind names of functions, arguments, tags and their attributes; methods etc – we’ve implemented it for you in the autocomplete feature for PHP, HTML, JavaScript and even CSS. Also, you can always get necessary help information by F1 or using special Help control.

Internal free PHP Debugger allows validating code locally. It automatically detects your current server settings and configures corresponding files in order you can use the Debugger.

Supported CMS: Drupal CMS  Joomla CMS  WordPress Blogging Platform

Supported Frameworks: Symfony framework  CakePHP framework  CodeIgniter framework  FaceBook Social Network  Yii framework Smarty Template Engine

Supported JavaScript Libraries: JQuery library

2. PHP Storm

PhpStorm is a lightweight and smart PHP IDE focused on developer productivity that deeply understands your code, provides smart code completion, quick navigation and on-the-fly error checking. It is always ready to help you shape your code, run unit-tests or provide visual debugging.

PHP code completion
PHP refactoring
Smarty and PHPDoc support
Quick navigation
Language mixing (JS/SQL/XML etc.)

3. Net Beans

The NetBeans project offers a version of the IDE tailor-made for developing PHP web sites that comprise a variety of scripting and mark-up languages. The PHP editor is dynamically integrated with HTML, JavaScript and CSS editing features.

Focus on the code and speed up code scanning by excluding individual directories in the Project properties. The NetBeans IDE fully supports iterative development, so testing PHP projects follows the classic patterns familiar to web developers.

The Main features:

PHP Files Without Projects
Rename Refactoring and Instant Rename
PhpDocumentor Support
Zend and Symfony Framework
PHP Source Code Editor
Namespace and Variable Types
Easy Code Navigation
Code Coverage
PHP Unit Testing
Remote and Local Project Development
PHP Debugging
MySQL Integration

4. Eclips

Two plug-ins support PHP in the Eclipse development platform. The first, PHP IDE Project, is an Eclipse Foundation project, which means it is released under the Eclipse license and is developed using the Eclipse Foundation’s tools and processes.

The other is PHPEclipse and is developed independently. As with Eclipse, both run on the Big Three operating systems: Windows, Linux®, and Mac OS X. You can download just the plug-ins (if you already use Eclipse), or download a pre-fab version with everything you need.

Both plug-ins support core IDE features you would expect to find. The code intelligence is rock-solid, pops up when you want it, and displays all the information you need for classes, methods, and arguments.

Figure 1 shows PHPEclipse running on Mac OS X. On the left side is the project view with the files in the project. Below that is the class view, which shows any classes I’ve defined. In the center is my code. I can have multiple files open in multiple tabs simultaneously. On the right side are panels for debugging and browsing. This is the stock PHPEclipse user interface.

5. Komodo

Next up is ActiveState’s Komodo IDE. This IDE runs on Windows, Mac OS X, and Linux, and it supports the usual open source language suspects — Perl, PHP, and Ruby. The code intelligence engine is solid. It scans all your language installations to find custom extensions, such as PEAR modules. On the project side, it supports integration with CVS, Subversion, and Perforce, as well as allowing for direct FTP transfer of code to the server.

Figure 3 shows Komodo running on Windows. A class view is on the left, and the project view is on the right. Dominating the center is the tabbed code view. To the bottom are the breakpoints for the debugger, the command output, and so on. As with all these systems, you can significantly customize the UI to match your preferences.

6. PHP Designer

PHP Designer takes a different tack from the other IDEs. Sure, it supports limited code intelligence. However, its focus is on further enabling the design aspect of the PHP Web application. This is evidenced by its integrated browser being adorned with pixel rulers to help in positioning elements on the page.

  • PHP code completion
  • PHP refactoring
  • Smarty and PHPDoc support
  • Quick navigation
  • Language mixing (JS/SQL/XML etc.)