Developing Locally






Developing Locally

Developing your construct on your local computer is the recommended way to learn, since you can interact with all of the components on your own machine and not risk causing problems on a production server. That way, if there are problems in the local environment, you can fix them immediately without exposing them to your site's visitors. Working with your files locally means that you don't have to FTP them to a server, you don't have to be connected to the Internet, and you know exactly what's installed since you did it yourself.

There are three components to install:

  • Apache

  • PHP

  • MySQL

You will install the programs in that order. All our examples will be from the installation perspective of a PC with Windows installed, with notes for the Macintosh.

Installing Apache

First, Apache needs to be installed and operational before PHP and MySQL can be installed, or else they won't work correctly. Plus, there wouldn't be any use for the coding application and database without the Apache web server. A web server delivers web pages, has an IP address, and might have a domain name. For example, if you enter http://www.oreilly.com/ in your browser, this sends a request to the server whose domain name is oreilly.com. The server fetches the page named index.html and sends it to your browser.

Any computer can be turned into a web server by installing server software and connecting the machine to the Internet, which is why you need to install Apache.

  1. Download the Apache 2.0.5 Win32 binary. It's downloadable from http://httpd.apache.or/download.cgi. The file that you save to your desktop is called apache2_0.55-win32-x86-no_ssl.msi.

    If you are on Mac OS X, you already have Apache installed. Open up System Preferences, select the Sharing panel, and click to activate Personal Web Sharing (which is actually Apache). Mac OS X 10.2, 10.3, and 10.4 all come with different versions of Apache, but each works perfectly fine.


  2. Install Apache using the Installation Wizard. Double-click the MSI installer file on your desktop, and you see the installer shown in Figure.

    The Installation Wizard prompts you for basic configuration

    The Installation Wizard walks you through the installation process.

  3. Accept the license terms by clicking the radio button shown in Figure, and then click Next.

    Apache license terms and conditions for use

  4. You'll see a "Read This First" box, as shown in Figure. Additionally, this window offers a number of excellent resources related to the web server. Click Next.

    Apache HTTP Server information

  5. In the blank dialog box shown in Figure, enter all pertinent network information.

    Blank Server Information dialog box

    A sample is provided in Figure. Then click Next.

    Sample server information

    Port 80 is the default HTTP port. In other words, when you request http://www.oreilly.com, you're implicitly requesting port 80. By accepting this port, your web requests can be made without specifying a nondefault port.


  6. In the next screen, shown in Figure, select the setup type. The Typical install will work for your purposes. Click Next.

    Selecting a setup type

  7. Accept the default installation directory, as shown in Figure. Click Next.

    Destination Folder dialog box for the Apache installation files

    The default installation directory, C:\Program Files\Apache Group\, is both standard and easy to find, especially when you need to make changes to your configuration.


  8. As Figure shows, it's time for the installation to begin. Click Install. The installer installs a variety of modules, and you will see some DOS windows appear and disappear. Click Finish when the installer is done.

    Ready to Install dialog box

  9. Test your installation by entering http://localhost/ in your browser's location field. The local host's IP address is 127.0.0.1.

  10. After entering the URL in your browser, the default Apache page displays, which is similar to the one shown in Figure. Notice the circled part of the window where it indicates that the installation was successful.

    Apache's default index page after installation

    Now that you can serve up web pages, you're ready to add PHP.

    Apache is installed along with Mac OS X on Macintoshes. To activate it, go to Sharing under System Preferences, and start Personal Web Sharing.


Installing PHP

Go to http://www.php.net/downloads.php to download the latest version of PHP; both binaries and source code can be found on this web site. You need to download the zip file rather than the Windows installer, because the Windows installer only configures the IIS web server.

Create a directory for PHP on your desktop. You don't need all the files provided in the archive, so unzip the PHP archive into another temporary location. (A temporary location could be on your desktop, which will be easier to find when you go looking for the files.)

  1. Copy the following files into C:\php: php.exe, php4ts.dll, and php2apache2.dll. The php2apache2.dll file is located in the sapi directory.

    Use php2apache.dll instead of php2apache2.dll if you are using Apache 1.3.x.


  2. Load PHP extensions. If you plan to load PHP extensions, you need to copy the files in the extensions directory (or the ext directory, for PHP 5.x) to C:\php, or to a subfolder in C:\phpany subfolderas long as the PHP.ini file is updated to reflect the appropriate directory.

    MySQL support is now integrated into PHP and doesn't need an extension .dll. However, PHP 5 supports MySQL as a separate module for download. Follow the instructions below to make sure that your installation of PHP works before you start experimenting with installing extensions.


  3. To configure PHP, copy php.ini-dist from the extracted files into your Windows directorytypically C:\winnt or C:\Windowsand rename it php.ini.

  4. Set up PHP to load Apache as a module. To configure Apache to load PHP as a module to parse your PHP scripts, use a text editor to open the Apache configuration file, httpd.conf, typically found in C:\Program Files\Apache Group\Apache2\conf \. You can use Notepad, as shown in Figure.

    Viewing httpd.conf in Notepad

  5. Load PHP as an Apache module. Open the httpd.conf file in Notepad or another text editor and search for the section that has a series of commented out LoadModule statements. You can find this around line 134, as shown in Figure. In Notepad, choose Edit Go To . . . 134.

    Notepad httpd.conf file with the LoadModule section highlighted

    Statements prefixed by the hash sign (#) in HTML and PHP are considered commented out and can be seen only by you, never your end user in a browser window.


  6. Restart the Apache server by selecting Start Apache HTTP Server 2.0.x Control Apache Server Restart, so that it can read the new configuration directives you placed into httpd.conf. Alternatively, in the system tray, double-click on the Apache icon and click the Restart button.

  7. Search for AddType in the file, around line 754 (shown in Figure), and add the following line after the last AddType statement:

    AddType application/x-httpd-php .php
    

    Notepad httpd.conf file with the AddType section highlighted

To test the installation, do the following:

  1. Create a PHP file in any text editor, with the following line:

    <?php phpinfo(); ?>
    

  2. Save the file as phpinfo.php, and then save it under the Apache htdocs directory, usually located at C:\Program Files\Apache Group\Apache2\htdocs.

  3. Open your browser of choice.

  4. Access the file you just created by typing localhost/phpinfo.php into your browser's location bar. You should see a page of information about your PHP setup, like Figure.

    PHP introduction screen

If for some reason this doesn't work, check to see whether your PHP or Apache setup is causing the problem. Go to the command-line prompt and type cd \php. The prompt should change to C:\php. Then type in php "C:\Program Files\Apache Group\Apache2\htdocs\phpinfo.php", since the file isn't in the c:\php directory.

If invoking PHP from the command line causes a large HTML file with all the PHP configuration information to be displayed, then your PHP setup is fine. The problem is probably your Apache configuration; go ahead and reread the procedure on how to install and configure Apache.

If you are on Mac OS X, you have PHP preinstalled on your computer. You need to edit the Apache configuration file to enable PHP in much the same way you edited the PC text file.


Installing MySQL 4.1

The final component you need to develop and test pages on your local computer is MySQL. Now download the MySQL Installer.

  1. Download the MySQL binaries. Both the binaries and the source code can be found at http://dev.mysql.com/downloads/. The link takes you to a page where you can enter personal info, or just click No Thanks to download the file. A number of download locations (or mirrors) are available; select one. Download the recommended version for the Windows Essentials (x86) MySQL 4.1 Installer. Save the installer file to your desktop.

  2. Double-click the MSI installer file on your desktop. A setup wizard, shown in Figure, walks you through the installation process. (The install process for MySQL 5.0 is the same as it is for MySQL 4.1.) Click Next.

    The Setup Wizard prompts you for configuration settings

  3. Select Typical installation by clicking the Typical radio button shown in Figure, and then click Next.

    Select a setup type

  4. Click Install on the Ready to Install the Program dialog box shown in Figure.

    Confirm installation folder

  5. The setup program next will ask you to create a MySQL.com account, as shown in Figure. Select Skip Sign-Up and click Next, or sign up for an account, which provides access to a monthly newsletter as well as the ability to post bugs and comments on the online forums.

    The MySQL.com account provides newsletters and bug reporting resources

  6. Select the "Configure the MySQL Server now" checkbox shown in Figure, and then click Finish.

    Configure the MySQL Server

    This brings up the dialog box shown in Figure, where you can just click Next.

    The Configuration Wizard customizes the database settings

  7. Select the Standard Configuration radio button from the dialog shown in Figure. Click Next.

    Choose the level of detail dialog box

  8. In the dialog shown in Figure, check both Install As Window Service and "Launch the MySQL Server automatically." Click Next.

    How to launch MySQL window

  9. Enter a password for the root user in the password and confirm fields shown in Figure. Click Next. You do not need the Anonymous Account since you can do everything with named accounts.

    Security settings for the database window

  10. Click Execute, as shown in Figure.

    Commit the configuration settings window

  11. Click Finish, as shown in Figure. MySQL is now configured and running on your computer.

    Installation is complete

At this point, all critical componentsApache, PHP 5.0, and MySQL 4.1are installed.

The wizard will inform you of basic problems during installation, such as running out of free disk space or not having proper permissions on your system to install MySQL.




 Python   SQL   Java   php   Perl 
 game development   web development   internet   *nix   graphics   hardware 
 telecommunications   C++ 
 Flash   Active Directory   Windows