Set up Apache, PHP, and MySQL (MariaDB) in Windows under Cygwin

This is my preferred way of setting up Apache, PHP, and MySQL on a Windows system. Note that this guide is not intended so much for setting up a fully-hardened production system, but it is very useful for a local setup for a development system.

This information is updated to include a recent change to Apache 2.4 for Cygwin. The old command to configure the Apache web server /usr/sbin/httpd2-config is now obsolete. The new command is /etc/rc.d/init.d/httpd install

Also note that the old Cygwin package apache2-mod_php5 is now called httpd-mod_php7 and the apache2 package is called httpd.

One other thing to note is that the package mysql in Cygwin is actually MariaDB.

See below for full instructions.

1. Install Cygwin or Cygwin64

2. During install, select the following packages to install:

httpd libapr1-devel libaprutil1-devel httpd-mod_php7 php7 mysql mysql-client

If you want other optional PHP components, select those also. Here are a few popular examples:

php-fileinfo php-mysqli php-gd php-Archive_Tar php-bz2 php-zlib

3. After the install is finished, open a Cygwin terminal as Administrator and run:

cygserver-config (answer “yes” to prompt)

/etc/rc.d/init.d/httpd install

4. Start cygserver and httpd:

From within the same Cygwin terminal running as Administrator, run:

cygrunsrv -S cygserver

cygrunsrv -S httpd

5. Everything should be working. If there was an error at startup look at /var/log/httpd/error_log

6. Reload Apache with the new configuration:

from the Administrator terminal run:

/etc/rc.d/init.d/httpd reload

8. The document root is at:

/srv/www/htdocs/

Set up MariaDB (MySQL)

Note that this actually sets up MariaDB which is named mysql in Cygwin.

from the Administrator terminal run:

mysql_install_db

Start the mysql server:

mysqld_safe &

then run:

mysql_secure_installation

You should then be able to log in to MariaDB from a non-administrative shell with:

mysql -u root -p

Install the MariaDB service

First stop any running instance (run in an admin shell):

mysqladmin.exe -u root -p shutdown

Then install the service:

cygrunsrv.exe -I mysqld -d "Cygwin MariaDB server" -p /usr/bin/mysqld_safe

You can start it with:

cygrunsrv -S mysqld

Updating Cygwin

Updating is an important part of maintaining security and ensuring you have the latest packages with bugfixes and improvements.

Here’s convenient way to easily update Cygwin:

1. If you don’t have a bin directory inside your home directory, create it:
mkdir ~/bin

2. Make sure ~/bin is included in your $PATH. Edit ~/.bash_profile and make sure your PATH statement includes ~/bin. It should look something like:
PATH="${HOME}/bin:${PATH}"

3. Create a script in ~/bin called setup. Use your favorite editor. It should contain:
#!/bin/sh
cygstart -- /setup-x86_64.exe &

Note the above code assumes that you have the Cygwin setup file in the root directory of your Cygwin installation (for example C:\cygwin64\setup-x86_64.exe)

Run a terminal as Administrator and then just type setup and press enter to start the Cygwin setup/update process. For updating just click next/ok for each dialog Window.


References: sourceware.org: Re: New apache 2.4 package

Troubleshooting
If something goes wrong, look at the logfile /var/log/httpd.log

I had the following error when trying to start httpd:

(20014)Internal error (specific information not available): AH00058: Error retrieving pid file /var/run/httpd/httpd.pid
AH00059: Remove it before continuing if it is corrupted.

What happened is that Apache probably crashed in the past and the PID file in /var/run never got deleted. This was solved by manually deleting /var/run/httpd/httpd.pid and restarting httpd.