Tuesday, October 9, 2012

Setting Up PHP and MySQL Development Environment on Windows 7

This is a step by step way of installing Apache Web Server, MySQL database and PHP pre-processor on Windows 7.

What You Need To Know To Get Started

Apache HTTP Server is the famous Web server nowadays. It is available for a wide variety oft operating systems such as UNIX, Linux, Solaris, BSD, Windows, Mac OS, etc. Apache is developed and maintained by open community of developers under the hood of Apache Software Foundation (www.apache.org).
 PHP (www.php.net) is the most popular scripting language on the web. PHP is a server-side language as opposed to JavaScript which runs on client side (user agent, e.g. Web browser). It is embedded into HTML and puts the tools for creating dynamic websites in the hands of the people.  PHP is penetrating Web technology so that a number of websites now are developed with PHP. Originally, it stands for Personal Home Page and was created by Rasmus Lerdorf in 1995. However, the main implementation of PHP is now produced by The PHP Group sponsored by Zend Technologies, the company founded by Zeev Suraski and Andi Gutmans, two Israeli developers at the Technion IIT. They redesigned the parser in 1997 and formed the base of PHP 3; they also changed the language's name to the recursive acronym PHP: Hypertext Preprocessor. PHP is free software released under the PHP License. It as an alternative to other scripting languages or tools such as that of Oracle’s JSP and Microsoft’s ASP.NET.


MySQL (www.mysql.com) database server is the world's most popular open-source DBMS. Like Apache and PHP, MySQL is a free software, but under the license of GNU General Public License (GPL).  It is originally owned and sponsored by MySQL AB, a single-profit Swedish company , acquired by Sun Microsystems which in turn was acquired by Oracle Corporation.
Apache Installation

Software: httpd-2.2.22-win32-x86-openssl-0.9.8t.msi

1. Double-click, or right-click installer and click Install.

2. Click Next.


3. Click I accept the terms in the license agreement.


4. Click Next.


5. Type in localhost for Network Domain and Server Name; type in also Email Address for Administrator's Email Address. By default, for All Users, on port 80, as a Service – Recommended is checked. It is the recommended option. Using port 80 lets you use only domain name or IP address without appending the port, hence localhost instead of localhost:80, and so is yahoo.com instead of yahoo.com:80. Other than this port number, you need to append the port like localhost:8080, 127.0.0.1:8081, etc. Click Next.


6. Check Typical and click Next.


7. Click Next. Do not change installation directory unless you have reasonable preference.


8. Click Install.



9. Wait while the software is being installed.



10. Click Finish.



11. Apache is now installed, and included in Windows menu.


12. To test if Apache is running, open your favorite browser (mine is of course Firefox), type localhost  in the address and hit Enter. Browser should open something like “It works!” Good job!



You can also use computer local name in replace to localhost; open Computer properties, click Advanced system settings, look for Full computer name: under Computer Name tab. Oppps! There's more. You can also use the default IP address of any local computer – 127.0.0.1. And finally, you can mimmick a FULLY QUALIFIED DOMAIN NAME (FGDN) that you want in the form of www.mydomainname.com. Go to C:\Windows\System32\drivers\etc and open hosts file in you favorite text editor (Notepad, Notepad++). Yes. It doesn't have file extension like .txt or .odt. Now add the following on the last line and save the file:

127.0.0.1     www.mydomainname.com

Opening IP address (127.0.01), computer name, or the fake FQDN should open the same page as     localhost.


13. Go to Apache HTTP Server menu when starting, restarting or stopping the server or you can click the Apache icon on Windows tray bar:

14. Default Web documents directory is C:\Program Files\Apache Software Foundation\Apache2.2\htdocs. This can be changed depending to your preference through httpd.conf configuration file of the server which is located on the menu or in C:\Program Files\Apache Software Foundation\Apache2.2\conf directory.



Congratulations!

Apache alone is enough to run HTML/CSS/JavaScripts and all other client-side scripts as Web browser does.PHP Installation and Configuration for Apache

Software: php-5.4.0-Wind32-VC9-x89.zip
Note: zipped (.zip) package is more recommended than installer (.msi).

1. Extract or decompress php-5.4.0-Win32-VC9-x89.zip to C:\ drive. Or extract to any directory and copy it to C:\ drive. Folder should be renamed to php so that we will have C:\php.


2. PHP folder contains dev, ext, and extras folders. It also contains files such as glib-2.dll, php.exe, php5apache2_2.dll, and a lot more:


3. Look for the file php.ini-development (for development purpose) or php.ini-production (for production purpose) from php folder and copy to the same folder. Rename it to php.ini and open with your favorite text editor, e.g. Notepad and Notepad++. Note: Click Yes if there prompts "If you change a file name extension, the file might become unusable. Are you sure you want to change it?"


4. Open php.ini and look for display_errors = Off. With this directive set to off, errors that occur during the execution of scripts will no longer be displayed as a part of the script output, and thus, will no longer be exposed to remote users. With some errors, the error message content may expose information about your script, Web server, or database server that may be exploitable for hacking. Production sites should have this directive set to Off.


For development purposes, this directive should be set to On.


5. Open httpd.conf on C:\Program Files\Apache Software Foundation\Apache2.2\conf, or to easily open this file, click Edit the Apache httpd.conf Configuration File  under Start>All Programs> Apache HTTP Server 2.2>Configure Apache Server. Add PHP module to last line of list of modules on Apache configuration file. PHP module can be found on the top level of PHP directory. Since the Apache version is 2.2.x or 2.2.22, we are going to use php5apache2_2.dll module.

Add LoadModule php5_module “C:/php/php5apache2_2.dll”. Take note: use “/” and not “\”.

6. Look for the line DirectoryIndex index.html which can be found on <IfModule dir_module> section.


Change it to DirectoryIndex index.php index.html. Apache will serve the file index.php (or the index.html if the former is not present) if a directory is requested. index.php must come first otherwise index.html will be opened if both exist.


7. Look for the following lines under <IfModule mime_module> section:

AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz


Add AddType application/x-httpd-php .php to the next line.


At the last line, add PHPIniDir <php.ini directory> in order for the Apache to look up php.ini configuration. Save the file.


8. Restart the Apache Server. Every time you make any changes to php.ini or httpd.conf you have to restart the server.


9. To test if Apache and PHP work together, make a PHP file with the following codes on your text editor and save with the desired name and with .php extension. In my case, phpinfo.php.

<?php
phpinfo();
?>

Save this file to the top level of your document directory htdocs (you should have permission) or you can create a folder to place to it. Open http://localhost/phpinfo.php with your favorite Web browser. You can change localhost as discussed in Apache Installation. It should open the following:


Congratulations.

Apache and PHP alone are enough to run PHP scripts. However, if your PHP application includes database functionalities, you will need to install DBMS like MySQL.MySQL Installation and Configuration for PHP

Software: mysql-5.5.25-win32.msi

1. Double-click installer or right-click and click Install.

2. When installer opens, click Next.


3. Check I accept the terms in the License Agreement and click Next.


4. Choose setup type depending on your needs. Typical installs the most common program features; recommended for most users. Custom allows users to choose which program features will be installed and where they will be installed; recommended for advanced users. Complete will install all program features; it requires the most disk space. (Mine is Complete)


5. Click Next.

6. Just click Next when more windows open up.

7. The following step completes MySQL setup. Check Launch the MySQL Instance Configuration Wizard to start instance configuration. Configuration wizard is necessary for creating user, password and other settings. Leaving it unchecked will do otherwise but you can later invoke instance configuration MySQLInstanceConfig.exe from C:\Program Files\MySQL\MySQL Server 5.5\bin. Click Finish.

8. Click Next when configuration wizard fires up.

9. Select configuration type. I chose Standard Configuration. Click Next.

10. Check Install As Windows Service, select Service Name from the drop-down list, and check Launch the MySQL automatically and Include Bin Directory in Windows PATH. Click Next.

11. Check Modify Security Settings and enter root password. By default, MySQL has root user which consists all the privileges. Note: root acts as superuser that manages all the server resources and therefore it is recommended that you create another user for day-to-day data management for your applications; consult MySQL documentation available from its Website. Click Next.

12. Click Execute.

13. Click Finish.

14. MySQL Server is now installed, and included in Windows menu.


15. To start using MySQL, click MySQL 5.5 Command Line Client from the menu.

 







Since we checked  Include Bin Directory during the instance configuration, we can also open  MySQL console by opening cmd/command and type in mysql -u root -h localost -p.





 

To exit from MySQL console, just type in exit and hit Enter or simply click button of the cmd.



16. After MySQL has been installed, we need to configure PHP and enable MySQL extension.

There are two ways to connect PHP with MySQL (I prefer the second approach):

a. Copy mysql.dll and/or mysqli.dll from C:\php\ext to the top level directory of PHP installation, C:\php. Open php.ini and look for php_mysql and php_mysqli under the list of extensions.



Uncomment php_mysql and php_mysqli, save the file and restart Apache server:


   
b. Open php.ini and look for extension_dir = "ext"



Change this line to extension_dir = “<extension directory>” where extension directory is the directory of the extensions to be loaded such as mysql.dll and/or mysqli.dll. The directory is C:\php\ext. Note: instead of changing the same line, I would copy and edit it.



Uncomment php_mysql and php_mysqli, save the file and restart Apache server:



17. To check if PHP connection with MySQL works fine, reload phpinfo.php on the browser. You should now see mysql and mysqli support entries.



You can also test by running the following codes:

<?php
$host = 'localhost';
$user = 'root'; //we will use root user since we have not created new user
$pwd = 'admin012345';

$con = mysql_connect($host, $user, $pwd);
if($con) echo “Connection successful!”;
else die(“Connection failed!”);

mysql_close($con);
?>

Save the file as mysql_test.php in htdocs and run on the browser: http://localhost/mysql_test.php. Your browser should render the following:



Congatulations!!! Now that you have already installed PHP, you may now take the power of PHP and MySQL Web development.

Download PDF here.

4 comments:

  1. try Development Tools like XAMPP or WAMPP! great for starters!

    ReplyDelete
  2. Natry ko na din yan lahat Jeff, Wampserver, XAMPP, WampStack, EasyPHP, yung iba nakalimutan ko na, pero bumalik ako sa manual installation, mas gusto sa manual, mas naiintindihan ko, OK naman mga stacks easy to install, at mgagamit agad, wla problema, yun lng gusto ko mas naiintindihan, tska for development lng, hindi pang production, maliban kung Application server katulad ng Zend Application Server...

    ReplyDelete
  3. Replies
    1. Thanks for your comment. I haven't completed the screenshots above as my connection is too slow.

      Delete