It is very tedious process to install and configure Open Source Technologies like PHP, Apache, MySQL and Perl on Windows. A few settings need to be done to work with each other especially if we want to enable PHP, MySQL and Perl on Apache server. Of course, if someone wants to work with PHP only, he can make use of IIS also. Since PHP needs to be connected to MySQL to display data to website from database, so we need to learn how to make all applications connected to each other. Here I have enlisted the procedures step by step to make the following tasks very easy-
- Apache Installation
- Apache Configuration
- Enabling SSI with Apache
- Enabling PHP with Apache
- Enabling Perl with Apache
- Enabling CGI scripts with Apache
- PHP Installation
- PHP Configuration
- Enabling MySQL with PHP
- Enabling email functionality with PHP
- MySQL Installation
- PHP and MySQL Database Connectivity
- Perl Installation
There is no sequence to install these software, you can install anyone first. But I prefer to install Apache first. So let us start from Apache installation.
Apache Installation: (Apache_2.2.4-win32-x86-no_ssl)
So many sites are there that offer free apache download. You can download any updated version of apache from any website. Here I have downloaded apache_2.2.4-win32-x86-no_ssl. This is .msi file, so just click on it and the installation will start. While installing apache, keep all the default settings except following—
- ServerName : localhost
- Network Domain : localhost
- Admin email : Put any email id you want.
- Setup Type : Typical
Configuring Apache
Apache has got a dynamic configuration file named “httpd.conf”. Whatever changes we make here reflects to other applications after restarting the apache service. To open the httpd.conf file we can go to Start-> Programs-> Apache HTTP Server->
Configure Apache Server-> Edit the Apache httpd.conf Configuration File. Or we can open it manually from the location: "C:\Program Files\Apache Software Foundation\Apache\conf\httpd.conf".
We will make some changes into the httpd.conf configuration file after installing every software to make
them enabled with Apache. Let us make some basic changes into httpd.conf file now.
To Enable Server Side Includes (SSI), find out following lines and uncomment them by removing # sign. Apache uses # sign to comments a line.
AddType text/html .shtml
AddOutputFilter INCLUDES .shtml
To make .shtml file the default index page in apache, write following line—
DirectoryIndex index.shtml index.html [I will explain this later on]
Close the httpd.conf file and restart the Apache Service.
PHP Installation (PHP-5.2.3-Win32)
In case of PHP, it is better to download the zip package from the "Windows Binaries" section instead of downloading installer as because in Windows Binaries we get some .dll files, which are very much necessary for configuration. Here I have downloaded PHP
5.2.3 zip package. You can install any other updated version.
It is completely different to install Windows Binary. Under windows binary package, there is no setup file.
So simply we need to extract the .zip file to the location wherever we want. Let’s say, we want to install PHP to "C:/PHP" location. So just
create a folder called PHP in C: drive and extract all the files from the zip package into that folder.
PHP installation is done!
Important: if you had installed PHP earlier and you want to upgrade it now, ensure that there is no old PHP.INI file existed in C:/Windows folder before installing the updated version of PHP.
Configuring PHP
There is a file namely "php.ini-recommended" in C:/PHP folder, rename it into “php.ini”. You need not to copy this file to C:/Windows now because we will write a directive in httpd.conf
file later on to enable PHP with Apache.
Let us open php.ini file in order to make some changes based on our requirements.
We know that PHP script starts with “< ? php”. So we need to enable “< ?” sign, find the following line to do same—
short_open_tag = On
While testing and debugging a PHP script, it is necessary to display the appropriate errors so that we can understand the mistakes and accordingly we can resolve it. But for a live website, it is better not to display errors.
We can enable or disable displaying error by the following directive—
display_errors = On
If you want to use mail() function to send mail successfully, first of all you have to be connected with ISP and in php.ini file you need to change your SMTP Server(name or IP address) and email account like follows—
[mail function]
SMTP = mail.alzouman.com.sa
smtp_port = 25
sendmail_from = shafiq@alzouman.com.sa
[suppose my server name is : mail.alzouman.com.sa and email id is: shafiq@alzouman.com.sa]
Enabling PHP with Apache
Now we can enable PHP script to run on Apache Server. There is a .dll file in C:/PHP/ location named php5apache2_2.dll that we need to add in httpd.conf file. You will find several .dll files there and their names are specified based on PHP version number and Apache version number. Since our PHP version number is 5.2.3 and Apache version number is 2.2.4, php5apache2_2.dll is appropriate for us to establish a bridge between PHP and Apache.
Open the httpd.conf file and look for LoadModule statement. Add following line under all LoadModule statements-
LoadModule php5_module “c:/php/php5apache2_2.dll”
Find out “AddType” statement inside < IfModule mime_module> section and add following line before < / IfModule> statement.
AddType application/x-httpd-php .php
If you need to support other file types like .phtml, simply add them to the list like below-
AddType application/x-httpd-php .phtml
Finally to indicate the location of php.ini file, add following line-
PHPIniDir “C:/PHP”
MySQL Installation (mysql-essential-4.1.11-win32)
It is very easy to install MySQL. Download mysql-essential-4.1.11-win32 or any other version.
Run the .msi file and choose the following options while installing MySQL—
- Typical Setup
- Skip Sign-Up
- Configure the MySQL Server now
- Detailed Configuration
- Developer Machine
- Multifunctional Database
- InnoDB Tablespace Settings
- Decision Support (DSS)/OLAP
- Enable TCP/IP Networking
- port number at 3306
- Standard Character Set
- Install As Windows Service
- Enter your root password
- Enable root access from remote machines
- Execute
Enabling MySQL with PHP
We need to establish a connection between PHP and MySQL in order to work with database. Open the php.ini file once again. Find out the following line and remove comments (;) from the beginning of the line.
;extension=php_mysql.dll
Save the modified php.ini file.
Copy C:/PHP/libmysql.dll and C:/PHP/ext/php_mysql.dll to the C:/Windows/system32 folder.
Restart Apache. In order to check whether PHP and MySQL are working with Apache or not we can write a simple PHP script like follows—
<?php
phpinfo();
?>
Save this file in C:\Program Files\Apache Software Foundation\Apache\htdocs\ location with “anyname.php” extension. Open Internet Explorer or any other browser and type http://localhost/anyname.php it should display mysql modules. If it doesn’t display, ensure that php_mysql.dll is copied to system32 folder.
PHP-MySQL Connection test
<?php
$server='localhost';
$user='root';
$pass='1234';
$dbName='mysql';
$con = mysql_connect("$server","$user", "$pass");
if($con)
{
print "Connected successfully";
}
else
{
die("Unable to connect");
}
$selectdb= mysql_select_db("$dbName") or die("Could not select database");
$sqlquery= ("select * from user;");
$result=mysql_query($sqlquery);
if(mysql_num_rows($result)<1)
{
echo "No result";
}
else
{
while($row=mysql_fetch_array($result))
{
echo "<br><br>$row[Host]";
echo " $row[User]";
echo " $row[Password]";
}
}
mysql_close($con);
?>
Perl Installation (ActivePerl-5.10.0.1002-MSWin32-x86-283697)
I have downloaded ActivePerl-5.10.0.1002-MSWin32-x86-283697. You may download any updated Perl as you like. Installing Perl is as simple as other application’s installation just by pressing next button with the default values and finish.
Enabling Perl & CGI Scripts with Apache
We need to configure Apache to make the perl actively working with Apache Web Server. Let us look into the steps to run Perl scripts and CGI scripts.
Open the Apache configuration file httpd.conf again. There is a specific folder for CGI script that is: "C:/Program Files/Apache Software Foundation/Apache/cgi-bin/". We can save our CGI script here as well as we can run from other location also. To enable this folder to run CGI script, find out “ScriptAlias” statement in httpd.conf file and uncomment following line by removing # sign—
ScriptAlias /cgi-bin/ "C:/Program Files/Apache Software Foundation/Apache/cgi-bin/"
To run CGI script anywhere in your domain, add the following line—
AddHandler cgi-script .cgi
If you want to recognize .pl extension as a CGI script, you can add .pl extension along with .cgi like follows—
AddHandler cgi-script .cgi .pl
We should have the permission to run any CGI script. To invoke the execution permission to CGI script, look for the following block and make the changes like follows—
<Directory/>
Options FollowSymLinks +ExecCGI
AllowOverride None
Order deny,allow
Deny from all
Satisfy all
</Directory>
And—
Options Indexes FollowSymLinks +ExecCGI
Apache Server can contain so many types of file for example, .cgi, .pl, .html, .phtml, .shtml etc. The default index page, which is written under DirectoryIndex directives displays when we open http://localhost/ from browser. If multiple index pages are mentioned there, it looks for first index page, in case of unavailability of first index page it looks for second and so forth. So according to your expectation, you can enlist the index pages based on the priority you want—
DirectoryIndex index.html index.php index.cgi index.pl index.shtml
Restart the Apache Server and That’s All.
0 comments:
Post a Comment