Pi My Life Up

DIY Raspberry Pi Web Server Tutorial


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.

This web server is only really suitable for a small amount of traffic and/or development purposes. If you want to setup a website that you plan on driving a lot of traffic to then you might want to take a look into 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

4GB SD Card (8 GB+ Recommended) or Micro SD Card if you’re using a Raspberry Pi 2 or B+

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.

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.
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.

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 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.

7. 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

8. 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

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 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.
1. 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.

2. 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

3. 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

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

create database wordpress;

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

Query OK, 1 row affected (0.00)

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

7. 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 Prefix: wp_

8. 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 is able to 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 to well due the limited resources available to the Pi. However as I mentioned above it makes for a great home web server.

51 Comments

Matthew barnard on May 23rd, 2015 at 4:52am
this really give me the motivation to do this as I've been putting it off for a long time because I thought it would be very difficult on my own but this helps lots
matthew on June 7th, 2015 at 7:33am
I can't add themes to the web server. I think I has something to do with permissions but I can't seem to find an answer.
Gus on June 11th, 2015 at 12:32pm
Hey Mathew, I have looked into this and there was an issue with the ownership we set for the WordPress install. To get this to work run the following command: (It will make sure apache now owns the www folder rather than the Pi user) sudo chown -R -f www-data:www-data /var/www
Aidan on July 1st, 2015 at 12:46am
When I try to install php, I get this after I enter the command: pi@raspberrypi ~ $ sudo apt-get install php5 libapache2-mod-php5 -y ... E: Unable to fetch some archives, maybe run apt-get update or try with --fix-missing? php does not get installed.
Gus on July 1st, 2015 at 1:04am
Hi Aiden, Can you please try running the following commands and then attempt to install PHP. sudo apt-get update sudo apt-get upgrade Let me know how you go.
Matthew Barnard on July 5th, 2015 at 8:20pm
hey guss iv made a websever but it only seems to work on my wifi, when ever i use anther wifi or internet connection it come up with an "isn't responding" message. even the word press page can only be seen on my internet what can i do to make it work on the wed like over website?
Gus on July 7th, 2015 at 5:10pm
Hi Mathew, You will need to setup up port forwarding then you will be able to access your website via your external IP. I have a guide that looks into setting up port forwarding you can see it here -> Raspberry Pi Port Forwarding (You will need to forward port 80) To view your external IP, you can just visit a website like this: What is My IP Let me know how you go!
Mason on July 14th, 2015 at 3:34pm
Hey gus after pressing the run the install butt, im always brought back to the begging of the wordpress thing where the button says "Lets go!" If you now whats happening, help would be muchappreciated.
Gus on July 17th, 2015 at 11:45am
Hey Mason, This is most likely caused by incorrect permissions for the www folder. To get this to work we will need to make sure Apache owns the directory of the web software. Running the the following command should make the correct change. sudo chown -R -f www-data:www-data /var/www Let me know how you go!
adarsh sawant on August 9th, 2015 at 8:21pm
thanks a lot mate .. cheers
George Van Winkle on August 15th, 2015 at 7:46am
Hi Gus, Thanks for the great tutorial. It worked perfectly after I ran the update and upgrade commands. I have plugged a 2TB external hard drive into my Pi which it now recognizes as SDA1. Can you provide suggestions of how to set up storage of my web content/WordPress content on the external drive?
Sinmson on October 20th, 2015 at 6:59am
Thanks, but I had a problem, when I repacked wordpress to /var/www/ i could now reach it in browser so i moved it to /var/www/html now i can reach the /index.php but I get this error: "Your PHP installation appears to be missing the MySQL extension which is required by WordPress." at /wp-admin/setup-config.php Thanks
Tucker Ritti on March 18th, 2016 at 10:38am
Hi! I had the same problem. All you have to do is these commands: 1. cd /etc/php5/apache2 2. sudo nano php.ini 3. hold down the down arrow key until you reach the bottom of the file 4. right above where you see ";Local Variables:", type ;extension=mysql.so 5. Press Control+X to save the file 6. cd 7. sudo reboot I hope I helped you!
Stef on November 4th, 2015 at 5:58pm
Hi Gus, I have just purchased a Raspberry Pi and want to set it up as a web server, but it doesn't actually need to be connected to the Internet. It just needs to be on a local network and be capable of serving up web pages to people who are connected on that network. My idea is to setup a website which is basically a file manager, where documents can be stored and accessed by typing in the website URL. I was also thinking of using a QR code to instantly serve up a PDF file from the Raspberry Pi. There is a HTML5 file manager I downloaded which I can't seem to get to work at all on my local PC. Here is the link for the file manager. There is a demo and it can also be downloaded. http://demo.tutorialzine.com/2014/09/cute-file-browser-jquery-ajax-php/ Any help on this would be most appreciated. Thanks. Knowing your experience what would you suggest is the best method to achieve this?
Dhaval Gajjar on November 9th, 2015 at 6:42pm
When I wrote a command to install apache ( sudo apt-get install apache2 -y ), I get the following result with errors and my basic web-server is not running when I type my R-pi's IP in browser. Please help me out... Failed to fetch x x 404 Not Found E: Unable to fetch some archives, maybe run apt-get update or try with --fix-missing? I also tried these two command 'run apt-get update' and '--fix-missing' but still getting error as command not found
Iain on November 11th, 2015 at 5:14am
Thanks for a great tutorial. I get this error when running # mysql -uroot -mydb Access denied for user 'root'@'localhost' (using password: NO)
Iain on November 11th, 2015 at 9:37am
Thanks. Figured it out. I didn't understand that '-uroot -pmypw' is the sname as '- u root -p mypw'
Shehz on November 25th, 2015 at 12:14am
Hey, Im getting a problem when I go to my Pi's IP address. Instead of getting the "It works" page, Im getting a different page with this page source: Index of / Name Last modified Size Description Apache/2.4.10 (Raspbian) Server at 192.168.2.124 Port 80 Could you please help me with this? Thank you for the tutorial.
Ryan on December 12th, 2015 at 2:15am
Hi, this works great on my Pi until I try to press 'Run the install' button which loads load the /wp-admin/install.php after creating my wp-config.php file. Once I get to this stage nothing seems to happen any suggestions on what to do?
Ryan on December 12th, 2015 at 3:20am
Got it working - not sure why it wasn't working. I uninstalled everything and installed them again and it worked fine! Thanks for the tutorial! :)
Tyler on January 17th, 2016 at 12:00pm
Can I do this side by side with my OwnCloud Server on the same raspberry pi? Thanks!
Nick on January 20th, 2016 at 12:32am
Great tutorial. But i have a question. I dont use wordpress, so i write my site on my on. BUt how can i get my files on the webserver with ftp ???
Chris on February 10th, 2016 at 12:30am
Nick - you would need to connect to your Pi using SSH. I use FileZilla. Connect to port 22 using pi/raspberry as the login credentials (assuming you have not changed them from the defaults)
Chris on February 10th, 2016 at 12:34am
Thanks for the tutorial. I followed this, and discovered that Apache2 now expects documents to be in /var/www/html so I had to move all of my Wordpress files into there. Also, in order to upload some themes and plugins, I also had to modify the uploads_max_filesize directive in php.ini. By default it is set to 2M - I increased this to 10M and then restarted Apache2.
Tyler on February 17th, 2016 at 12:43am
My friend is trying to view my web server, I port forward it but still it says connection timed out
Alex on April 3rd, 2016 at 3:31am
Great! Now, how do I setup a domain name for my website?
Ashley on April 29th, 2016 at 10:54am
I'm having trouble finding the IP address. I used hostname -I, but I don't know where the browser is. Can you give me directions to open the browser?
Bill on July 7th, 2016 at 3:12pm
I used to have this working but now I moved house and since connecting to a new router the raspberry pi does not seem to be serving the webpage any more. "Safari can't open the page... the server where the page is located isn't responding." I've checked my Apache and PHP installations and all seems fine: pi@raspberrypi /var/www/html $ sudo apt-get install apache2 -y Reading package lists... Done Building dependency tree Reading state information... Done apache2 is already the newest version. 0 upgraded, 0 newly installed, 0 to remove and 6 not upgraded. pi@raspberrypi /var/www/html $ sudo apt-get install php5 libapache2-mod-php5 -y Reading package lists... Done Building dependency tree Reading state information... Done libapache2-mod-php5 is already the newest version. php5 is already the newest version. 0 upgraded, 0 newly installed, 0 to remove and 6 not upgraded. My web page is located here: /var/www/index.php and it worked fine recently until I moved. I called my ISP and they looked into my router and said there is no firewall or block that they can see. Is there any easy way to test if the apache server is running or other ideas why it might not be serving the page over the LAN or how to trouble-shoot this?
Bill on July 7th, 2016 at 3:26pm
I think this answers my question about how to test the server is running: pi@raspberrypi ~/code $ curl 192.168.1.69 Status Page ... I ran this on the pi itself via SSH. So this proves the server is running doesn't it? So now I'm back to the first question which is why is the page not being served across the LAN?
Kibitz on September 10th, 2016 at 7:06am
This was awesome. Really appreciated! FYI, maybe it's because my pi has debian, but all the editing needed to be in /var/www/html/ folder. I also had to restart the apache server a few times. THANKS AGAIN!
Kae Yan on October 5th, 2016 at 2:26am
Hi, this is awesome! Anyway, can the Pi WebServer being accessed globally? Is it just restricted for local access only whereby devices are connected to the same router as Pi's?
Gus on October 7th, 2016 at 10:45pm
It can be configured to be accessed globally but requires port forwarding to be setup and you will probably also want to setup dynamicDNS. I have a tutorial on how to it if you want to check that out: https://pimylifeup.com/raspberry-pi-port-forwarding/
Arda on October 7th, 2016 at 4:51am
Hi, I don't understand entering the long code there like the list. Am I entering one by one or both of them at the same time?
Gus on October 7th, 2016 at 10:38pm
I assume you're talking about step 2 in the install Raspberry Pi WordPress section. If you're entering them manually then yes one by one or you can just copy paste the block and it should execute them 1 by 1 in the terminal.
Tóth Ambrus on October 15th, 2016 at 11:34pm
Hello! The new release sites have to put in /var/www/html/ folder, not into the /var/www/ folder.
Gus on November 14th, 2016 at 12:06am
Thanks Tóth, I have updated the tutorial!
Nathan on January 3rd, 2017 at 6:15am
Hi i get this, i only installed the apache server. Bad Request Your browser sent a request that this server could not understand. Size of a request header field exceeds server limit. Cookie Apache/2.4.10 (Raspbian) Server at 127.0.1.1 Port 80 I also have a tvheadend server running i dont know if that's interfering
Thomas on March 27th, 2017 at 5:30pm
I need help on my website when I have installed wordpress it says: Your PHP installation appears to be missing the MySQL extension which is required by WordPress.
Thomas on March 27th, 2017 at 5:32pm
It fixed on reboot!
George on April 29th, 2017 at 8:21pm
I am on the stage where you goto your pi's ip and it should come up with the wordpress setup. For me it dosent. It says "Your PHP installation appears to be missing the MySQL extension which is required by WordPress." Plz Help Me
Darrik on July 11th, 2017 at 9:14am
Im on step 2 of the word press When i type rm * i keep getting Rm: remove write protected regular file 'example.php'? What do i do
Gus on July 11th, 2017 at 11:31am
Typing the command rm -f * will force delete the file. Just make sure you're in the right folder when you do this.
Simon on July 31st, 2017 at 11:17pm
Hello, Brilliant guide and everything worked perfectly so thanks for that. The only problem is I cannot upload anything to the Wordpress themes folder or add anything. I tried sudo chown -R -f www-data:www-data /var/www but everything I try and do I get 'permission denied' through FileZilla. Any help would be great.
Gus on August 4th, 2017 at 12:53am
This is likely because your FTP user will be the user Pi and the /var/www/ is assigned to the www-data user & group. Adding the Pi user to the www-data group should fix this. usermod -a -G www-data pi