PHP Web Applications Development — A Starter Tutorial

December 18, 2007 on 2:16 am | In General |

This is a beginner’s guide to setting up an environment to start doing web applications development in PHP. If you are anything past the beginner level, this article may not be any beneficial to you at all; so you may stop reading here.

A friend of mine has recently requested me to write a starter tutorial for someone (presumably, who already has some programming experience and web site development experience, as he does) to start doing web applications development, particularly in PHP. I thought, hey, maybe I should just write it up here, since I haven’t written anything here for a while.

In this tutorial, I will cover some simple steps on how to setup a web server (Apache), PHP, and a database (MySQL) on your machine. In particular, I will assume the use of the Windows machine. Why? Honestly — if you’re operating Linux already, shouldn’t you know enough to be able to setup all that on your own? ;) I will also be writing some of my preferred methods of managing the server.

Getting a Text Editor

Before I get into anything, let me first talk about text editors. Throughout this tutorial, you will find several instances where you need to be editting configuration files. I recommend a text editor called TextPad. This editor deserves a lot more fame than it has. It is best text editor I’ve ever used. It’s not only good for normal editting, I even use it to code a lot of my PHP web applications. It has an unlimited free trial so if you are poor, this is the way to go. You can also always pay $32 to get rid of the nagging box that asks you to buy it.

Installing Apache

For PHP web applications in particular, the Apache HTTP server is the server of choice. For Windows users who just need to install the web server, unless you need the source code of it for any reason, you can simply download the binary. This Apache archive contains what you need. Download the .msi file of the latest version (2.2.6 at the point of this writing). Apache insists you must also check your downloaded file against the .md5 code for security. (If you are just downloading it for personal use and testing on your local machine, I don’t think it’s necessary.) If you do want to be secure and run a md5 sum of your downloaded file, you can use this MD5summer (since WinXP doesn’t have any md5 tool).

Installation of Apache is fairly straightforward. On Windows, you have the option of running it as a permanent process (on port 80), or running it per user (on port 8080 by default for this option). For any normal web server, port 80 is what is used. When you type in a URL (say, http://www.agum.com/), port 80 is assumed. You can also specify a port, such as http://www.agum.com:8080/. That is what will happen when you run it per user. Typically, if you are just using this as a developer machine, you should probably just use the 8080 option. Most other options can be left at default for a new developer.

Configuring your web server

Once installed, before you run the web server, the first thing you will want to know is how to configure your web server correctly. This is done by editting the httpd.conf file. This file is located at C:\Program Files\Apache Software Foundation\Apache2.2\conf\httpd.conf, assuming you installed Apache using the default location.

There are many things going on here, but here are a few things of interest, for a beginner.

  1. Find the line “Listen 8080″. On my file it’s line #53, so it should be near there. You can change the port number of your web server here if you need to, to pretty much anything (unused). Leaving it at 8080 is fine.
  2. Find the line starting with “ServerName” (line #142 here). You can enter your IP address here, or you can enter a hostname if you have one. (places like Dyndns.org gives free hostnames you can use) If you are behind a router/firewall, and you are only going to be needing to test locally with no one else needing to access your server, it might be easiest just to use your LAN IP, or even 127.0.0.1, to save a lot of trouble with port forwarding on the router settings.
  3. Now the most important: the line with “DocumentRoot” (line #149 here). This is the location your files are served from. I have this on my test machine:
    DocumentRoot “C:/webserv”
    This means I have created a folder on C drive called “webserv”, and all my html files, image files, php files reside in there. After editting this line, you will also need to edit a line a little below it (line #177 here), it looks like this:
    <Directory “C:/webserv”>
    Obviously, you will want to use the same location for here.

That should cover everything you might need to edit in the configuration. Now, you can run your web server to test it out. If you followed through the typical Windows installation, you can run it from Start -> Program Files -> Apache HTTP Server -> Control Apache Server -> Start Apache in Console. What this does is it will run httpd.exe with the default options. Don’t close that DOS prompt when you run the server. When you close it, Apache closes.

You can test to see if you have everything correct by going to http://127.0.0.1:8080/. It should show whatever you have in C:/webserv (if you have the same setup as me). Put an index.html file in there to try it out.

Installing PHP

When your web server is configured, the next step is to install PHP. Download PHP first. Again, on Windows, you will just really need the Windows binaries. (you really won’t be needing to modify any PHP source, for a while..) You only need to download the one that says “PHP 5.2.5 installer [19,803Kb] - 15 November 2007″ (at the time of this writing, this is the latest version.). You don’t need the PECL extension for now.

While installing PHP, you will run into a screen that asks you what extensions you’d like to install with PHP. Do not be intimidated by this huge list. For a lot of web applications, you don’t even need most of these. The ones that I do think will be needed are: gd2 (for manipulating images), mbstring (for handling foreign languag), mcrypt (encryption), mysql and mysqli (needed for using MySQL). You can pretty much just follow my recommendations if you have no clue what you’re doing.

Configuring PHP

Once installed, the configuration file of interest is C:\Program Files\PHP\php.ini. It’s a very long file, but there aren’t much that you need to pay special attention to. Here are a couple of items you’d want to look for:

  1. Look for the line: (line #117 here)
    engine = On
    Make sure it says on. There shouldn’t be any reason it would be off.
  2. Shortly after that, find this line: (line #128 here)
    short_open_tag = On
    Make sure this one is on. This is actually important because many pre-built PHP scripts out there (if you are running any) use the short open tag.

There isn’t much else you need to change. You can now test your PHP. Run your web server again the same way you did before. Put a test.php file in your C:/webserv folder. Put some test lines in there, like <?php echo ‘Hello world.’; ?>. See if it runs correctly.

Running phpinfo()

A good way to test out everything in your installation is phpinfo(). It is a function provided by the PHP language. You can simply put this in a file:

<?php
phpinfo();
?>

And save it, then run it from your browser. You should see a very long page detailing all the configurations of your PHP setup and extensions.

Installing MySQL

You can download the MySQL 5.0 Windows binaries here. The “Windows ZIP/Setup.EXE (x86)” option is probably the easiest to install. The installation will prompt you for several things, but should be quite straightforward. You can simply answer them based on how your machine will be used. (as a developer machine, only use part of the memory/cpu for the database, etc.)

One interesting thing I’ve noticed is that, after running through the installation, there is no place that asks you to set your admin password. I always use the way on the MySQL reference manual to reset the password after the installation.

The configuration file is at C:\Program Files\MySQL\MySQL Server 5.0\my.ini. However, there really isn’t anything you need to change in there, so I won’t be going through it.

Once my MySQL database is installed, I personally find it more convenient to have a quick shortcut to start my database, and to stop my database. Here they are.

Starting MySQL:
“C:\Program Files\MySQL\MySQL Server 5.0\bin\mysqld.exe” –standalone

Stopping MySQL:
“C:\Program Files\MySQL\MySQL Server 5.0\bin\mysqladmin.exe” -u root –password=1234 shutdown
(where your password is 1234)

Make a shortcut containing those commands. Correct the paths if you need to. Then, whenever you need to start or stop the database, simply run these shortcuts.

To easily test out your MySQL database installation, you will need a PHP script that uses the database. The easiest way is to download phpMyAdmin.

Installing phpMyAdmin

The phpMyAdmin software basically lets you administer your database from a web interface. To clear out any confusion — if you are fluent with SQL commands, you can simply use the text-based MySQL client (mysql.exe) to do anything that you could do in phpMyAdmin. This software is only an interface, written in PHP, for you to do anything to your database easily from your web browser. Installing it is optional.

Download phpMyAdmin. (There is a Quick Downloads box near the top left) Once downloaded, simply unzip the package to your web server’s document root, which is C:/webserv in the examples I’ve been using above.

There are a few steps in setting up phpMyAdmin. At this point, I’m not sure if my friend needs to do this or not. I will continue this writing when I find out that he does. For now, I’d like to publish this article as soon as I could so I could help out.

No Comments yet »

RSS feed for comments on this post. TrackBack URI

Leave a comment

You must be logged in to post a comment.

Powered by WordPress with Pool theme design by Borja Fernandez. Entries and comments feeds. Valid XHTML and CSS. ^Top^