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.

7 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.

  2. This post is great. I now have a fully functioning server on my Raspberry Pi. But I need help with the next steps to get my dynamic site published. On my site I would like to publish information and collect information from the outside world.

    Right now I am stuck as I do not have permissions to modify files in the /var/www/html folder.

    1. How can I securely configure permissions to my folder i.e. /var/www/html so that I can proceed to make changes to the site?
    2. Can you recommend any other steps to increase security of my Raspberry server?

    • Thanks!

      1. How did you try to modify the files? If you’re using an editor like nano, you should be able to modify them by adding sudo to the command (like sudo nano file.php).

  3. Hi Ville, many thanks for your reply. I tried modifying and creating a new file using “Geanny programmer’s editor”.
    Then adjusted the permissions to the /var/www/html folder and it seems to be working now.
    I enjoyed your article. I wonder if you have a follow up, i.e. what to do afterwards? i.e. security, best way to get start with the website using a database mySql / php.
    Are there any good examples (websites) that I can aim to make using LEMP?

  4. Hi all,

    Going though this earlier today I got to Step 4 and installing PHP. However the command given returns a series of errors detailing that both ‘php-fpm’ and ‘php-mysql’ have no installation candidate but are referred to in another package meaning the package is ether missing, obsolete or is only available from another source.

    Any ideas?

    Thanks in advance.

    • sudo apt install php-fpm

      As of November 2017, it will install php-fpm7. You can find the config files in /etc/php/7.0. Program name to reload/start/stop/restart is probably php7.0-fpm.

Leave a comment

Your email address will not be published.


*