Apache Configuration for PHP

By | October 2, 2021
Apache Configuration for PHP

Apache Configuration for PHP

Apache is considered to be the most commonly used web server if you want to code locally on your system. Apache is available freely and is open-source, so it does not require any cost to start your coding process. You can easily download the latest version of your Apache for Windows. If you are working on Mac OS, then it comes with pre-installed PHP and Apache. All you have to do is ‘enable the features’. If you are on your Linux system then Apache is pre-installed or you can find it in the base repository.

There are some packages available that contain Apache, PHP, MySQL and other applications like XAMPP, web. Developer etc.

Method- Configure Apache to load PHP as an Apache module.

  • Configuring Apache that will load PHP as a module will parse the code.
  • Open Apache configuration file ‘httpd.conf’ in ASCII editor and you can find this config file under the conf folder irrespective of what version of Apache you are using.
  • Look for the ‘LoadModule’ section of the file and add the following code change to the file.
    • For Apache 1.x
LoadModule php5_module "c:/php/php5apache.dll"
    • For Apache 20.x
LoadModule php5_module "c:/php/php5apache2.dll"
    • For Apache 2.2.x
LoadModule php5_module "c:/php/php5apache2_2.dll"
  • Look for ‘AddModule’ statements and make the following changes after the existing code.
    • For Apache 1.x
AddModule mod_php5.c

No changes required for another version in Add Module section.

  • Look for ‘AddType’ statements and add below line after the existing statements.
AddType application/x-httpd-php .php

For Apache 2.2.x these statements exist under IfModule mime_module, make the above change there and close the section with </IfModule>.

  • To support files with a different extension just add the extension in the list by adding the below statement.
AddType application/x-httpd-php .phtml
  • In order to indicate the location of your php.ini file, you can add the below statement at the end of the config file.
PHPIniDir "c:/php"
  • After applying the above changes, restart the Apache webserver.
  • Test your PHP code with below code written in a file.
<?php phpinfo(); ?>
  • Save the file with .php extension in the Apache htdocs directory. Open the browser and type “localhost/test.php’, use this URL so that your server is accessed which will run PHP to interpret the code.
  • If you get the desired output then you have implemented the above steps in the right way. Now you can quickly start with the tutorial for learning the basics.
  • In case you do not get the desired result then check whether your PHP or Apache setup is not working. To confirm this, open command prompt and run below command
c:\php\php-cgi test.php
  • If you get the right output then something is wrong with Apache setup. This problem can be avoided by making sure that after making the configuration file changes you have restarted the Apache server.

People are also reading:

Leave a Reply

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