Raspberry Pi Web Server

A Raspberry Pi web server makes for a great lightweight Web Server. It can handle a small amount of traffic and lightweight scripts making it fantastic for a home server or an area you can learn web programming languages such as PHP.

The Pi can even handle WordPress, so if you want to launch your own blog/website with a proper back-end, you can easily do it. If you want it to be accessible via the internet, you will need to do some port forwarding.

Ultimate Books of Pi

This web server is only really suitable for a small amount of traffic and development purposes. If you want to set up a website that you plan on driving a lot of traffic to, then you might want to take a look at shared, cloud or dedicated web hosting.

If you want to see, it done visually then check out my video I have prepared for you below. If you like it, then please subscribe, so you stay up to date with all the latest Pi videos.

Equipment

I used the following equipment for this Raspberry Pi web server project.

Recommended:

Raspberry Pi

8GB+ Micro SD Card if you’re using a Raspberry Pi 2, 3, B+ or an SD Card for old versions of the Pi.

Ethernet Cord or Wifi dongle

Optional:

USB Keyboard

USB Mouse

Installing the Raspberry Pi Web Server

In this tutorial, I make use of the standard operating system for the Raspberry Pi known as Raspbian. If you haven’t got this installed, then you can follow my guide that will take you from start to finish on installing NOOBS (Raspbian) on the Pi. This tutorial assumes you’re running the latest version of the operating system.

You can do this tutorial via Raspberry Pi SSH or the terminal on the actual Pi.

If you’re using a different operating system then most the syntax will remain the same.

1. Firstly, update the Raspberry Pi, so you’re running on the latest packages. You can do this by running the following commands.

sudo apt-get update
sudo apt-get upgrade

1. Next, we will need to install the apache2 package:

sudo apt-get install apache2 -y

2. Now that’s all you need to do to have the server up and running. To check that it is working you should be able to enter the Pi’s IP address. If you don’t know the IP you can enter the following line:

hostname -I

3. Browse to the IP address in a browser, and you should get a page like the one below.

raspberry pi apache success

4. Now you’re able to edit this basic file by simply entering the following command.

sudo nano /var/www/html/index.html

5. You can now add as many new web pages as you want in the www folder and they will be viewable from the browser.

6. This web server is a very basic server and is great if you want to learn HTML, JS or CSS but if you’re after PHP (Used for dynamic web pages) then read on.

7. To install PHP, we will need first to get the PHP package, so, let’s download the PHP7 module for apache. To do this enter the following.

sudo apt-get install php7.0 libapache2-mod-php7.0 -y

8. Now we can place PHP files in the html folder, and they will be processed and displayed. For example let’s make a file called example.php

sudo nano /var/www/html/example.php

9. In this file add the following:

<?php
echo "Today's date is ".date('Y-m-d H:i:s');
?>

There we have it a fully working Apache2 + PHP Raspberry Pi Web Server working. Now you’re able to have mix of HTML and PHP files all in the html folder but personally I would do all my web pages in PHP to keep it consistent. (You can have normal HTML outside the <?php ?> tags).

Now if you want to install WordPress then please keep reading! Otherwise, you should check out some great PHP and HTML resources and learn more about these great Web technologies. (Simply google either of these, and you will find a ton of great tutorials w3schools is a great starting point)

Install Raspberry Pi WordPress

WordPress is a content management system/blogging software that is used by over 60 million websites. Amongst the top 10 million websites on the web, 23.3% of these use WordPress. (January 2015).

It is straightforward to install & learn the basics making it a great way to launch your very own website or blog. Installing Raspberry Pi WordPress on to our web server is dead easy and can be done in just a few steps detailed below.

1. Now we have already installed PHP, but we will also need MySQL and the MYSQL PHP packages.

sudo apt-get install mysql-server php7.0-mysql -y

You will be asked for a root password make sure you remember this as we will need it to access the database.

2. If you’re not asked for a password, then we will need to run the following command. This command will ask a few questions and help you setup MYSQL so that it is secure.

sudo mysql_secure_installation

3. We will need to download and extract the WordPress package that we can easily download. To do this do the following commands:

cd /var/www/html
sudo chown pi: .
rm *
wget http://wordpress.org/latest.tar.gz
tar xzf latest.tar.gz
mv wordpress/* ./
rm -rf wordpress latest.tar.gz
rm -rf index.html
sudo chown -R -f www-data:www-data /var/www/html

4. Restart Apache otherwise you will get an error that PHP is missing the MYSQL module.

sudo service apache2 restart

5. Now that’s installed we will need to set up a database to connect to. Now we could install PHPMyAdmin for database management, but instead, we will do it via command line. First, we need to use the MySQL command to log in, use the –p flag for the password and –u for the username. Leave no space between the flag and required text.

sudo mysql -uroot -ppassword

6. Now let’s create a database for this tutorial let’s call it WordPress.

create database wordpress;

7. You should now see a success statement that looks something like the following:

Query OK, 1 row affected (0.00)

8. Next, we need to create a user for the WordPress installation to access the database.

wpdb is the username, and raspberry is the password, make sure you change these to something more secure.

GRANT ALL PRIVILEGES ON wordpress.* TO 'wpdb'@'localhost' IDENTIFIED BY 'raspberry';

9. Now let’s exit out of the MySQL prompt by pressing Ctrl + D

10. Now in a browser go to the Pi’s IP address, and you should be presented with a WordPress setup screen. Fill in the relevant information such as:

  • Database Name: wordpress
  • User Name: wpdb
  • Password: (Password you created at the MYSQL user creation)
  • Database Host: localhost
  • Table Prefix: wp_

Wordpress Setup

11. Now you should be now able to hit next to complete the installation. If an error occurs then this might mean the permissions are incorrect for your web folder, make sure that www-data can read and write to this folder.

Be sure to check out my Raspberry Pi PHPMyAdmin tutorial if you would like to learn how to install PHPMyAdmin. If you have never heard of PHPMYAdmin, to put it simply, it’s a database management tool that makes managing and configuring databases extremely easy.

You should now have a fully working version of WordPress on your Raspberry Pi. Running more than one copy on a Raspberry Pi web server might not work too well due to the limited resources available to the Pi. However, as I mentioned above, it makes for a great home web server.

Ultimate Books of Pi Bottom
Pi My Life Up's Crash Course
to the Raspberry Pi

Pi My Life Up's Crash Course

to the Raspberry Pi

Subscribe to our email list to get the

Crash Course delivered straight to your inbox

Please check your inbox for a confirmation email!