Apache/Php with MySQL - Ubuntu

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
/home/
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.

Preliminaries

This assumes you have installed MySQL as in MySQL on Ubuntu. The setup described adds the native Apache/Php for Ubuntu version 16.04. It should work equally well on Linux Mint 18.

Editors

In many situations you will need to edit your own files or system configuration files (as root). I would recommend one of these two:
nano       non-graphical
gedit      graphical
For your files:
$ nano <MY FILE>
$ gedit <MY FILE>
For system files:
$ sudo nano <MY FILE>
$ sudo gedit <MY FILE>

Software installations

Both Php versions 5 and 7 are supported. We'll use version 7. Install these packages:
$ sudo apt install \
apache2 php libapache2-mod-php php-cli php-mysql php-cgi \
php-curl php-json php-mcrypt php-apcu php-gd php-xml \
php-mbstring php-gettext php-sqlite3 sqlite3 \
It's a bit of overkill, but they may be useful later.

Apache

The Apache Web service will start automatically after installation. Check it out by looking at the site:
http://localhost
To start the Apache service (if necessary), do:
$ sudo service apache2 start 
For the most part you'll want to either restart or reload the service by doing
$ sudo service apache2 restart 
or
$ sudo service apache2 reload 
The main Apache configuration file is:
/etc/apache2/apache2.conf
Apache also spreads out configuration information into other files within subdirectories. For simplicity, we'll use this single file.

Upon restarting, Apache may complain
apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress this message
To fix this edit the configuration file:
$ sudo nano /etc/apache2/apache2.conf
Scroll to the bottom and add this line:
ServerName localhost
Then restart Apache to observe the warning disappear.

Create the "/default" URL

There are better places to put the code we need, but for simplicity, we'll use the global configuration file, so edit:
$ sudo nano /etc/apache2/apache2.conf
and add this code:
Alias /default "/home/LOGIN/NetBeansProjects"
 
<Directory "/home/LOGIN/NetBeansProjects">
   Options All
   AllowOverride All
   Require all granted
</Directory>
select
Restart Apache:
$ sudo service apache2 restart 
and then test:
http://localhost/default
to see the listing of your NetBeansProjects directory. If this fails it may be that the permissions on your home directory are blocking Apache access. The easiest fix is this:
$ chmod 755 ~/

The Php configuration files

Ubuntu supports 3 versions of the php.ini config file, two of which are in place by virtue of the initial installations: You need to modify these Php init files. Start by:
$ sudo nano /etc/php/7.0/apache2/php.ini
Search for "timezone" and "display_errors" and make these changes:

/etc/php/7.0/apache2/php.ini
...
date.timezone = "America/New_York"
...
display_errors = On
...
Afterwards, restart Apache as before. Do the same thing for the Command-line Php init file:
$ sudo nano /etc/php/7.0/cli/php.ini

Test Programs

Info Test Program

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.

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


© Robert M. Kline