Local Drupal/Wordpress webserver on Raspberry Pi 3

Alexandru Calcatinge bio photo By Alexandru Calcatinge

All you need in order to run your local micro webserver is a Raspberry Pi running with the latest Raspbian distribution and the LAMP stack installed. At the end of this post I will discuss the steps required to install Drupal and WordPress. In this regard, I will advice you that you should install the one that suits your needs the most, or even both of them, if you have two different online projects that are based on either one of this two CMS’s.

Install LAMP on Raspbian

LAMP stands for Linux, Apache, MySQL and PHP and we will install this stack on the current Raspbian distribution on our Raspberry Pi 3 model B. As we already have the L(inux) from the stack installed, we will dive into the installation of the other three. After AMP is installed, we will configure their components and further install Drupal (or Wordpress), depending on what you like more. As openLARK was once built on Wordpress and then on Drupal, we have a direct interest in showing you how this works. We use the Raspberry Pi because of its size and low power consumption. I think that this little piece of technology is just great for a small home server that will host some relatively small websites. So, here we go…

Install Apache Web server

Apache is one of the widely used web server applications out there, it is constantly updated and maintained and is pretty secure if you configure it right. To install it use the command:

sudo apt install apache2 -y

After the installation is complete, you can test to see if everything is done right by opening a new web page on your browser inside your Pi, with the address:

http://localhost/

After this, you should look for the default web page that came with the apache installation. This is located in /var/www/html on the latest Raspbian Jessie distribution. Go to this directory by using the command:

cd /var/www/html

doing a ls -l will show you the contents of the directory, in our case a index.html file. This is the default web page that came with the installation.

Install MariaDB (or MySQL)

MariaDB, a fork of MySQL is more and more becoming defacto installation on major new distributions. Debian 9 has it by default, openSUSE Leap too… This is because it is completely open source compared to MySQL that still has some proprietary code from Oracle. Thus, I prefer intalling it over mysql, as it is 100% compatible with the former. MariaDB/MySQL database engines are used by both Drupal and Wordpress, so it is crucial to have it installed and configured on your system. To install mariadb use the command:

sudo apt install mariadb-server mariadb-client php5-mysql -y

The installation process is interactive, so you will be asked several questions as for a root password for the mysql root user. Enter it and be sure to remember it afterwords. Be cautios as this should not be the same password as the one you use for the system root user. They are two completely different “root” users. After the installation si complete, restart the apache service by using the command:

sudo service apache2 restart

Install PHP

The PHP preprocessor is used by both Drupal and Wordpress as their server side programming languages. Thus, this is needed on you system. Drupal and Wordpress are both written in PHP. Install PHP by using the command:

sudo apt install php5 libapache2-mod-php5 -y

We will use PHP5, even if PHP7 is also available, as it is yet more stable then the newer version. At least in my opinion. Now, in order to test your PHP installation, you should create a new file in your document root /var/www/html. Assuming that you are placed in the /var/www/html/ directory, you now can create the file with the command:

sudo vim index.php

this command will open a new empty file in vim editor called index.php. Now you can create content inside the file. Remember that VIM is not installed by default in Raspbian. You should install it if you haven’t done so yet. For this, use the command:

sudo apt install vim

Now, let us get back to the index.php file. Usually the following lines are written into the file, to test your PHP installation:

<?php
phpinfo();
?>

After this, save the file and exit vim using the commands: ESC - to enter in command mode from Insert mode :wq - to (w)rite the file to disk and then (q)uit the vim program Restart the apache service with the command:

sudo service apache2 restart

While still inside the /var/www/html directory, you should remove the html file and keep only the php one. Do this with the following command:

sudo rm index.html

Now, if you open a new browser tab and type

http://localhost/

you should see information about your php installation.

Close to finish…

Now, this was the easiest part. Based on your prefs, you should install Drupal or Wordpress. I would personally go with Drupal for this one, even if Wordpress is yet the most widely used CMS out there. You will need this if you want to create your own webpage that is responsive and easy to create and manage. As I am planning on creating some personal online LMS projects later this year, I intend to learn Drupal to use it for those projects. It is higly secure and it has a great dedicated community too.

Get a domain

I assume that you already have a domain that you would like to host on your personal RPi web server and I will not go talking about how to buy your own domain. As we set this straight, one more thing you should have in order to use your own web server is a static IP address. You might not have one, as you are a private user. I don’t have one either. For this, I use dynu.com services to make my domain point to the same IP address always. This is a free service, among many other. Just search the internet and use whichever you find appropriate for you. To install dynu.com client on your system, just do the following:

sudo apt install ddclient

After installation run the client with the command:

/usr/sbin/ddclient -daemon 300 -syslog

You can also run a debug with the command:

ddclient -daemon=0 -debug -verbose -noquiet

To configure the client edit the /etc/ddclient.conf with your options, based on the following tutorial.

Installing Drupal 8 on your Pi

First, you have to download Drupal from their website. The final version at the time of writing this article was 8.3.5. Inside the commands below, replace the numbers with the exact version numbers of your files. After download, you should create a directory called drupal8 inside /var/www/html with the command:

sudo mkdir /var/www/html/drupal8

Go to the directory where your Drupal source is (usually in Downloads):

cd ~/Downloads

Copy the archive to the /var/www/html/drupal8 directory with the command:

sudo cp drupal-8.3.5.tar.gz /var/www/html/drupal8/

Go back to /var/www/html/drupal8 directory with the command:

cd /var/www/html/drupal8/

Now extract the drupal files from the tar.gz archive to this directory (while inside that directory):

sudo tar xzf drupal-8.3.5.tar.gz

This will create a directory called drupal-8.3.5 inside the drupal8 directory you created earlier. After this, remove the archive:

sudo rm drupal-8.3.5.tar.gz

Now change the folder permissions for apache to have access to drupal’s installation:

sudo chown www-data:www-data -R /var/www/html/drupal8/
sudo chmod -R 755 /var/www/html/drupal8/

Configuration of MariaDB for Drupal 8

With MariaDB and Apache already installed, it is time to configure them for your needs. First let us configure MariaDB. Run the configuration script with the following command:

sudo mysql_secure_installation

and follow the on screen questions. You will be asked to set a root password. You did that earlier, so you could enter the same password as before. You should answer with “Y” when asked to remove anonymous users, answer with “Y” to disallow remote root login, “Y” to remove test database and “Y” to reload privilege tables. After that you will have to log into mariadb with the root user using the command:

sudo mysql -u root -p

and insert the password afterwords Create a new user in MariaDB:

CREATE USER 'drupaluser'@'localhost' IDENTIFIED BY 'password';

Now you will have to create a database for drupal to use. Use the following commands while in mariadb command line interface:

CREATE DATABASE drupal;
GRANT ALL PRIVILEGES on drupal.* TO 'drupaluser'@'localhost' IDENTIFIED BY 'password';
FLUSH PRIVILEGES;

Ctrl+D to exit mariadb.

Configuration of Apache2 for Drupal 8

Once you have your domain, you can create a new virtual host directive inside apache configuration file. In order to configure a new virtual host for your domain, first you have to create a configuration file for that specific domain. For example, I have my domain called peisajcultural.ro and I would like to create a virtual host for that and use Drupal to build the website. Thus, I will have to create a new file inside /etc/apache2/sites-available/ with the command:

sudo touch /etc/apache2/sites-available/peisajcultural.conf

Open the file for editing:

sudo vim /etc/apache2/sites-available/peisajcultural.conf

And enter the following lines, but adapted for your domain name:

<VirtualHost *:80>
 ServerAdmin your@email.com
 DocumentRoot /var/www/html/drupal8/drupal-8.3.5/
 ServerName peisajcultural.ro
 ServerAlias www.peisajcultural.ro
 <Directory /var/www/html/>
 Options FollowSymLinks
 AllowOverride All
 Order allow,deny
 allow from all
 </Directory>
 ErrorLog /var/log/apache2/peisajcultural-error_log
 CustomLog /var/log/apache2/peisajcultural-access_log common
</VirtualHost>

Enable the new configuration file by running the command:

sudo a2ensite calcatinge_ro.conf

Now restart apache service with the command:

sudo service apache2 restart

Now that you have seen how to install Drupal 8 on your system, it is time to start it and configure it in a graphical manner. For this, you will have to open a web browser and add your domain address in the address bar. But before you could test it, you must make sure that your router is set up to forward port 80, otherwise it won’t work.

Installing Wordpress on your Pi

You can download Wordpress from their website. After downloading it, you can do exactly what you did in the case of Drupal. First you should create a directory called wordpress in /var/www/html/ using the command:

sudo mkdir /var/www/html/wordpress

Go to the directory where your Wordpress source is (usually in Downloads):

cd ~/Downloads

Copy the archive to the /var/www/html/wordpress directory with the command:

sudo cp wordpress-4.8.tar.gz /var/www/html/wordpress/

Move back to your /var/www/html/wordpress/ directory with the command:

cd /var/www/html/wordpress/

Now extract the wordpress files from the tar.gz archive to this directory (while inside that directory):

sudo tar xzf wordpress-4.8.tar.gz

After this, remove the archive:

sudo rm wordpress-4.8.tar.gz

Now change the folder permissions for apache to have access to wordpress’s installation:

sudo chown www-data:www-data -R /var/www/html/wordpress/
sudo chmod -R 755 /var/www/html/wordpress/

Now change the folder permissions for apache to have access to wordpress’s installation:

sudo chown www-data:www-data -R /var/www/html/wordpress/
sudo chmod -R 755 /var/www/html/wordpress/

Configuration of MariaDB for Wordpress

With MariaDB and Apache already installed, it is time to configure them for your needs. First let us configure MariaDB. Run the configuration script with the following command:

sudo mysql_secure_installation

and follow the on screen questions. You will be asked to set a root password. You did that earlier, so you could enter the same password as before. You should answer with “Y” when asked to remove anonymous users, answer with “Y” to disallow remote root login, “Y” to remove test database and “Y” to reload privilege tables. After that you will have to log into mariadb with the root user using the command:

sudo mysql -u root -p

and insert the password afterwords Create a new user in MariaDB:

CREATE USER 'wordpressuser'@'localhost' IDENTIFIED BY 'password';

Now you will have to create a database for drupal to use. Use the following commands while in mariadb command line interface:

CREATE DATABASE wordpress;
GRANT ALL PRIVILEGES on wordpress.* TO 'wordpressuser'@'localhost' IDENTIFIED BY 'password';
FLUSH PRIVILEGES;

Ctrl+D to exit mariadb.

Configuration of Apache2 for Wordpress

Once you have your domain, you can create a new virtual host directive inside apache configuration file. In order to configure a new virtual host for your domain, first you have to create a configuration file for that specific domain. For example, I have my domain called peisajcultural.ro and I would like to create a virtual host for that and use Wordpress to build the website. Thus, I will have to create a new file inside /etc/apache2/sites-available/ with the command:

touch /etc/apache2/sites-available/peisajcultural.conf

Open the file for editing:

sudo vim /etc/apache2/sites-available/peisajcultural.conf

And enter the following lines, but adapted for your domain name:

<VirtualHost *:80>
 ServerAdmin your@email.com
 DocumentRoot /var/www/html/wordpress/wordpress/
 ServerName peisajcultural.ro
 ServerAlias www.peisajcultural.ro
 <Directory /var/www/html/>
 Options FollowSymLinks
 AllowOverride All
 Order allow,deny
 allow from all
 </Directory>
 ErrorLog /var/log/apache2/peisajcultural-error_log
 CustomLog /var/log/apache2/peisajcultural-access_log common
</VirtualHost>

Enable the new configuration file by running the command:

sudo a2ensite peisajcultural.conf

Now restart apache service with the command:

sudo service apache2 restart

Now that you have seen how to install Wordpress on your system, it is time to start it and configure it in a graphical manner. For this, you will have to open a web browser and add your domain address in the address bar.

Now, this is it, you have installed two of the most powerful CMS applications, and all you have to do is to start building your website! There would still be some steps required to secure your Raspberry Pi, but this will be treated in a different post!

Enjoy!!!