Installing PHP 5.2 and configure Apache 2.2 on Windows & Linux

 

Over the years it has been times when i have to reinstall PHP to configure with Apache to work together. Today, it has been that occasion where i have to set up my local developing environment on new laptop to support some WordPress, Zend and Magento developments. It has never been a smooth process and it was no different this time….i guess i was too naive thinking that things have changed….Perhaps, things have changed, i just should have looked at new ways – deploying on cloud via Saas, however. This post is PHP(3.2.17) installed and configured with Apache 2.2.22

 

Installing PHP 5.2

My first failure come with the fact that the newest PHP 5.4 and 5.3 does not support Apache. Make sure you download the right  version VC6 (on Sep 13,2012).  Later, i also learn there is differences between Thread Safe and Non-Thread Safe versions. To run with Apache 2.2.22, you need Thread safe version

1. Download proper PHP version to work with Apache on Windows

2. Extract to your chosen directory. In our case, we use ‘C:/DevTools/php’

3. Update the windows environment variable ‘Path’.

We create new variable ‘PHP_HOME’ and then append ‘%PHP_HOME%’ to the ‘PATH’ variable. To check it works, open command line and type ‘php -v’. If it works then you should see php version displayed

 

Configuring Apache 2.2

I have Apache 2.2.x installed and tested currently. For how to install Apache server, please, consult other source

1. Edited Apache Configuration file httpd.conf (by default in C:\Program Files\Apache Software Foundation\Apache2.2)

In the httpd.conf, we add these lines,  right below the other LoadModule lines:
LoadModule php5_module "c:/DevTools/php/php5apache2_2.dll" #(change the path to wherever the file is)
AddType application/x-httpd-php .php .phtml .inc .php3
AddType application/x-httpd-php-source .phps

!!! Important to note, if you have Apache2.0, then use php5apache2.dll instead of php5apache2_2.dll

 

2. For Apache to use index.php as the default index page, change the line
DirectoryIndex index.html
-to-
DirectoryIndex index.php index.html index.htm

 

3. Copy the file “php5ts.dll” to “c:\Program Files\Apache Group\Apache2\bin\”.

The ‘ts’ at the end of php5ts.dll must mean Thread Safe, so if you don’t see it you must have happen to download & install non thread safe version of PHP that will not work with Apache 2.2.22

 

4. Copy the file “php.ini-recommended” to c:\Program Files\Apache Group\Apache2\ and rename it to “php.ini”

 

5. For PHP sessions to work properly, you need to specify where to save the sessions.

Edit the php.ini file, and find the setting for session.save_path. Set it to “c:\windows\temp\” or another folder you use as a temp directory.

5a.Enable GD extension for Image handling
The GD extension is used so often to handle the images that we recommend to enabled this extension at this time. To do so uncomment the following line and specify path to the GD extension in php directory

extension="C:\path_to_php\ext\php_gd2.dll"

 

6.  Restart Apache server:

Go to start -> run -> services.msc

or

From Apache Service Monitor (Start->All Programs->Apache HTTP Server 2.2->Apache Service Monitor->ReStart)

Test Drive

Its time to test our php configuration to work with appache server. To do so, create a file phpinfo.php with content:

<?php print phpinfo(); ?>

Save this file into dir Apache is serving pages(by default its “c:\Program Files\Apache Group\Apache2\htdocs”)
This should display php configuration info file that is also useful when installing add-ons of PHP because you can come here and always check if a particular add-on was installed for the PHP

Configure MySql

We assume you have mysql installed. To configure PHP to work with mysql you have to uncomment the following line and specify path to the libraries in php.ini file

extension="C:\path_to_php\ext\php_mysql.dll"
extension="C:\path_to_php\ext\php_pdo_mysql.dll"

This will enable necessary libraries for your php sites to talk to mysql database. We also enabled pdo_mysql library that is often used by opens source platforms such as Drupal

Virtual Hosts

Virtual host allows you easy to configure multiple sites to run on your apache server with different domain names

1. Enable Virtual Hosts

To enable the virtual hosts, uncoment the following line in path_to_apache/conf/httpd.conf

# Virtual hosts
Include conf/extra/httpd-vhosts.conf

Here we uncomment line ‘Include..’ so the vhosts configurations are loaded

2. Configure Virtual host

To configure virtual host, add the following in path_to_apache/conf/extra/httpd-vhosts.conf

<VirtualHost *:80>
    DocumentRoot "C:\path_to_sites\my-site"
    ServerName some-domain
    <Directory C:\path_to_sites\my-site>
        Options FollowSymLinks
        AllowOverride All
	Allow from all
	Order Deny,Allow
    </Directory>
    ErrorLog "C:\server\apache\logs\stefan-cascade-error_log"
    CustomLog "C:\server\apache\logs\stefan-cascade-access_log" common
</VirtualHost>

Here, the ‘DocumentRoot and ‘Direcory’ specifies the path where is the code base for your site located. The ‘ServerName’ specifies domain name of your site(i.e. some-domain). This can be anything as long as you update the hosts file to point request to your local computer/apache server the next step

3. Point Domain name to Apache server

In previous step, you specified domain. Lets update hosts file, so all requests made to this domain name from your browser goes to the local apache server. To do so update %SystemRoot%\system32\drivers\etc\hosts as following

127.0.0.1      some-domain

This will make all requests to ‘some-domain’ go to your local computer/apache server

Test Virtual Host

Create file of index.html with some text and put it into your virtual host as specified above(i.e.C:\path_to_sites\my-site). Afterwards restart apache and open browser with ‘some-name’ as url. This should display the text in your index.html file

Issue Encountered

1. When restarting Apache, it was giving the following error:

“The Requested operation has failed!”

The Apache logs showed no clues of whats the problem, so I run 'httpd.exe -e debug' from command line in apache/bin folder. From there, it was clear the problem:

Solution: this is because we are installing PHP that is Non-Thread safe version but Appache 2.2.22, apparently, requires to have PHP Thread Safe

2. “Permission denied: AH00529: […].htaccess pcfg_openfile: unable to check htaccess file”

This happens when the Apache user (often www-data or if not, check the file – envvars in apache dir) doesn’t have access to read your web directory. The solution is to add Appache user to the group assigned to the web dir

usermod www-data --append --groups root

Here, we add user www-data to a group named root that is currently assigned to the web directory serving your site

AH01630: client denied by server configuration

This error came after upgrading from Apache 2.2 to Apache 2.4 with old configurations of “Order allow,deny

Allow from all” in virtual host setting in 2.2 with new configuration of”Require all granted” in the 2.4

Other

1. Host File

In case you forgotten, the host file in windows are located

 	%SystemRoot%\system32\drivers\etc\hosts

Leave a Reply

Your email address will not be published. Required fields are marked *