Sunday, March 18, 2012

Configure manually Apache HTTPD and PHP in your machine.

There are "WAMP" and "XAMP" softwares that install all three apache httpd web server, php interpreter and mysql server, and automate the configuration process with just few clicks. But just  think of a situation where you have previously installed mysql instance running !


Either you have to uninstall the current instance of mysql server and install one of above "three in one" packs or change the port number of the newly installing my sql server. But what is the point of running two mysql instances at the same machine ???? (adding another service to windows means resources will be eaten more). Even though you uninstall current mysql server and install WAMP or XAMP there might be high chance that you will end up those installations not working due to some conflict with previously installed and now removed mysql instance and the one in WAMP or XAMP.


So it is always better to keep the current mysql instance and configure the other two with the existing one. And after all it is fun! and you can  be proud of your self about what you have done because almost all the php developers depends on wamp and when it comes to the server configuration and deployment environment you cant depend on a WAMP ! its just a development tool (and what if you do you client wants MS SQL server instead of mysql offer in WAMP ? ). So be an adult and do things by your self.


Ok enough talking lets start the work.


First download the latest releases of Apache HTTPd web server, PHP interpreter from the links. (Please download the executable files since that will be helpful with some auto configurations )


Then create a directory called "web" in the location c drive "c:\web". (name of the directory can be anything you like but without spaces, we dont install to "program files" because of the space in file path, try to avoid spaces in file path.)


Installing apache httpd server


Launch the setup (double click on setup.exe) and choose custom. Then choose the directory as "c:\web\apache" and enter the information of your server in to the following window.
( I prefer,
Network Domain - localhost
server name - localhost
Email Address - anything@you.want  If you intend to use the server in local development)


apache-install 
  Click next and choose content to install as showed in below.


apache-install-2 


And finish the setup.


Installing PHP setup.


Launch the setup and when prompt to select a location select the "c:\web\php" as the location.


In next window select the server which u wish to configure the PHP interpreter with shown as below. ( here we choose apacheXXX where XXX is the version of the apache httpd server that you have downloaded and installed previously)


php-install-2 


In next prompt choose the apache configuration directory in our case "c:\web\Apache\conf". 


On next window choose the libraries you wanted to install with PHP.


(Caution!!! some of the modules will not be compatible with the current PHP interpreter version that we are installing this might give us errors when we run the interpreter. If you are a newby most of the time you will miss understand this errors as miss configurations. So its better to leave not installing modules for now since we can add modules by editing "php.ini" manually later.)


Configuring apache server


First you have to add the a map to localhost and your server name if it is not "localhost" that you have entered in the beginning of apache server installation process. To do this open the host file in the "c:\windows\system32\drivers\etc\hosts" location using text editor (Most of the time if you are not in the administrator account you wont be able to save the edited host file. In that case copy the host file to Desktop edit it, save it there and copy back to the location of the original host file by overwriting it).



127.0.0.1 localhost
127.0.0.1 yourDomain


Then we are going to edit apache configuration files. ( keep in mind to use forward slash '/' instead of back slash '\' in file paths ).

(First of all copy and backup the "c:\web\apache\conf\httpd.conf" file in case of things gone wrong)

Open "c:\web\apache\conf\httpd.conf" and search for the lines.

1. DocumentRoot
DocumentRoot "c:/web/Apache/htdocs"

change this to any directory you prefer to use to  store your html and php files. (lets say "c:\web\www").

DocumentRoot "c:/web/www"
2. Then change 

<Directory "c:/web/Apache/htdocs">
to
<Directory "c:/web/www">

3. Add entry for php in the apache configuration file.
#BEGIN PHP INSTALLER EDITS - REMOVE ONLY ON UNINSTALL
PHPIniDir "C:/web/PHP"
LoadModule php5_module "C:/web/PHP/php5apache2_2.dll"
AddType application/x-httpd-php .php
#END PHP INSTALLER EDITS - REMOVE ONLY ON UNINSTALL

4. Include index.php to apache directory index files.
DirectoryIndex index.html index.htm index.php




Configuring PHP

(First of all copy and backup the "c:\web\php\php.ini " file in case of things gone wrong) 

Edit php.ini file located in "c:\web\php\php.ini".

1. Change the display errors on.
display_errors = Off
to
display_errors = On

2. Copy all the "php5apache2_2.dll" in php root directory and ext directory to "c:\windows\system32" directory with edited php.ini file.


Adding or removing modules to php.ini

Edit php.ini file and add entries for required modules.

Eg:

[PHP_MCRYPT]
extension=php_mcrypt.dll
[PHP_MYSQL]
extension=php_mysql.dll
[PHP_MYSQLI]
extension=php_mysqli.dll
[PHP_OPENSSL]
extension=php_openssl.dll
[PHP_PDO]
extension=php_pdo.dll
[PHP_SOAP]
extension=php_soap.dll
[PHP_SQLITE]
extension=php_sqlite.dll
[PHP_XMLRPC]
extension=php_xmlrpc.dll
[PHP_ZIP]

Adding MySQL support to PHP


Here i assumed that you have already installed mysql server and configured it. Do not store your mysql host, user and password fields in php.ini file even though php mysql configuration allows to store them.

[MySQLi]

; Maximum number of links.  -1 means no limit.
mysqli.max_links = -1

; Default port number for mysqli_connect().  If unset, mysqli_connect() will use
; the $MYSQL_TCP_PORT or the mysql-tcp entry in /etc/services or the
; compile-time value defined MYSQL_PORT (in that order).  Win32 will only look
; at MYSQL_PORT.
mysqli.default_port = 3306

; Default socket name for local MySQL connects.  If empty, uses the built-in
; MySQL defaults.
mysqli.default_socket =

; Default host for mysql_connect() (doesn't apply in safe mode).
mysqli.default_host =

; Default user for mysql_connect() (doesn't apply in safe mode).
mysqli.default_user =

; Default password for mysqli_connect() (doesn't apply in safe mode).
; Note that this is generally a *bad* idea to store passwords in this file.
; *Any* user with PHP access can run 'echo get_cfg_var("mysqli.default_pw")
; and reveal this password!  And of course, any users with read access to this
; file will be able to reveal the password as well.
mysqli.default_pw =

; Allow or prevent reconnect
mysqli.reconnect = Off

Testing
 



Write the following php code. Store it as index.php in your webroot location. ( here we use "c:\web\www" ).

<?php phpinfo(); ?>



Then open a browser and type localhost in address bar. You will get a similar page as below.



No comments:

Post a Comment