INF105 gruppetime 07
☛ Handwritten notes can be included in the quizzes
Contents
- How to send file to the VM?
- Securing the connection
- Studying website configuration file
- Simple authentication
- Virtual hosts
- Nginx Logs
- Script Site Generation
How to send file to the VM?
We’ve received multiple questions of what is the best way to edit webpage’s content and send it to the hosting server. Of course you can edit files directly on the server with nano, but it is often more comfortable to work on your laptop and upload the result.
scp -r mysite/ alpine@host:/var/www/inf105/ (-r means recursive)copies a whole directory to the serverrsync -av mysite/ alpine@host:/var/www/inf105/does the same but only sends files that changed ( requiresrsyncinstalled )- if you already use
git, you can push from your laptop and git pull on the server.
Securing the connection
Right now your website is configured for HTTP access, but most modern browser mark this as not secure. The reason is that plain HTTP sends everything in plain text, meaning that data can be intercepted easily. To make it more secure, one must apply a security protocol that encrypts the link between client and server.
Task. Let’s fix it for our websites:
- Install
certbotandcertbot-nginxpackages on your VM. Hint: you need to do this as a superuser (doas) and with Alpine’s package manager (apk) - It’s a good habit to make copies of the original config files before editing them. Run:
doas cp /etc/nginx/http.d/default.conf /etc/nginx/http.d/default.conf.orig
- Run the line below with your domain:
doas certbot --nginx -d yourdomain.inf105.org
- Test config syntax correctness (
doas nginx -t), reload nginx (doas rc-service nginx reload) and open your website in the browser - no complains now! - (Optional) by default certbot certificate’s lifespan is 90 days. Figure out how to autorenew it either by using cronjob or by alpine’s periodic folders.
Studying website configuration file
Let’s have a look at your website’s configuration file: /etc/nginx/http.d/default.conf
There you can see new lines added automatically by Certbot. But what does this structure mean? Study it in groups: find the lines below and write down what kind of incoming requests do they handle using lecture notes and nginx’s beginner’s guide:
- server
{ listen 80 default_server;..______________________________________ - server
{ listen 443 ssl;...____________________________________________ - server
{ if ($host = domainname.inf105.org)...____________________________
Now, find a root directive (set as /var/www/inf105 by default). Root location means that every file within this directory can be accessed from your domain:
> alpine@inf105: /var/www/inf105$ ls
> index.html about.html document.pdf meme.png
Both pdf file and image above can be accessed using proper path in address bar.
Task. Create an element (a hyperlink / a button) in your HTML page that will redirect user to a picture, video, or any other file different to html.
Simple authentication
Not everything you publish on your website is intended for everyone. Sometimes what you want is to restricted access - a standard username-password pair required to see the page.
Task. Let’s figure out how to set it:
- Install apache2-utils package
- Generate a flat-file with the username and password for basic authentication of HTTP users at your page using the command below (you can change access_username to whatever username you want):
doas htpasswd -c /etc/nginx/htpasswd access_username
- Protect a file from the previous task by adding these lines inside the
listen 443 sslserver block of your website’s config file (look carefully for indents):
location = /yourfile.ext {
auth_basic "Restricted area";
auth_basic_user_file /etc/nginx/htpasswd; }
- Reload the nginx and try to open this file from your browser.
Discuss in groups: why is auth_basic method a bad idea with plain HTTP?
Virtual hosts
A virtual host is nginx’s way of serving several websites from one machine and one IP address. nginx looks at the Host header of each request (the domain the browser typed) and picks the server block whose server_name matches. We can set it using:
- Our VM’s name (starting with h26, new): ___________________________.inf105.org …
- …and your exclusive domain name (original): _______________________.inf105.org.
Task. Create a virtual host on your server:
- Create a new root directory within
/var/www/and place some index.html within it. - Copy your existing site’s config with a new name and .conf extension.
In the new file:
- keep only inf105 initial config blocks
- set root directive to directory created at the first step,
- delete default_server from the copy.
- Add a
server_namedirective inside both site configs’ server blocks (see above). - Test config file’s syntax, reload nginx and see the result in http://h26-XX.inf105.org *
Now you have 2 websites (virtual hosts) by the price of 1 server!
* Note: the new site is exactly in the “not secure” state that we started with. You can set a separate SSL certificate to enable HTTPS connection (without it it will only be accessible with
http://)
Nginx Logs
Every request and every error i nginx is logged. This makes our life easier when debugging configs, spotting errors, and seeing who is actually visiting your site.
In /var/log/nginx/ you’ll find two files:
access.log- records every successful request nginx handles (client IP, request line, status code, and more),error.log- records everything that went wrong (config errors, permission issues, failed proxy connections).
Task. Let’s see how many unique visitors your website had so far. Edit the command below that prints visitors’ IP addresses to sort them (1), take only unique values (2), and count lines (3):
doas awk '{print $1}' /var/log/nginx/access.log | (1) | (2) | (3) ⠀
Script Site Generation
Writing HTML by hand is fine for one page. It gets boring fast when a page has 200 table rows, and it is impossible when the content changes every day. This is why people generate pages with scripts:
Task. Turn a CSV file into an HTML table with a Python script:
- Create a csv file with the content that matches your website’s idea, for example:
Country, year, days, rating
Uganda, 2026, 7, 8
Germany, 2025, 5, 5
Iceland, 2025, 7, 10
- Using inf100 course’s notes, write a python script that:
- Opens a csv file, and for each line strips the newline, splits it by commas, and wraps each value (each cell) in
<td>tag; - prints all rows inside a
<table><tr>tags with proper parameters (recall the structure of html tags for tables)
- Opens a csv file, and for each line strips the newline, splits it by commas, and wraps each value (each cell) in
- Run your python script, copy the output and paste it into your page. Open the site and check that the table appears.
- Now - automate everything! Redirect the output straight into a new html file, and make the site serve it. Test it without touching an HTML file.