Uni-Sine
Computer ScienceElectronics

How to set up a Raspberry Pi as a web server

A Raspberry Pi is a small, cheap computer that runs Linux and uses only a few watts, which makes it a good home for a personal site, a project dashboard or a learning server. You will need a Raspberry Pi (a Pi 4 or Pi 5 is comfortable), a power supply, a microSD card of 16 GB or more, a network connection and another computer.

Step 1: install Raspberry Pi OS

  1. Download Raspberry Pi Imager on your computer from the Raspberry Pi website.
  2. Choose your Pi model, then Raspberry Pi OS. The Lite version has no desktop, which leaves more memory for the server.
  3. Choose your microSD card.
  4. In the OS customisation settings, set a hostname (such as myserver), a username and a strong password, your Wi-Fi details if you aren't using a cable, and turn on SSH.
  5. Write the card, put it in the Pi and power it on. The first boot takes a minute or two.

There is no default username or password any more. Since an April 2022 update, Raspberry Pi OS makes you create your own account, partly because some countries now ban internet-connected devices with default logins. The full process is in the official getting started guide.

Step 2: connect over SSH

SSH lets you type commands on the Pi from your own computer. Open a terminal (Terminal on a Mac or Linux, PowerShell or Terminal on Windows) and run:

ssh yourname@myserver.local

Use the username and hostname you chose. Type yes the first time to trust the Pi, then enter your password. If myserver.local isn't found, look up the Pi's IP address in your router's list of connected devices and use that instead, for example ssh yourname@192.168.1.50. The remote access documentation covers other ways in.

Step 3: update the system

sudo apt update
sudo apt full-upgrade -y

apt update refreshes the list of available software, and apt full-upgrade installs the latest versions. sudo runs a command with administrator rights.

Step 4: install a web server

A web server is the program that answers browsers' requests and sends back pages. nginx is fast and light, which suits a Pi. Apache, installed with sudo apt install apache2, is a popular alternative and works just as well for a small site.

sudo apt install nginx -y
hostname -I

hostname -I prints the Pi's IP address. Type it into a browser on any device on the same network, such as http://192.168.1.50, and you should see the nginx welcome page. Your Pi is now a web server.

Try putting the setup steps in order, then press Mark to check them in our Raspberry Pi course.

Put the Imager and first-boot steps in order (OrderSteps).

Press Mark to have your answer marked in the Raspberry Pi's course.

Step 5: publish your own page

On Raspberry Pi OS, nginx serves files from /var/www/html. Replace the welcome page with your own:

sudo nano /var/www/html/index.html
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>My Pi server</title>
  </head>
  <body>
    <h1>Hello from my Raspberry Pi!</h1>
  </body>
</html>

Save with Ctrl+O and Enter, exit with Ctrl+X, and refresh the browser. If you still see the welcome page, your browser is probably showing a saved copy, so force a full refresh. To copy a whole site from your computer, use scp or an SFTP app over the same SSH login. Our HTML, CSS and JavaScript course covers building the pages.

Step 6: give the Pi a fixed address

Your router hands out addresses with DHCP, so the Pi's address could change after a restart. The easiest fix is a DHCP reservation in your router's settings, which always gives the Pi the same address. Our guide to IP addresses explains private addresses, DHCP and why your router has a different public address.

Step 7: make it reachable from the internet (optional)

So far, only devices on your home network can see the site. There are two common ways to publish it.

  • A tunnel service: software on the Pi makes an outgoing connection to a tunnel provider, which passes visitors' requests back down it. No ports open on your router, it works even behind carrier-grade NAT, and your home address stays hidden. This is usually the safer choice.
  • Port forwarding: tell your router to send incoming traffic on ports 80 (HTTP) and 443 (HTTPS) to the Pi. You also need a domain name pointing at your public IP address, plus dynamic DNS if your ISP changes it. Some ISPs block incoming ports or share addresses between customers, which stops this from working.

Check your internet provider's terms first, as some don't allow servers on home connections.

Step 8: add HTTPS

Browsers warn visitors about sites without HTTPS. Let's Encrypt issues free certificates, and the Certbot tool installs and renews them. Once your domain points at the Pi and port 80 reaches it:

sudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx

Certbot asks for your domain and email, gets a certificate, updates the nginx settings and sets up automatic renewal. The Certbot instructions list the exact steps for each system. A tunnel service often handles HTTPS for you instead.

Step 9: keep it secure

  • Use a strong, unique password, and better still, log in with SSH keys and turn off password logins.
  • Turn on a firewall: install ufw and allow only SSH and web traffic: sudo ufw allow 22/tcp, sudo ufw allow 80,443/tcp, then sudo ufw enable. Allow SSH before enabling, or you will lock yourself out.
  • Install updates regularly, or set up automatic security updates with the unattended-upgrades package.
  • Never forward the SSH port to the internet unless you really need to. Use a VPN or tunnel to reach it remotely.
  • Back up the SD card or your site files, since SD cards can wear out. A USB or NVMe SSD is more reliable for a server that runs all the time.

Going further: dynamic sites

nginx on its own serves fixed files. For pages that change, such as a dashboard of sensor readings or a guestbook, run a small app and let nginx pass requests to it. Python's Flask, Node.js with Express, and PHP all run happily on a Pi. A sensor project can even show live readings from the Pi's GPIO pins on your web page, which is where our Raspberry Pi course's sections on sensors and project ideas come in.

Common mistakes

  • Forgetting to turn on SSH in Imager. Re-run Imager's customisation, or connect a keyboard and screen and turn it on with raspi-config.
  • Editing files without sudo. Files in /var/www/html belong to the system, so save fails without it.
  • Using the Pi's private address from outside your network. Addresses like 192.168.x.x only work at home.
  • Opening ports without a firewall or updates. Bots scan the internet constantly for exposed devices.
  • Expecting a Pi to handle a big, busy site. It is great for personal sites and learning, not for heavy traffic.

Common questions

Can a Raspberry Pi be used as a web server?
Yes. It runs Linux, so it can run nginx or Apache like any server. It is ideal for personal sites, home dashboards and learning, though not for large, busy websites.
Which is better on a Raspberry Pi, nginx or Apache?
Both work well for small sites. nginx uses less memory, which suits a Pi. Apache is very widely documented and handles PHP simply.
Which Raspberry Pi is best for a web server?
A Raspberry Pi 4 or 5 is comfortable. Older or smaller models work for a simple static site, just more slowly.
Do I need a monitor and keyboard?
No. Set the hostname, user and Wi-Fi, and turn on SSH in Raspberry Pi Imager, and you can do everything else from another computer.
How do I find my Raspberry Pi's IP address?
Run hostname -I on the Pi, or look in your router's list of connected devices. You can often reach it by name too, such as myserver.local.
Is it safe to host a website from home?
It can be, with care: use a tunnel instead of opening ports where you can, keep the Pi updated, use a firewall and strong logins, and back it up. Never expose services you don't need.

Revision cards

Say the answer first, then flip the card to check it.

Where to go next

  • Learn it

    Raspberry Pi's

    Build projects with the most popular single-board computer

    Start the course
Read next
Computer Science · CoursesRaspberry Pi course: set up, GPIO, Python and projectsAn interactive Raspberry Pi course in 30 short sections: set up a Pi 5, use Linux, wire LEDs and buttons to the GPIO pins and code them in Python.