    <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:admin="http://webns.net/mvcb/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:content="http://purl.org/rss/1.0/modules/content/">
     <channel>
        <title>ACCU  :: LAMP on Ubuntu</title>
        <link>https://members.accu.org/index.php/articles/2075</link>
        <description>Professionalism in Programming</description>
        <dc:language>en-us</dc:language> 
        <dc:creator>Administrator</dc:creator> 
        <admin:generatorAgent rdf:resource="http://www.xaraya.org" /> 
        <admin:errorReportsTo rdf:resource="mailto:webeditor@accu.org" />
       <sy:updatePeriod>hourly</sy:updatePeriod>
       <sy:updateFrequency>1</sy:updateFrequency>
       <docs>http://backend.userland.com/rss</docs>




<div class="xar-mod-head"><span class="xar-mod-title">Programming Topics + CVu Journal Vol 27, #1 - March 2015</span></div>

<table border="0" cellpadding="1" cellspacing="0">
    <tbody>
    <tr>
        <td valign="top">
            Browse in :
       </td>
       <td valign="top">

                                            <a href="https://members.accu.org/index.php/articles/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c13/">Topics</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c65/">Programming</a>
<br />

                                            <a href="https://members.accu.org/index.php/articles/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c76/">Journals</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c77/">CVu</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c347/">271</a>
<br />

                                            <a href="https://members.accu.org/index.php/articles/c65-347/">Any of these categories</a>

                    -                        <a href="https://members.accu.org/index.php/articles/c65+347/">All of these categories</a>
<br />
</td>
   </tr>
   </tbody>
</table>




<div class="xar-error">
   <p>
 <strong>Note:</strong> when you create a new publication type,
the articles module will automatically use the templates
<em>user-display-[publicationtype].xt</em>
and <em>user-summary-[publicationtype].xt</em>.
If those templates do not exist when you try to preview or display a new article,
you'll get this warning :-)  Please place your own templates in themes/<em>yourtheme</em>/modules/articles . The templates will get the extension .xt there. </p>
</div>
<div class="xar-norm xar-standard-box-padding">
   <h1><strong>Title:</strong>&nbsp;LAMP on Ubuntu</h1>
<p><strong>Author:</strong>&nbsp;Martin Moene</p>
<p>
<strong>Date:</strong> 08 March 2015 20:58:50 +00:00 or Sun, 08 March 2015 20:58:50 +00:00</p>
<p><strong>Summary:</strong>&nbsp;Ian Bruntlett shares his notes on setting up a basic web application.</p>
<p><strong>Body:</strong>&nbsp;<p>Sometime in 2014 I started working my way through an Internet programming book â€“ <em>Learning PHP, MySQL, JavaScript, CSS &amp; HTML5</em> (LPMJ). Whilst it is a good guide, I had to research certain system administration details myself. This document was written using my personal notes, text books and a computer with a fresh install of Ubuntu 14.10. As approved of by that book, I have reused some of their examples. Please note this is a study exercise â€“ commercial websites are likely to do things differently for scalability and security.</p>

<p>First my personal preferences. I installed Synaptic Package Manager because I like its front end â€“ it helps me explore the packages available. I also installed ttf-mscorefonts-installer which in turn installs some free fonts from Microsoft â€“ in particular it provides me with Times New Roman and Comic Sans MS. I also installed an editor â€“ emacs â€“ because I like it :)</p>

<h2>Packages to install..</h2>

<p>Here are the actual names of the packages involved:</p>

<ul>
	<li>apache2, apache2-doc â€“ this is the webserver and its documentation in particular /usr/share/doc/apache2-doc/manual/en/index.html</li>
	<li>mysql-server, mysql-client (for the server you will need to decide on a â€˜rootâ€™ MySQL password â€“ this is not the same as your â€˜rootâ€™ Linux password). Do not lose it.</li>
	<li>php5, php-doc (manual in /usr/share/doc/php-doc/html/index.html)</li>
	<li>php5-mysql</li>
</ul>

<h2>Setting files up</h2>

<p>Start up a terminal window/shell window. When you are typing these commands, replace <code>ian</code> with your username. Type in this command:</p>

<pre class="programlisting">
  sudo chown ian:ian /var/www/html</pre>

<p>The directory /var/www/html is used by Apache to find its HTML files. This command changes the owner and group ID of that folder so that you can put your HTML files there.  There is an index.html file, owned by root, already in that directory. To view it, start a web-browser and type in an address of localhost. You should see the Apache2 Ubuntu Default Page. As we are going to modify that file for our own purposes, do this:</p>

<pre class="programlisting">
  cd /var/www/html
  mv index.html index_original.html</pre>
  
<p>From now on I am going to refer to var/www/html as â€˜HTML Homeâ€™.</p>

<h2>Our first webpage</h2>

<p>Still in HTML Home, start a text file and type in Listing 1, saving it as <span class="filename">index.html</span>.</p>

<table class="sidebartable">
	<tr>
		<td>
			<pre class="programlisting">
&lt;!DOCTYPE html&gt;
&lt;html&gt;
  &lt;head&gt;
    &lt;title&gt;Ian's LAMP index.html&lt;/title&gt;
    &lt;meta http-equiv=&quot;Content-Type&quot; 
          content=&quot;text/html;charset=utf-8&quot;&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;h2&gt;Ian's LAMP files test area&lt;/h2&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;http://validator.w3.org/&quot;&gt;
         Site to validate HTML5 files.&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/body&gt;
&lt;/html&gt;
			</pre>
		</td>
	</tr>
	<tr>
		<td class="title">Listing 1</td>
	</tr>
</table>

<p>You can then check the integrity of the HTML script by clicking on the â€˜Site to validate etcâ€™ link, select â€˜Validate by File Uploadâ€™ and use the â€˜Browseâ€™ button to get to /var/www/html/index.html</p>

<h2>Configuring Apache for ease of development.</h2>

<p>Apache â€“ displaying errors present in PHP.</p>
<p>To display syntax errors, assign rights to edit <span class="filename">/etc/php/apache2/php.ini</span> and in that file, set these settings on: <code>display_errors</code>, <code>display_startup_errors</code>.</p>

<p>They are very useful. After changing these settings either send the Apache process a SIGHUP or reboot your computer.</p>

<p>When you want to use your development computer as a webserver (not always the best idea),  set these (<code>display_errors</code>, <code>display_startup_errors</code>) settings to Off.</p>

<h2>Our first piece of PHP5</h2>

<p>Insert this line into <span class="filename">index.html</span>:</p>

<pre class="programlisting">
  &lt;li&gt;&lt;a href=&quot;http://localhost/blank.php&quot;&gt;An
  almost blank PHP file.&lt;/a&gt;&lt;/li&gt;</pre>
  
<p>And put the HTML file in Listing 2 in HTML Home, named <span class="filename">blank.php</span>.</p>

<table class="sidebartable">
	<tr>
		<td>
			<pre class="programlisting">
&lt;!DOCTYPE html&gt;
&lt;html&gt;
  &lt;head&gt;
    &lt;title&gt;Ian's LAMP experiments&lt;/title&gt;
    &lt;meta http-equiv=&quot;Content-Type&quot;
          content=&quot;text/html;charset=utf-8&quot;&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;h2&gt;Ian's LAMP &lt;?php echo __FILE__ ?&gt; for CVu
    magazine &lt;/h2&gt;
    &lt;p&gt;
      &lt;?php
        echo &quot;blank&quot;;
      ?&gt;
    &lt;/p&gt;
  &lt;/body&gt;
&lt;/html&gt;
			</pre>
		</td>
	</tr>
	<tr>
		<td class="title">Listing 2</td>
	</tr>
</table>

<p>Save it, press F5 and click on â€˜An almost blank etcâ€™ file. In the <code>&lt;h2&gt;</code> tag pair, you should notice there is a bit of PHP there. Congratulations, you have created and run your first PHP programme.</p>

<p>Our system has been set up to have a webserver (Apache), a scripting language (PHP5) and a database server (MySQL).</p>

<h2>Creating a database</h2>

<p>Go to a command prompt and type in â€œmysql -u root -pâ€ and type in your MySQL root password. This gets you to the MySQL command prompt.</p>

<pre class="programlisting">
  ian@turing:/var/www/html$ mysql -u root -p
  Enter password:
  Welcome to the MySQL monitor.  Commands end with
  ; or \g.
  (some redundant details removed).
  Type 'help;' or '\h' for help. Type '\c' to clear
  the current input statement.
  mysql&gt; </pre>
  
<p>Before we can store any information, we need to set up a database. Type in these commands:</p>

<pre class="programlisting">
  create database publications;
  use publications;</pre>
  
<p>And you should see:</p>

<pre class="programlisting">
  mysql&gt; create database publications;
  Query OK, 1 row affected (0.00 sec)
  mysql&gt; use publications;
  Database changed
  mysql&gt;</pre>

<h2>Creating a database table</h2>

<p>Also at the MySQL prompt, type:</p>

<pre class="programlisting">
  drop table classics;
  CREATE TABLE classics (
   author   VARCHAR(128),
   title    VARCHAR(128),
   category VARCHAR(16),
   year     SMALLINT,
   isbn     CHAR(13),
   INDEX(author(20)),
   INDEX(title(20)),
   INDEX(category(4)),
   INDEX(year),
   PRIMARY KEY (isbn)
  ) ENGINE MyISAM;</pre>

<h2>Populating a database table</h2>

<p>Here are some SQL statements to populate the above <code>classics</code> table:</p>

<pre class="programlisting">
  INSERT INTO classics (author, title, category,
  year, isbn )
  VALUES('Mark Twain','The adventures of Tom
  Sawyer','Fiction', 1876,'9781598184891');</pre>
  
<p>And to verify that those statements worked, type this into a MySQL prompt:</p>

<pre class="programlisting">
  select * from classics order by author;</pre>
  
<p>You should get the result shown in Figure 1.</p>

<table class="sidebartable">
	<tr>
		<td>
			<pre class="programlisting">
+---------------------+------------------------------+-------------+------+---------------+
| author              | title                        | category    | year | isbn          |
+---------------------+------------------------------+-------------+------+---------------+
| Mark Twain          | The adventures of Tom Sawyer | Fiction     | 1876 | 9781598184891 |
+---------------------+------------------------------+-------------+------+---------------+
1 row in set (0.00 sec)
			</pre>
		</td>
	</tr>
	<tr>
		<td class="title">Figure 1</td>
	</tr>
</table>

<h2>Accessing a database on a web page</h2>

<p>First you need to have database login details. In the LPMJ book, a file called <span class="filename">login.php</span> is used. Type Listing 3 into <span class="filename">/var/www/html/login.php</span> :</p>

<table class="sidebartable">
	<tr>
		<td>
			<pre class="programlisting">
&lt;?php // login.php
  $db_hostname = 'localhost';
  $db_database = 'publications';
  $db_username = 'root';
  $db_password = 'Your Password Here';
?&gt;
			</pre>
		</td>
	</tr>
	<tr>
		<td class="title">Listing 3</td>
	</tr>
</table>

<p>Then you need a programme to access the database in HTML Home. Something like <span class="filename">/var/www/html/db_experiment.php</span> (Listing 4).</p>

<table class="sidebartable">
	<tr>
		<td>
			<pre class="programlisting">
&lt;!DOCTYPE html&gt;
&lt;html&gt;
  &lt;head&gt;
    &lt;title&gt;Ian's LAMP and PHP experiments&lt;/title&gt;
    &lt;meta http-equiv=&quot;Content-Type&quot;
          content=&quot;text/html;charset=utf-8&quot;&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;h2&gt;Ian's LAMP &lt;?php echo __FILE__ ?&gt; from
      Chapter 10 Accessing MySQL Using PHP&lt;/h2&gt;
    &lt;p&gt;
    Doing database stuff
&lt;?php
  function connect_to_db($host,$user,$passwd)
  {
    echo &quot;&lt;br&gt;Hi from connect_to_db()&lt;br&gt;&quot;;
    echo &quot;&lt;br&gt;login details : Username $user,
    Hostname $host&lt;br&gt;&quot;;
    $db_server = mysql_connect($host, $user,
      $passwd);
    print &quot;&lt;br&gt;db_server = $db_server&lt;br&gt;&quot;;
    if (!$db_server) die(&quot;Unable to connect to
      MySQL: &quot; . mysql_error() );
    else echo &quot;Connected to server&lt;br&gt;&quot;;
    mysql_select_db(&quot;publications&quot;)
    or die (&quot;Unable to select database: &quot; 
     . mysql_error() );
    echo &quot;Database selected&lt;br&gt;&quot;;  
    return $db_server;
  }
  function one_at_a_time_results($result)
  {
     echo &quot;Hi from one_at_a_time_results $result
       &lt;br&gt;&quot;;
     $rows = mysql_num_rows($result);
     for ( $j=0; $j&lt;$rows; ++$j)
     {
       echo 'Author: '   . mysql_result
         ($result, $j, 'author') . '&lt;br&gt;';
       echo 'Title: '    . mysql_result
         ($result, $j, 'title') . '&lt;br&gt;';
       echo 'Category: ' . mysql_result
         ($result, $j, 'category') . '&lt;br&gt;';
       echo 'Year: '     . mysql_result
         ($result, $j, 'year') . '&lt;br&gt;';
       echo 'ISBN: '     . mysql_result
         ($result, $j, 'ISBN') . '&lt;br&gt;&lt;br&gt;';
      }
  }
  function row_at_a_time_results($result)
  {
    echo &quot;Hi from row_at_a_time_results $result
      &lt;br&gt;&quot;;
    $num_rows = mysql_num_rows($result);
    for ( $j=0; $j&lt;$num_rows; ++$j)
    {
      $row = mysql_fetch_row($result);
      echo 'Author: '   . $row[0] . '&lt;br&gt;';
      echo 'Title: '    . $row[1] . '&lt;br&gt;';
      echo 'Category: ' . $row[2] . '&lt;br&gt;';
      echo 'Year: '     . $row[3] . '&lt;br&gt;';
      echo 'ISBN: '     . $row[4] . '&lt;br&gt;&lt;br&gt;';
    }
  }
  require_once 'login.php';
  $db_server=connect_to_db($db_hostname,
    $db_username,$db_password);
  $query = &quot;SELECT * FROM classics&quot;;
  $result = mysql_query($query);
  if (!$result) die (&quot;Database access failed: &quot; 
    . mysql_error() );
  echo &quot;Query $query succeeded&lt;br&gt;&lt;br&gt;&quot;;
  //one_at_a_time_results($result);
  row_at_a_time_results($result);
  mysql_close($db_server);
?&gt;
    &lt;/p&gt;
  &lt;/body&gt;
&lt;/html&gt;
			</pre>
		</td>
	</tr>
	<tr>
		<td class="title">Listing 4</td>
	</tr>
</table>

<p>Once that programme is stored, it can be run from within a web browser.  Start a browser and type in this address: http://localhost/db_experiment.php</p>

<p>You should see something like Figure 2.</p>

<table class="sidebartable">
	<tr>
		<td>
			<pre class="programlisting">
Ian's LAMP /var/www/html/db_experiment.php from Chapter 10 Accessing MySQL Using PHP

Doing database stuff
Hi from connect_to_db()

login details : Username root, Hostname localhost

db_server = Resource id #2
Connected to server
Database selected
Query SELECT * FROM classics succeeded

Hi from row_at_a_time_results Resource id #3
Author: Mark Twain
Title: The adventures of Tom Sawyer
Category: Fiction
Year: 1876
ISBN: 9781598184891
			</pre>
		</td>
	</tr>
	<tr>
		<td class="title">Figure 2</td>
	</tr>
</table>

<p>And thatâ€™s it!</p>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
