This post is a small write up of a part of my project, in which I have hosted my WordPress blog on Raspberry Pi web server (Apache2). I have decided to pen down my experiences and how I did it. This post may be helpful for someone who is trying to achieve the same.
What you already know – For this post I assume that you have already have a Raspberry Pi computer running Raspbian and connected to internet.
For software I decided to keep things simple with LAMP stack. Those of you doesn’t know, it is very popular open source (free) Web development platform to work with.
Update OS and install Apache web server
Let’s start with updating the Raspbian. SSH in your raspberry Pi and run following commands on shell. It is that simple for a root user!
sudo apt-get update sudo apt-get upgrade –y
Once you are finished updating OS, time to install the webserver
sudo apt-get install apache2 –y
Please note that –y flag is telling apt-get command to answer ‘Yes’ for any question it will generate during installation process. Pretty handy.
Time to quickly test if web server is responding as expected. If you are connected from any other computer in your network then enter the I.P address of your Raspberry Pi in browser. E.g. http://192.168.1.20 you should see a default response from Apace web server. The default response is coming from a static html file located at – /var/www/html folder. If you are curious go ahead and modify the contents in file and it will reflect when you refresh the website. You can also check if Apache service is active by running following command on console
sudo service apache2 status
Install server side scripting: PHP
WordPress is written in PHP and if you don’t know, it is a hugely popular scripting language suited for server side web development. So let’s install PHP and dependencies using a simple command.
sudo apt-get -y install php
Finished! time to quickly test if it is working. Testing is totally optional but can’t help being skeptical. Let’s run the following commands. It will change directory to default apache web root folder -> Remove existing Index.html -> create a new Index.php file with some magic PHP code.
cd /var/www/html sudo rm index.html sudo echo "<?php phpinfo(); ?>" > Index.php
Refresh the browser ( http://192.168.1.20 ), if everything is fine then you will see lots of information like below:
Install MySql Database
MySQL is database management system and a popular choice for wordpress. Lets’ install it and all dependencies required for wordpress to work.
sudo apt-get install mysql-server php-mysql -y
Download and Setup WordPress
Make sure you are in /var/www/html folder. Download the latest version of WordPress from wordpress.org using wget command.
sudo wget http://wordpress.org/latest.tar.gz
Extract the tar and move all contents to current directory and then change the ownership of all files to the apache user (www-data)
sudo tar xzf latest.tar.gz sudo mv wordpress/* . sudo chown -R www-data: .
Setup database for wordpress
It’s time to make WordPress and MySql database talk each other. Before that let’s improve the security of our database with following command
This command is a way to securely setup the MySql database. This enables you to improve security of MySQL installation in following ways.
- You can set a password for root accounts.
- You can remove root accounts that are accessible from outside the local host.
- You can remove anonymous-user accounts.
- You can remove the test database
Answer ‘Yes’ (Y) for all above . Time to run mysql in the terminal window:
sudo mysql -uroot -p
Enter the root password you created in previous step. Now you have Mysql prompt, which looks like this: MariaDB [(none)]>
We can use default user to connect with database, but it is recommend creating another user for WordPress
CREATE USER 'dbuser'@'%' IDENTIFIED BY 'password123';
You will see Query Ok message upon successful command execution. We have created a new user ‘dbuser’, now let’s create a database for WordPress and grant this user access to this database
CREATE DATABASE wpdatabase;
We have created an empty database ‘wpdatabase for WordPress. Now grant the database privileges to the dbuser.
GRANT ALL PRIVILEGES ON wpdatabase.* TO ‘dbuser’@'%' IDENTIFIED BY 'password123';
Finally, flush the database privileges for changes to take effect using
At this point we have setup all dependencies to install and configure the WordPress blog. To start the WordPress configuration point your browser to the URL of Apache web root e.g. http://192.168.1.20 (if you are accessing from other computer in network) or http://localhost if you are logged in locally.
You will be taken to Select your language page where you can make the desired selection and click on Continue. The next step is to provide the database information. Please provide the following information:
Database Name – ‘wpdatabase’ Database username – ‘dbuser’ Database password – ‘password123’ Database host – ‘localhost’
Click on Submit and proceed with installation.