Bird
Raised Fist0
Raspberry Piprogramming~10 mins

HTTPS for web server in Raspberry Pi - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to start a simple HTTPS server using Python's http.server module.

Raspberry Pi
import http.server
import ssl

server_address = ('localhost', 4443)
httpd = http.server.HTTPServer(server_address, http.server.SimpleHTTPRequestHandler)
httpd.socket = ssl.wrap_socket(httpd.socket, keyfile='[1]', certfile='server.pem', server_side=True)
httpd.serve_forever()
Drag options to blanks, or click blank then click option'
Aserver.key
Bserver.pem
Ckey.pem
Dcert.pem
Attempts:
3 left
💡 Hint
Common Mistakes
Using the certificate file instead of the key file for the keyfile parameter.
2fill in blank
medium

Complete the command to generate a self-signed certificate using OpenSSL.

Raspberry Pi
openssl req -x509 -newkey rsa:4096 -keyout [1] -out server.pem -days 365 -nodes
Drag options to blanks, or click blank then click option'
Aserver.pem
Bkey.pem
Cserver.key
Dcert.pem
Attempts:
3 left
💡 Hint
Common Mistakes
Confusing the certificate output file with the key output file.
3fill in blank
hard

Fix the error in the Python code to correctly wrap the socket for HTTPS.

Raspberry Pi
import http.server
import ssl

httpd = http.server.HTTPServer(('0.0.0.0', 4443), http.server.SimpleHTTPRequestHandler)
httpd.socket = ssl.wrap_socket(httpd.socket, certfile='server.pem', keyfile=[1], server_side=True)
httpd.serve_forever()
Drag options to blanks, or click blank then click option'
A'server.key'
Bserver.key
C'server.pem'
D'key.pem'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the filename without quotes causing a NameError.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps domain names to their certificate expiration days.

Raspberry Pi
cert_days = {domain: [1] for domain, cert in certs.items() if cert.[2] > 0}
Drag options to blanks, or click blank then click option'
Acert.expiration_days
Bcert.expiry
Cexpiration_days
Ddays_left
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect attribute names that do not exist in the certificate object.
5fill in blank
hard

Fill all three blanks to create a secure HTTPS server setup with certificate and key files.

Raspberry Pi
import http.server
import ssl

server_address = ('', 443)
httpd = http.server.HTTPServer(server_address, http.server.SimpleHTTPRequestHandler)
httpd.socket = ssl.wrap_socket(httpd.socket, keyfile=[1], certfile=[2], server_side=[3])
httpd.serve_forever()
Drag options to blanks, or click blank then click option'
A'server.key'
B'server.pem'
CTrue
DFalse
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect file names or forgetting to set server_side to True.

Practice

(1/5)
1. What is the main purpose of using HTTPS on a Raspberry Pi web server?
easy
A. To make the website load faster
B. To change the website's design automatically
C. To allow more users to connect simultaneously
D. To encrypt data and keep the website safe

Solution

  1. Step 1: Understand HTTPS function

    HTTPS encrypts data between the user and the server to protect privacy and security.
  2. Step 2: Compare options

    Only To encrypt data and keep the website safe correctly describes HTTPS purpose; others are unrelated to encryption.
  3. Final Answer:

    To encrypt data and keep the website safe -> Option D
  4. Quick Check:

    HTTPS = encrypt data [OK]
Hint: HTTPS means secure data transfer with encryption [OK]
Common Mistakes:
  • Thinking HTTPS speeds up the website
  • Confusing HTTPS with website design
  • Believing HTTPS increases user capacity
2. Which command correctly stops the Apache web server on Raspberry Pi before obtaining an SSL certificate?
easy
A. sudo systemctl stop apache2
B. sudo start apache2
C. apache2 stop sudo
D. stop apache2 sudo

Solution

  1. Step 1: Recall correct systemctl syntax

    The command to stop a service is 'sudo systemctl stop servicename'.
  2. Step 2: Identify correct option

    sudo systemctl stop apache2 matches the correct syntax to stop Apache2; others are invalid commands.
  3. Final Answer:

    sudo systemctl stop apache2 -> Option A
  4. Quick Check:

    Stop service = sudo systemctl stop [OK]
Hint: Use 'sudo systemctl stop apache2' to stop Apache safely [OK]
Common Mistakes:
  • Using incorrect command order
  • Omitting 'sudo' for permissions
  • Using 'start' instead of 'stop'
3. What will be the output of this command on Raspberry Pi after installing Certbot and running it to get a certificate?
sudo certbot --apache
medium
A. It automatically obtains and installs an SSL certificate for Apache
B. It deletes all Apache configuration files
C. It starts the Apache server without SSL
D. It shows an error because Certbot is not compatible

Solution

  1. Step 1: Understand Certbot command

    The command 'sudo certbot --apache' requests and installs SSL certificates automatically for Apache.
  2. Step 2: Evaluate options

    Only It automatically obtains and installs an SSL certificate for Apache correctly describes the expected behavior; others are incorrect or false.
  3. Final Answer:

    It automatically obtains and installs an SSL certificate for Apache -> Option A
  4. Quick Check:

    Certbot --apache = get SSL cert [OK]
Hint: Certbot with --apache auto installs SSL cert [OK]
Common Mistakes:
  • Thinking Certbot deletes files
  • Assuming Certbot starts server
  • Believing Certbot is incompatible with Apache
4. You tried to get an SSL certificate but forgot to stop Apache first. What error will most likely occur?
medium
A. No internet connection error
B. Certificate installed successfully
C. Port 80 is already in use error
D. Apache configuration syntax error

Solution

  1. Step 1: Understand port usage during certificate request

    Certbot needs to use port 80 to verify domain ownership, but Apache uses it if running.
  2. Step 2: Identify error from port conflict

    If Apache is not stopped, Certbot cannot bind port 80, causing 'Port 80 is already in use' error.
  3. Final Answer:

    Port 80 is already in use error -> Option C
  4. Quick Check:

    Apache running blocks port 80 for Certbot [OK]
Hint: Stop Apache before Certbot to avoid port 80 conflict [OK]
Common Mistakes:
  • Ignoring need to stop Apache
  • Expecting success without stopping server
  • Confusing error with internet issues
5. After successfully obtaining an SSL certificate on Raspberry Pi, what is the correct sequence to enable HTTPS on your Apache web server?
hard
A. Restart Apache, then configure firewall to allow port 443
B. Configure firewall to allow port 443, then restart Apache
C. Restart Apache only, no firewall changes needed
D. No action needed; HTTPS works automatically without restart

Solution

  1. Step 1: Open firewall port 443 for HTTPS

    Before restarting Apache, ensure firewall allows HTTPS traffic on port 443.
  2. Step 2: Restart Apache to apply SSL certificate

    Restarting Apache activates the new SSL certificate and enables HTTPS.
  3. Final Answer:

    Configure firewall to allow port 443, then restart Apache -> Option B
  4. Quick Check:

    Firewall open 443, then restart Apache [OK]
Hint: Open port 443 before restarting Apache for HTTPS [OK]
Common Mistakes:
  • Restarting Apache before opening firewall port
  • Assuming no firewall changes needed
  • Believing HTTPS works without restart
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard