Apache | Configuration for Using PHP | Adding the Module (php7_module) and Registering the Extension (.php)

This article explains how to add the module required to use PHP through Apache, php7_module, and how to register the .php extension.

Adding the module

First, add the module required to use PHP. Open the Apache configuration file, Apache24/conf/httpd.conf, in a text editor and search for Dynamic Shared Object (DSO) Support. You will find content like the following.

#
# Dynamic Shared Object (DSO) Support
#
# To be able to use the functionality of a module which was built as a DSO you
# have to place corresponding `LoadModule' lines at this location so the
# directives contained in it are actually available _before_ they are used.
# Statically compiled modules (those listed by `httpd -l') do not need
# to be loaded here.
#
# Example:
# LoadModule foo_module modules/mod_foo.so
#
#LoadModule access_compat_module modules/mod_access_compat.so
LoadModule actions_module modules/mod_actions.so

.. (omitted) ..

#LoadModule watchdog_module modules/mod_watchdog.so
#LoadModule xml2enc_module modules/mod_xml2enc.so

The modules that can be added dynamically are written here. If you are using Apache 2.4, add the following line at the end of this block. This example assumes PHP 7.

#LoadModule watchdog_module modules/mod_watchdog.so
#LoadModule xml2enc_module modules/mod_xml2enc.so
LoadModule php7_module C:/apache/php-7.4.0-Win32-vc15-x64/php7apache2_4.dll

Change the path of the DLL file to match the environment where PHP is installed.

If you check the directory where PHP is installed, you can confirm that the DLL file above exists.

C:\apache\php-7.4.0-Win32-vc15-x64>dir
 C 드라이브의 볼륨에는 이름이 없습니다.
 볼륨 일련 번호: 4E53-ECB8

 C:\apache\php-7.4.0-Win32-vc15-x64 디렉터리

2019-12-05  오후 11:06    <DIR>          .
2019-12-05  오후 11:06    <DIR>          ..
2019-11-27  오후 08:24           119,808 deplister.exe
2019-12-05  오후 11:06    <DIR>          dev
2019-12-05  오후 11:06    <DIR>          ext
2019-12-05  오후 11:06    <DIR>          extras
2019-11-27  오후 08:24         1,374,720 glib-2.dll
2019-11-27  오후 08:24            18,432 gmodule-2.dll
2019-11-27  오후 08:24        27,980,288 icudt65.dll
2019-11-27  오후 08:24         2,632,192 icuin65.dll
2019-11-27  오후 08:24            61,440 icuio65.dll
2019-11-27  오후 08:24         1,890,304 icuuc65.dll
2019-12-05  오후 11:06    <DIR>          lib
2019-11-27  오후 08:24         3,422,208 libcrypto-1_1-x64.dll
2019-11-27  오후 08:24            47,616 libenchant.dll
2019-11-27  오후 08:24           282,624 libpq.dll
2019-11-27  오후 08:24           108,544 libsasl.dll
2019-11-27  오후 08:24           300,544 libsodium.dll
2019-11-27  오후 08:24         1,301,504 libsqlite3.dll
2019-11-27  오후 08:24           225,792 libssh2.dll
2019-11-27  오후 08:24           679,424 libssl-1_1-x64.dll
2019-11-27  오후 08:24             3,272 license.txt
2019-11-27  오후 08:24            18,782 news.txt
2019-11-27  오후 08:24           197,120 nghttp2.dll
2019-11-27  오후 08:30                43 phar.phar.bat
2019-11-27  오후 08:30            53,231 pharcommand.phar
2019-11-27  오후 08:24            72,704 php-cgi.exe
2019-11-27  오후 08:24            36,864 php-win.exe
2019-11-27  오후 08:24           128,512 php.exe
2019-11-27  오후 08:24            74,599 php.ini-development
2019-11-27  오후 08:24            74,904 php.ini-production
2019-11-27  오후 08:24            35,840 php7apache2_4.dll <------------ 여기 있다.
2019-11-27  오후 08:24           890,770 php7embed.lib
2019-11-27  오후 08:24           344,064 php7phpdbg.dll
2019-11-27  오후 08:24         9,496,064 php7ts.dll
2019-11-27  오후 08:24           346,112 phpdbg.exe
2019-11-27  오후 08:24            30,257 readme-redist-bins.txt
2019-11-27  오후 08:24             4,846 README.md
2019-12-05  오후 11:06    <DIR>          sasl2
2019-11-27  오후 08:24             2,224 snapshot.txt
              33개 파일          52,255,648 바이트
               7개 디렉터리  450,575,360,000 바이트 남음

C:\apache\php-7.4.0-Win32-vc15-x64>

With this, the PHP module is loaded when Apache starts.

Registering the extension (.php)

Connect the extension used by PHP, .php, with PHP. By registering it, when a file with the .php extension is accessed, Apache returns the result executed using PHP to the client. Write the following at the bottom of the httpd.conf file.

<FilesMatch "\.php$">
  AddHandler php7-script .php
  AddType application/x-httpd-php .php
</FilesMatch>

This registers the extension.

Specifying the php.ini location (PHPIniDir)

Specify the path where the PHP configuration file, php.ini, is installed. Open the httpd.conf file and write the following at the end.

PHPIniDir "C:/apache/php-7.4.0-Win32-vc15-x64"

This example assumes that the php.ini file is installed in the C:/apache/php-7.4.0-Win32-vc15-x64 directory. Change it to match your environment. If php.ini does not exist after the initial installation, copy php.ini-development or php.ini-production and change the extension to ini.

With this, Apache can reference the php.ini file at the specified location.

After changing the httpd.conf file, restart Apache to apply the new settings.