How to Setup Webserver on Raspberry Pi and Host a WordPress Website

Published  August 6, 2018   0
How to Setup Webserver on Raspberry Pi and Host a WordPress Website

One of the fascinating things about system on chips (SoC) like Raspberry Pi is their ability to serve as web servers to host websites and other online applications. This webserver serves host files when request is made from the client end. Today, I demonstrate How to Setup a Webserver on Raspberry Pi and Install a Wordpress Website which can be accessed by any device on the same network as the raspberry pi. Even you can put Raspberry Pi online by port forwarding technique and can access the website from anywhere in the world.

 

Required Component

The following components are required to build this project;

  • Raspberry pi 2 or 3
  • SD Card (8gb Minimum)
  • LAN/Ethernet Cable
  • WiFi Adapter (if using the Raspberry pi 2)
  • Power source

Optional

  • Keyboard
  • Mouse
  • Monitor
  • HDMI Cable

 

To proceed, we will be using the Raspbian stretch OS for this tutorial and since its setup is same as that of the Jessie, I will assume you are familiar with setting up the Raspberry Pi with the Raspbian stretch OS. I also assume you know how to SSH into the Raspberry Pi using a terminal software like putty. If you have issues with any of the things mentioned, there are tons of Raspberry Pi Tutorials on this website that can help.

For new Stretch users (fresh installs), you should note that SSH is disabled and you will need to enable SSH before you can talk to the raspberry pi over SSH. One way to do this is to activate it by connecting a monitor and enabling SSH, while the second which is my favourite is by creating a file named ssh (with no extension) and copying it to the root folder on your SD card. This can be done by inserting the SD card into your PC.

We will start the tutorial by setting up the raspberry pi as a web server which can be used to host any kind of website after which we will look at setting up a WordPress website on the server.

Also check other Raspberry Pi Server for media and print server:

 

Setup Webserver on Raspberry Pi

There are several server stacks but for this tutorial, we will be using the LAMP stack which stands for Linux, Apache, MySQL and PHP.

 

Step 1: Update the Pi

It is important to update the Pi at the start of any project as this installs update for all the packages installed previously and ensures compatibility issues do not arise, when the software packages needed for the new projects are installed. To update the pi run;

Sudo apt-get update
Sudo apt-get upgrade

 

Step 2:  Install Apache

Since we already run a Linux machine, the first thing to be done is to install Apache. Apache like most other webserver applications can be used to serve HTML file over http or used with additional modules and packages to serve dynamic web pages like most wordpress websites, which are built using languages like PHP.

To install apache run;

Sudo apt-get install apache2

 

With the installation done, you can test it by visiting the IP address on your browser. You should see a page like the one shown in the image below.

Installing Apache on RaspberryPi

 

The page shown above represents the html contents of the index.html file located in the /var/www/html directory which was created during the apache installation.

To display a unique webpage or create multiple pages, the contents of the index.html file can be edited to reflect the information to be displayed.

To edit the file, we need to change the ownership from root to your own username. Assuming your username is the default username “pi” change into the www directory and change the ownership of the file;

cd /var/www/html
sudo chown pi: index.html

 

With the ownership changed, we can then edit the script using the nano text editor. Run;

Sudo nano index.html              

 

Changing login details for Apache on Pi

 

Change the code to reflect whatever changes you desire, save and refresh the page on the browser to see the change.

 

Step 3:  Install PHP

To give the web server the ability to serve some complex and dynamic webpages, to give it the ability to process html, CSS JavaScript and PHP we will need to install the other components of the LAMP stack. Since we are already running on a Linux machine, the next component of the stack we will be installing is PHP. To install, run;

Sudo apt-get install php libapache2-mod-php

 

With this done, we can test the installation by creating an index.php file and insert it into the www directory. This should be done only after the index.html file has been removed from the directory as the .html takes precedence over .php.

To remove the .html file, while still within the www directory, run;

sudo rm index.html

 

Create the index.php file using;

sudo nano index.php

 

Insert some line of PHP code in the file.

<?php echo “ server up and running”;?>

 

Save and exit the editor. Refresh the page on the browser to see the changes.

If the raw php script is shown on the webpage instead of the “server up and running” text, restart the apache server. This is done using;

sudo service apache2 restart 

 

You should now be able to see the content of the webpage properly.

Instead of removing the index.html page, another page can be created with a name other than index. For eample page.php.
This page can be accessed on the browser via http://<youripaddress>/page.php

Installing PHP on Raspberry Pi

 

Step 4: Install MySQL Sever

Next, we need to install a database engine to manage and store data on the server. For the Lamp stack, we will be using MySQL. We need to install MySQL server and the PHP support packages for MySQL. An alternative to these will be to use PHPmyAdmin.

To install the MySQL server run;

Sudo apt-get install mysql-server php-mysql

 

With this done, restart Apache using;

sudo service apache2 restart

 

With this done, you now have a complete web server up and running and the database should now be administrable. At this point, you can create and host a website on this server by putting the html and PHP pages of the website in the www directory of the webserver and it will be accessible by anyone on the same network as the raspberry pi.

 

Install and Setup WordPress on the Raspberry Pi

With our webserver up and running one good way to test what we have done is to install the popular Content management system WordPress. With this, we will be able to create a website in few minutes.

 

Step 1: Download and Install WordPress

To clear things up and free some space on the raspberry pi, we remove the contents of the www directory. To do this run;

Cd ~
Cd /var/www/html
sudo rm *

 

After deleting all the files, we then download WordPress from their official website using;

sudo wget http://wordpress.org/latest.tar.gz

 

Once the download is complete, extract the tarball using;

sudo tar xzf latest.tar.gz

 

Move the contents of the WordPress folder into the current directory using;

Sudo mv wordpress/*  . 

 

Note the space before the “.”

Then remove the tarball to free up space on the pi using;

Sudo rm –rf wordpress latest.tar.gz

 

Before we continue, we need to change the ownership of all the wordpress files to the apache user. Run;

Sudo chown -R www-data: .

*don’t forget to add the “.” after the column.

 

Step 2: Setup the DataBase

All websites need a database; this is where MySQL comes in. To set up a database for WordPress, run;

sudo mysql_secure_installation

 

You will be prompted to enter the default/current password. Just press the enter key. Follow the prompt to complete the setup by creating a new password (Ensure you use a password you can easily remember), remove anonymous users, disallow remote root login, remove test database, and reload privileges table.  You should see an all done remark when everything is done.

Setting up Database for Wordpress on Pi

 

Next we create a database for WordPress. Run;

sudo mysql  -uroot  -p

 

Enter the root password we created above, you should see a welcome to mariaDB monitor prompt on the screen. When this appears, create a new DB using the command;

create database wordpress;

 

Note that the “WordPress” in the command above is my preferred name for the DB. Feel free to choose yours.

If this is successful, you should see a screen similar to the one in the image below.

Successfully Setup Database for Wordpress on Pi

 

Next, grant database privileges to the root user using;

GRANT ALL PRIVILEGES ON wordpress.* TO 'root'@'localhost' IDENTIFIED BY 'YOURPASSWORD';

 

Grant database privileges to  the root user

 

For the changes made to the DB to take effect, we need to flush the database privileges. Run;

FLUSH PRIVILEGES;

With this done, we then exit mariaDB using CTRL+D.

 

Step 3: Configure WordPress

Open a web browser on the pi and go to http://localhost you should see a WordPress page asking you to select your preferred language, select your preferred language and click continue.

Configuring Wordpress on Pi

 

On the next page, click on let’s go to proceed with installations.

It will request for basic site information. Fill them as shown below;

Database name: wordpress
Username: root
Password: <insert your password>
Database host: localhost
Table prefix: wp_

 

Setting up wordpress on Pi

 

Click the “submit” button followed by the “Run the Install” button. This will request an email, a username and password for your website. Supply this information and hit the “install wordpress” button. With this done, you should now be able to login to the backend of the websites and customize its look and use by visiting http://localhost/wp-admin

Login on Wordpress using Pi

 

To make the URL friendlier for users viewing from a different device on the same network, we will change the permalinks settings. To do this, from the wordpress backend, go to settings, select permalinks, select the “post name” option and click on the “save changes “ button.

So the webserver is aligned with these change, we will need to enable apache’s rewrite mod. Run;

Sudo a2enmod rewrite

 

We also need to instruct the virtual host to allow requests to be overwritten. To do this we will need to edit the default configuration of the available sites using the nano editor.

Run;
sudo nano /etc/apache2/sites-available/000-default.conf

 

Add the following lines after the first line

<Directory “/var/www/html”>
                AllowOverride All
</Directory>

 

Ensure it’s within the <VirtualHost *: 80> frame as shown  below.

<VirtualHost*: 80> 
                <Directory “/var/www/html”>
                                AllowOverride All
</Directory>

 

Save the file and exit using CTRL+X followed by Y and enter.

Restart Apache to effect the changes made to the configuration files. Run;

Sudo service apache2 restart

 

That’s it, we have website running on our Raspberry webserver. WordPress can be easily customized to your taste. You can easily change themes, add pages, posts, change the menu etc. 

Wordpress website running on Raspberry webserver

 

Generally, there are lot of things that can be achieved with a private webserver. One of those key uses is for file sharing between devices connected on the same network as the server.

By connecting the raspberry pi to a router and employing port forwarding techniques, the webserver can be deployed to serve webpages on the internet. This means the files stored on the webserver can be accessed from anywhere in the world.

It should be noted that the raspberry pi as a hardware is limited and may not perform optimally if used to host websites with high traffic.

Have any question realated to this Article?

Ask Our Community Members