How to set up a web server on the Raspberry Pi

How to set up a web server on the Raspberry Pi

Hosting a website on your own isn’t hard – in fact, you can do it on your Raspberry Pi with nothing more than a Raspbian installation and a little command line work. Your Raspberry Pi offers a simple way to host a personal or professional website, and everything you need to know about getting it done is in this handy guide. Here’s how to set up a web server on the Raspberry Pi.


How to set up a web server on the Raspberry Pi

To run our web server, we’re using what’s commonly called the LEMP stack: Linux, nginx, MySQL, and PHP. In short, LEMP uses Linux as the operating system on the device hosting the server, nginx as the server itself, MySQL as a database management system, and PHP for dynamic processing.

In our case, the Linux distro in question will be Raspbian. Once Raspbian is up and running on your Raspberry Pi, you’ll find that it’s pretty easy to get everything else working!


Step 1: Install Raspbian

Install Raspbian using an SD card, just as you would for any other Raspbian-based project. For a refresher on OS installation, head over to our post on how to install Raspbian on the Raspberry Pi.


Step 2: Install Nginx

The “E” in LEMP is “nginx,” believe it or not. Let’s get that next. We’ll update our packages and then install nginx. Open up the terminal and run these commands, one after the other:

sudo apt-get update
sudo apt-get install nginx

Say yes when prompted. Nginx is the server itself, and it’s very efficient.


Step 3: Install MySQL

We need MySQL, a database management system, to store and manage the data for our site. As with nginx, MySQL is installed with a couple of easy lines in the terminal.

sudo apt-get install mysql-server

During the installation, you’ll be asked to set a root password (leaving the spot blank means you’ll have no password).

sudo mysql_secure_installation

In this stage, you’ll have the option to change your root password. Since you only just set it, you might as well say no. Say yes to everything else.


Step 4: Install PHP

We’re finally at the end of our acronym! PHP is responsible for the dynamic content of our site. Back to the terminal, now, with this command:

sudo apt-get install php5-fpm php5-mysql

Let’s edit a file. These are PHP’s settings, and we’re going to make it more secure.

sudo nano /etc/php5/fpm/php.ini

Find the line that says #cgi.fix_pathinfo=1 and change it to cgi.fix_pathinfo=0. You can find it with the search function (Ctrl+W). Then exit with Ctrl+X and save with Y.

Then you’ll just restart PHP:

sudo systemctl restart php5-fpm


Step 5: Configure nginx to use PHP

Let’s get these two to play nice together. Time to edit another file:

sudo nano /etc/nginx/sites-available/default

Once in here, you’ll want to change a few things. Edit it so that it looks like this:

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www/html;

    index index.php index.html index.htm index.nginx-debian.html;

    server_name [your public IP];

    location / {
        try_files $uri $uri/ =404;

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php5-fpm.sock;

    location ~ /\.ht {
        deny all;

Next to server_name, where I have [your public IP], plug in your public IP address (you can find this by asking Google or another search engine).

Then we’ll just test this and re-load nginx.

sudo nginx -t
sudo systemctl reload nginx

That’s all we need to do on the Pi’s end!


Step 6: Set up port forwarding

Access your router’s admin interface (you’ll need to put your router’s private IP address into your browser and then, usually, log in – check your router for more information). Set up port forwarding like so:

Service Port: 80
Internal Port: 80
IP Address: [your Pi's IP address]
Protocol: TCP
Common Service Port: HTTP

Replace [your Pi’s IP address] with – you guessed it – your Pi’s IP address, which you can find with the terminal command hostname -I. Use 80 for your ports and RCP for your protocol. Your options may look slightly different from this, but it should be more or less recognizable.

Welcome to nginx

If you did everything right, typing your public IP address into your browser address bar should give you this page.

That’s it! You now have a functioning web server. Access your website by typing your public IP address into your web browser’s address bar from any internet-connected device. Your site will just have the nginx welcome page for now,  located at /var/www/html. You can remove that and create a file called index.php (sudo nano index.php) – or index.html, if you know that you’re not going to use PHP – and continue from there.

2 Comments on "How to set up a web server on the Raspberry Pi"

  1. It didn’t work. Your paragraph which starts, “Next to server name…” is unclear. Then the line that says, “sudo systemctl reload nginx” doesn’t work either. When I put either my private IP in or my public IP it doesn’t work.

Leave a comment

Your email address will not be published.