Raspberry Pi Web Server – Step by Step

By Tom LedfordThe Practical Computer 

Can you use a $35 computer as a web development server? Indeed you can! You may think that only small and uncomplicated websites are appropriate projects for the Raspberry Pi, but I’m happily using one for a large AngularJS project now. Here is how I installed and configured the software on my Raspberry Pi 3…

If you don’t already have a Raspberry Pi and want to purchase one, make sure it is the Model 3. The $35 price tag is a bit misleading though. The basic Raspberry Pi doesn’t come with a case or power supply. You will also need to buy a microSD card for the OS and other software. Altogether, you can get everything you need for a basic Raspberry Pi for around $60. 

Raspberry Pi 3 Basic Starter Kit (includes: power supply, black case, and 2 heat sinks)

16 GB MicroSD with adapter (minimum recommended size. You will also need the adapter to transfer files from your PC or laptop)

 

Raspberry Pi 3 Model B
Processor ChipsetBroadcom BCM2837 64Bit Quad Core Processor powered Single Board Computer running at 1.2GHz
Processor SpeedQUAD Core @1.2 GHz
RAM1GB SDRAM @ 400 MHz
StorageMicroSD
USB 2.04x USB Ports
Max Power Draw/voltage2.5A @ 5V
GPIO40 pin
Ethernet PortYes
WiFiBuilt in
Bluetooth LEBuilt in

 

The Raspberry Pi 3 Model B is the latest, greatly improved model. Now with a 64 bit quad core processor, 1GB of RAM, 4 USB 2.0 ports, built in WiFi and Bluetooth, the Raspberry Pi 3 is a surprisingly powerful little computer.  My Raspberry Pi 3 is configured with a 16GB microSD card, which should be adequate for most web projects.

Preparing Your Raspberry Pi for Software Installation

The Raspberry Pi 3 can use several different operating systems, even Windows 10 IoT Core. For my server, I used the latest version of Raspbian, a lightweight version of Debian 8 that is specifically designed for Raspberry Pi devices.

To install the OS, you will need a formatted microSD card. I used a program called SD Card Formatter from SD Association and an microSD to SD card adapter (the card linked to above comes with an adapter) to format my microSD card with my Windows 10 Laptop. If your PC, Mac or Laptop doesn’t have an SD card reader, you can use a USB card reader adapter.

Once you have formatted your microSD card, download NOOBS ( New Out Of Box Software) software installation manager from raspberrypi.org. Extract the NOOBS zip file to the microSD card or, to a directory on your computer and copy all the files to your microSD card.

Installing the Raspberry Pi Operating System – Raspbian

Insert your microSD card into your Raspberry Pi, connect a USB keyboard and mouse to any USB ports on your Raspberry Pi, and connect a monitor to the HDMI port on your Raspberry Pi. If you have a monitor with a VGA or DVI port, you can use an adapter cable to connect it to your Raspberry Pi. Don’t worry, we will later configure VNC to remote into our server so you don’t have to keep the keyboard, mouse and monitor connected to the Raspberry after the initial setup.

Now, connect your Raspberry Pi’s power adapter to boot up to NOOBS. The initial NOOBS screen will look something like this:

NOOBS Install Screen
NOOBS Install Screen

Select Wifi networks and enter your wireless network settings. NOOBS will then download several more software options. But for this web server project, we will install Raspbian, so select itand select your language and keyboard at the bottom of the screen. Click on Install.  After the installation is finished, your Raspberry Pi will reboot. Login with the default user pi and password raspberry.

Configure Raspbian settings

You will want to be prudent and change the Raspberry’s default password. From the main menu, select Preferences, then Raspberry Pi Configuration to change the password. Then select the Interfaces Tab, and enable VNC. While you are here, select Localisation and configure your location and time zone settings:

Change Default Password
Change Default Password
Configure VNC
Configure VNC

Open a terminal window and at the prompt, type “ifconfig” to find the Raspberry’s IP address. Now you can disconnect your keyboard, mouse and monitor if you want and use VNC to continue configuring your Raspberry Pi from a Windows computer. For convenience, I suggest using the VNC Viewer extension for Chrome.  To establish a VNC session use the Raspberry’s IP address, the default username Pi, and the password you set in the step above.

Building the Web Server

We will be creating a LEMP server by installing NGINX, PHP, and MariaDB (an open source fork of MySQL). We will also install FTP and the Chromium browser to make it easier to work with our server. If you are using VNC, it is easier to cut and paste from Chromium on the Raspberry Pi to its command line, than from the computer you use to launch VNC. This is more convenient when you need to reference a command or configuration text.

Install Chromium Browser

Open a terminal window and at the prompt, run these commands one at a time:

[email protected]: ∼ $ sudo apt-get update
[email protected]: ∼ $ sudo apt-get install chromium-browser

 

Install FTP

You will need FTP to transfer your web files from your development computer, to the Raspberry. I use Aptana Studio 3 as my IDE (integrated development environment). It has built in FTP, but I also use FileZilla for bulk file transfers. Both are available for WIndows and Linux.

To install FTP on your Raspberry Pi, open a terminal window and run:

[email protected]: ∼ $ sudo apt-get install pure-FTP

 

Install NGINX

NGINX is the second most popular web server software, behind only Apache. It’s  a better choice than Apache for the Raspberry Pi because of it’s light-weight resource utilization and its ability to scale easily on minimal hardware.

To install NGINX on your Raspberry Pi, open a terminal window and run:

[email protected]: ∼ $ sudo apt-get install nginx

 

Configure NGINX

We will be adding a virtual server that will respond to a particular URL instead of only an IP address. This means you will need to add an entry to your development computer’s host file.

I use a Windows PC to create web pages and FTP them to, and test them on my Raspberry Pi server. You will need administrator privileges to edit the hosts file. The easiest way to do this is by opening a command prompt as Administrator and running notepad from the command line.

 C:\Windows\system32\drivers\etc\hosts file:

# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a ‘#’ symbol.
#
# For example:
#
# 102.54.94.97 rhino.acme.com # source server
# 38.25.63.10 x.acme.com # x client host

# localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost

#Add the following:
192.168.1.6     mydevsever.com
192.168.1.6     www.mydevserver.com

 

Now create a directory on your Raspberry Pi in /var/www/This directory will hold your web site files. Open a terminal window and create /var/www/mydevserver.com:

[email protected]: ∼ $ sudo mkdir /var/www/mydevserver

 

To configure NGINX for your virtual server, you will need to add a file in /etc/nginx/sites-available. In a terminal window, open the text editor as sudo, and create a file named mydevserver:

[email protected]: ∼ $ cd /etc/nginx/sites-available
[email protected]: /etc/nginx/sites-available $ sudo leafpad mydevserver

 

Add the following text to mydevserver and save the file (We will return to this file later to configure PHP) :

server {
listen 80;
listen [::]:80;
server_name mydevserver.com;
root /var/www/mydevserver.com;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}

 

Now open a terminal window and create a symlink to this file in /etc/nginx/sites-enabled:

[email protected]: ∼ $ sudo ln -s /etc/nginx/sites-available/mydevserver /etc/nginx/sites-enabled/mydevserver

 

Restart NGINX:

[email protected]: ∼ $ sudo /etc/init.d/nginx restart

 

To test your new web server, copy the index.nginx-debian.html file from /var/www/html/ to var/www/mydevserver.com/index.html:

[email protected]: ∼ $ sudo cp /var/www/html/index.nginx-debian.html /var/www/mydevserver.com/index.html

 

Open your browser on your Windows PC and go to www.mydevserver.com. You should see:

nginx start page


Install PHP

Now, we can install PHP with the following command:

[email protected]: ∼ $ sudo apt-get install php5-fpm

 

Configure PHP

To configure PHP, we need to edit two files. First, we will edit the mydevserver file we created in /etc/nginx/sites-available. In a terminal window:

[email protected]: ∼ $ cd /etc/nginx/sites-available
[email protected]: /etc/nginx/sites-available $ sudo leafpad mydevserver

 

Add the following highlighted text to mydevserver:

server {
listen 80;
listen [::]:80;
server_name mydevserver.com;
root /var/www/mydevserver.com;
index index.html index.php;
location / {
try_files $uri $uri/ =404;
}

location ~ \.php$ {
     include snippets/fastcgi-php.conf;
     fastcgi_pass unix:/var/run/php5-fpm.sock;
     }
location /phpmyadmin {  
     root /usr/share/;  
     index index.html index.php;  
     try_files $uri $uri/ =404;

     location ~ ^/phpmyadmin/(doc|sql|setup)/ {    
          deny all;  
          }
     location ~ /phpmyadmin/(.+\.php)$ {    
          fastcgi_pass unix:/var/run/php5-fpm.sock;    
          fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
          include fastcgi_params;
          include snippets/fastcgi-php.conf; 

          }
}

 

Next, edit the /etc/php5/fpm/php.ini file. I had to increase a few parameters on my Raspberry Pi server in order to run phpmyadmin. It’s also a good idea to turn error displaying and logging on for a development server. Find the following settings in the php.ini file and make sure they are uncommented and settings are sufficient for your applications. These settings are working for me:

max_execution_time = 120
max_input_time = 60
memory_limit = 256M
error_reporting = E_ALL
display_errors = On
display_startup_errors = On
log_errors = On
html_errors = On
post_max_size = 257M
upload_max_filesize = 256M
max_file_uploads = 20

Restart nginx and php:

[email protected]∼ $ sudo service nginx restart
[email protected]: ∼ $ sudo service php5-fpm restart

 

To test PHP, create a file named index.php with the following content in your web site’s root directory – /var/www/mydevserver.com:

[email protected]∼ $ sudo leafpad /var/www/mydevserver.com/index.php

 

index.php:

<? php
phpinfo();
?>

 

If php is working correctly, you should something similar to the following when you browse to http://mydevserver.com/index.php

mydevserver - phpinfo

Install MySQL / MariaDB

MariaDB is a “drop-in replacement” for MySQL. It is 100% command compatible with MySQL, so there will be no reason to change any code. I chose MariaDB because it is faster and I need every available performance advantage I can get for a Raspberry Pi web server. To install MariaDB:

[email protected]: ∼ $ sudo apt-get -y install mariadb-server mariadb-client

During installation, you will be asked to enter and confirm a  new MariaDB root password:

mariadb password

Install PHPMyAdmin

We are almost done!  To work with your new database server, you will need to install phpmyadmin:

[email protected]i: ∼ $ sudo apt-get install phpmyadmin

 

You will be asked to create database needed for phpMyAdmin. Click on “Yes”, it will create the required database for phpMyAdmin.

You will also be asked for the MariaDB root user password that we set in the above steps. Finally, you will be asked to enter and confirm a phpmyadmin password. If you set this password different from the DB root password, you will be able to use either one to access phpmyadmin.

We have already configured our virtual web server for phpmyadmin when we edited /etc/nginx/sites-available/mydevserver, above. Test phpmyadmin by opening a browser window on your Windows PC and entering the URL mydevserver/phpmyadmin:

phpmyadmin

Wrap It Up

There are a few more packages you may need to install depending upon your web project. I installed php-pear so I could use email and php-mcrypt for encryption. These packages install like the ones above – sudo apt-get install package-name.

For your Windows development PC, you may want to install FileZilla so you can FTP files to and from your Raspberry Pi. To make working with databases much easier than working with phpmyadmin, download and install the free version of dbForge Studio for MySQL. It is a visual query builder much like Microsoft’s SQL Management Studio.

Enjoy your new development environment!

∼#∼

No Comments

What do you think?

%d bloggers like this: