Autocodewizard Logo Adding PHP to Apache - Autocodewizard Ebook: Web Development Getting Started

Chapter 4: Adding PHP to Apache

In this chapter, we’ll cover how to add PHP to an existing standalone Apache installation (such as Apache24) without using a package like XAMPP. We’ll guide you through downloading PHP, configuring Apache to work with PHP, and editing configuration files like php.ini, httpd.conf, and vhosts to ensure everything is set up correctly.

Step 1: Downloading PHP

First, you’ll need to download the PHP binaries. Visit the PHP for Windows download page and download the latest Thread Safe version of PHP. Make sure to download the ZIP file (not the installer).

Once downloaded, extract the contents of the ZIP file to a folder on your system, for example, C:\PHP. Remember this location as we’ll reference it in Apache’s configuration files.

Step 2: Configuring Apache to Use PHP

To integrate PHP with Apache, you’ll need to edit Apache’s main configuration file, httpd.conf. This file is typically located in the conf folder inside your Apache directory, for example, C:\Apache24\conf\httpd.conf.

Editing the httpd.conf File

Open httpd.conf in a text editor (such as Notepad or VS Code) and add the following lines to load PHP as a module:

        # Load PHP module
        LoadModule php_module "C:/PHP/php8apache2_4.dll"
        
        # Set PHP as the default handler for PHP files
        AddHandler application/x-httpd-php .php
        
        # Define the location of the PHP ini file
        PHPIniDir "C:/PHP"
                

Make sure the paths in these lines match the location where you extracted PHP (e.g., C:/PHP). Save and close the httpd.conf file after making these changes.

Step 3: Configuring PHP with php.ini

Next, configure PHP itself by editing the php.ini file. You’ll find a file called php.ini-development in your PHP folder (e.g., C:\PHP). Rename this file to php.ini.

Open php.ini in a text editor and make the following changes to configure PHP for Apache:

Save and close the php.ini file after making these changes.

Step 4: Configuring Virtual Hosts (Optional)

If you plan to host multiple sites on your local server, you can set up virtual hosts in Apache. Open the httpd-vhosts.conf file, typically located in C:\Apache24\conf\extra\httpd-vhosts.conf.

Add the following configuration for each virtual host, replacing mywebsite.local and C:/path/to/your/site with your site’s details:

        <VirtualHost *:80>
            DocumentRoot "C:/path/to/your/site"
            ServerName mywebsite.local
            <Directory "C:/path/to/your/site">
                AllowOverride All
                Require all granted
            </Directory>
        </VirtualHost>
                

Save and close the file, then add mywebsite.local to your hosts file (usually located at C:\Windows\System32\drivers\etc\hosts), mapping it to 127.0.0.1:

        127.0.0.1 mywebsite.local
                

Step 5: Restart Apache

After configuring httpd.conf, php.ini, and any virtual hosts, restart Apache to apply the changes. You can do this from the command line or using the Apache Monitor tool.

        # Restart Apache
        httpd -k restart
                

Step 6: Testing PHP with Apache

To confirm that PHP is working with Apache, create a file named info.php in your document root (e.g., C:\Apache24\htdocs) and add the following code:

        <?php
        // info.php
        phpinfo();
        ?>
                

Open http://localhost/info.php in your browser. If PHP is configured correctly, you should see a PHP information page displaying configuration details.

Troubleshooting Common Issues

If you encounter issues, check the following:

With PHP successfully added to your Apache server, you’re ready to start developing dynamic web applications locally.