Setting up a PHP development environment


Before we begin talking about PHP, let us setup the environment we’ll be working in. What I mean by that is we’ll install all the necessary software that is required to run PHP and make it easier for us to develop PHP applications. So, what do we need to begin developing PHP applications?

  1. Operating System (obviously)
  2. Web Server
  3. PHP
  4. MySQL
  5. Text Editor or an IDE

I’m running Windows 7 on my laptop but i prefer (and would recommend) to use Linux for all your web development needs. There are many flavours of Linux available. You may install Ubuntu or Fedora, the two most popular desktop Linux. They both are different in terms that Ubuntu is a Debian based Linux while Fedora is based on Red Hat. If you don’t know what I’m talking about then here is an article to help you decide and understand which Linux should you be using. I am using CentOS 6.2, a Red Hat based Linux for following reasons:

  1. It is very stable!
  2. I hate GNOME3 (Fedora) and Unity (Ubuntu).
  3. Red Hat can be said to rule the server market.

Whichever flavour you wish to work in, download its latest ISO image. You can download the live cd image for Cent OS 6.2 at this link.

You may install a fresh copy of Linux. I would recommend you to install a Virtual Machine with the help of Virtual Box or VMWare Workstation. I am using Virtual Box for it’s free and very powerful. I have divided the whole process into following steps:

  1. Install Virtual Box (If you are already running Linux, you can skip steps 1, 2 & 3)
  2. Create a Virtual machine and install Cent OS (or your preferred version of Linux)
  3. Update Cent OS and install Virtual Box Guest Additions (to help Linux run smoothly on Virtual Box virtual machine)
  4. Install Apache Web Server, PHP and MySQL
  5. Configure Apache
  6. Install NetBeans IDE
  7. Setup automated backup of project files (optional)

Lets start with step 1.

Step 1. Install Virtual Box

Grab the latest copy of Virtual Box from https://www.virtualbox.org/wiki/Downloads and install it on your machine.

Step 2. Create a Virtual machine and install Cent OS

  1. Open Virtual Box and click on the new button.
  2. Go through the new virtual machine wizard and create a machine.
  3. Give it at least 15 GB of space and 512 MB RAM
  4. Under the Network section in Settings, go to Adapter 2 tab and check “Enable Network Adapter”. In the “Attached to” drop down select “Host-Only Adapter”.
  5. Run the Virtual Machine and choose the Linux ISO image you have downloaded.
  6. Complete the installation and boot into the Linux Desktop

Step 3. Update Cent OS and install Virtual Box Guest Additions

Right click on your desktop, open Terminal and run following commands:

Install RPMForge Repository:

Instructions can be found at Cent OS wiki

Update Cent OS:

su -
yum install -y gcc gcc+ dkms make
yum install -y kernel kernel-devel
yum update -y
reboot

Install Virtual Box Guest Additions:

  1. Go to the Devices menu of the Virtual Box and click on Install Guest Addtions. It’ll mount a Virtual Box disk image in Linux.
  2. Open terminal and run following commands:
cd /media/VBOXADDITIONS_4.1.12_77245
sh ./VBoxLinuxAdditions.run
reboot

Step 4. Install Apache Web Server, PHP and MySQL

Install Apache Web Server:

yum install -y httpd
chkconfig --level 345 httpd on

*httpd is the name of Apache’s service
*chkconfig command is used to add Apache service to system startup.

Install PHP & Mysql:

yum install -y php php-cli php-pear php-xml php-mysqli php-pdo php-pecl*
yum install -y mysql mysql-server

Once you have it all installed, make sure it is running properly.

Step 5. Configure Apache

By configuring Apache, we mean configuring Apache Virtual Hosts. Using Apache Virtual Hosts we can refer to and access the projects (lets say xyz.com) we are working on by typing http://www.xyz.com in the web browser on our local machine. If you are not aware what it means, you may  refer to online documentation.

Apache’s configuration file is stored at /etc/httpd/conf/httpd.conf however we will not touch this file. We’ll instead use /etc/httpd/conf.d/ directory to store all our config files. Apache loads all the *.conf files in this directory at start. I would still recommend you take a back up of your httpd.conf file, just in case.

Lets say we are working on http://www.xyz.com project. Before we can start using http://www.xyz.com for our project on local machine, we must give our local machine a fixed IP address. For that, go to System > Preferences > Network Connections.

If you have configured Adapter 2 as mentioned in Step 2, you’ll have two networks here, Auto eth0 & Auto eth1. Select Auto eth1 and click edit. Change its name to just eth1. Go to IPV4 Settings and assign it an IP of say 192.168.10.1 manually. Reconnect to this network to reflect IP address changes.

Let us now create a configuration file for our project, http://www.xyz.com.

Create a new config file in /etc/httpd/conf.d/

touch /etc/httpd/conf.d/xyz.com.conf

Edit xyz.com.conf

gedit /etc/httpd/conf.d/xyz.com.conf

In the text editor, enter following:

NameVirtualHost    *:80
ServerAdmin    admin@xyz.com
DocumentRoot    /var/www/xyz.com/public_html
ServerName    www.xyz.com
ErrorLog    /var/www/xyz.com/logs/error_log
CustomLog    /var/www/xyz.com/logs/access_log combined
<Directory    /var/www/xyz.com/public_html>
    Options Indexes  FollowSymLinks
    AllowOverride    None
    Order    allow,deny
    Allow    from  all
</Directory>
DirectoryIndex    index.html  index.php

Once you have edited the .conf file, you’ll have to update your machine’s /etc/hosts file so that your machine can resolve the address http://www.xyz.com to your project directory (as configured in your .conf file)

gedit /etc/hosts

Add following line to your hosts file:

127.0.0.1    www.xyz.com    xyz.com

Create Directories

Now we must create our project directories as mentioned in .conf file.

mkdir -p /var/www/xyz.com/{public_html,logs}
echo "<?php echo 'This is xyz.com' ?>" >> /var/www/xyz.com/public_html/index.php

File Permissions

Without right file permissions, we won’t be able to access our website from outside Linux, i.e over network or from the host operating system (Windows 7). Hence we must set the right permissions. For a safe website, public_html (public directory for our website) should have the permission of 711, every folder inside public_html directory should have the permission of 755 and every file should have a permission of 644.

chmod -R 711 /var/www/xyz.com/public_html
chmod -R 644 /var/www/xyz.com/public_html/index.php
umask 022

Restart Apache

service httpd restart

Now open http://www.xyz.com in your web browser and see the magic.

Access http://www.xyz.com from hosts OS (Windows 7)

To access http://www.xyz.com from your hosts operating system (i.e. outside Virtual machine), you’ll have to edit Windows’s hosts file. It can be found at /windows/system32/drivers/etc/hosts. You must be an administrator and must have write permissions to edit this file. Add following line to the hosts file:

192.168.10.1    www.xyz.com xyz.com

To be able to access http://www.xyz.com from Windows, you must add WWW as a trusted in your firewall setting. Go to System > Administration > Firewall, check WWW and apply changes.

Step 6. Install NetBeans IDE

NetBeans is one of the best code editors for PHP in Linux. You can download the latest copy from here.

And you are done! You now have a strong PHP development environment ready. You can add new projects by creating a new conf file in /etc/httpd/conf.d directory and adding a respective entry in the hosts file. You can access all your projects from windows. That gives you a great platform to check cross browser compatibility for all your projects.

Step 7. Setup automated backup of project files (optional)

I am the kind of person who likes to play. And that sometimes results in system crashes. So i prefer to have an automated backup process in place. I keep a copy of all my project files in Windows via shared folders over network. You can share your Windows folder with Virtual Box virtual machines without hassle. All you have to do is go to Settings > Shared Folders > Add new shared folder(Lets say you shared the folder BackUp). Make sure you have checked Auto Mount & Mount Permanently. Once done, go to your Linux virtual machine and run following commands.

Create a directory to mount the shared folder

mkdir /VBox_BackUp

Mount the shared folder

 mount -t vboxsh BackUp /VBox_BackUp
 

Setup crontab to automate backup task

crontab -e

This would open the crontab file where you can enter any command. This command will be executed at specified intervals. I generally run a copy command at an interval of 15 minutes to copy all my project files to the BackUp folder on Windows. Here is the command i have in my crontab file:

15 * * * * copy -rf -uva /var/www/* /VBox_BackUp >> /VBox_Backup/crontab_log

For more information on crontab, click here.

If you are not familiar with Linux or have any queries, you can reach me in person or post it in comments below.

Advertisements

2 thoughts on “Setting up a PHP development environment

  1. My partner and I would certainly like to say thanks to you for the following helpful content. One can’t imagine how long it required me to uncover your website and this handy. Looking forward to look at more articles like this. Have a wonderful day.

  2. Pingback: Enabling WebDAV On Fedora 16 | EssayBoard

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s