Tuesday 16 July 2013

Setup Apache, Php on Windows 7 and Centos

The check-list on Windows 7:

- Use Win32 Apache 2.4.4 from Apache Lounge and get Visual Studio 2012 run-time

- Download Thread Safe (TS) version of php 5.4.x from http://windows.php.net/download/

- Be sure you installed the Visual Studio 2012 (VC11) run-time, see left column at http://windows.php.net/download/

- Download php5apache2_4.dll-php-5.4-win32.zip from Apache Lounge and use the correct version in the zip

- Follow the instructions in the Readme in the php5apache2_4.dll zip

- First try to run without php extensions !

- Then run with php extensions !


Download:

  • Download the latest Apache 2.4.4 (as of writing this) from http://www.apachelounge.com/download/ and unzip it in C:/Apache24
  • Download latest PHP version 5.4 from http://windows.php.net/download and unzip it in c:/php. While downloading PHP, choose the Thread Safe version for Windows.
  • Download the PHP and Apache connector DLL file from http://www.apachelounge.com/download/ and extract the file php5apache2_4.dll and copy it to C:/php.


Apache:

Open the file C:/Apache24/conf/httpd.conf in any text editor then find and make the following changes/configurations as per your installation path.

Set the server root
ServerRoot "C:/Apache24"

Set the document root
DocumentRoot "C:/Apache24/htdocs"

Enable required Apache modules. I just un-comment one more for mod_rewrite module.

Add the following lines somewhere (You can add it at the end of the file)
LoadModule php5_module "C:/php/php5apache2_4.dll"
AddHandler application/x-httpd-php .php
PHPIniDir "C:/php"

Change server admin email address
ServerAdmin info@yoursite.com

Change the document root:
DocumentRoot "C:/Apache24/htdocs"
<Directory "C:/Apache24/htdocs">

If you want to allow .htaccess to be used under document root do the following under
<Directory “c:/Apache24/htdocs”>
AllowOverride All

Find the following and replace the path:
ScriptAlias /cgi-bin/ "C:/Apache24/cgi-bin/"
<Directory "C:/Apache24/cgi-bin">

Add info.php in directory index
DirectoryIndex index.html info.php

PHP:

Rename php.ini-development to php.ini
Find extension directory setting and set the path as per your installation.
extension_dir = "C:\php\ext"
Uncomment the extensions that you want to enable them. i.e.
extension=php_curl.dll
extension=php_mysql.dll
extension=php_mysqli.dll

If you want to test email sending from the PHP in local system, then you need to set the SMTP. i.e.
SMTP = smtp.yoursite.com
smtp_port = 25
sendmail_from = youremail@sender.com
Rest of the changes/configurations are totally depends on requirement project wise.

Check to make sure php shows loaded modules.
C:\> php -m

Putting it together:

Set Environment Variables for PHP and Apache (Windows 7):

To use PHP and Apache globally in Windows, the path of the PHP and Apache has to be added to the Path Environment Variables.
Right click on Computer and click Properties
Click on Advanced system settings on the left side (top).
Click on the Environment variables
Under System variables, double click the Path variable name and add ;C:\php without double quotes at the end where semicolon (;) is the separator.
Click OK to save. Sometimes you may need to restart the computer this to take it into effect.

Install Apache as Service:

Since we have just copied the files in a folder, the Apache is not yet not a windows service. To install it as a service follow the steps. We run the command line (cmd.exe) As Administrator:
C:\Apache24\bin>httpd -k install

Now restart the computer and test both PHP and Apache. To test both together, create a PHP file called  info.php in C:\Apache24\htdocs. We will see if Apache parses the info correctly to display the results.

Open Notepad and type and save the following.

<?php
phpinfo();
?>

Now open the browser and enter http://127.0.0.1/info.php. If you can see the PHP information then now you are done !

MySQL data Location:

On Windows 7, the MySQL data directory is, by default, "C:/ProgramData/MySQL/MySQL Server 5.5/Data/". Note that "C:/ProgramData" is a hidden directory.

Troubleshooting:

When i start the server it shows this message:

Apache 2.4 daemon is Starting ...
##########################################
## Stop Apache Please Close This Window ##
##########################################
(OS 10048)Only one usage of each socket address (protocol/network address/port)
is normally permitted.  : make_sock: could not bind to address 0.0.0.0:80
no listening sockets available, shutting down
Unable to open logs
Press any key to continue . .


If you see this error message, you check netstat -nato and find the current proccess that occupies your port 80.

It could happen like that (to have problems starting Apache) if you have Skype installed. It uses port 80 by default if has connection problems.

On Centos

Install Apache and ssl module

sudo yum install httpd mod_ssl
Start apache
sudo /etc/init.d/httpd start

If you see the error Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName.

You can modify ServerName value in the httpd.conf in /etc/httpd/conf, change it to your hostname
ServerName hostname
Reload apache
sudo /etc/init.d/httpd reload
Install Php

sudo yum install php-common php-gd php-mcrypt php-pear php-pecl-memcache php-mhash php-mysql php-xml
After that, please modify the Apache httpd.conf to enable php in Apache


#
# Cause the PHP interpreter to handle files with a .php extension.
#
LoadModule php5_module modules/libphp5.so
AddHandler php5-script .php
AddType text/html .php

#
# Add info.php to the list of files that will be served as directory
# indexes.
DirectoryIndex index.html info.php

No comments:

Post a Comment