Updated November 15, 2009 at 6:27 pm
Having written many tutorials on how to configure web servers, write small programs, and how to troubleshoot software, I will tell you that this type of tutorial--one where I explain how to configure something on your computer--is by far my least favorite and most-dreaded type of tutorial. Programming can be frustrating, but the added burden of getting your development environment setup is sometimes enough to make you want to abandon the project completely. The following process may not take much time at all, but if your computer has an abnormal configuration or if you make a minor mistake, you may seriously consider just giving up. While I cannot stop you from moving onto something else entirely, I encourage you to push on should something go awry. While programming as a whole does not get easier along the way, I will be able to guide you through the rest of this tutorial series without too many bumps in the road.
PHP can be used in many different environments; however, it is primarily used in conjunction with a web server. It excels in dynamically generating both page content and the formatting markup for websites. Since PHP is most commonly used on the web, we will be learning to program web applications.
One day I was talking to a graduate student in Computer Science who made a snarky comment along the lines of "People who suck at programming end up making websites for the rest of their lives." If I had given his opinion any credence, I would have wanted to crawl into a ball and die for having spent seven years devoting my life to web programming. I'm glad I didn't do that, and hopefully, if you read through this series in its entirety, you will also see how web development is not only useful and rewarding, but also very enjoyable.
You have two choices.
Since we will be studying PHP as it was made for the internet, you have two options on how you will develop your programs. The first method is the easiest to setup, but can be a really big pain once you want to start testing examples. The second will be my recommended configuration, but it could possibly make
you want to crawl up into a ball and die.
Your first option is to use an existing web host on the internet. Obviously the host will need to have PHP support. There are many hosts available, both free and paid services, and it really doesn't matter who you choose. Since I don't recommend this method for testing your code, and since writing a thorough step-by-step tutorial on web hosting would take me days, I'm just going to mention that this option exists. How you go about finding a host and sending your files to the hosting account will be left up to you. Many tutorials already exist on the internet that can explain the process, and in many cases the host will have documentation or will be able to help you get started. Keep in mind that, if you go this route, you will need to be able to upload files to your account, and, as previously stated, the host will need to have PHP support. Ideally you will want PHP 5 support, but you can accomplish most of the early parts of this series with PHP 4. Your host will also have to have support for MySQL databases. While this is not 100% necessary to learn PHP, it is highly recommended that the host provides you with at least one MySQL database. Without a database you will not be able to take advantage of our database examples.
The second option is to setup a test environment on your computer. Let's take a look at how this is accomplished.
Setting up the test environment
PHP is an interpreted language. This means that the PHP interpreter will read the PHP code and figure out what it means--every time the code is executed. Every time you go to a new page on Mowia, the interpreter will analyze Mowia's source code and figure out what to do. This makes it easy to test any changes to your code. Simply refresh the page and the new code is executed. But how do you tell the interpreter to look at your code?
PHP usually piggybacks on a web server such as Apache. A web server is simply the service that lets you access files on a remote or local server through the HTTP protocol. The web server can enforce permissions, authentication, and the passing of certain information between the client and server. Since this information isn't too important, let's just say a web server is the thing your computer communicates with when you access a website.
Since most of my experience is with the Apache web server, and since Apache is probably the most popular (keyword: probably) web server on the planet, we will be using it for our testing. By installing Apache, you will be connecting to your own computer through your web browser, just as if your local web server was on the other side of the world.
Installing on Windows
Who doesn't love Microsoft Windows? It is highly recommended that you install the following when you're logged in as an administrator on the system. If you're using Vista, it's also recommended to turn off User Account Control (UAC) while installing.
Let's get started by downloading and installing Apache. Here's the direct link to the Windows installer:
mirror.its.uidaho.edu . If this link doesn't work for you, or if you want to make sure you have the latest version, go here:
httpd.apache.org . The official documentation on how to install Apache for Windows can be found here:
httpd.apache.org . Run the installer once it's downloaded. The installer will ask you to fill out a few questions. For network domain and server name you can simply enter "localhost" without the quotes. The administrator email can be whatever you want. Since the server will run locally on your computer, a valid email is not necessary. Install the server for all users on port 80 with a typical installation. The install path can be the default path; however, keep a note of its location as we will have to make a few changes.
Once Apache is installed we need to make a few changes to its configuration file. The Apache configuration is normally found at < INSTALL PATH >/conf/httpd.conf. Navigate to the Apache install path as you selected during installation, then open the httpd.conf file in the "conf" directory. For this and all future code examples in this tutorial series, I recommend using Windows' built-in Notepad program. You can use whatever you want, but you may run into conflicts later if you're not using Notepad.
Once you open httpd.conf, search for the following lines and make the changes as noted:
1. DocumentRoot - The document root option is where the server will be pulling the files for testing. I prefer to create a new folder in the base directory of my hard drive and name it "server." For me the folder path would be C:\server. You can call it whatever you want and put it wherever is most convenient for you.
2. DirectoryIndex - The directory index is the default file name Apache will use when you navigate to a given directory. Normally the default is index.html or index.htm. Files closer to the front of the list will have precedence. For our testing, I recommend using the following configuration:
DirectoryIndex index.php index.html
Last, but not least, we have to tell Apache where our PHP installation will be located. You can install PHP wherever you like, but I recommend C:\php (or equivalent drive location). Add the following lines to the bottom of your httpd.conf file, replacing the C:\php with whatever destination you plan on installing PHP.
LoadModule php5_module "C:\php\php5apache2_2.dll"
AddType application/x-httpd-php .php
AcceptPathInfo on
PHPIniDir "C:\php"
When you're done, save the file and close.
Now let's install PHP itself. Head on over to
windows.php.net and grab the zip file under the category "VC6 x86 Thread Safe." Once you have the package downloaded, extract the contents to the PHP destination folder you specified in httpd.conf. If you used C:\php as I normally do, make sure there is a file called php5apache2_2.dll in the C:\php folder. If you extracted the PHP zip file correctly, everything should be set automatically. Keep in mind that if you're using a version of Apache that's later than 2.2, you would want the corresponding DLL. Make sure the DLL listed in httpd.conf matches both your version of Apache as well as the version of the file in your PHP directory.
Once you've confirmed everything is correct, copy and rename one of the php.ini-* files in your PHP folder to php.ini. I recommend copying php.ini-development to php.ini. Open up the new php.ini and confirm the following configuration:
error_reporting = 7
You will also want to make sure there is no semicolon before the following line:
extension=php_mysql.dll
In some versions of PHP, MySQL support is already built into PHP, so the extension line above may not appear in php.ini. If this is the case, the php.ini should note that MySQL is already installed. Simply search the file for "mysql" until you find either the notice or the extension.
Once both of these configurations are confirmed, we can restart Apache. To do this, navigate to your Apache installation folder then go to the "bin" folder and open ApacheMonitor.exe. This should turn on Apache Monitor. If the monitor does not open for you, it can usually be located in the taskbar by the clock. Double click the pink, white, and green icon to open the monitor. Restart Apache. If you set everything up correctly, Apache should restart without errors. The bottom of the monitor should list both the Apache version as well as the PHP version installed. If the PHP version is not listed then PHP did not install correctly. Confirm that you completed the above steps correctly. Since Apache is installed as a Windows service, you can also restart it through the services list in the Windows Control Panel.
If you continue to have problems, comment on this tutorial and someone should be able to help you correct any problems.
Once you have Apache up and running, it's time to install MySQL. MySQL is a database server that we will be using later in this series to store data. You can grab MySQL at:
dev.mysql.com . Select your appropriate version, either 32- or 64- bit, corresponding to the version of Windows you're using. If you have no idea what operating system you're on, select 32 bit. Run the installer and install with the default options. If a password is requested, make sure to write down your password for later use.
Depending on your settings, Windows may automatically hide the file extensions of some of your files, such as PHP files with the .php extension. You will want to make the extensions visible. To do this in, navigate to your document root folder, such as C:\server. Select Tools -> Folder Options. If you're in Vista, you may need to hit the Alt key to bring up the file menu. Once the folder options appear, select the "View" tab. There should be an option to "Hide extensions for known file types." Make sure this option is NOT checked off. If it is checked, uncheck it to view file extensions in the current folder and any subfolders. These steps and/or the titles may be a little different in non-Vista versions of Windows; however, the general process should be the same.
Once the installation is complete, continue to the section titled "Testing 1...2...3..." on page four.
Installing on Linux
Since each Linux distribution is a little different, we cannot provide precise instructions for all Linux configurations. To continue with this tutorial series you should have the complete LAMP configuration (Linux, Apache, MySQL, PHP). Apache, PHP, and MySQL are normally either pre-installed on Linux or they can be easily downloaded as a package or RPM bundle. If you need help, there are many tutorials available on the internet for your configuration. If you can't find an answer elsewhere, feel free to ask your questions in the comment section of this tutorial.
Installing on Mac OS X
Mac users have it relatively easy since our configuration is partially pre-installed on OS X. Before we enable and start Apache, we need to enable PHP. This can be accomplished by opening Apache's configuration file at /etc/apache2/httpd.conf. Update the configuration by removing the hash mark (#) from the following line.
#LoadModule php5_module libexec/apache2/libphp5.so
Now we need to confirm a few settings in the PHP configuration file. You should first copy the default php.ini file to /etc/php.ini. This can be accomplished with the following command, executed in the Terminal program:
sudo cp /etc/php.ini.default /etc/php.ini
Once the php.ini file is copied, open it and confirm the following configuration setting:
error_reporting = 7
Once this is confirmed, let's start Apache. Under System Preferences there is a Sharing panel that includes the Web Sharing service. Enable Web Sharing to load the Apache server. By default, you will be working out of the /Library/WebServer/Documents/ folder, however you can adjust this folder location in httpd.conf to one that is easier for you to access.
Next you have to install the database software that we will be using later in the series. To install MySQL, visit the download page at
dev.mysql.com and download and install your correct version. If you are having difficulty getting everything installed, please visit the official documentation available here:
dev.mysql.com or you can ask any questions in the comment section below.
Testing 1...2...3...
Whew! I don't know about you, but I'm tired. There are many different configuration options available across many platforms. The goal is to have Apache, PHP, and MySQL installed and running on your system. If you have any problems, please ask your questions in the comment section below. The first few times I setup this configuration I had one too many headaches. Just keep your eye on the prize.
From here on out, I will refer to all files relative to our working folder. For this first test, the working folder will be the document root as is set in httpd.conf. Make sure the document root is easy for you to access.
If you chose to test through a web hosting company, "localhost" in these examples will be replaced with the default hostname your host provided. That may be a domain name you selected or something else. For phpMyAdmin examples, you may already have this installed through the host, so the phpMyAdmin installation process may not be relevant to your setup. Also note that you may have to go through extra setups to create MySQL users and databases through your host. You should ask your hosting company if you do not know how to get started with creating MySQL users or databases, or if you are having trouble connecting to a database.
This test will be our first PHP code example. Create a file in your document root named test.php. If you're on Windows and using Notepad, when you want to save a file, go to File -> Save As. Make sure the "Save as type:" option is set to "All Files." If it is not, a ".txt" extension will be appended to the end of the PHP file name, so test.php would become test.php.txt. Unless you change the configurations in php.ini, all PHP files should end in .php. If they end in another extension then the code will not be interpreted.
The following code should be the only contents of test.php:
PHP INFO:
<?php
phpinfo();
?>
Save the file in your document root, open your browser, and go to
localhost . If everything is setup correctly, you should be presented with a page that lists all of the PHP configurations. If you're presented with the "PHP INFO:" text and nothing more, PHP is not installed correctly. If nothing is returned, then Apache is probably not turned on. If something other than the "PHP INFO:" or the configuration settings is returned, it's possible your document root is not set correctly. As always, if you're having problems, just let us know in the comments section and someone will respond.
Once the PHP settings appear, browse down the listing of installed modules and confirm that MySQL is installed. The configuration should include a list of all MySQL settings including the Client API version. If MySQL is not listed, you may have to restart Apache. If everything looks correct, we can continue onto our last part.
Setting Up phpMyAdmin
This final installation will confirm that everything is working correctly. If we can get this to work, you shouldn't have any trouble with the rest of the series. Do not continue onto tutorial three until phpMyAdmin is working.
phpMyAdmin is a tool that we will use to manage our MySQL databases. I recommend installing phpMyAdmin into a folder directly in your document root called "phpmyadmin." Remember, even though its folder name is irrelevant, since phpMyAdmin is a PHP application that needs to be interpreted by PHP, the phpMyAdmin source code files MUST be located somewhere in your document root. For me that would be C:\server\phpmyadmin.
The phpMyAdmin development team updates their program every so often, so these installation instructions may be different if you're using an older or newer version of the software. For this example I will be installing version 3.2.2, which can be downloaded here:
www.phpmyadmin.net . Download the program (zip for Windows) and extract the contents to your selected phpmyadmin folder. Make sure all of the PHP files can be found directly in that folder. You don't want to extract to DocumentRoot\phpmyadmin\phpMyAdmin-3.2.2-rc1-english\ or some equivalent.
Once you extract the files, direct your browser to the installation. If you installed directly into a folder named "phpmyadmin," you should go to
localhost and the system will request a username and password. The username, by default, is root. The password will either be blank, or it will be the password you entered when you installed MySQL. On some configurations it could also be your computer's root password.
You can continue if everything loaded correctly. If you have problems and have verified that you completed the above steps, ask any questions in the comments below. Once phpMyAdmin accepts your login and you're presented with the welcome screen, continue below.
Now we're going to test to see if we can setup a database. There should be an option under "MySQL xxx," where xxx is either "localhost" or the MySQL server hostname, to create a new database. Enter a database name, say "newdb" without the quotes, then click the "Create" button. The collation can be left alone. If a database is created, you can select "Drop" in the top right corner of the screen. You will be asked to confirm the deletion of the database. Confirm your choice.
Now you can optionally configure phpMyAdmin to load without asking you for a username and password. Rename the config.sample.inc.php file in your phpMyAdmin directory to config.inc.php. Open the file for editing.
A note for Windows users: While I recommend that you use Notepad for all file editing in this series, it is possible that some files, such as the phpMyAdmin configuration file, may appear unreadable in Notepad due to all of the text being on one or two lines. This is due to the file encoding and Notepad's inability to read the file correctly. If you ever experience this problem, simply open the file in Wordpad and copy its contents. Open the file again in Notepad, delete the file's compressed contents, then paste your copied text into the window. Save the file. This should overwrite any of the formatting errors, and then you should be able to view the file, without difficulty, in Notepad.
Once the file is open and readable, change the $cfg['Servers'][$i]['auth_type'] option to "config". Delete all of the options immediately under "Server Parameters," and replace them with the following settings:
$cfg['Servers'][$i]['user'] = 'root';
$cfg['Servers'][$i]['password'] = 'password';
$cfg['Servers'][$i]['host'] = 'localhost';
$cfg['Servers'][$i]['connect_type'] = 'tcp';
$cfg['Servers'][$i]['compress'] = false;
Update the user/password in the configuration to reflect the username and password combination you used to log into phpMyAdmin. Save the updated file, navigate to phpMyAdmin in your browser, and confirm that the page loads. If everything loads correctly, even after browser refresh, phpMyAdmin should be installed and you are you ready to begin learning the PHP programming language. Continue to the next tutorial.
If you are having problems or if you receive an error, make sure you copied the configuration settings exactly. An extra quote mark, or lack thereof, could cause the system to break. If your password contains the single quote ( ' ), you may have to change the configuration to read: $cfg['Servers'][$i]['password'] = "password"; . This should let you paste the password. If you are using double quotes around the password and you also have a double quote within the password, you will receive an error. You can get around this by adding a backslash ( \ ) before the double quotes that appear within the password. For example, if your password is f9k'fz"f!fb@, then your configuration setting would look like: $cfg['Servers'][$i]['password'] = " f9k'fz\"f!fb@"; .