Lab #13 : LAMP
This is an extra credit lab and it applies concepts learned throughout the tutorial. Here you’ll install some PHP and HTML files to run a small application that uploads large text and binary image files.
LAMP (Linux, Apache, MySQL, Perl, PHP, Python) has many derivative labels. It is a platform that deploys Apache Modules instead of CGI (Common Gateway Interface) Server-Side Includes (SSIs). In lieu of Java, C#, or C++, this stack uses common scripting languages, like Perl, PHP, or Python. They each have benefits and hurdles but they’re free of licensing expense to companies that deploy them. Alternatively, you can implement Microsoft’s Internet Information Services (IIS) as a web server.
The following are the most widely used:
- LAMP – Linux, Apache, MySQL, and Perl, PHP, or Python
- MAMP – Mac OS X, Apache, MySQL, and Perl, PHP, or Python
- OLAP – Oracle, Linux, Apache, and Perl, PHP, or Python
- OWAP – Oracle, Windows, Apache, and Perl, PHP, or Python
- SAMP – Solaris, Apache, MySQL, and Perl, PHP, or Python
- WAMP – Windows, Apache, MySQL, and Perl, PHP, or Python
- WIMP – Windows, Internet Information Services (IIS), MySQL, and Perl, PHP, or Python
- XAMPP – Cross (X) Platform, Apache, MariaDB (Open Source MySQL), PHP and Perl
Objectives ↓
All the lab objectives are based on the Fedora instance, and all core components have been deployed. Your lab objectives are limited to the following four things, which you can see in steps below. Expand this section to read a summary of the 10 steps below.
- Create a
lob
subdirectory in the/var/www/html
directory. - Create the following files with the source code provided in the
/var/www/html/lob
directory, or copy the following files from their staged location in Fedora’s instance. The files are stored in the/home/student/Data/cit225/mysql/web
directory.ConvertMySQLBlobToImage.php
MySQLCredentials.inc
UploadItemDescMySQLForm.htm
UploadItemDescMySQL.php
UploadItemBlobMySQLForm.htm
UploadItemBlobMySQL.php
- Create a
client
directory in the/home/student/Data/cit225/mysql
directory. - Create
image
andtext
directories in the/home/student/Data/cit225/mysql/client
directory. - Download & extract the text files into the
/home/student/Data/mysql/client/text
directory. - Download & extract the image files into the
/home/student/Data/mysql/client/image
directory. - Connect to MySQL using the
studentdb
database and run thecreate_mysql_store_ri2.sql
andseed_mysql_store_ri2.sql
script files. - Modify the
item
table by adding both a large text and binary image column in thestudentdb
database. - Test with the
UploadItemDescMySQLForm.htm
form. - Test with the
UploadItemBlobMySQLForm.htm
form.
The following detailed instructions walk you through the lab objectives. They should help you see and understand how relational databases support real time web applications.
- [2 points] Create a
lob
subdirectory in the/var/www/html
directory.
Instruction Details ↓
Open a terminal session. Use the following command to change user account from the student user to the root user:
su - root |
As the root
user, change the directory from the /root
directory to the /var/www/html
directory, and create a lob
subdirectory with the following syntax:
cd /var/www/html mkdir lob |
Change to the lob
directory, and verify the directory with the following commands:
cd /var/www/html/lob pwd |
It should return the following:
/var/www/html/lob |
Keep the session open because you’ll need to use it in future steps. This completes this step.
- [2 points] Create the following files with the source code provided in the
/var/www/html/lob
directory, or copy the following files from their staged location in Fedora’s instance. The files are stored in the/home/student/Data/cit225/mysql/web
directory.
Instruction Details ↓
The program files are in the /home/student/Data/cit225/mysql/web
directory of the Fedora image. If you don’t have the current Fedora image, you can create the files with the following source code as the student
user. After you create the files, you will use the previous root
terminal session to move the files from where you created them to where the Apache server can access them.
MySQLCredentials.inc
1 2 3 4 5 6 7 | <?php // Connection variables. define('HOSTNAME',"localhost"); define('USERNAME',"student"); define('PASSWORD',"student"); define('DATABASE',"studentdb"); ?> |
UploadItemDescMySQLForm.htm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | <html> <head> <title> UploadItemDescMySQLForm.htm </title> </head> <body> <form id="uploadForm" action="UploadItemDescMySQL.php" enctype="multipart/form-data" method="post"> <table border=0 cellpadding=0 cellspacing=0> <tr> <td width=175>Item Number</td> <td> <input id="id" name="id" type="text"> </td> </tr> <tr> <td width=175>Item Title</td> <td> <input id="title" name="title" type="text"> </td> </tr> <tr> <td width=175>Select File</td> <td> <input id="uploadfilename" name="userfile" type="file"> </td> </tr> <tr> <td width=175>Click Button to</td> <td><input type="submit" value="Upload File"></td> </tr> </table> </form> </body> </html> |
UploadItemDescMySQL.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 | <head> <title> Extension of Chapter 8 : UploadItemBlobMySQL.php </title> <style> .e {background-color: #ccccff; font-weight: bold; color: #000000;} .v {background-color: #cccccc; color: #000000;} </style> </head> <body> <?php // Set database credentials. include_once("MySQLCredentials.inc"); // Displayed moved file in web page. $item_desc = process_uploaded_file(); // Return successful attempt to connect to the database. if (!$c = @mysqli_connect(HOSTNAME,USERNAME,PASSWORD,DATABASE)) { // Print user message. print "Sorry! The connection to the database failed. Please try again later."; // Assign the mysqli_error() and format double and single quotes. print mysqli_error(); // Kill the resource. die(); } else { // Declare input variables. $id = (isset($_POST['id'])) ? (int) $_POST['id'] : $id = 21; $title = (isset($_POST['title'])) ? $_POST['title'] : $title = "Harry #1"; // Initialize a statement in the scope of the connection. $stmt = mysqli_stmt_init($c); // Declare a PL/SQL execution command. $sql = "Update item set item_desc = ? where item_id = ?"; // Prepate statement and link it to a connection. if (mysqli_stmt_prepare($stmt,$sql)) { mysqli_stmt_bind_param($stmt,"si",$item_desc,$id); // Execute it and print success or failure message. if (mysqli_stmt_execute($stmt)) { query_insert($id,$title); } else { print "You're target row doesn't exist."; } } // Disconnect from database. mysqli_close($c); } // Query results afret an insert. function query_insert($id,$title) { // Return successful attempt to connect to the database. if (!$c = @mysqli_connect(HOSTNAME,USERNAME,PASSWORD,DATABASE)) { // Print user message. print "Sorry! The connection to the database failed. Please try again later."; // Assign the OCI error and format double and single quotes. print mysqli_error(); // Kill the resource. die(); } else { // Initialize a statement in the scope of the connection. $stmt = mysqli_stmt_init($c); // Declare a SQL SELECT statement returning a CLOB. $sql = "SELECT item_desc FROM item WHERE item_id = ?"; // Prepare statement. if (mysqli_stmt_prepare($stmt,$sql)) { mysqli_stmt_bind_param($stmt,"i",$id); // Execute it and print success or failure message. if (mysqli_stmt_execute($stmt)) { // Bind result to local variable. mysqli_stmt_bind_result($stmt, $desc); // Read result. mysqli_stmt_fetch($stmt); // Format HTML table to display biography. $out = '<table border="1" cellpadding="3" cellspacing="0">'; $out .= '<tr>'; $out .= '<td align="center" class="e">'.$title.'</td>'; $out .= '</tr>'; $out .= '<tr>'; $out .= '<td class="v">'.$desc.'</td>'; $out .= '</tr>'; $out .= '</table>'; // Print the HTML table. print $out; } } // Disconnect from database. mysqli_close($c); } } // Manage file upload and return file as string. function process_uploaded_file() { // Declare a variable for file contents. $contents = ""; // Define the upload file name for Windows or Linux. if (preg_match(".Win32.",$_SERVER["SERVER_SOFTWARE"])) $upload_file = getcwd()."\\temp\\".$_FILES['userfile']['name']; else { // $upload_file = getcwd()."/temp/".$_FILES['userfile']['name']; $upload_file = "/tmp/".$_FILES['userfile']['name']; } // Check for and move uploaded file. if (is_uploaded_file($_FILES['userfile']['tmp_name'])) move_uploaded_file($_FILES['userfile']['tmp_name'],$upload_file); // Open a file handle and suppress an error for a missing file. if ($fp = @fopen($upload_file,"r")) { // Read until the end-of-file marker. while (!feof($fp)) $contents .= fgetc($fp); // Close an open file handle. fclose($fp); } // Return file content as string. return $contents; } ?> </body> </html> |
UploadItemBlobMySQLForm.htm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | <html> <head> <title> UploadItemBlobFormMySQL.htm </title> </head> <body> <form id="uploadForm" action="UploadItemBlobMySQL.php" enctype="multipart/form-data" method="post"> <table border=0 cellpadding=0 cellspacing=0> <tr> <td width=175>Item Number</td> <td> <input id="id" name="id" type="text"> </td> </tr> <tr> <td width=175>Item Title</td> <td> <input id="title" name="title" type="text"> </td> </tr> <tr> <td width=175>Select File</td> <td> <input id="uploadfilename" name="userfile" type="file"> </td> </tr> <tr> <td width=175>Click Button to</td> <td><input type="submit" value="Upload File"></td> </tr> </table> </form> </body> </html> |
UploadItemBlobMySQL.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 | <html> <head> <title> Sample Demonstrating Large Text and Blob Data </title> <style> .e {background-color: #ccccff; font-weight: bold; color: #000000;} .v {background-color: #cccccc; color: #000000;} </style> </head> <body> <?php // Set database credentials. include_once("MySQLCredentials.inc"); // Displayed moved file in web page. $item_blob = process_uploaded_file(); // Return successful attempt to connect to the database. if (!$c = @mysqli_connect(HOSTNAME,USERNAME,PASSWORD,DATABASE)) { // Print user message. print "Sorry! The connection to the database failed. Please try again later."; // Assign the mysqli_error() error and format double and single quotes. print mysqli_error(); // Kill the resource. die(); } else { // Declare input variables. $id = (isset($_POST['id'])) ? (int) $_POST['id'] : 1021; $title = (isset($_POST['title'])) ? $_POST['title'] : "Harry #1"; // Initialize a statement in the scope of the connection. $stmt = mysqli_stmt_init($c); // Declare a PL/SQL execution command. $sql = "UPDATE item SET item_blob = ? WHERE item_id = ?"; // Prepare statement and link it to a connection. if (mysqli_stmt_prepare($stmt,$sql)) { mysqli_stmt_bind_param($stmt,"bi",$item_blob,$id); // Send the binary data. $start = 0; $chunk = 8192; while ($start < strlen($item_blob)) { mysqli_stmt_send_long_data($stmt,0,substr($item_blob,$start,$chunk)); $start += $chunk; } // Execute the PL/SQL statement. if (mysqli_stmt_execute($stmt)) { query_insert($id,$title); } else { print "You're target row doesn't exist."; } } else { print "mysqli_stmt_prepare() failed."; } // Disconnect from database. mysqli_close($c); } // Query results afret an insert. function query_insert($id,$title) { // Return successful attempt to connect to the database. if (!$c = @mysqli_connect(HOSTNAME,USERNAME,PASSWORD,DATABASE)) { // Print user message. print "Sorry! The connection to the database failed. Please try again later."; // Assign the OCI error and format double and single quotes. print mysqli_error(); // Kill the resource. die(); } else { // Initialize a statement in the scope of the connection. $stmt = mysqli_stmt_init($c); // Declare a SQL SELECT statement returning a CLOB. $sql = "SELECT item_desc FROM item WHERE item_id = ?"; // Prepare statement and link it to a connection. if (mysqli_stmt_prepare($stmt,$sql)) { mysqli_stmt_bind_param($stmt,"i",$id); // Execute the PL/SQL statement. if (mysqli_stmt_execute($stmt)) { // Bind result to local variable. mysqli_stmt_bind_result($stmt, $data); // Read result. mysqli_stmt_fetch($stmt); // Format HTML table to display BLOB photo and CLOB description. $out = '<table border="1" cellpadding="5" cellspacing="0">'; $out .= '<tr>'; $out .= '<td align="center" class="e">'.$title.'</td>'; $out .= '</tr>'; $out .= '<tr><td class="v">'; $out .= '<div>'; $out .= '<div style="margin-right:5px;float:left">'; $out .= '<img src="ConvertMySQLBlobToImage.php?id='.$id.'">'; $out .= '</div>'; $out .= '<div style="position=relative;">'.$data.'</div>'; $out .= '</div>'; $out .= '</td></tr>'; $out .= '</table>'; // Print the HTML table. print $out; } else { print "You're target row doesn't exist."; } } // Disconnect from database. mysqli_close($c); } } // Manage file upload and return file as string. function process_uploaded_file() { // Declare a variable for file contents. $contents = ""; // Define the upload file name for Windows or Linux. if (preg_match(".Win32.",$_SERVER["SERVER_SOFTWARE"])) $upload_file = "C:\\temp\\".$_FILES['userfile']['name']; else $upload_file = "/tmp/".$_FILES['userfile']['name']; // Check for and move uploaded file. if (is_uploaded_file($_FILES['userfile']['tmp_name'])) move_uploaded_file($_FILES['userfile']['tmp_name'],$upload_file); // Open a file handle and suppress an error for a missing file. if ($fp = @fopen($upload_file,"r")) { // Read until the end-of-file marker. while (!feof($fp)) $contents .= fgetc($fp); // Close an open file handle. fclose($fp); } // Return file content as string. return $contents; } ?> </body> </html> |
ConvertMySQLBlobToImage.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | <?php // Database credentials must be set manually because an include_once() function // call puts something ahead of the header, which causes a failure when rendering // an image. // Return successful attempt to connect to the database. if (!$c = @mysqli_connect("localhost","student","student","studentdb")) { // Print user message. print "Sorry! The connection to the database failed. Please try again later."; // Assign the OCI error and format double and single quotes. print mysqli_error(); // Kill the resource. die(); } else { // Declare input variables. $id = (isset($_GET['id'])) ? (int) $_GET['id'] : 1023; // Initialize a statement in the scope of the connection. $stmt = mysqli_stmt_init($c); // Declare a SQL SELECT statement returning a MediumBLOB. $sql = "SELECT item_blob FROM item WHERE item_id = ?"; // Prepare statement and link it to a connection. if (mysqli_stmt_prepare($stmt,$sql)) { mysqli_stmt_bind_param($stmt,"i",$id); // Execute the PL/SQL statement. if (mysqli_stmt_execute($stmt)) { // Bind result to local variable. mysqli_stmt_bind_result($stmt, $image); // Read result. mysqli_stmt_fetch($stmt); } } // Disconnect from database. mysqli_close($c); // Print the header first. header('Content-type: image/x-png'); imagepng(imagecreatefromstring($image)); } ?> |
After you have edited these files a student
directory, you can move them as the root
user to where the Apache web server can access them. Assuming you followed the instructions, you should have them in the /home/student/Data/cit225/mysql/web
directory. Open a new session, or use the open session from step 1, and use the su
command to become the root
user.
As the root
user, change directory as follows:
cd /var/www/html/lob |
Copy the files to from the student
‘s directory to this directory with the following syntax:
cp /home/student/Data/cit225/mysql/web/* . |
- [2 points] Create a
client
directory in the/home/student/Data/cit225/mysql
directory./li>
Instruction Details ↓
Open a terminal session. As the student
user, create the client
directory as a subdirectory in the /home/student/Data/cit225/mysql
directory:
cd /home/student/Data/cit225/mysql mkdir client |
Change to the new client
directory and verify the directory with the pwd
command:
cd /home/student/Data/cit225/mysql/client pwd |
It should return the following:
/home/student/Data/cit225/mysql/client |
Keep the session open because will need it in future steps. This completes this step.
- [2 points] Create
image
andtext
directories in the/home/student/Data/cit225/mysql/client
directory.
Instruction Details ↓
Open a terminal session. As the student
user, create the text
and image
directories as subdirectories in the /home/student/Data/cit225/mysql/client
directory:
cd /home/student/Data/cit225/mysql/client mkdir text mkdir image |
Verify the new subdirectories from the client
directory with the pwd
and ls
commands:
pwd ls |
It should return the following:
image text |
Keep the session open because will need it in step 7 when you log into the MySQL database. This completes this step.
- [2 points] You can find the text files that you need to download for the lab in this Text File zip file. Unzip it into the
/home/student/Data/mysql/client/text
directory.
Instruction Details ↓
When you click on the textfiles.zip
link, you should see the following dialog:
Click the OK button to open the textfiles.zip
file:
Click the Extract button to extract the files to the target directory. The /home/student/Data/cit225/mysql/client/text
is the target directory.
This completes downloading the text files.
- [2 points] You can find the text files that you need to download for the lab in this Image File zip file. Unzip it into the
/home/student/Data/mysql/client/image
directory.
Instruction Details ↓
When you click on the imagefiles.zip
link, you should see the following dialog:
Click the OK button to open the imagefiles.zip
file:
Click the Extract button to extract the files to the target directory. The /home/student/Data/cit225/mysql/client/image
is the target directory.
This completes downloading the text files.
- [2 points] Connect to MySQL using the
studentdb
database and run thecreate_mysql_store_ri2.sql
andseed_mysql_store_ri2.sql
script files.
Instruction Details ↓
Open a terminal session if you closed the terminal session from step 4. As the student
user, connect to MySQL with the following syntax:
mysql -ustudent -pstudent -Dstudentdb |
Run the the studentdb
database and run the create_mysql_store_ri2.sql
and seed_mysql_store_ri2.sql
script files with the following syntax:
\. /home/student/Data/cit225/mysql/lib/create_mysql_store_ri2.sql \. /home/student/Data/cit225/mysql/lib/seed_mysql_store_ri2.sql |
Keep the session open because you will modify the item table in step 8. This completes this step.
- [2 points] Modify the
item
table by adding both a large text and binary image column in thestudentdb
database. Label the large text columnitem_desc
and use thetext
data type. Label the binary image columnitem_blob
and use themediumblob
data type. Open the following instruction details to see the statement syntax.
Instruction Details ↓
Use the following ALTER
statement to add the large text and binary image columns to the item
table:
ALTER TABLE item ADD (item_desc TEXT, item_blob MEDIUMBLOB); |
After modifying the table you can verify whether it’s correct by describing it. It should show the following:
+-------------------+------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------------------+------------------+------+-----+---------+----------------+ | item_id | int(10) unsigned | NO | PRI | NULL | auto_increment | | item_barcode | char(18) | NO | | NULL | | | item_type | int(10) unsigned | NO | MUL | NULL | | | item_title | char(60) | NO | | NULL | | | item_subtitle | char(60) | YES | | NULL | | | item_rating_id | int(10) unsigned | NO | MUL | NULL | | | item_release_date | date | NO | | NULL | | | created_by | int(10) unsigned | NO | MUL | NULL | | | creation_date | date | NO | | NULL | | | last_updated_by | int(10) unsigned | NO | MUL | NULL | | | last_update_date | date | NO | | NULL | | | item_desc | text | YES | | NULL | | | item_blob | mediumblob | YES | | NULL | | +-------------------+------------------+------+-----+---------+----------------+ 13 rows in set (0.00 sec) |
After you’ve added the large text and binary large image columns, you have completed this step.
- [2 points] Test with the
UploadItemDescMySQLForm.htm
form.
Instruction Details ↓
You need to launch the UploadItemDescMySQLForm.htm
in the browser of your Fedora image. Use the following URL:
http://localhost/lob/UploadItemDescMySQLForm.htm |
You should then input the following values and select the HarryPotter1.txt
file from the /home/student/Data/student/cit225/client/text
directory:
- Item Number:
1001
- Item Title:
Harry Potter
The completed form should look like the following. Click the Upload File button to continue.
You should see the following result posted, which means the file was copied, uploaded to the server, and then inserted into MySQL.
This completes the test of uploading a large text file.
- [2 points] Test with the
UploadItemBlobMySQLForm.htm
form.
Instruction Details ↓
You need to launch the UploadItemBlobMySQLForm.htm
in the browser of your Fedora image. Use the following URL:
http://localhost/lob/UploadItemBlobMySQLForm.htm |
You should then input the following values and select the HarryPotter1.png
file from the /home/student/Data/student/cit225/client/image
directory:
- Item Number:
1001
- Item Title:
Harry Potter
The completed form should look like the following. Click the Upload File button to continue.
You should see the following result posted, which means the file was copied, uploaded to the server, and then inserted into MySQL.
This completes the test of uploading a large text file.