Installing Apache and setting up user pages

During our last session of Linux server course we learned about setting up an Apache server on a Linux machine and enabling user web pages that can be edited from each user's home directory. I will now go through the process as well as check some of the logs that Apache stores as it runs.

Installation

To install Apache I ran the following commands:

  • sudo apt-get update
  • sudo apt-get install Apache2

After installing I checked whether the Apache daemon had started with:

  • systemctl status Apache2

Apache status

It was already running so I checked whether my ufw settings allowed traffic into port 80 that http-services use.

  • sudo ufw status

ufw status

Port 80 was already open so nothing needed to be done. If it wasn't open it could be set with:

  • sudo ufw allow 80 (allows all traffic both ways from port 80)

I then checked the local ip-address of my laptop on my desktop computer's browser to see if the server is running like it should, and it is.

Apache default page

Setting up user homepages

To enable user homepages that are editable in ${HOME}/public_html you need to enable the userdir mod for Apache

  • sudo a2enmod userdir

and restart the Apache daemon

  • sudo systemctl restart Apache2

To see if the mod was enabled successfully, I created the folder public_html in my home directory and created an index.html file that said Hello World and browsed to {laptop's ip}/~juuso

  • cd
  • mkdir public_html
  • nano public_html/index.html (write something in the file)

user home page

Logs from page loads

To read logs live from Apache's access logs I used the following command:

  • sudo tail -f /var/log/Apache2/access.log | grep --line-buffered -v 'favicon' | grep '404|200'

The tail command with the -f flag returns a continuous stream as the file is updated and it is piped into a grep that removes all lines containing favicon with the -v flag as there isn't an icon in the user pages. Then it is piped into another grep that gets all the lines containing either 404 or 200 for a page not found error and a succesful page load respectively.

Apache access logs

I picked a few lines to analyze. I used the information in this post to help with this.

  • 192.168.0.140 - - [10/Sep/2021:17:30:55 +0300] "GET /icons/ubuntu-logo.png HTTP/1.1" 200 3623 "http://192.168.0.239/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36"

  • 192.168.0.140 - - [10/Sep/2021:17:31:12 +0300] "GET /asd HTTP/1.1" 404 492 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36"

  • 192.168.0.140 is the address where the request came from

  • [10/Sep/2021:17:30:55 +0300] is the date and time of the request

  • "GET /icons/ubuntu-logo.png HTTP/1.1" tells the request type and the requested resource

  • 404 492 and 200 3623 tell the status code that was responded with and the response message size in bytes

  • "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36" is the user agent that identifies the browser that the user is using

Causing an error in Apache's config

One task was this week was to cause an error in Apache's config and to find the error and analyze the findings. To cause this error I wrote hello on the first line of Apache's main config file found in /etc/apache2/apache2.conf. After that I ran Apache's config test and got this result:

  • apache2ctl configtest

Apache config test

It identified that there was an incalid command on line 1 of /etc/apache2/apache2.conf and thus the test failed. I then removed the extra line I added to the config and ran the test again.

Apache config test

Causing different response codes intentionally

Next I started doing different requests to get different responses from the server. I was able to get the following:

  • 192.168.0.140 - - [10/Sep/2021:18:09:34 +0300] "GET /~juuso/ HTTP/1.1" 403 557 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36"

    • This is the response 403 for a request on a forbidden resource. I got it by temporarily changing the permissions on my public_html folder with
    • chmod o-x public_html/ (removed access to files inside the folder from others)
    • chmod o+x public_html/ (to change it back)
  • 192.168.0.140 - - [10/Sep/2021:18:10:09 +0300] "GET /~juuso/ HTTP/1.1" 200 294 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36"

    • This is the same type of response for a successful request as seen before
  • 127.0.0.1 - - [10/Sep/2021:18:11:03 +0300] "GET /~juuso HTTP/1.1" 301 512 "-" "curl/7.68.0"

    • 301 is the response for the requested document being moved elsewhere. I got the response by using the curl localhost/~juuso command. The response is 301 because the html document is in localhost/~juuso/.
  • 127.0.0.1 - - [10/Sep/2021:18:11:38 +0300] "GET /~juuso/hei HTTP/1.1" 404 432 "-" "curl/7.68.0"

    • 404 is the response for page not found error and I got it by requesting a non-existent URL-path with curl
  • 127.0.0.1 - - [10/Sep/2021:18:22:10 +0300] "DELETE / HTTP/1.1" 405 498 "-" "curl/7.68.0"

  • 405 is the response to http request with an unallowed method, in this case DELETE. I got it with curl -X DELETE localhost

Changing the default page

To change the default page so that Apache serves it from a folder in the user's home directory I followed the top answer from this post. First I edited a file found in /etc/apache2/sites-available/000-default.conf. There is a line which specifies the DocumentRoot and I edited the line from DocumentRoot /var/www/html to DocumentRoot /home/juuso/site. Then I edited the Apache config file in /etc/apache2/apache2.conf. There was the following declaration:

<Directory /var/www/>
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>

And I changed it to

<Directory /home/juuso/site/>
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>

After that I restarted the Apache daemon, made an index.html file in my home directory in the site folder and checked whether the default page had changed.

  • mkdir site
  • echo testi > site/index.html
  • sudo systemctl restart apache2
  • curl localhost

Apache default page changed

Links