Connect with us

Raspberry Pi

How to set up a Web Server on the Raspberry Pi?

Published

on

Raspberry Pi Web Server

Raspberry Pi Web Server can provide you with plenty of advantages. From the economic point of view, you should know that the web hosting services are not free So that you have to pay every month/year. unlike the Raspberry Pi who just needs the connection. In addition by choosing the Raspberry Pi. You can have the possibility to modify the services such as Size of the disk, the hosting of Database, etc. This is not the case with the specialized hosts, which often sell the shared hosting with the low-configuration capacity. but to support more users, you should use the Raspberry Pi 3, the Raspberry Pi with 1GB of RAM rather than the Pi type B+ (512 MB of RAM).

Raspberry Pi Web Server
Raspberry Pi Web Server

Hosting the website on your own is not a hard thing, in fact, you can do it on your Raspberry Pi with nothing more than the Raspbian installation and some command line work. The Raspberry Pi gives a simple way to hosts a personal or professional website. This article explains you everything you need to know about getting it done. Here’s how to set up the web server on the Raspberry Pi.

Related: How to Boot Your Raspberry Pi from a USB Mass Storage Device?

How to set up a web server on the Raspberry Pi | Raspberry Pi Web Server

Just follow the given steps to set up the Raspberry Pi Web Server

To run the web server, we are using the LEMP stack: Linuxnginx, 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 the dynamic processing.

In this case, the Linux distro is the Raspbian. Once the Raspbian is up and running on the Raspberry Pi, you can find that it is pretty easy to get everything else working!

Step 1: Install Raspian using the SD card, just as you would for any other Raspbian-based project. For the refresher on the OS installation, Check out this post on How to install Raspbian on Raspberry Pi.

Raspberry Pi Web Server
Raspberry Pi Web Server

Step 2: Install Nginx the “E” in the LEMP is “nginx,” Then we’ll update our packages and then install nginx. Open up the terminal and the following 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, a database management system, to store and manage data for your site. As with nginx, MySQL is installed with a couple of easy line in the terminal.

sudo apt-get install mysql-server

During the installation, It will ask you to set the root password, If you don’t want to set the password, you can leave the spot blank.

sudo mysql_secure_installation

In this stage, you have the option to change the root password.

Raspberry Pi Web Server
Raspberry Pi Web Server

Step 4: Install PHP, Now we are at the end of our acronym! PHP is responsible for the dynamic content of your website. Back to the terminal, with the following command.

sudo apt-get install php5-fpm php5-mysql

Now edit the 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 which shows #cgi.fix_pathinfo=1 and change it to cgi.fix_pathinfo=0. You can locate it with the search function (Ctrl+W). Then exit with Ctrl+X and you can save with Y.

Now you can restart the  PHP:

sudo systemctl restart php5-fpm

Step 5: Configure the 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 have 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 the server_name, where I have [your public IP], plug in your public IP address (you can find this on Google or another search engine).

Then we can test this and re-load nginx.

sudo nginx -t
sudo systemctl reload nginx

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

Step 6: Set up port forwarding, Now access the Router’s admin interface, here you have to put your router’s private IP address into your browser and then, usually, login – check the 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

Now replace your Pi’s IP address with your Pi’s IP address, that 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 that might be pretty recognizable.

Raspberry Pi Web Server
Raspberry Pi Web Server

Related: Raspberry Pi SSH | Raspberry Pi Netflix

If you did everything right, then type your public IP address into the browser address bar shows you this page.

That’s all, Now you have the functioning Raspberry Pi web server. You can access your website by typing your public IP address into the web browser’s address bar from any internet-connected device. Then your site will 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 are not going to use the PHP- and continue from there.

Thank you for reading this post.

Facebook

Trending