Install and Configure Apache and PHP on Windows

This post explains how to install and configure Apache and PHP on Windows. Sure, there are convenient options like: Bitnami WAMP Stack, WampServer or XAMPP. But over the years I found that installing each stack separately is more reliable and provides greater flexibility. You will probably also want MariaDB for a database which you can download and install from here: https://mariadb.org. The database itself doesn’t have to be configured directly with either Apache or PHP so there are no steps provided here for the database. You only need to enable a line in PHP ini for PHP to provide the database functions.

Versions

  • Windows 10, 64-bit
  • PHP 7.4.x
  • Apache 2.4.x

PHP

Download PHP from the php.net Windows download page. We will use PHP as an Apache module, so make sure to get the Thread Safe version.

After downloading the zip file, unzip it to a convenient location on your hard drive. I usually keep all my stand-alone programs that I use under ~/bin folder. So the location of my unzipped PHP folder is: C:\Users\sanji\bin\php-7.4.27-Win32-vc15-x64

Copy the file php.ini-development and rename it to php.ini, then edit it:

# The default input vars, upload and post sizes are small 
# so I make sure they are big enough
max_input_vars = 5000
post_max_size = 100M
upload_max_filesize = 100M
max_file_uploads = 50
# Only for local development machine, I set the max execution time at 120 seconds
# instead of the default 30 seconds, because some PHP application updates fail 
# locally without a high enough timeout threshold
max_execution_time = 120

# I usually set the full-path because sometimes just specify "ext" doesn't work
extension_dir = "C:\Users\sanji\bin\php-7.4.27-Win32-vc15-x64\ext"

# I have the following extensions enabled which 
# is also good for WordPress development
extension=bz2
extension=curl
extension=fileinfo
extension=gd2
extension=mbstring
extension=exif      ; Must be after mbstring as it depends on it
extension=mysqli
extension=openssl
extension=pdo_mysql

I also added the location “C:\Users\sanji\bin\php-7.4.27-Win32-vc15-x64” to the Path in my Windows Environment Variables under System Properties.

Nothing more needs to be configured for PHP. Let’s get Apache installed and configured.

Apache

Apache binaries are not provided by the official Apache HTTPD project. You can can get either from ApacheHaus or Apache Lounge. Here is the download page screenshot of the binary for Apache 2.4.52 x64 on ApacheHaus:

After downloading the zip file, unzip it at a convenient location on your drive. The full path on my machine is: C:\Users\sanji\bin\Apache24. For convenience, I also added to the environment Path variable “C:\Users\sanji\bin\Apache24\bin” via Windows System Properties.

The file “Apache24\conf\httpd.conf” needs to be edited.

# Change the following to the location where you 
# unzipped the downloaded Apache zip file
Define SRVROOT "/Users/sanji/bin/Apache24"

# I use port 8080 instead of the default port 80
Listen 8080

# I use virtual hosts, so following needs to be enabled 
# and the httpd-vhosts.conf file included
LoadModule vhost_alias_module modules/mod_vhost_alias.so
Include conf/extra/httpd-vhosts.conf

# The following modules are also enabled
LoadModule rewrite_module modules/mod_rewrite.so

# The following settings enable PHP
LoadModule php7_module "/Users/sanji/bin/php-7.4.27-Win32-vc15-x64/php7apache2_4.dll"
<FilesMatch \.php$>
    SetHandler application/x-httpd-php
</FilesMatch>
PHPIniDir "/Users/sanji/bin/php-7.4.27-Win32-vc15-x64"

# I also had to add the following line to get curl working with PHP 
# (aside from enabling curl in php.ini)
LoadFile "/Users/sanji/bin/php-7.4.27-Win32-vc15-x64/libssh2.dll"

ApacheHaus uses “extra/httpd-ahssl.conf” for SSL configuration. I use the port 4430. Here is my “Apache24\conf\extra\httpd-ahssl.conf” change:

# I changed the default port 443 to 4430
Listen 4430 https

Finally here is my vhosts configuration file (located in Apache24\conf\extra\httpd-vhosts.conf).

# I serve all my sites from the folder "C:\Users\sanji\pdn"
# So the following entry affects all my development sites
<Directory "/Users/sanji/pdn">
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
    DirectoryIndex index.html index.php
</Directory>

# I use port 4430 for SSL
# Here are settings for a site called viaye.san, you can create your own by 
# editing the C:\Windows\System32\drivers\etc\hosts file
# To generate and self-sign SSL certificates, please see 
# https://oak.dev/2021/02/03/create-a-self-signed-certificate-on-windows-for-local-development/
# I like to keep my docroot under "www" and the 
# access and error logs under the "logs" folder
<VirtualHost *:4430>
    SSLEngine on
    ServerName viaye.san
    DocumentRoot "/Users/sanji/pdn/viaye/www"
    ErrorLog "/Users/sanji/pdn/viaye/logs/error.log"
    CustomLog "/Users/sanji/pdn/viaye/logs/access.log" common
    SSLCertificateFile "/Users/sanji/_pepper/_ssl_local/viaye.san/cert.pem"
    SSLCertificateKeyFile "/Users/sanji/_pepper/_ssl_local/viaye.san/server.key"
</VirtualHost>

That’s it for configuration. The final step is to add httpd as a Windows service. Head over to C:\Users\sanji\bin\Apache24\bin and run:

httpd -k install

You can now start, restart the Apache HTTPD server from the Windows Services panel as seen below:

The following video shows how to install Apache, PHP and MariaDB on Windows using the steps above.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s