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 programing 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.
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.
I used the following equipment for this Raspberry Pi web server project.
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.
You can do this tutorial via Raspberry Pi SSH or via the terminal on the actual Pi.
If you’re using a different operating system then most the syntax will remain the same.
- First we will need install the apache2 package:
sudo apt-get install apache2 -y
- 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
- Browse to the IP address in a browser and you should get a page like the one below.
- Now you’re able to edit this basic file by simply entering the following command.
sudo nano /var/www/html/index.html
- You can now add as many new webpages as you want in the www folder and they will be viewable from the browser.
- This is a very basic server and is great if you just want to learn HTML, JS or CSS but if you’re after PHP (Used for dynamic web pages) then read on.
- To install PHP we will need to first get the PHP package so lets download the PHP5 module for apache. To do this enter the following:
sudo apt-get install php5 libapache2-mod-php5 -y
- Now we can place php files in the www and they will be processed and displayed. For an example let’s make a file called example.php
sudo nano /var/www/html/example.php
- 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 www 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 very easy 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.
- Now we have already installed PHP but we will also need MySQL and the MYSQL PHP packages.
sudo apt-get install mysql-server php5-mysql -y
You will be asked for a root password make sure you remember this as we will need it to access the database.
- We will need to now 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 sudo chown -R -f www-data:www-data /var/www/html
- Now that’s installed we will need to setup 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 login, use the –p flag for the password and –u for the username. Leave no space between the flag and required text.
mysql -uroot -ppassword
- Now let’s create a database for this tutorial let’s call it WordPress.
create database wordpress;
- You should now see a success statement that looks something like the following:
Query OK, 1 row affected (0.00)
- Now let’s exit out of MySQL prompt by pressing Ctrl + D
- 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: root
- Password: (Password you created at the MYSQL setup)
- Database Host: localhost
- Table Prefex: wp_
- Now you should be hit next and be able to complete the installation.
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 to well due the limited resources available to the Pi. However as I mentioned above it makes for a great home web server.