Home Server: Webserver

Intro

Having your own server running can be a great addition to your network.
There are many types of servers, so what are you waiting for?
In this tutorial we are going to take a closer look at a webserver for hosting your website localy.

Requirements

  • Raspberry pi (2 or above, preferably 3B or above)
  • Network connection (preferably wired)
  • Power supply
  • SSH connection (see Remote connections)
  • USB drive (or stick) for storage

Preferably you also installed samba so you can get easy access to the files of your website from your windows PC.

Installing NginX

There are multiple packages that allow you to host your website on a pi or Linux system.
In this tutorial we’ll use NginX as the hosting package.

sudo apt update
sudo apt upgrade
sudo apt install nginx php -y

This last line will install both NginX and PHP. Since NginX is installed lets try to go to the IP of out Pi on the browser. (note this tutorial has been written on a Debian VM, because Raspbian is a Debian derivative this should be no problem). The PHP package will automatically install the latest version of php, if you like an older version you’ll have to specify.

installation of NGINx

When we go to our IP address of out pi (you can get the ip using the hostname -I command) we should see the apache2/nginx default page.

Now thats it you’ve got your webserver running. All that remains is configuring it correctly.

Configuring NginX

Before we configure NginX you’ll need to create a folder where we’ll store our site in.
Preferably not on the SD card but that’s up to you.

mkdir /media/usb/Website

In my case my RPi was set up with an additional storage on an USB that is mounted at boot on /media/usb. Learn how to do this in our previous Server tutorial.

Now that we have that set up we can take a look at the NginX configuration that is required to change this. So lets head to the configuration file located in the Sites-available folder.

cd /etc/nginx/sites-available
sudo nano default
sudo service nginx restart

On a clean install this configuration file for that specific site is called default.
At the top of this file you’ll find a bunch of comments you can ignore, the good stuff start on the server { line.
Here you’ll see the port used is the standard 80 port and that the default location is /var/www/html. Change this location to the new folder for your website if you want it stored on a different drive.

NginX Setup

As we installed php earlier it’s important to add the index.php on the bottom line in the image, this is around line 44.
Under this you’ll find the Passing for PHP. uncomment this and re-comment the CGI.

NginX PHP setup

Now exit nano using ctrl + x and press y.
Afterwards restart nginx.

If you did not change the root directory you’ll see some default files in it. Delete them.
Use following commands with care as they can break your linux. The last command isn’t strictly required, but this shows us that all files in that directory are deleted.

cd /var/www/html
ls -la
sudo rm *
ls -la

Now that our folder is empty we will create our first page.
First lets create a file using nano and display the php info, this will be our first page.

sudo nano index.php

Using the above command nano will open a file called index.php, if it does not exists it’ll still open this file but it will be an empty one.
Copy the next code into that file and save it.

<html>
<head>
<title>
This is a test page
</title>
</head>
<body>
<? php phpinfo() ?>
</body>
</html>

now head to the ip of the pi in a browser.
If all went well we should see our php info page now.

WAIT A MINIT, what happend here? i can’t access the site anymore?
Well this is perfectly normal. We created a file using the root user but our NginX doesn’t have rights to display or execute these files.
We can fix this!

First we will investigate and check the file’s premitions using the ls command. Using this command we can see that it has only read and write permisions but no execute permisions and that root is the owner of the file.
To change this we’ll use the CHMOD and CHOWN commands.
The ./ means the active directory, the -R means all files in this folder and all files in child folders.

sudo chmod 755 ./ -R
sudo chown www-data:www-data ./ -R

Now this might not have fixed it, in that case you don’t have fpm installed.

sudo apt install php-fpm -y
sudo service nginx restart

Now that fixed it 🙂

our first test page

Conclusion

Now that we have our first test page it’s up to you to fill it with meaningful information (or at least meaningful for you)
We’ll leave the tutorial here, in our next Home Server tutorial’s we’ll get more in depth in some usages of Samba and NginX web server.

Leave a reply:

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.