Apache/Php with MySQL/MariaDB - Windows

Specify your computer login – PLEASE DO THIS!!

The standard NetBeansProjects directory is in the Documents subdirectory of your home directory and so it is important to identify then name of your home directory which is dictated by your computer login. If you don't know what your login is, or that you even have such a thing, look for your login as a directory name in the folder
C:\Users\
Key your computer login and click the Set Change button:
Once you've done this, the value keyed in will be used throughout the document to personalize it for your computer.

XAMPP Apache/Php with XAMPP MariaDB

This is the easiest combination to use. We'll assume you've already done the XAMPP Installation. Start up the XAMPP Control Panel as administrator and then start up both the MySQL and Apache services. Verify that the following URL is working:
http://localhost
On Windows, this redirects to the so-called "dashboard" site.

Establish the "/default" URL

I want to map a dedicated URL to the directory which holds your Php projects. For definiteness, I will assume that this is your NetBeansProjects folder. Also, for definiteness, I am going to use this as the dedicated URL:
http://localhost/default
I will also assume the standard NetBeansProjects installation directory:
C:\Users\LOGIN\Documents\NetBeansProjects
Edit the following file (through Notepad++). Scroll to the end and make this addition:

c:\xampp\apache\conf\httpd.conf   (appended)
Alias /default "C:/Users/LOGIN/Documents/NetBeansProjects"
 
<Directory "C:/Users/LOGIN/Documents/NetBeansProjects">
   Options All
   AllowOverride All
   Require all granted
</Directory>
select
Afterwards, restart Apache from the XAMPP Control Panel: Stop it, then Start it.

Activate the target URL to verify that it gives a listing of your NetBeansProjects directory:
http://localhost/default
You should get the empty directory listing with header:
Index of /default

Edit the Php init file

Edit the following file (through Notepad++). Search for timezone. There are two occurrences, both which must be reset from the initial value (Europe/Berlin):

c:\xampp\php\php.ini
...
date.timezone = "America/New_York"
...
date.timezone = "America/New_York"
...
Afterwards, restart Apache as before.

Php Command-line program

The Php command-line program is
c:\xampp\php\php
If you want to make this usable simply as php, then add the folder c:\xampp\php into the PATH. From the Control Panel, select
System ⇾ Advanced system settings ⇾ Environment variables.
Edit for Path in the System Variables. Append this text to the end:
;c:\xampp\php
or prepend it to the beginning (the Windows Home key gets you there immediately):
c:\xampp\php;
In any case we'll refer to the executable simply as "php". Confirm its usage by running this in a command shell to see the version of Php:
> php -v

Php/MySQL Test Programs

Php Information Test Program

Go into the NetBeansProjects directory and create this file:

phpinfo.php
<?php
phpinfo();
 
select
Unfortunately you cannot create "just a file" via NetBeans, so do it more simply. Make sure Windows is showing file extensions! Create an empty text file (.txt) and change the full name to what you want (yes, Windows will complain!). Then open it with Notepad++ and copy the code into the file.

With the file in place, refresh the default URL
http://localhost/default
You should see the file appear in the listing where you can activate it.

Php PDO/MySQL test program

If necessary, prepare the standard MySQL setup with the test database accessed by the guest user with empty password. Test it first:
> mysql -u guest test -p
Create and run the following program in the web server:

mysql-pdo-test.php
<h3>The Php MySQL PDO interface</h3>
<pre>
<?php 
try {
  $database = "test";
  $user = "guest"; 
  $pass = "";
  $table = "test_tab";
 
  $url = "mysql:host=127.0.0.1;dbname=$database";
  //$url = "mysql:host=localhost;dbname=$database";
 
  echo "--> connect url = $url \n";
  $cx = new PDO($url, $user, $pass);
 
  # generate exceptions on error
  $cx->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
 
  $sql = "CREATE TABLE IF NOT EXISTS $table (thing VARCHAR(10))";
  echo "--> $sql\n";
  $cx->exec( $sql );
 
  $sql = "INSERT INTO $table VALUES ('book'),('pencil')";
  echo "--> $sql\n";
  $cx->exec( $sql );
 
  $sql = "SELECT thing FROM $table";
  echo "--> $sql\n";
  $res = $cx->query( $sql );
  foreach ($res as $row) {
    echo $row['thing'], " ";
  }
  echo "\n";
 
  $sql = "DROP TABLE $table";
  echo "--> $sql\n";
  $cx->exec( $sql );
 
  echo "\nSUCCESS!\n";
} 
catch (PDOException $e) {
  die( "Error: " . $e->getMessage() . "\n" );
}
select
Refresh the default URL to pick up the added file and activate this to see final SUCCESS message.

You should also be able to run it from the shell, seeing the HTML output. Open a shell and navigate to the NetBeansProjects folder and run:
> php mysql-pdo-test.php

XAMPP Apache/Php with Native MySQL

This assumes you have installed XAMPP as in XAMPP on Windows and have installed MySQL as in MySQL on Windows. Start the XAMPP control panel, but only start up Apache. Simply do all the steps in the previous 2 sections. Everything should work.

Native Apache/Php with Native MySQL

This assumes you have installed MySQL as in MySQL on Windows. Here we are not going to use XAMPP at all.

Let's assume that you have the "Windows Essentials" in place as described in NetBeans/JDK on Windows. In particular, you want Notepad++ for editing and 7zip for extracting. You'll also definitely need to see file extensions.

Apache/Php Sites

The Php home site for Windows is
http://windows.php.net
The Php choice suitable for use within Apache is the thread-safe version.

According to the suggestions on the Php site, you should use Apache binaries found at the "Apache Lounge":
http://www.apachelounge.com

Download Software

The most significant choice for which package to download is based on whether your machine is 32-bit or 64-bit. For simplicity, I'll assume it is 64-bit. If your computer is 32-bit, you'll have to make the appropriate adjustments.

The next choice is whether to download the most recent version or an older version. I'll assume the most recent one (as of January 2017), compiled by VC14. These are the downloads:
Thread-safe Php:   php-7.1.0-Win32-VC14-x64.zip
Apache:          httpd-2.4.25-win64-VC14.zip
C++ Redistributable Visual Studio You also may need to install Microsoft's Visual C++ Redistributable for Visual Studio 2015 package installed on your computer. I would suggest waiting to see if "Apache service" installation fails before fixing the problem (you should get the error about a missing DLL).

The Apache Lounge Download site provides a link just above the Apache 2.4 binaries section. It says:
Be sure that you have installed the latest C++ Redistributable Visual Studio 2015 : vc_redist_x64/86.exe.
Look for the download file (assuming 64-bit) and install it (note the software you download is vc_redist.*, not vc_redist_*):
vc_redist.x64.exe

Running a command shell as administrator

For many purposes we need to run the command shell in this way:
  1. Click the Start button
  2. Type "cmd" into the search field. Don't hit return.
  3. Wait for an entry with the name icon "cmd.exe" to appear in the Programs list. Right-click on this entry and select "Run as administrator". Say yes when Windows starts throwing a fit.
A useful thing to do is to locate the command shell in Accessories and copy it as a shortcut on your Desktop. When controlling Apache, right-click and select "Run as administrator".

Install Apache and Apache Service

Right-click on the http archive
http-DOWNLOADED_ARCHIVE.zip
Open the archive with 7zip. Then simply drag Apache24 into the C:\ folder and close the archive.

Bring up an administrative command shell according to the previous section. From the command shell access the Apache bin directory:
> cd C:\Apache24\bin
You may want to leave this command shell open throughout the development for ease of access.

To install the Apache service type this command:
> httpd -k install
If it is OK, you should see feedback something like:
Installing the Apache2.4 service
The Apache2.4 service is successfully installed.
Testing httpd.conf....
Errors reported here must be corrected before the service can be started.
You may be queried to open a firewall entry for the Apache. Go ahead and "Allow access".

Start and control Apache service

After installation, start the Apache service (from the administrative command shell) by doing:
> httpd -k start
We can completely control the Apache service with the following commands (with obvious effects):
> httpd -k start
> httpd -k restart    (mostly you'll do this one)
> httpd -k stop
After starting the service, test it out:
http://localhost
You should see the congratulatory "It Works!" message. Note that you can test configuration changes by:
> httpd -t

Edit the Apache config file and make a "/default" URL

Edit the file
C:\Apache24\conf\httpd.conf
Right-click on it and select "Edit with Notepad++". Go to the end of the file, add a few blank lines and this:

C:\Apache24\conf\httpd.conf   (appended)
ServerName localhost
 
Alias /default "c:/Users/LOGIN/Documents/NetBeansProjects"
 
<Directory "c:/Users/LOGIN/Documents/NetBeansProjects">
   Options All
   AllowOverride All
   Require all granted
</Directory>
select
To get the right path name, simply navigate Windows Explorer to the NetBeans folder, right-click on the location bar and select Copy Address, then paste it in to the two directories listed.

Forward or back slash directory separators are both OK, but no terminating separator.

Restart Apache and Test

From our administrative command shell, type:
> httpd -k restart
If there are no errors, test with this link:
http://localhost/default
You should see the contents of the NetBeansProjects directory listed.

Install Php

From the c:\ folder, create the subfolder
c:\php
Then right-click on the Php archive
php-DOWNLOADED_ARCHIVE.zip
Open with 7zip. This time click the Extract button and choose the newly-create folder
c:\php
First, copy this file into C:\Windows\System32:
c:\php\php7ts.dll
Then access the already-opened Apache configuration file in Notepad++. Go to the end and add this:

C:\Apache24\conf\httpd.conf   (appended)
PHPIniDir "C:/php/" 
LoadModule php7_module "C:/php/php7apache2_4.dll" 
AddType application/x-httpd-php .php 
DirectoryIndex index.php
select

Restart Apache and Test

From our administrative command shell, type:
> httpd -k restart
If there are no errors, go into the NetBeansProjects directory and create this file see the Test Programs section above.

phpinfo.php
<?php
phpinfo();
select
You cannot create "just a file" via NetBeans, and Windows makes it painful to create a file with the .php extension in a simple way. Create an empty text file (.txt) and change the full name (yes, Windows will scream!). Then open it with Notepad++ and copy the code into the file.

Refresh the URL:
http://localhost/default
Locate the newly-created phpinfo.php file and activate it.

Create the Php configuration file

When Apache restarts the Php module will reload its own configuration taken from the file:
C:\php\php.ini
If this init file is not found, default Php configurations settings are used.

There are two suggested files which can be serve as a basis:
php.ini-development    and     php.ini-production
We will use the former because our interest is development.

Make sure that you are viewing files extensions! Select and copy
php.ini-development
Paste the copy back into C:\php and rename it:
php.ini

Create a Php session folder

In the default Php configuration, session support assumes the existence of a folder which does not exist on Windows. Within the C:\php folder, create a new subfolder by the name
C:\php\tmp

Edit the Php configuration file

The php.ini file is rather large, and so it is often easiest to use a search utility in the editor to locate the relevant configuration setting. Right-click on
C:\php\php.ini
and select Edit with Notepad++. Look for and modify these settings:
  1. Default Timezone. Search for the "date.timezone" and make the setting (suitable for the Middle Atlantic region):
    date.timezone = "America/New_York"
    
    Make sure you remove the initial ";" comment character.
  2. Specify the session folder. We created the target folder in the previous section:
    C:\php\tmp
    
    Search for and modify this setting (forward or backward slashes are both OK):
    session.save_path = "C:/php/tmp"
    
  3. Set the extension directory. Php extensions are DLL files which provide extended functionality to Php. Look for "extension_dir" and change the default setting to this:
    extension_dir = "C:/php/ext/"
    
  4. Add the PDO database extensions. Look for the "Windows Extensions" section and within it, look for and uncomment these lines by removing the initial semicolons:
    extension=php_pdo_mysql.dll
    extension=php_pdo_sqlite.dll
    
    It's easiest to search for "php_pdo"
  5. Error display settings (this is probably OK). In a production environment, error display should always be turned off so as to avoid the inadvertent revealing of server error information. Error display is extremely convenient for debugging in a development environment.

    Search for display_errors and verify that it is on:
    display_errors = On
    
  6. Error reporting level (this is probably OK). You'll also want the full error_reporting setting. Search for and verify this setting:
    error_reporting = E_ALL
    
Afterwards, restart Apache from an administrative command shell in c:\Apache24\bin:
> httpd -k restart

Test sessions

In the NetBeansProject folder, create the file

session-test.php
<?php
session_start();     // the FIRST THING YOU DO!
 
//unset( $_SESSION['testing'] );
 
if (!isset($_SESSION['testing'])) {
  echo "unset";
  $_SESSION['testing'] = 0;
} 
else {
  echo ++$_SESSION['testing'];
}
select
Refresh the URL:
http://localhost/default
Within the directory listing you should see the file. Activate it and refresh the browser repeatedly. You should see the sequence of responses:
unset, 1, 2, 3, ...

Test MySQL access

The test program to run is mysql-pdo-test.php described in the Test Programs section above.

Add c:\php to system PATH

From Control Panel, select System → Advanced system settings → Environment variables. You can modify either the PATH either as a User or System Variable. Look for Path in one of these two lists. The easiest thing to do is to append this text to the end of the variable definition:
;C:\php
or prepend to the beginning this text (use the Home button):
C:\php;
Verify that the command-line Php is working by entering this into a command shell:
> php -v
You can also double-check the presence of C:\php in the PATH by:
> path

Speed up localhost access

Hopefully this section is not necessary, but if for some reason access to localhost sites seems overly delayed, it may be because the Windows' name resolution for localhost is too slow.

A possible fix is to specify the definition of the localhost site in the Windows hosts file:
C:\Windows\System32\drivers\etc\hosts
An easy, quick-and-dirty approach is to start up an administrative command shell. Change to the desired directory:
cd C:\Windows\System32\drivers\etc
and then run the notepad editor:
notepad hosts
Look for the lines
# localhost name resolution is handled within DNS itself
#       127.0.0.1  localhost
#       ::1        localhost
Simply uncomment the IP4 line:
       127.0.0.1  localhost
Save the changes and exit. The fix should be immediate.


© Robert M. Kline