Raspberry Pi – Setting up a web server

In this guide, we will be setting up your Raspberry Pi as a web server by installing Nginx, PHP, and MySQL. After doing this, we will perform port forwarding and Dynamic DNS host update to make Pi accessible through the internet.
Raspberry Pi – Setting up a web server

Before you begin, you should have installed a Debian based distribution onto your Pi. You can learn on how to install Raspbian by following previous article.

Installation

The required packages are available in Raspbian default repositories, so the installation is pretty straight forward.

To install and launch Nginx run the command

sudo apt-get install nginx

In order to use PHP with Nginx we will use PHP-FPM

sudo apt-get install php-fpm

Then run the following command (this will install MariaDB by default instead of MySQL)

sudo apt install mysql-server php-mysql

Type the Pi’s IP address in your browser and you should see a page saying “Welcome to nginx!”.

We need to make some changes to the default server file

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

For the PHP processing we need to add index.php as the first value of our index directive, and fastcgi-php.conf snippet with the socket associated with php-fpm.

After these changes Nginx default server file should look something like this

Static IP address

To easily access your Pi, without having to look for its IP every time, we’re going to set a static IP address. First, check your current PI’s IP address

ifconfig

Then we need to edit the dhcpcd.conf file

sudo nano /etc/dhcpcd.conf

And enter the following (change 192.168.1.48 to your IP address)

interface eth0

static ip_address=192.168.1.48/24
static routers=192.168.1.1
static domain_name_servers=192.168.1.1

interface wlan0

static ip_address=192.168.1.48/24
static routers=192.168.1.1
static domain_name_servers=192.168.1.1

Close the configuration file and restart your Pi with its new static IP address

sudo reboot

Port Forwarding

Open up your favorite browser and go to the router’s default gateway address (e.g. http://192.168.1.1). After logging into your router you should see something similar to this.

To enable port forwarding, on the Asus RT-N12+ router, click on “WAN” on the left hand side and then “Virtual Servers/Port Forwarding” at the top. Here you will be able to forward any port you need on your Pi.

Please keep in mind your local ISP or modem/router could be blocking certain ports, such as port 80 or 22. After saving, you can check if port forwarding was successful using portchecktool.com website.

DDNS Update

We are going to use simple bash script that fetches your current IP address, via a web API,  and if it has changed then send the new IP address to your Namecheap Dynamic DNS host.

You need to change the following config variables

DOMAIN – name of domain to update e.g. halibegic.space
PASSWORD – the password given from Namecheap

Your Dynamic DNS password is located unter Domain List >> Manage >> Advanced DNS tab >> Dynamic DNS. If it is not enabled, enable it to check the password.

You can setup a Cron job to automate the whole process, just include the script in your cron file

crontab -e

and run the script e.g. every fifteen minutes

*/15 * * * * /path/to/update-namecheap-ddns.sh

That’s it! Now, you should be able to access the web server via domain name.

You can also check Namecheap official guideline of setting up Dynamic DNS using your browser.

Leave a Reply