Setting up a Development Environment for PHP and MySQL

Introduction

The development environment is the location where you write and test your web applications before uploading them to the staging environment and then to the production environment. The most common location for your development environment is your own computer. This walk-through shows you how to set up a development environment on a PC for development of web applications using PHP, MySQL, and Apache.

Software

The target development environment will consist of the following five software components:

  • Web Server – Apache
  • Database Server – MySQL
  • Web Programming Language – PHP
  • Database Management Tool – phpMyAdmin
  • Integrated Development Environment (IDE) – Zend Eclipse PHP Development Tools (PDT)

Integrated Development Environment

You need software to write the program. Since PHP programs are just text files, you can use a text editor (such as NotePad on Windows or TextEdit on Macintosh). However, there are software tools that offer features that help make program writing easier, they are collectively called Integrated Development Environment or IDE.

An IDE offers a composing and editing environment that typically consists of a code editor, a compiler, a debugger, and a graphical user interface (GUI) builder. In this walk-through, we will install Zend Eclipse PHP Development Tools (PDT) as the IDE.

Apart from the PDT, you can install Apache, MySQL, PHP, and phpMyAdmin software on your computer for testing web applications using one of the following two methods:

Install from an All-in-One Package

Installing the software from an all-in-one package is faster and easier. For a Windows computer, there is a free package called XAMPP that you can download from Apache Friends. For Macintosh users, there is also a free package called MAMP.

XAMPP stands for Cross-platform, Apache, MySQL, PHP and Perl. Within a few mouse clicks, you can install Apache, MySQL, PHP, and phpMyAdmin on the PC. XAMPP was created primarily as a PHP development environment for Windows computer. MAMP stands for Macintosh, Apache, MySQL and PHP. With just a few mouse-clicks, you can install Apache, MySQL, PHP, and phpMyAdmin on the Macintosh computer. MAMP was created primarily as a PHP development environment for Macintosh computers.

Install each Software Package Separately

If you are braver, you can also download and install Apache, MySQL, PHP, and phpMyAdmin software separately on your PC. They are downloadable from their respective websites:

This walk-through will use the XAMPP all-in-one package.

Install XAMPP

If you have an older version of XAMPP on your machine, you have to uninstall it prior to installing the new version. Before the un-installation, however, remember to backup any web projects and databases that resides in the existing XAMPP folder so that you can re-locate them to the new version.

Download the XAMPP for Windows Installer. Close all applications on your computer, double-click the downloaded exe file to start the installation. When prompted, just leave every option to default. By default, the installer will extracts all the necessary files to a new folder at c:\xampp.

After successful installation, you should find an XAMPP icon on your desktop, double-click it to launch the XAMPP Control Panel as shown in Figure 1. Alternatively, you can locate c:\xampp-control.exe, the control panel program, and drag a shortcut to the desktop.

xampp_control_panel_stop
Figure 1: XAMPP Control Panel

On the XAMPP Control Panel, click the Start buttons for Apache and MySQL. Messages that they have started successfully are displayed, and the labels on their respective Start buttons changes to Stop. (Figure 2)

xampp_control_panel_start
Figure 2: Apache and MySQL Started

Click the Admin button for the Apache server, or alternatively launch a browser and enter http://localhost/ into the address bar. If everything has been correctly set up, you should see the following main admin page on the browser. (Figure 3)

xampp_main_admin
Figure 3: XAMPP Main Admin

The menu on the left of the main admin page gives you web-based access to the various components of XAMPP.

Click the phpinfo() link under the Php section of the menu on the main admin page. You should see a page showing PHP configuration information below. (Figure 4)

php_config
Figure 4: PHP Configuration

Click the phpMyAdmin link under the Tools section of the menu on the main admin page, or type http://localhost/phpmyadmin on a browser address bar. This will launch the phpMyAdmin page for managing the MySQL database server. (Figure 5) You can also launch the phpMyAdmin through the Admin button beside MySQL on the XAMPP Control Panel.

phpMyAdmin
Figure 5: phpMyAdmin

You have been able to launch the above pages without being prompted for logging in. Your development environment is not secured. Everyone can access your XAMPP pages through the network.

Click the Security link under the Welcome section of the menu on the main admin page. You should see the XAMPP SECURITY page below. (Figure 6)

xampp_security
Figure 6: XAMPP Security

Click the red link http://localhost/security/xamppsecurity.php on the XAMPP SECURITY page to navigate to the Security Console to create log in accounts for MySQL and XAMPP. (Figure 7)

security_console
Figure 7: Security Console

After this, you will be prompted to log in when you attempt to access the XAMPP pages through the network. Access to phpMyAdmin page will also requires log in. (Figure 8)

phpmyadmin_login
Figure 8: phpMyAdmin Login

Install Zend Eclipse PDT

Download Zend Eclipse PHP Development Tools (PDT) and extract it to a location on your PC. You are required to sign up a free account in order to proceed with the download. Locate and double-click the zend-eclipse-php.exe in the extracted folder to launch the PDT. (Figure 9)

eclipse_pdt_logo
Figure 9: Zend Eclipse PDT Launching

You will be prompted to enter the path to the workspace. (Figure 10) Change the workspace to c:\xampp\htdocs where your PHP projects will be residing.

workspace
Figure 10: workspace

The initial screen of the PDT will look like Figure 11. Close the Welcome screen.

eclipse_pdt1
Figure 11: Welcome Screen

Create a PHP Project

You have completed the installation of XAMPP and Zend Eclipse PDT. Let’s create a PHP project with a one-page PHP script to test the connection with MySQL.

Make sure that the Apache and MySQL are started. In the PDT,

  • click File > New > Local PHP Project
  • Ener the PhpTest as Project Name > Finish
  • An empty index.php page will be created by default
  • Enter the following script into index.php and save it. Remember to change the password in the script to that of your password for your MySQL if you have set it during installation.
    <?php
    $host = "localhost";
    $user = "root";
    $password = "password";
    
    $cnn = mysqli_connect ( $host, $user, $password );
    $sql = "SHOW DATABASES";
    $result = mysqli_query ( $cnn, $sql );
    if ($result == false) {
        echo "Error: " . mysqli_error($cnn);
    } else {
        if (mysqli_num_rows ( $result ) < 1) {
            echo "Databases not found.";
        } else {
            echo "<ul>";
            while ( $row = mysqli_fetch_row($result)) {
                echo "<li>$row[0]</li>";
            }
            echo "</ul>";
        }
    }
    ?>
test
Figure 12: Result of index.php
  • Right-click index.php on the PHP Explorer > Run As > PHP Web Application, you should see all the databases in the MySQL being displayed on the screen (Figure 12)

Points of Interest

Congratulations. You have successfully set up a development environment on your PC. You can use it to develop and test your PHP and MySQL projects at your convenience.

If you encounter any problems in starting up the Apache or MySQL, look at the error messages that appear on the XAMPP Control Panel for clues. Ensure that your PC security software does not block port 80 (default port for Apache) and port 3306 (default port for MySQL).

If you encounter errors running PHP script apart from syntax errors, check out the following areas:

  • Have the Apache and MySQL started?
  • Are the user name and password supplied to the MySQL correct?

Last but not least, you may want to set up the Apache and MySQL as Windows services so that they will start up automatically when the PC boots up. This can be done by checking the check boxes beside the respective Modules on the XAMPP Control Panel.

Reference

Posted in General Tagged with: , ,