Setup A Raspberry Pi Web Server
Configuring a Raspberry Pi as a web server has the advantage of allowing you to host your own webpages. Out of the box, Raspian does not have this configured to ensure that the operating system is as small as possible so we will need to add some packages. We will be creating what is commonly know as a LAMP stack, Linux, Apache, MySQL and PHP, by adding these 4 items we then have the ability to host webpages either within your home network or with the addition of a domain name and a Domain Name Server (DNS), to the worldwide web.
To break down the packages of the LAMP stack:
- Linux – In the case of the Raspberry Pi this is going to be the latest version of Raspbian
- Apache – is a popular web server application that can serve HTML files over HTTP
- MySQL – provides the database engine for your web site allowing us to store information such as sensor readings
- PHP – is a popular scripting language that allows us to serve dynamic webpages, It can present your webpage in a different way depending on circumstances.
To configure the Pi I am assuming that you are running the latest version of Raspbian and have the ability to connect to your Pi either through SSH with putty and FTP with Filezilla or directly with a keyboard and monitor. If you haven’t set-up your Pi yet then check out my getting started section.
In this tutorial I will be using the following materials:
In This Article
- Install the Apache Web Server
- Set Up PHP for Apache
- Install the MySQL (MariaDB) Database
- Access MySQL (MariaDB) with Python
sudo apt update sudo apt dist-upgrade sudo apt autoremove sudo apt clean
Next download the Apache web server package, to do this log into your Pi via ssh or the desktop and enter the following command
sudo apt install apache2 -y
Once this is complete we can check that it has been successful but accessing the initial welcome page that it has created. Open a web browser and in the address line type “http://192.168.1.20” (enter the address of your Pi). If you’re not sure of the IP address of the Pi, enter the following command
Your browser should present the following page.
This is just the default index.html, in order to edit the page we need to adjust the permissions on the file. Log into your Pi and access the web server folder using the following commands
cd /var/www/html ls -al
This shows you the index.html file that was presented to your browser, you will notice that it’s permissions are currently set to the root user. We need to change the user permissions if we want to make any changes to the file. First, add the user “pi” to the “www-data” user group, next, change the owner of the “/var/www/html” folder to “www-data”.
sudo usermod -a -G www-data pi sudo chown -R -f www-data:www-data /var/www/html
Now any user that is a member of the “www-data” group will be able to exit files within the folder, to change aspects of the index page enter the following command
You will see the HTML code for the index page, if you scroll down to the <body> section of the page you will see the writing that was presented in the browser, make some changes to the body text, then press Ctrl+X, then Y, then Enter to save the changes. If you now go back to your browser and refresh you should see your changes.
In order to serve dynamic web pages from the Apache web server, you need to install a scripting language called PHP. To download the PHP package log into your Pi via ssh, or on the desktop and enter the following command
sudo apt install php libapache2-mod-php -y
To confirm that it has been loaded properly we will need to create a file called index.php and put some content into it. Log into your Pi and enter the following commands.
cd /var/www/html sudo rm index.html
These commands will select the correct folder and then remove the existing Index.html file as that will take precedence over the new index.php file that we are going to create. To make the new file enter the following commands
sudo nano index.php
Save the following line to the index.php file
<?php phpinfo(); ?>
To save the new file press Ctrl+X, then Y, then Enter.
With the new file created you now need to refresh your browser to see the new PHP index page.
If you are still seeing the original page then you may need to restart the web server.
sudo /etc/init.d/apache2 reload sudo /etc/init.d/apache2 restart
The benefit of PHP webpage scripting is that it presents dynamic pages based on changing information. If you want to see this in action try changing your index.php file with the following entry
<?php echo date('Y-m-d H:i:s'); ?>
This will show you the current date and time, click refresh on your browser to call the index.php file and see the page update.
Install the MySQL (MariaDB) Database
MariaDB is a community-developed fork of the MySQL relational database management system and provides us with an open-source database package to store any information required for your website and is used with many websites on the internet today. To add this capability to our Pi we again need to load a couple of packages.
sudo apt install mariadb-server
This will install the MySQL (MariaDB) Database, by default the database will be installed without any password protection, to establish the “root” user password and other security settings execute the following command.
Follow the prompts to set the password, make sure that you remember or write down the password as it will be needed later to access MySQL (MariaDB) to create other users and databases. Unless you have any particular reason, answer “Y” to all the questions to secure the database and disable settings that would allow easy access.
To confirm that we have installed the software successfully we will log into our MySQL database
sudo mysql -p -u root
Enter your password when requested and you will now see the mysql> prompt, type quit to get back to the Pi prompt.
Now that the MySQL (MariaDB) has been installed successfully we will add a database, a new user and then grant full access to the database for the new user.
Open MySQL using the root user
sudo mysql -p -u root
Then create a database, in this case, we will call it “hydropidb”
CREATE DATABASE hydropidb;
Next, we will create a new user and password, for this example, the username will be “piuser” and the password will be “pipasswd”
CREATE USER 'piuser'@'localhost' IDENTIFIED BY 'pipasswd';
Now that the new user has been created we will grant all privileges to this user so that you can work with the database.
GRANT ALL PRIVILEGES ON hydropidb.* TO 'piuser'@'localhost';
In order to enable the new user to access the new database, we have to flush the privileges table with the following command. Once completed “Quit” out of the MySQL program
FLUSH PRIVILEGES; QUIT;
Log back into MySQL as the new user
mysql -p -u piuser
Finally, confirm that you have access to the new database with the following command, this will display the the database that you created and the “information schema” database that comes as part of MySQL (MariaDB).
If you want to learn how to work with the MySQL (MariaDB) relational database using the command line then check out the Beginner MariaDB Articles as an introduction, or if you prefer to manage your databases using a graphical interface then you should consider installing PHPMyAdmin.
Access MySQL (MariaDB) with Python
If you would like to access your MySQL database using Python commands then you will need to install the following additional software package.
sudo apt install python-mysqldb
If you’re looking to take your knowledge further why not check out this book covering all these topics in one.
Domain Name (Optional)
While the website can be accessed locally using the IP address or localhost connections if you want to create a website that’s available on the internet then you will need a couple of additional items.
1. Your own Domain Name
2. Connection to a Domain Name Server (DNS)
DNS is like the Internets address book, allowing others to find your website by looking up your domain name. I use Namecheap as they can provide both of these services while you do the hosting yourself on the Pi. A domain name generally costs around $5-$20 a year depending on the name you choose, Namecheap also provides free DNS. Just follow the link below to their website, find an available domain name and you should be up and running in no time. If you would like a more detailed explanation of how to set up your own hosting you can follow these instructions.
If you have any thought’s about this article, improvements or errors let me know in the comments below and if you found this helpful, why not share it with others.