Apache/Php with MySQL/MariaDB - MAC

Specify your computer login – PLEASE DO THIS!!

The standard NetBeansProjects directory is in 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
/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.

The MAC Native Apache may already be running on your system. What might happen is the XAMPP Apache service stops by itself when you try to start it. The remedy is to disable MAC OS Apache (at least temporarily) by:
$ sudo apachectl stop
To be more sure that you're using the right one:
$ sudo /usr/sbin/apachectl stop
Verify that the following URL is working:
http://localhost
On MAC this will either give the default "It Works" message, or else redirect 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:
/Users/LOGIN/NetBeansProjects
Edit the following file (via nano). You may have to edit as administrator:
$ cd /Applications/XAMPP/xamppfiles/apache2/conf/
$ sudo nano httpd.conf
Scroll to the end and make this addition:

/Applications/XAMPP/xamppfiles/apache2/conf/httpd.conf   (appended)
Alias /default "/Users/LOGIN/NetBeansProjects"
 
<Directory "/Users/LOGIN/NetBeansProjects">
   Options All
   AllowOverride All
   Require all granted
</Directory>
select
Afterwards, restart Apache from the XAMPP Control Panel.

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 (via nano, or sudo nano). Search for timezone. There are two occurrences, both which must be reset from the initial value (Europe/Berlin):

/Applications/XAMPP/etc/php.ini
...
date.timezone = "America/New_York"
...
date.timezone = "America/New_York"
...
select
Afterwards, restart Apache.

Php Command-line program

All the XAMPP command line programs are in the directory:
/Applications/XAMPP/bin
The Php command-line program is
/Applications/XAMPP/bin/php
If you want to make this usable simply as "php", then add the folder /Applications/XAMPP/bin into the PATH. You have probably already done so for the mysql executable. Confirm by checking:
$ echo $PATH
If necessary, edit ~/.profile adding an expression like this (no blanks around the "="):
PATH=/Applications/XAMPP/bin:$PATH
Open a new terminal and check again. 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

Test Programs

Php Information Test Program

Go into the NetBeansProjects directory and create this file (via nano):

phpinfo.php
<?php
phpinfo();
select
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 MAC and have installed MySQL as in MySQL on MAC. 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 MAC. Here we are not going to use XAMPP at all.

Apache and Php are already installed on the MAC. There is usually some gimmick you have to go through to have Apache run as a service, but will at least serve temporarily to start it up as follows:
$ sudo apachectl start
Verify that Apache is running by testing this URL:
http://localhost

The apachectl service

On the MAC you will need to use the executable (as root)
/usr/sbin/apachectl
You should be able to use this just as sudo apachectl with these commands:
$ sudo apachectl start       start Apache
$ sudo apachectl stop        stop Apache
$ sudo apachectl restart     restart Apache after modifications

Project Directory

On the MAC, the standard NetBeans directory is this:
/Users/LOGIN/NetBeansProjects
Certain directories on the MAC are unsuitable for web programming. For example, the /Users/LOGIN/Documents directory permits only owner access, which means the Apache cannot "pass through" to read web scripts.

Configuration

The Apache-related files you will need to edit are all found in
/etc/apache2
The main configuration file is
/etc/apache2/httpd.conf

Create the "/default" URL

Check the Apache version:
$ /usr/sbin/apachectl -v
You'll see either 2.2.x or 2.4.x. Edit the main Apache configuration file:
$ sudo nano /etc/apache2/httpd.conf
scroll down to the end of the file. Choose one or the other In any case, always avoid any terminal "/" in the directory names.

Restart Apache and check that "/default" works:
$ sudo apachectl restart
Activate this URL:
http://localhost/default
You should see the contents of the NetBeansProjects directory listed.

The Php configuration file: php.ini

The file /etc/php.ini is Php's main configuration file. On the MAC this serves as the configuration file for both the Apache and the command-line versions.

If it does not exist, Php uses certain default settings. For our purposes we need to override these default settings. You can see if it exists by running this command from a shell:
$ ls /etc/php.*
If it doesn't exist, MAC will provide a suitable starter file with all the default settings:
/etc/php.ini.default
You have to copy this into the official init file:
$ sudo cp /etc/php.ini.default /etc/php.ini
You need to modify the Php init file (via sudo-nano unless you prefer something else):
$ sudo nano /etc/php.ini
Search for timezone:

/etc/php.ini
...
date.timezone = "America/New_York"
...
Also, error display needs to be enabled in a development environment. Search for display_errors. If it is off, turn it on:

/etc/php.ini
...
display_errors = On
...
Afterwards, restart Apache as before.

Verify that Apache/Php is working

The Native Php version is MAC OS X (up to 10.12) is version 5, which is perfectly fine. Double-check that the Php module is being loaded.
$ sudo nano /etc/apache2/httpd.conf
Search for "php", looking for the line:
LoadModule php5_module libexec/apache2/libphp5.so
If it is commented out with a leading "#" character, remove that character and restart Apache:
$ sudo apachectl restart
Go into the NetBeansProjects directory and create this file:
<?php
phpinfo();
select
You cannot create "just a file" via NetBeans. The simplest way to create it may be using nano (as normal user) in a terminal shell:
$ cd ~/NetBeansProjects
$ nano phpinfo.php
With this in place, refresh the default URL:
http://localhost/default
You should see the contents of the phpinfo.php in the listing. Activate it to see the Apache/Php information listed.

Test MySQL access

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

MySQL network vs file socket access

The mysql-pdo-test.php test program illustrates two allegedly equivalent ways to connect to the database via either of the URLs:
  $url = "mysql:host=127.0.0.1;dbname=$database";
  //$url = "mysql:host=localhost;dbname=$database";
In UNIX-like systems (MAC and Linux), the connection via "127.0.0.1" will attempt to connect to MySQL via the network socket on port 3306, which is guaranteed to succeed no matter which version of SQL is running.

In contrast, the connection via "localhost" will attempt to connect to MySQL by a file socket which varies based on the version of Php being used.

In particular, if we want to use the "localhost" version, we must prepare Php to connect to the dedicated MySQL socket. You would have to edit /etc/php.ini and set this value:

/etc/php.ini
...
pdo_mysql.default_socket = /tmp/mysql.sock
...


© Robert M. Kline