PHP Tutorial: Installation
Installing PHP, MySql and Apache on Windows
N.B: This tutorial contains images. Over a dial up connection, it will take approximately 90 seconds to download all supported images.
It is often useful to have an installation of PHP on your local system for testing purposes. This tutorial will show you how to install and configure your own web server with PHP and MySql in a Windows environment. I will also show you how to secure your installation to ensure that no one but you can gain access to it.
Prerequisites
- Make sure you are logged onto your computer using an account with administrative privileges before continuing.
- You may encounter error messages when installing MySql and Apache if you have Microsoft Windows XP service pack 2 installed and have the Internet Connection Firewall enabled. It is safe to ignore these errors, as they are caused when a service attempts to listen for connections on an unsafe network interface.
- You must be running Windows NT, Windows 2000 or Windows XP.
- Before installing Apache, ensure that all other web servers installed on the system, such as IIS have been turned off, otherwise you will be unable to start the web server.
Contents
- Mysql
- Download & Installation
- Testing the Installation
- Finding your way Around
- Securing the Installation
- Apache
- Download & Install
- Testing the Installation
- Finding your way around
- Securing the Installation
- PHP
- Download & Install
- Configuring PHP
- Configuring Apache
- Testing the Installation
- Securing the Installation
- Sources
- Aknowledgments
Re: PHP Tutorial: Installation
Apache
Apache is a powerful HTTP web server. A web server enables you to host specific files which can be downloaded via HTTP (Hyper Text Transfer Protocol) and displayed in a web browser. As well as displaying files Apache allows you to invoke programs on the server and have their output (HTML) sent to the web browser. Apache will allow us to execute our PHP scripts. When a request is made for a PHP script Apache will run the PHP executable and feed in the requested script. PHP will process the script and finally Apache will send the output of the PHP executable back to the client who requested it.
Download & Install
N.B: Before installing Apache, ensure that all other web servers installed on the system, such as IIS have been turned off, otherwise you will be unable to start the web server.
- The latest version of Apache can be found at http://httpd.apache.org/download.cgi. At the time of writing, the latest stable version of Apache was version 2.0.53, I will be downloading and using the Windows Installer version.
- After reading the license agreement and the introduction page you will be asked to enter some basic information. Unless you plan to access your server from other computers on your network or the Internet, enter “localhost” in the domain and server name fields. Ensure that the option to run Apache as a service on port 80 is selected, similar to MySql, this will allow Apache to start up with Windows.
http://www.vbforums.com/attachment.p...id=35051&stc=1
- For the next two steps select the Typical install and confirm the directory you want Apache to be installed in. You are then ready to install Apache.
Testing the Installation
If you have Windows XP Service Pack 2 installed and the Internet Connection Firewall enabled, you may see a warning when Apache starts. This is because it is attempting to listen on port 80 for remote connections originating from any computer, whether it be on the local network or the Internet. I will show you how to disable this in the section on Securing the Apache Installation. To test the installation was successful, open your favourite web browser and enter the following URL:
http://localhost/
You should see the Apache start page. This page will contain a link to a local copy of the Apache manual.
http://www.vbforums.com/attachment.p...id=35052&stc=1
Finding your way around
The root installation directory C:\Program Files\Apache Group\Apache2 contains the following significant sub directories:
bin – this directory contains all Apaches executable files
cgi-bin – contains CGI programs / scripts which are executed by Apache.
htdocs – contains the web pages for your site
conf – contains Apaches configuration files. The main configuration file is called httpd.conf.
Securing the Installation
At this point we want to ensure that our web server is only accessible via the local machine and not to people from outside on the Internet. I strongly recommend that unless you are an advanced user, you follow these steps to prevent outside access.
- Open the web servers main configuration file, httpd.conf. A link to this file can be found in:
Start Menu -> Programs -> Apache HTTP Server -> Configure Apache Server -> Edit the httpd.conf Configuration File
- Find the line:
This line tells the server which TCP port to listen for HTTP connections on. At present is listens on all available network interfaces (i.e: modem, wireless, LAN, etc). We will change this to the loopback interface only, which has the name localhost. This ensures connections only originate from the PC itself:
Code:
Listen localhost:80
- Save and close the configuration file. For the changes to take effect you need to restart the web server. There is again a shortcut in the start menu to restart the web server:
Start Menu -> Programs -> Apache HTTP Sever -> Control Apache Server -> Restart
Re: PHP Tutorial: Installation
PHP
It is now time to install PHP. After installation, we will need to go back to the Apache configuration file and make some changes so it knows how to deal with PHP files. PHP is an HTML embedded server side scripting language. It allows you to introduce logic to what would otherwise be static web pages and create server side applications.
Download & Install
The latest version of PHP can be found at http://www.php.net/downloads/. At the time of writing there were two stable versions of PHP. PHP 4 and PHP5. For this tutorial we will be downloading the Windows Zip archive of PHP 5.0.3 You'll need to ensure you have a compression utility such as quickZip which can extract zip archives to view this file.
- Open up the ZIP archive and create a directory on your C drive called PHP
- Extract the entire archive into the newly created directory.
You have now installed PHP. The next step is to configure it.
Configuring PHP
Before running PHP for the first time we need to create a configuration file. PHP's configuration file is called php.ini.
- Open the directory C:\PHP
- Rename the file php.ini-dist to php.ini
- Open the php.ini file in a text editor.
- Find the following settings and edit them as follows
Code:
short_open_tag = Off
error_reporting = E_ALL
extension_dir = "C:\PHP\ext"
extension = php_mysql.dll <-- remove the (semi colon) ; from the beginning of this line
SMTP = post.demon.co.uk <-- your ISP's web SMTP server address goes here
sendmail_from = [email protected] <-- your email address goes here
mysql.default_socket = mysql
include_path = ".;c:\PHP\PEAR\;C:\Program Files\Apache Group\Apache2\htdocs\"
- To ensure PHP can find the MySql library we need to copy the file libmysql.dll (which can be found in the bin directory of the MySql root installation directory), to the windows directory. Either C:\winnt\ or C:\windows\
The edited PHP file can also be downloaded from here.
Configuring Apache
- You now need to edit the httpd.conf configuration file. Re-open the file as detailed in step 1 of the “Securing Apache” section.
- Add the following lines to the end of your http.conf file:
Code:
### PHP Configuration
LoadModule php5_module "c:/php/php5apache2.dll"
AddType application/x-httpd-php .php
# configure the path to php.ini
PHPIniDir "C:/php"
- To complete the setup restart the Apache server as detailed in step 3 of the “Securing Apache” section.
Testing the Installation
We should now be able to run PHP through the web server and connect to the MySql database server through PHP. Follow these steps to test whether or not the installation of PHP has been successful.
- Open up the root installation directory of the Apache web server.
- Create a file in the htdocs directory called phpinfo.php with the following line:
PHP Code:
<?php phpinfo(); ?>
- Open the file via a web browser: http://localhost/phpinfo.php.
The phpinfo page will show information on PHPs configuration, Apache and the environment.
http://www.vbforums.com/attachment.p...id=35054&stc=1
Next we need to write a script to test the database connectivity.
- Open up the MySql client and connect to the php_general database which you created earlier.
- Enter the following queries to create a test table:
Code:
CREATE TABLE test (colname CHAR(25));
INSERT INTO test VALUES ('value1'), ('value2');
http://www.vbforums.com/attachment.p...id=35055&stc=1
- To display the contents of this table, we need to write a short PHP script. Place the following code in a file called db.php and save it in the htdocs directory of your Apache installation.
PHP Code:
<?php
$username = 'php';
$host = '.'; // this forces connection through a named pipe
$pw = 'secret';
if (! mysql_connect($host, $username, $pw)) {
die('Connection error: ' . mysql_error());
}
mysql_select_db('php_general');
$query = 'SELECT colname FROM test';
if (! $result = mysql_query($query)) {
die('Query error: ' . mysql_error());
}
?>
<html>
<head>
<title>Test Database Script</title>
</head>
<body>
<table>
<?php while($row = mysql_fetch_row($result)): // loop through all records?>
<tr>
<td><?php echo($row[0]) ?></td>
</tr>
<?php endwhile; ?>
</table>
</body>
</html>
- Run the script by typing the following URL into your web browser: http://localhost/db.php
Securing the Installation
Most of the steps we have already taken to secure our setup also filter through to PHP. Both PHP and Apache will however, run with administrative privileges. This means they will have access to any file on your system. To lock down the installation and ensure that only the Administrator can change the configuration:
- Make the following directories and sub directories read only to everyone except administrators.
C:\Program Files\Apache Group
C:\Program Files\MySql
C:\PHP
http://www.vbforums.com/attachment.p...id=35056&stc=1
- Make the htdocs directory writeable to users who are allowed to add and change files for the local website.
http://www.vbforums.com/attachment.p...id=35057&stc=1
- To prevent PHP from accessing files it shouldn't we can turn on safe mode and set the open_basedir directive. This will prevent PHP scripts from executing programs and only allow it to open files within the directory set by open_basedir. Open up your php.ini configuration file as detailed in step 4 of “Configuring PHP” and change the following:
Code:
safe_mode = On
open_basedir = "c:\PHP\PEAR\;C:\Program Files\Apache Group\Apache2\htdocs\"
safe_mode_include_dir = ".;c:\PHP\PEAR\;C:\Program Files\Apache Group\Apache2\htdocs\"
- For changes to take effect, restart the Apache web server as detailed in step 3 of the “Securing Apache” section.
Re: PHP Tutorial: Installation
Hi , everything worked for me Exept http://localhost/db.php
when i enter
Code:
### PHP Configuration
LoadModule php5_module "c:/php/php5apache2.dll"
AddType application/x-httpd-php .php
# configure the path to php.ini
PHPIniDir "C:/php"
and restart it gets error and shut down appache and dosent start agian.
Re: PHP Tutorial: Installation
If only I were able to read your mind. Unfortunately I cannot, so you are going to have to give me details of this mysterious error.
Re: PHP Tutorial: Installation
P.s: please post this in the discussion thread: http://www.vbforums.com/showthread.php?t=330926