Apache2, PHP, logs and Speed ticket calculator!

 

H3

I did this exercise with:

HP Pavilion 15-aw022no

AMD A9-9410 (2.9GHz, 2 Cores)

8GB DDR4-SDRAM 2133MHz (2 x 4)

256GB SSD

AMD Radeon R7 M440 (2GB, GDDR3)

Ubuntu 7.2.0 (Live USB)

Instructions for exercises from: http://terokarvinen.com/2017/aikataulu

(EDIT: 12.1.2018 00:58 NOTICED THAT ONLY 4 EXERCISES ON THIS POST CRUD TEST ADDED ON NEXT BLOG POST!!)

4.2.2018

a) Install Apache and make users homepage work (http://example.com/~matias) . Test it.

23:18

Command setxkbmap fi to enable familiar keyboard configuration.

To install apache I typed command sudo apt-get install apache2. Do you want to continue (y/n), I answered y. To test it I gave command firefox “http://localhost”. It directed me to Apache2 default page.

local.png

Apache is working.

Next I created user homepage.

First enabled Userdir on server with command sudo a2enmod userdir. To enable changes, Apache2 server must be restarted with command sudo systemctl restart apache2.service.

Then I created a folder for public html files, mkdir public_html.

To test can I see changes on the web page I checked my username with command whoami.

My username is xubuntu, so I need to visit page localhost/~xubuntu to see the changes.

 

index

It works!

b) Surf on your web pages and find examples of successful and unsuccessful logs from Apache’s logs. Analyze the logs.

Successful:

log found from /var/log/apache2 folder with command tail -f access.log

This log was recorded as I visited localhost/~xubuntu with Firefox webbrowser.

127.0.0.1 – – [04/Feb/2018:22:08:47 +0000] “GET /icons/back.gif HTTP/1.1” 304 179 “http://localhost/~xubuntu/” “Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:58.0) Gecko/20100101 Firefox/58.0″

127.0.0.1 →IP address of the client

→ These two hyphens indicates that requested information is not avaible.
First hyphen should PROBABLYbe RFC 1413 identity of the client. Second hyphen should be user ID of the person requesting the document.

[04/Feb/2018:22:08:47 +0000] → day, month, year, hour, minute, second, time zone.

“GET /icons/back.gif HTTP/1.1” →Get indicates the method used by the client.iconsback.gif indicates requested resource by client. HTTP/1.1 indicates
protocol used by client.

304 →This indicates status code that the server sends back to the client.

179→ This indicates the size of the object returned to the client.

“http://localhost/~xubuntu/” →HTTP request header.

Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:58.0) Gecko/20100101 Firefox/58.0″ →This is information of the client’s browser.

https://httpd.apache.org/docs/2.4/logs.html

Overall view from this log is that client visited http://localhost/~xubuntu/at
22:08:47 on 4.2.2018 (+0000 timezone)

Unsuccessful:

Log found from /var/log/apache2 folder with command tail -f error.log

 

This log was recorded before I issued command sudo a2enmod userdir and
I tried to visit localhost/~xubuntu with Firefox web browser.

[Sun Feb 04 22:00:40.335807 2018] [mpm_event:notice] [pid 7098:tid
140475917810624] AH00491: caught SIGTERM, shutting down

[Sun Feb 04 22:00:40.403289 2018] [mpm_event:notice] [pid 8090:tid 139870138825664] AH00489: Apache/2.4.27 (Ubuntu) configured — resuming normal operations

[Sun Feb 04 22:00:40.403529 2018] [core:notice] [pid 8090:tid 139870138825664] AH00094: Command line: ‘/usr/sbin/apache2’

[Sun Feb 04 22:00:40.335807 2018] →Date and time

[mpm_event:notice] →Module producing the message and severity of message

[pid 7098:tid 140475917810624] →Process ID and thread ID

AH00491: caught SIGTERM, shutting down →Error message

First line means process with process ID 7098 has shut down.

Second line probably means that earlier process is now functioning normally.

I didn’t find any information about the third line.

c)Make an error to a code line on your website, find it from the log and analyze it.

First I made a text file a.txt in the public_html folder. Then I realized that I haven’t installed PHP, so I gave command sudo apt-get update && sudo apt-get -y install apache2 mysql-client mysql-server libapache2-mod-php php-mysql curl lynx. I found this line from our teacher’s web page http://terokarvinen.com. I searched for a basic code lines from Internet and copied it in the a.txt.

It didn’t show the results which I assumed (just a “Hello World!”).

what.png

I forgot that the text file must be in right format. I renamed a.txt to a.php with command mv a.txt a.php.

hello

Now it is working. Next I needed to get a error message, so I erased a question mark from the PHP code line. This does not cause a error log to /var/log/apache2/error.log. I erased h1> from the header line but this didn’t show up in the apache’s error log either.

5.2.2018

13:00

Yesterday when I was trying to get the log events, Libreoffice Writer crashed.
When I restarted the Writer, it tried automatically restore my unsaved report.

error

I clicked ok but it just quickly showed the Libreoffice logo and went back to this window in the picture above. I tried this many times but without success. Then I started to google if I could find the unsaved report from some kind of temporary folder or something alike.

I found instructions how to recover the file but at this time I realized that I can’t open Libreoffice Writer at all, because it couldn’t get past the recover window shown above. I wasn’t able to proceed with these instructions but I followed the folder path told in these instructions on the file manager. Path was /home/xubuntu/.config/libreoffice/4/user/backup. And there it was, my report. But still I wasn’t able to open the file with Libreoffice Writer because the previous problem. I tried to open the file with multiple applications like Notes and Document viewer but with no success. I also tried to open it with command nano untitled1.odt but it didn’t open it. And just a reminder, I was working with live USB so I couldn’t just restart my computer.

I couldn’t figure out other ways and it was getting pretty late, so I copied the report to my USB drive. I restarted computer with my installed Xubuntu OS and prayed that report wasn’t corrupt or anything and could be opened. My report opened with Libreoffice Writer normally and my work was saved. Unfortunately I didn’t remember copy images to USB drive so they were lost during this tragedy.

13:36

Now that my live USB session doesn’t exist anymore, I have to begin
from the start. I’m going to eat something and then follow the exact same path as earlier and going to report only if any problems occur.

14:35

Now I am at the same point as earlier. So remembered that i need to allow PHP by changing the PHP.7.1 file. To edit this config file I gave command sudoedit /etc/apache2/mods-available/php7.1.conf  and then commented out lines below. Put a hashtag in front of the line to make it a comment.

phpenable.png

Then i changed a.php file name to index.php so it shows directly the wanted web page instead of folders. Then I restarted Apache with command sudo systemctl restart apache2.system

Then i edited index.php with few code lines with the needed error for this exercise. Error is the missing ) after calculation!

codeline

This was noted in Apache’s error log. Command tail -f error.log in the /var/log/apache2 folder to follow the log in real time.

[Mon Feb 05 12:42:41.962804 2018] [php7:emerg] [pid 16875] [client 127.0.0.1:35672] PHP Parse error: syntax error, unexpected ‘?>’ in /home/xubuntu/public_html/index.php on line 7

This log indicates that php7 module has a emergency. Process ID 16875. I think that this means the client’s IPv4 address is 127.0.0.1 but i don’t know what this :35672 means. Error message says that there is a syntax error on line 7 with the code from /home/xubuntu/public_html/index.php file.

f) Make a application which runs on your server.

I made a calculator which calculates if you are going to get a ticket for speeding and how much it is going to be in euros. Prices and speeds are made up and not real. Here is the end result.

fine

Some test examples:

drivingSpeed = 120, speedLimit = 120 -> No speeding, no fine
<span id="line65"></span>drivingSpeed = 65, speedLimit = 60 -&gt; Infraction fine is 85 euros.  
<span id="line66"></span>drivingSpeed = 77, speedLimit = 60 -&gt; Infraction fine is 115 euros.
<span id="line67"></span>drivingSpeed = 105, speedLimit = 100 -&gt; Infraction fine is 70 euros.  
<span id="line68"></span>drivingSpeed = 117, speedLimit = 100 -&gt; Infraction fine is 100 euros.
<span id="line69"></span>drivingSpeed = 121, speedLimit = 100 -&gt; Income-based unit fines.

And here is the code.
<span id="line2"></span><span class="doctype">&lt;!DOCTYPE html&gt;</span>
<span id="line3"></span>&lt;<span class="start-tag">html</span>&gt;
<span id="line4"></span>&lt;<span class="start-tag">head</span>&gt;
<span id="line5"></span>&lt;<span class="start-tag">meta</span> <span class="attribute-name">charset</span>="<a class="attribute-value">UTF-8</a>" /&gt;
<span id="line6"></span>&lt;<span class="start-tag">title</span>&gt;Infraction fine &lt;/<span class="end-tag">title</span>&gt;
<span id="line7"></span>
<span id="line8"></span>&lt;<span class="start-tag">script</span> <span class="attribute-name">type</span>="<a class="attribute-value">text/javascript</a>"&gt;
<span id="line9"></span>  function tellInfractionFine() {
<span id="line10"></span>
<span id="line11"></span>var drivingSpeed = document.getElementById("drivingSpeed").value;        
<span id="line12"></span>var speedLimit = document.getElementById("speedLimit").value;        
<span id="line13"></span>x = (drivingSpeed - speedLimit); 
<span id="line14"></span> 
<span id="line15"></span>if (x &lt;=4){
<span id="line16"></span>    x = "No Speeding";
<span id="line17"></span>}
<span id="line18"></span>else if (speedLimit &gt;= 10 &amp;&amp; speedLimit &lt;=60 &amp;&amp; x &lt;= 15 &amp;&amp; x &gt;4) {
<span id="line19"></span>    x = "Infraction fine is 85 euros";
<span id="line20"></span>}       
<span id="line21"></span>else if (speedLimit &gt;= 10 &amp;&amp; speedLimit &lt;= 60 &amp;&amp; x &gt; 15 &amp;&amp; x &lt;= 20) {
<span id="line22"></span>    x = "Infraction fine is 115 euros";        
<span id="line23"></span>}
<span id="line24"></span>else if (speedLimit &gt;= 70 &amp;&amp; speedLimit &lt;=120 &amp;&amp; x &lt;=15 &amp;&amp; x &gt;4) {
<span id="line25"></span>    x = "Infraction fine is 70 euros";
<span id="line26"></span>}
<span id="line27"></span>else if (speedLimit &gt;= 70 &amp;&amp; speedLimit &lt;=120 &amp;&amp; x &gt;15 &amp;&amp; x &lt;= 20) {
<span id="line28"></span>    x = "Infraction fine is 100 euros";
<span id="line29"></span>}
<span id="line30"></span>else if (x &gt;20) {
<span id="line31"></span>    x = "Income-based unit fine.";
<span id="line32"></span>}
<span id="line33"></span>
<span id="line34"></span>document.getElementById("answer").innerHTML = x;        
<span id="line35"></span>        
<span id="line36"></span>        
<span id="line37"></span>        
<span id="line38"></span>  }
<span id="line39"></span>&lt;/<span class="end-tag">script</span>&gt;
<span id="line40"></span>&lt;/<span class="end-tag">head</span>&gt;
<span id="line41"></span>&lt;<span class="start-tag">body</span>&gt;
<span id="line42"></span>  &lt;<span class="start-tag">h3</span>&gt;Infraction fine &lt;/<span class="end-tag">h3</span>&gt;
<span id="line43"></span>
<span id="line44"></span>  &lt;<span class="start-tag">form</span>&gt;
<span id="line45"></span>    &lt;<span class="start-tag">p</span>&gt;
<span id="line46"></span>      &lt;<span class="start-tag">label</span>&gt;Driving Speed: &lt;/<span class="end-tag">label</span>&gt; &lt;<span class="start-tag">input</span> <span class="attribute-name">type</span>="<a class="attribute-value">text</a>" <span class="attribute-name">name</span>="<a class="attribute-value">drivingSpeed</a>" <span class="attribute-name">id</span>="<a class="attribute-value">drivingSpeed</a>"
<span id="line47"></span>        <span class="attribute-name">size</span>="<a class="attribute-value">3</a>"&gt;
<span id="line48"></span>    &lt;/<span class="end-tag">p</span>&gt;
<span id="line49"></span>    &lt;<span class="start-tag">p</span>&gt;
<span id="line50"></span>      &lt;<span class="start-tag">label</span>&gt;Speed limit: &lt;/<span class="end-tag">label</span>&gt; &lt;<span class="start-tag">input</span> <span class="attribute-name">type</span>="<a class="attribute-value">text</a>" <span class="attribute-name">name</span>="<a class="attribute-value">speedLimit</a>"
<span id="line51"></span>        <span class="attribute-name">id</span>="<a class="attribute-value">speedLimit</a>" <span class="attribute-name">size</span>="<a class="attribute-value">3</a>"&gt;
<span id="line52"></span>    &lt;/<span class="end-tag">p</span>&gt;
<span id="line53"></span>    &lt;<span class="start-tag">p</span>&gt;
<span id="line54"></span>      &lt;<span class="start-tag">input</span> <span class="attribute-name">type</span>="<a class="attribute-value">button</a>" <span class="attribute-name">value</span>="<a class="attribute-value">Tell about the ticket</a>" <span class="attribute-name">onClick</span>="<a class="attribute-value">tellInfractionFine()</a>"&gt;
<span id="line55"></span>    &lt;/<span class="end-tag">p</span>&gt;
<span id="line56"></span>  &lt;/<span class="end-tag">form</span>&gt;
<span id="line57"></span>
<span id="line58"></span>    <span class="comment">&lt;!-- This is the target where the JavaScript will write the answer --&gt;</span>
<span id="line59"></span>  &lt;<span class="start-tag">div</span> <span class="attribute-name">id</span>="<a class="attribute-value">answer</a>"&gt;&lt;/<span class="end-tag">div</span>&gt;
<span id="line60"></span>&lt;/<span class="end-tag">body</span>&gt;
<span id="line61"></span>&lt;/<span class="end-tag">html</span>&gt;

Leave a Reply

Your email address will not be published. Required fields are marked *