{"id":97,"date":"2018-02-23T12:00:29","date_gmt":"2018-02-23T20:00:29","guid":{"rendered":"http:\/\/courses.haigarmen.com\/intd320\/?p=97"},"modified":"2018-03-14T13:15:15","modified_gmt":"2018-03-14T21:15:15","slug":"setting-up-apache-php-and-mysql","status":"publish","type":"post","link":"https:\/\/courses.haigarmen.com\/intd320\/setting-up-apache-php-and-mysql\/","title":{"rendered":"Setting up Apache, PHP and MySQL"},"content":{"rendered":"<h3 class=\"p1\">1. Web Server<\/h3>\n<p class=\"p2\">To explore the data we will be collecting, we need to set up a web server that will present web pages to other computers within the network (remembering that this is not intended to be connected to the internet, just inside your home network).<\/p>\n<p class=\"p2\">Run the following command;<\/p>\n<pre class=\"p3\">sudo\u00a0apt-get install apache2 apache2-utils<\/pre>\n<p class=\"p1\">Once complete we need to restart our web server with the following command;<\/p>\n<pre class=\"p2\">sudo service apache2 restart<\/pre>\n<p class=\"p1\">We can now test our web server from\u00a0our laptops by\u00a0opening up a web browser and type in the IP address of our\u00a0Pi into the URL bar at the top. Hopefully we will see\u2026<\/p>\n<h3><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-99\" src=\"http:\/\/courses.haigarmen.com\/intd320\/files\/2017\/02\/apache-test-1024x655.png\" alt=\"\" width=\"840\" height=\"537\" srcset=\"https:\/\/courses.haigarmen.com\/intd320\/files\/2017\/02\/apache-test-1024x655.png 1024w, https:\/\/courses.haigarmen.com\/intd320\/files\/2017\/02\/apache-test-300x192.png 300w, https:\/\/courses.haigarmen.com\/intd320\/files\/2017\/02\/apache-test-768x491.png 768w, https:\/\/courses.haigarmen.com\/intd320\/files\/2017\/02\/apache-test-1200x767.png 1200w\" sizes=\"auto, (max-width: 840px) 100vw, 840px\" \/><\/h3>\n<h3 class=\"p2\">2. Installing PHP<\/h3>\n<p>To install PHP on Raspbian Stretch you&#8217;ll need to run the following command:<\/p>\n<pre class=\"p2\">sudo apt-get install php<\/pre>\n<p class=\"p2\">Your Pi\u00a0will advise you of the range of additional packages that will be installed at the same time (to support those we\u2019re installing (these additional packages are dependencies)). Agree to continue and the installation will proceed. Once complete we will test the PHP installation by putting a file in our web documents folder <strong>\/var\/www\/html\u00a0<\/strong>Create a new file <strong>index.php<\/strong><\/p>\n<p class=\"p2\">sudo nano \/var\/www\/html\/index.php<\/p>\n<p class=\"p2\">and give it the following script:<\/p>\n<pre>&lt;?php\r\nphpinfo();\r\n?&gt;<\/pre>\n<h3 class=\"p1\">Tweak permissions<\/h3>\n<p class=\"p2\">The default installation of the Apache web server has the location of the files that make up our web server owned by the \u2018root\u2019 user. This means that if we want to edit them we need to do so with the permissions of the root user. This can be easily achieved by typing something like:<\/p>\n<pre class=\"p2\"><span class=\"s1\">sudo\u00a0<\/span><span class=\"s1\">nano \/var\/www\/index.html\r\n<\/span><\/pre>\n<p class=\"p2\">but if we want to be able to use an editor from our GUI desktop, we will need the permissions to be set to allow the \u2018pi\u2019 user to edit them without having to invoke sudo<span class=\"s2\">.<\/span><\/p>\n<p class=\"p2\">We\u2019re going to do this by making a separate group (\u2018www-data\u2019 will be its name) the owners of the <span class=\"s1\">\/var\/www <\/span>directory (where our web files will live) then we will add the \u2018pi\u2019 user to the \u2018www-data\u2019 group.<\/p>\n<p class=\"p2\">We start by making the \u2018www-data\u2019 group and user the owner of the <span class=\"s1\">\/var\/www <\/span>directory with the following command;<\/p>\n<pre class=\"p4\">sudo chown www-data:www-data \/var\/www<\/pre>\n<p class=\"p1\">Then we allow the \u2018www-data\u2019 group permission to write to the directory:<\/p>\n<pre class=\"p1\">sudo chmod 775 \/var\/www<\/pre>\n<p class=\"p1\">Then we add the \u2018pi\u2019 user to the \u2018www-data\u2019group;<\/p>\n<pre class=\"p2\">sudo usermod -a -G www-data pi<\/pre>\n<p class=\"p1\">This change in permissions are best enacted by rebooting the Raspberry Pi;<\/p>\n<pre class=\"p2\">sudo reboot<\/pre>\n<p class=\"p1\">Now the \u2018pi\u2019 user has the ability to edit files in the <span class=\"s1\">\/var\/www <\/span>directory without problem. By looking typing your PI&#8217;s IP address along with a \/index.php you should see the following web page:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-224\" src=\"http:\/\/courses.haigarmen.com\/intd320\/files\/2017\/02\/phpinfo-1-1024x766.png\" alt=\"\" width=\"1024\" height=\"766\" srcset=\"https:\/\/courses.haigarmen.com\/intd320\/files\/2017\/02\/phpinfo-1-1024x766.png 1024w, https:\/\/courses.haigarmen.com\/intd320\/files\/2017\/02\/phpinfo-1-300x224.png 300w, https:\/\/courses.haigarmen.com\/intd320\/files\/2017\/02\/phpinfo-1-768x575.png 768w, https:\/\/courses.haigarmen.com\/intd320\/files\/2017\/02\/phpinfo-1-510x382.png 510w, https:\/\/courses.haigarmen.com\/intd320\/files\/2017\/02\/phpinfo-1-1080x808.png 1080w, https:\/\/courses.haigarmen.com\/intd320\/files\/2017\/02\/phpinfo-1.png 1188w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<h3 class=\"p1\">MySQL Database<\/h3>\n<p class=\"p2\">We will use a MySQL database to store the information that we collect. We will install MySQL in a couple of steps and then we will install the database administration tool <strong>phpMyAdmin<\/strong> to make our lives easier.<\/p>\n<p class=\"p3\">From the command line run the following command;<\/p>\n<pre class=\"p4\">sudo apt-get install mysql-server<\/pre>\n<p class=\"p1\">You will be prompted (twice) to enter a root password for your database. Note it down somewhere safe.<br \/>\n<strong>NOTE:<\/strong> For Raspbian Stretch it&#8217;s common to NOT get the password prompt. We&#8217;ll fix that later.<\/p>\n<p class=\"p1\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-102\" src=\"http:\/\/courses.haigarmen.com\/intd320\/files\/2017\/02\/mysql-password-1024x745.png\" alt=\"\" width=\"840\" height=\"611\" srcset=\"https:\/\/courses.haigarmen.com\/intd320\/files\/2017\/02\/mysql-password-1024x745.png 1024w, https:\/\/courses.haigarmen.com\/intd320\/files\/2017\/02\/mysql-password-300x218.png 300w, https:\/\/courses.haigarmen.com\/intd320\/files\/2017\/02\/mysql-password-768x559.png 768w, https:\/\/courses.haigarmen.com\/intd320\/files\/2017\/02\/mysql-password-1200x873.png 1200w, https:\/\/courses.haigarmen.com\/intd320\/files\/2017\/02\/mysql-password.png 2034w\" sizes=\"auto, (max-width: 840px) 100vw, 840px\" \/><\/p>\n<p class=\"p1\">Once this installation is complete, we will install a couple more packages that we will use in the future when we integrate PHP and Python with MySQL. To do this enter the following from the command line;<\/p>\n<pre class=\"p2\">sudo apt-get install mysql-client php-mysql python-mysqldb<\/pre>\n<p class=\"p1\">Agree to the installed packages and the installation will proceed fairly quickly.<\/p>\n<p class=\"p1\">That\u2019s it! MySQL server installed. However, it\u2019s not configured for use, so we will install phpMyAdmin to help us out.<\/p>\n<h3 class=\"p2\">phpMyAdmin<\/h3>\n<p class=\"p1\"><span class=\"s1\">phpMyAdmin\u00b2\u2078 <\/span>is free software written in PHP to carry out administration of a MySQL database installation.<\/p>\n<p class=\"p1\">To begin installation run the following from the command line;<\/p>\n<pre class=\"p3\">sudo apt-get install phpmyadmin<\/pre>\n<p class=\"p1\">Agree to the installed packages and the installation will proceed. You will receive a prompt to ask what sort of web server we are using:<\/p>\n<p class=\"p1\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-103\" src=\"http:\/\/courses.haigarmen.com\/intd320\/files\/2017\/02\/phpmyadmin-1024x745.png\" alt=\"\" width=\"840\" height=\"611\" srcset=\"https:\/\/courses.haigarmen.com\/intd320\/files\/2017\/02\/phpmyadmin-1024x745.png 1024w, https:\/\/courses.haigarmen.com\/intd320\/files\/2017\/02\/phpmyadmin-300x218.png 300w, https:\/\/courses.haigarmen.com\/intd320\/files\/2017\/02\/phpmyadmin-768x559.png 768w, https:\/\/courses.haigarmen.com\/intd320\/files\/2017\/02\/phpmyadmin-1200x873.png 1200w, https:\/\/courses.haigarmen.com\/intd320\/files\/2017\/02\/phpmyadmin.png 2034w\" sizes=\"auto, (max-width: 840px) 100vw, 840px\" \/>Select \u2018apache2\u2019 and tab to \u2018Ok\u2019 to continue.<\/p>\n<p class=\"p1\">We will then be prompted to configure the database for use with phpMyAdmin. We want the program to look after it for us, so select \u2018Yes\u2019 and continue. We will then be prompted for the password for the administrative account for the MySQL database.<\/p>\n<p class=\"p1\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-104\" src=\"http:\/\/courses.haigarmen.com\/intd320\/files\/2017\/02\/phpmyadmin-2-1024x745.png\" alt=\"\" width=\"840\" height=\"611\" srcset=\"https:\/\/courses.haigarmen.com\/intd320\/files\/2017\/02\/phpmyadmin-2-1024x745.png 1024w, https:\/\/courses.haigarmen.com\/intd320\/files\/2017\/02\/phpmyadmin-2-300x218.png 300w, https:\/\/courses.haigarmen.com\/intd320\/files\/2017\/02\/phpmyadmin-2-768x559.png 768w, https:\/\/courses.haigarmen.com\/intd320\/files\/2017\/02\/phpmyadmin-2-1200x873.png 1200w, https:\/\/courses.haigarmen.com\/intd320\/files\/2017\/02\/phpmyadmin-2.png 2034w\" sizes=\"auto, (max-width: 840px) 100vw, 840px\" \/><\/p>\n<p class=\"p1\">This is the root password for MySQL that we set up earlier. Enter it and tab to \u2018Ok\u2019 to continue.<\/p>\n<p class=\"p1\">We will then be prompted for a password for phpMyAdmin to access MySQL.<\/p>\n<p class=\"p1\">The installation should conclude soon.<\/p>\n<p class=\"p1\">Once finished, we need to edit the Apache web server configuration to access phpMyAdmin. To<\/p>\n<p class=\"p1\">do this execute the following command from the command line;<\/p>\n<pre class=\"p2\">sudo nano \/etc\/apache2\/apache2.conf<\/pre>\n<p class=\"p1\">Get to the bottom of the file by pressing ctrl-v a few times and there we want to add the line;<\/p>\n<pre class=\"p1\">Include \/etc\/phpmyadmin\/apache.conf<\/pre>\n<p class=\"p1\">Save the file and then restart Apache2;<\/p>\n<p class=\"p1\">Now if we go to our browser on our laptops\u00a0(or on the Pi) and enter the IP address followed by \/phpmyadmin (in the case of our example 10.1.1.8\/phpmyadmin) it should start up phpMyAdmin in the browser.<\/p>\n<p class=\"p1\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-105\" src=\"http:\/\/courses.haigarmen.com\/intd320\/files\/2017\/02\/phpmyadmin-3-1024x942.png\" alt=\"\" width=\"840\" height=\"773\" srcset=\"https:\/\/courses.haigarmen.com\/intd320\/files\/2017\/02\/phpmyadmin-3-1024x942.png 1024w, https:\/\/courses.haigarmen.com\/intd320\/files\/2017\/02\/phpmyadmin-3-300x276.png 300w, https:\/\/courses.haigarmen.com\/intd320\/files\/2017\/02\/phpmyadmin-3-768x707.png 768w, https:\/\/courses.haigarmen.com\/intd320\/files\/2017\/02\/phpmyadmin-3-1200x1104.png 1200w, https:\/\/courses.haigarmen.com\/intd320\/files\/2017\/02\/phpmyadmin-3.png 1680w\" sizes=\"auto, (max-width: 840px) 100vw, 840px\" \/><\/p>\n<p class=\"p1\">If you enter the username as \u2018root\u2019 and the MySQL root password that we set earlier, it will open up the phpMyAdmin interface.<\/p>\n<p class=\"p1\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-106\" src=\"http:\/\/courses.haigarmen.com\/intd320\/files\/2017\/02\/phpmyadmin-4-1024x675.png\" alt=\"\" width=\"840\" height=\"554\" srcset=\"https:\/\/courses.haigarmen.com\/intd320\/files\/2017\/02\/phpmyadmin-4-1024x675.png 1024w, https:\/\/courses.haigarmen.com\/intd320\/files\/2017\/02\/phpmyadmin-4-300x198.png 300w, https:\/\/courses.haigarmen.com\/intd320\/files\/2017\/02\/phpmyadmin-4-768x506.png 768w, https:\/\/courses.haigarmen.com\/intd320\/files\/2017\/02\/phpmyadmin-4-1200x791.png 1200w\" sizes=\"auto, (max-width: 840px) 100vw, 840px\" \/><\/p>\n<h3 class=\"p1\">Allow access to the database remotely<\/h3>\n<p class=\"p2\">It might seem a little strange to say that we want to allow access to the database remotely since this would appear to be part of the grand plan all along. But there are different types of access and in particular there may be a need for a remote computer to access the database directly.<\/p>\n<p class=\"p2\">This direct access occurs when (for example) a web server on a different computer to the Raspberry Pi wants to use the data. In this situation it would need to request access to the database over the network by referencing the host computer that the database was on (in this case we have specified that it is on the computer at the IP address 192.168.1.74).<\/p>\n<p class=\"p2\">By default the Raspberry Pi\u2019s Operating System is set up to deny that access and if this is something that you want to allow this is what you will need to do.<\/p>\n<p class=\"p2\">On the Raspberry Pi we need to edit the configuration file \u2018my.cnf\u2019 in the directory <span class=\"s1\">\/etc\/mysql\/<\/span>. We do this with the following command;<\/p>\n<pre class=\"p3\">sudo nano \/etc\/mysql\/my.cnf<\/pre>\n<p class=\"p1\">Scroll down the file a short way till we find the section <span class=\"s1\">[mysqld]<\/span>. Here we need to edit the line that reads something similar to;<\/p>\n<pre class=\"p2\">bind-address <span class=\"s2\">= <\/span>127.0.0.1<\/pre>\n<p class=\"p1\">This line is telling MySQL to only listen to commands that come from the \u2018localhost\u2019 network (127.0.0.1). Essentially only the Raspberry Pi itself. We can change this by inserting a \u2018#\u2019 in front of the line which will turn the line into a comment instead of a configuration option. So change it to look like this;<\/p>\n<pre class=\"p1\"># bind-address = 127.0.0.1<\/pre>\n<p class=\"p2\">Once we have saved the file, we need to restart the MySQL service with the following command;<\/p>\n<pre class=\"p3\">sudo service mysql restart<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>1. Web Server To explore the data we will be collecting, we need to set up a web server that will present web pages to other computers within the network (remembering that this is not intended to be connected to the internet, just inside your home network). Run the following command; sudo\u00a0apt-get install apache2 apache2-utils [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-97","post","type-post","status-publish","format-standard","hentry","category-tutorials"],"_links":{"self":[{"href":"https:\/\/courses.haigarmen.com\/intd320\/wp-json\/wp\/v2\/posts\/97","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/courses.haigarmen.com\/intd320\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/courses.haigarmen.com\/intd320\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/courses.haigarmen.com\/intd320\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/courses.haigarmen.com\/intd320\/wp-json\/wp\/v2\/comments?post=97"}],"version-history":[{"count":14,"href":"https:\/\/courses.haigarmen.com\/intd320\/wp-json\/wp\/v2\/posts\/97\/revisions"}],"predecessor-version":[{"id":241,"href":"https:\/\/courses.haigarmen.com\/intd320\/wp-json\/wp\/v2\/posts\/97\/revisions\/241"}],"wp:attachment":[{"href":"https:\/\/courses.haigarmen.com\/intd320\/wp-json\/wp\/v2\/media?parent=97"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/courses.haigarmen.com\/intd320\/wp-json\/wp\/v2\/categories?post=97"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/courses.haigarmen.com\/intd320\/wp-json\/wp\/v2\/tags?post=97"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}