How to set up a web server on a Raspberry Pi


Getting your own web server can be very easy to set up. Unlike the old days where you had to work to get all the parts of the server to talk to each other, you can now have a fully functioning server in about 15 minutes and a few commands. Of course, there are advantages and disadvantages of running your own web server. Advantages are that you have complete control of how things are set up, you don’t have to pay for your own hosting which cuts way down on costs, and you can choose other services to run like NPM or NodeJS. But, you also need to keep up on security, backups, and know and follow all applicable laws about internet privacy and such.

I started out running my web server on a Raspberry Pi Zero W with a 16GB MicroSD card. This setup will work great for a small server but as popularity of your site (if you choose to have a public one) grows, it will start to struggle. I eventually upgraded to a Raspberry Pi 3B+ and 32GB MicroSD card for more storage. I did this because the 3B+ is able to handle more traffic, has faster loading times, and an ethernet port so I can hardwire it to make pages load even faster. But ultimately, you do you for your situation.

Amazon Associate Program:
As an Amazon Associate I earn from qualifying purchases through my links. There is no extra cost to you.

Hardware used:

Programs Used:


Glossary

  1. Flash the Raspbian image to the MicroSD card.
  2. Create a wpa_supplicant.conf and ssh files and put them in the root folder of the boot drive.
  3. Find the IP address of the Pi.
  4. Use SSH to remote in to the Pi.
  5. Install Apache.
  6. Install PHP
  7. Install MySQL
  8. Install phpMyAdmin
  9. Conclusion


Flash the Raspbian image to the MicroSD card:

So the first thing to do is get Raspbian Lite installed on to our SD card. To do this, I used a program called Etcher. Etcher is an easy to use image flashing program that makes it rather foolproof to mess up your operating system accidentally. But first, you need to download this image to download. So lets head over to the Raspbian download page and download the lite version. You can use the desktop version but this would bog down resources.

Once you have Raspbian downloaded, insert your MicroSD card in to your computer and open Etcher. You should be greeted with a window like the image belowto the right.

Click on the “Select Image” button and then select either the zip file of our Raspbian image or if you have already extracted the image, you can select that also.

When you have the image and correct device to flash the image to (Etcher is good about selecting the device you want. It selects removable storage first before listing any extra internal drives you have. It will not let you flash and overwrite your computers file system.) selected, simply hit the “Flash!” button and let Etcher do its magic.

While flashing, Windows will shoot out a warning saying that there is a drive connected with an unrecognized format, simply just close this window or click on “Cancel”. DO NOT FORMAT THE DRIVE!

Now, safely eject your MicroSD card and plug it back in to the computer so that you are able to see the boot drive. You will see the error again about there being an unformatted drive. Simply close the window or click on “Cancel”.


Create a wpa_supplicant.conf and ssh files:

Now that you have the file system flashed, you need to be able for it to connect to the internet so that you can SSH in to it and install the rest of the components you need to run our own website. To do this, you need to create a wpa_supplicant and ssh file on the root folder of the boot drive. Lets start with the wpa_supplicant.

Note: If you are using a hard wire connection on say a Raspberry Pi 3B+, you can skip the supplicant part. You will still need to create the SSH file.
I have made a tool on Codepen that will allow you to enter in your WiFi info and download a properly configured wpa_supplicant.conf file. Simply put in the info, click on “Download wpa_supplicant.conf” button, and move that file to the root folder of the boot drive.

If you are looking to do this yourself and are using WPA/WPA2 security on your WiFi (If you enter in a password, you most likely are), you can download a blank supplicant and edit the file in your favorite text editor as long as its NOT Notepad or Wordpad built in to windows or any of the Office suite (Word) as they do not support unix line breaks. Notepad++ is the most popular free solution.

To make the SSH file, you need to create a blank file called ssh. To do this, you are going to need to be able to edit file extensions. To turn this feature on, please follow this guide. Now, create a new file on the root folder, and name it just plain ssh. No file extension or anything. The way I do this is when I rename the file, I hit Ctrl + A and type s-s-h (without the dashes) and hit Enter. Windows will warn you about changing the file extension, press the yes button. Alternatively, you can download and unzip the file to the root directory of the boot drive.

Now that you have the SSH or both the files that you need to connect the Pi to the network, can can now safely eject the MicroSD card, plug it in to our Pi, and plug in the Pi to let it boot. This might take a couple of minutes for the first boot.


Find the IP address of the Pi

So this is going to be the hard part because the process is going to be different for everybody. Basically you need to log in to your router, find a list of attached devices, and find the IP address associated with your Raspberry Pi. Really, my only suggestion is to find the model name of your router and google how to list the devices connected your router. When you get to the list of devices, you are looking for the IP address of the device named “RASPBERRYPI”. It should look something along the line of this:

In my case, the IP I am looking for is “192.168.1.14”. Remember this, write it down, because you will be needing this.


Use SSH to remote in to the Pi

Now that you have the hard part done, let’s get to installing everything.

Open up PuTTY. Remember that number that I said to remember/write down earlier? Well this is where you get to use it. Type that number in to the “Host Name” box. In the port box to the right, type in 22. For the connection type, make sure “SSH” is selected.

If you want to save this info, you can input a name in the “Saved Sessions” box under all that and click on the “Save” button. This way, whenever you need to connect to your server, you can just double click on the name to connect instead of having to remember the IP address.

On the bottom of the screen, click on the “Open” button. This will bring up a console looking window. It should ask you for a username and password.

The default information is:
Username: pi
Password: raspberry
While typing in the password, it will remain blank even while typing. This is normal. Is is just a security feature to prevent wandering eyes from getting your password.

After logging in, you will get a message that says:
“SSH is enabled and the default password for the ‘pi’ user has not been changed. This is a security risk – please login as the ‘pi’ user and type ‘passwd’ to set a new password.”

So lets set a new password. This is one that you need to be able to remember. Type passwd and hit Enter. You should get a screen asking you for the current password. Type in raspberry and hit enter. The next line will ask you for the new password. Enter in the password you would like it to use (case sensitive) and hit Enter. It will then ask you to confirm the password. Enter in your new password again and hit Enter. You should then get a prompt that says “passwd: password updated successfully”.

After a new password has been set, you want to update the system. To do this, enter in the following lines hitting the Enter key after every line:

sudo apt update
sudo apt upgrade -y

Now that everything is set up and updated, we can start installing the web server components.


Install Apache

The first thing you are going to install is Apache. Apache is the actual web server per se. This takes the requests that are sent to the server, processes the requests, and then returns the data to the requester.

So lets get installing! Enter in the following:

sudo apt install apache2 -y

This may take a couple of minutes to run. When that is done, you are going to take ownership of the HTML directory:

sudo chown -R www-data:www-data /opt/bitnami/apps/wordpress/htdocs/
sudo usermod -a -G www-data pi
sudo chmod -R 770 /opt/bitnami/apps/wordpress/htdocs/

What the above does is gives ownership to the user pi and group www-data and then adds the pi user to that group so you are able to control the directories easily.

Apache uses the “/opt/bitnami/apps/wordpress/htdocs” as the root directory for your files. This means that you should put your files in to this folder. If you are planning on adding user folders (which is beyond the scope of this guide), this is where those folders would go.

Now, you check to see if Apache is working. Open up your web browser of choice and type in the url bar your IP address preceded by http://. Another way to do it but doesn’t always work is to use the host name: http://raspberrypi.local.

You should see something along these lines:

If you get that, then you are able to start using HTML, CSS, and Javascript. But HTML and CSS are very static. Sure, you can do a lot but to really harness the web, you need to install PHP.


Install PHP

Here is a very basic read on what PHP is: What is PHP?

That sounds great! Let’s install it:
sudo apt install php php-mbstring -y

When that is done, you will want to test to see if it is working. The process is a bit different for this one though. Basically, you need to remove the default HTML file and insert a PHP one in its place. Run the following commands:

sudo rm /opt/bitnami/apps/wordpress/htdocs/index.html
echo "<?php phpinfo ();?>" > /opt/bitnami/apps/wordpress/htdocs/index.php

Now, if you still have the page open from before, you can press the F5 key to refresh the page. If not, open a web browser and follow the steps you used to see it Apache is working. You should see something similar to this:

Now that you have that up and running, you should remove that file because it can pose a security risk.
sudo rm /opt/bitnami/apps/wordpress/htdocs/index.php

Now, lets set up so you can make databases.


Install MySQL

MySQL is the program we use to create and store databases. Most packages like WordPress require you to have a database software set up to use. MySQL is used by most major companies like Google, Facebook, and PayPal.

You will be installing the MySQL server and php-msql so that PHP and MySQL will be able to talk to each other. To install both, run the following command:
sudo apt install mysql-server php-mysql -y

When that is finished, you are going to test MySQL with the command line. Type in:
sudo mysql --user=root
You should get a screen like this:

While you are here, you are going to take care of a major security issue. By default, the root account is open to use with all privileges and without a password. This account is also only usable by the Linux root account (pi) and not by the web server or PHP scripts.
You can either recreate the root account with a password, or you can delete the root account and make a new account with all privileges (suggested).

If you want to continue to use root, use the following commands: (replace password with the password you want to use. Leave the single quotes. Ex: IDENTIFIED BY 'squirr3l!'; (not any of my passwords so don’t try))

DROP USER 'root'@'localhost';
CREATE USER 'root'@'localhost' IDENTIFIED BY 'password';
GRANT ALL ON *.* TO 'root'@'localhost'

If you want to create a new user, run the following commands: (this time, you will be replacing the “‘username'” and “‘password'”) You are pretty much going to use the same commands in a different order:

CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';
GRANT ALL ON *.* TO 'username'@'localhost';

Now type exit and hit Enter and it should bring you back to the Linux command line. Now type sudo mysql -u (username you used) -p and hit Enter. The next line should ask you for your password. Type that in and hit Enter. If you get logged in again, your all set! Now run this command to remove the root user:

DROP USER 'root'@'localhost';

If this completes successfully, you are set. Type exit and hit Enter. You should now be back at the Linux command line.

You could leave it at this and you would be set to run your own server at this point. But you don’t have a GUI that you can control MySQL with. So the bonus step is to install phpMyAdmin.


Install phpMyAdmin

phpMyAdmin is a GUI that allows us to view and manage MySQL. Very useful if you don’t want to use the terminal to manipulate a database. There are security risks about adding phpMyAdmin but addressing those is beyond the scope of this guide.

Use this command to install phpMyAdmin:
sudo apt install phpmyadmin -y

There are a few steps to installing this one though.
First, it will ask you what type of server you are running so that phpMyAdmin knows how to run. You are going to make sure that the box next to “apache2” is red and then hit Spacebar. This should add an asterisk (*) within that red box so it should look like:
[*] apache2
and then hit the Enter key to continue.

Next, the installer will ask you if you want to configure a database with “dbconfig-common”, you should refuse this option by using the arrow keys so that “<No>” is red and hit Enter since you already have a database set up and running. If the system asks you for MySQL information, just use the information you set up while setting up MySQL.

To make sure everything has worked correctly, you should be able to access phpMyAdmin at http://(yourIP)/phpmyadmin. You log in with your credentials that you entered in when setting up MySQl.

When you log in, you should see an error message at the bottom of the main screen saying:

you are going to click the link of “Find out why”. This will bring you to a new page that will give you this message:

Go ahead and click the “Create” link. This will set up a new database with a bunch of dummy tables. These tables actvate the rest of the features that phpMyAdmin offer that was not set up while installing MySQL.


Conclusion

Congratulations! You have now set up your own web server! Just note, this is only going to be accessible within your network. To open it up to the internet, you will have to open ports on your router. There are many security risks to doing this. As every router has a different setup, you will need to Google how to open ports for your specific setup.

If you do open your server up to the internet, it is a really good idea to set up SSL to make the connection more secure. I will be writing a guide on how to do this in the near future. It will be linked here when complete.

If you are going to keep it a very small server, a Raspberry Pi Zero will work fine. But as your website starts growing, I would advise upgrading to a beefier system like a Raspberry Pi 3B(+). The glory of using Raspberry Pis is that all you need to do is shutdown the system (sudo shutdown -h now), take the MicroSD card out of the Pi Zero, Plug it in to the new Pi, and power it on. That’s it.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.