Install WordPress on a Raspberry Pi

WordPress the CMS that doesn’t need introduction.
In this guide we are going to install it and configure it a bit.

It is required you took the previous steps of setting up the Raspberry, installed php, nginx and mysql.

Setup Url

So far we’ve setup example.local we will create a second url and add configure it. I will use wordpress.local but feel free to use whatever you want.

First lets setup the correct directories.

sudo mkdir -p /var/www/wordpress.local/public_html
sudo chown www-data:www-data /var/www/wordpress.local -R
sudo nginx -t && sudo service nginx reload

Create the vhost and fill it with with the following.
sudo nano /etc/nginx/sites-enabled/wordpress.local.conf

server {
    listen 80;
    
    ## Your website name goes here.
    server_name wordpress.local www.wordpress.local;
    ## Your only path reference.
    root /var/www/wordpress.local/public_html;
    ## This should be in your http block and if it is, it's not needed here.
    index index.php;

    location = /favicon.ico {
            log_not_found off;
            access_log off;
    }

    location = /robots.txt {
            allow all;
            log_not_found off;
            access_log off;
    }

    location / {
            # This is cool because no php is touched for static content.
            # include the "?$args" part so non-default permalinks doesn't break when using query string
            try_files $uri $uri/ /index.php?$args;
    }

    location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
            expires max;
            log_not_found off;
    }
    
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        fastcgi_intercept_errors on;
        fastcgi_buffers 16 16k;
        fastcgi_buffer_size 32k;
    }
}

This config is the bare minimum as recommended by the nginx manual.
It works but it is not the best. The official WordPress docs has some more examples.
The main thing to improve is security.
I might do a better config in the future but for now this is all.

For the final step reload nginx.

sudo nginx -t && sudo service nginx reload

Create DB

Login to the mysql prompt:

sudo mysql -uroot

After that run the following lines to create a database, create a user with a strong password and assign permissions.

CREATE DATABASE raspimain_db;
CREATE USER 'raspimain_user'@'localhost' IDENTIFIED BY 'P@SSW0RD_Str0nG_R@nD0m_&_L0ng';
GRANT ALL PRIVILEGES ON `raspimain_db`.* TO `raspimain_user`@`localhost`;
FLUSH PRIVILEGES;
EXIT;

Download WordPress

Go to the new WordPress directory.

cd /var/www/wordpress.local/public_html/

Download the latest version of WordPress, and unzip it.

sudo wget https://wordpress.org/latest.zip
sudo unzip latest.zip
sudo mv wordpress/* ./
sudo rm -r wordpress latest.zip
sudo chown www-data:www-data . -R

Install WordPress

Now go to the url and just follow the famous 5 minute install.
And that is all.
A few things to check after the install is done.

Gravity Forms give editors access

By default Gravity forms isn’t accessible by editors. There are ways to do this with plugins like members.
As some might guess I prefer wp-cli for this:

wp cap add editor gravityforms_create_form gravityforms_edit_forms gravityforms_view_entries gravityforms_export_entries gravityforms_delete_entries graviyforms_delete_forms gravityforms_edit_entries gravityforms_view_entry_notes gravityforms_edit_entry_notes --grant
Difference in rights after setting capabilities

Running this will pretty much give all rights except the settings of Gravifty Forms.
It will allow creatign, editing & deleting forms. But also view entries and export them.

Full list of Gravity Forms Capabilities

Nano Shortcuts

I’ve know about nano and some it’s shortcuts. Today I explored them a bit more deeply.
So a list of shortcuts I find useful:

  • ctrl+K Cut the current line and put it in the nano clipboard (it’s not the same as the general clipboard)
  • ctrl+U Paste the line
  • ctrl+W Open search, type and hit enter. For the next match press alt+W
  • ctrl+Q Search backwards. For the next backward match press alt+Q
  • alt+U Undo
  • alt-E Redo
  • alt+C show the line number
  • alt-G Go to line number

Sources:

  • https://www.nano-editor.org/dist/latest/cheatsheet.html
  • https://askubuntu.com/a/672091/208343

PHP namespaces grouped

Consider the following code:

<?php
namespace a\b\c;
use function is_string;
use function get_class;

class main {
    protected $string;
    public function __construct( $string ) {
        if ( is_string( $string )) {
            $this->string = $string;
        } else {
            $this->string = get_class( $this );
        }
    }
}

Nothing fancy. Just a class in a namespace using two functions from the global namespace (is_string & get_class).
Those two functions are imported from the global namespace as that will give a small performance boost.

But if you have 20-30 build in PHP functions that list will get very long….

Luckily you can merge them:

use function is_string, get_class;

For now I’m not sure I’ll always import build in PHP functions, the boost is small. And it’s annoying to keep track of.

sumcheck a whole directory

For some reason files changed on a server. Site down, always fun.
Restored a backup all good. This site did not have git on the server. But I still wanted to monitor the files for changes.

The one I landed on was:

find ./ -type f -name "*.php" -not -path "./wp-content/cache/*" -exec md5sum {} + | sort -k 2 | md5sum

Let’s dissect

What does this command do step by step

In the current directory and sub directory, list all files (not directories)

find ./ -type f

Limit it to php files

find ./ -type f -name "*.php"

Exclude the files in the caching directory, a bit weird syntax but it’s the one.

find ./ -type f -name "*.php" -not -path "./wp-content/cache/*"

For each file found run the command md5sum making a sum per file.

find ./ -type f -name "*.php" -not -path "./wp-content/cache/*" -exec md5sum {} +

Next we sort the output based on filepath+name.
We sort because find might return file order inconsistently.

find ./ -type f -name "*.php" -not -path "./wp-content/cache/*" -exec md5sum {} + | sort -k 2

Finally we create the grand total sumcheck based on all other sumchecks.

find ./ -type f -name "*.php" -not -path "./wp-content/cache/*" -exec md5sum {} + | sort -k 2 | md5sum

Sources:

  • https://stackoverflow.com/a/1658554/933065
  • https://unix.stackexchange.com/a/35834

WordPress the_date skipping same days

Lets take a look at this very basic loop.

<?php
$query_name = new WP_Query();

if ( $query_name->have_posts() ) :
    while ($query_name->have_posts()): $query_name->the_post();
        the_date();
        the_title();
        the_content();
    endwhile;
endif;

Nothing special right? Will by default just display the date, title and content of the first 10 posts.
But if 2 posts are published on the same day it will skip display that posts date.

When looking at the source code of the_date it compares the date of the previous post with the current using is_new_day.
I guess it can make sense in some scenario’s but too me it’s a bit weird by default.

To prevent this just use

<?php
echo get_the_date();

php shorthand if

I’ve known about the shorthand if for years:

<?php echo ($username) ? $username : ''; ?>

Today If saw this which is called Ternary and has been available since 5.3 which is older than my days of coding….

<?php echo ($username) ?: ''; ?>

Update PHP7.4
When assigning value especially in an array or object you can now do the following.

<?php

$person = [ 'date_of_birth' => '1970-01-01' ];

$person['name'] = $person['name'] ?? 'John Doe'; //php7.3
$person['name'] ??= 'John Doe';                  //php7.4

var_dump( $person );

for & foreach loops performance

Today we will handle a case of “Premature Optimization Is the Root of All Evil“.
But this is my blog and I was working with a very big api set, and will only get bigger, so (premature) thinking about memory usage and execution time might be a good idea in the long run.

Before I started this I thought that a for loop was faster then a foreach loop. And I usually pick foreach because it’s easier to write and read.

A quick google lands on a stackoverflow question which concludes the opposite. So I started to test a bit.

There is a big difference in my use case here.

I need to remove array items that need to be excluded from the api results. Most examples you will find online are about editing items.

First tests where quite clear, using a foreach was in most configurations faster than for. The test array I create has 10000 items and every 3rth item should be excluded:

<?php
$test_array = array();
for ( $i = 0; $i <= 10000; $i ++ ) {
    $test_array[] = [
        'index'   => $i,
        'include' => ( $i % 3 === 0 ) ? true : false,
    ];
}

The traditional foreach loop

The way I have been filtering arrays for years.
Create a empty array and only put in the elements the need to be included.

<?php
$filtered_array = [];
foreach ( $test_array as $item ) {
    if ( true === $item['include'] ) {
        $filtered_array[] = $item;
    }
}
return $filtered_array;

Immediately delete item

Don’t use a between array, just unset the item in the parent array

<?php
foreach ( $test_array as $key => $item ) {
    if ( true === $item['include'] ) {
        unset( $test_array[ $key ] );
    }
}
return $test_array;

Traditional pass by reference

The same as before, but the $item is passed by reference.
This is the big difference, since we are not editing the $item we want to remove it from the parent array

<?php
$filtered_array = [];
foreach ( $test_array as &$item ) {
    if ( true === $item['include'] ) {
        $filtered_array[] = $item;
    }
}
return $filtered_array;

Immediately delete pass by reference

Again the same and again passed by reference

<?php
foreach ( $test_array as $key => &$item ) {
    if ( true === $item['include'] ) {
        unset( $test_array[ $key ] );
    }
}
return $test_array;

The for loop

And last the for loop I was wondering about

<?php
$length = count( $test_array );
for ( $i = 0; $i < $length; $i ++ ) {
    if ( false === $test_array[ $i ]['include'] ) {
        unset( $test_array[ $i ] );
    }
}
return $test_array;

the results

I ran each of these loops 5000 times and measured the total time that took.
This was to insure the time between results was big enough to exclude the randomness (at least enough)
The test code I ran

Speed of Loops
  1. 5.6122910976414sec Loop: foreach traditional
  2. 6.0467801094055sec Loop: foreach unset key
  3. 7.7878839969635sec Loop: foreach traditional pass by reference
  4. 7.0686309337616sec Loop: foreach unset key pass by reference
  5. 8.6388339996338sec Loop: for

The traditional foreach I’ve been using for years turned out to be the fastest.
Research hours well spend ?

Bonus edit array item

As said before most examples use editing a array.
So I also ran that scenario. Test code
I upped the loops from 5000 runs to 7500 because the difference was so small.
And still it’s close.

  1. 15.363855123523sec Loop: foreach traditional
  2. 10.987272024155sec Loop: foreach foreach edit array directly
  3. 11.358484983444sec Loop: foreach traditional by reference
  4. 14.363346099854sec Loop: for

Here the traditional is the slowest. Reference is a lot faster as most articles claim.
But editing the array directly was the fastest.

WordPress filters and anonymous functions

Anonymous functions have been around for a long time. And since WordPress now supports php 5.6 it can be safely used.
And appears to be allowed?

Personally I’m not a fan of anonymous functions in combination with WordPress actions and filters. My main concern is you can’t remove them once registered. How ever today I found a use case which was very usefull in combination with the use

My example:

<?php
/* Template name: some-template */

// Gather all data
$condition_for_title = true;
$h1_title_override = 'very heavy and complicated check';
// the H1 also needed as the <title>

add_filter( 'pre_get_document_title', function( $title ) use ($h1_title_override, $condition_for_title) {
    if ($condition_for_title) {
        return $h1_title_override;
    }
    return $title;
}, 20, 1 );

get_header();

// start body
?>
    <h1><?php echo $h1_title_override ?>

Here I pass 2 variables h1_title_override and $condition_for_title which are created outside the function. In my case these where quite complicated and heavy checks. Of course I could put those in a function and cache the result. And call that check in the filter function. But still I need to check the current template before doing the function.

More traditional Example:

in functions.php

<?php
function complicated_check() {
    // Gather all data
    $condition_for_title = true;
    $h1_title_override   = 'very heavy and complicated check';

    return [
        'condition_for_title' => $condition_for_title,
        'h1_title_override'   => $h1_title_override,
    ];
}

function title_exception_for_template( $title ) {
    if ( ! is_page_template('clean-template.php')) {
        return $title;
    }

    $template_data = complicated_check();

    if ( $template_data['condition_for_title'] ) {
        return $template_data['h1_title_override'];
    }

    return $title;
}

add_filter( 'pre_get_document_title', 'title_exception_for_template', 20, 1 );

in clean-template.php

<?php
/* Template name: clean-template */
$template_data = complicated_check();

get_header();

// start body
?>
    <h1><?php echo $template_data['h1_title_override'] ?>

Both these approaches do the same thing. But the more traditional way is a lot more code. Although it has cleaner template.
I probably won’t use this much. If the anonymous function was more complicated it will get hard to read.

But for this case I think it was neat that I could use this little feature.

Installing selfoss on a Raspberry Pi

Selfoss is an RSS reader.
We are going to install it and configure it using Nginx.

Preparing the vhost

In this example I’m using selfoss.local as the url to set it up. Change the url to whatever you want.

sudo mkdir -p /var/www/selfoss.local/public_html
sudo chown www-data:www-data /var/www/selfoss.local -R
cd /var/www/selfoss.local/public_html

Create the vhost file: sudo nano /etc/nginx/sites-enabled/selfoss.local.conf

server {
    listen 80;

    server_name selfoss.local;
    root /var/www/selfoss.local/public_html;

    index index.php index.html;

    location ~* \ (gif|jpg|png) {
        expires 30d;
    }

    location ~ ^/(favicons|thumbnails)/.*$ {
        try_files $uri /data/$uri;
    }

    location ~* ^/(data\/logs|data\/sqlite|config\.ini|\.ht) {
        deny all;
    }

    location / {
        index index.php;
        try_files $uri /public/$uri /index.php$is_args$args;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
        fastcgi_intercept_errors on;
        fastcgi_buffers 16 16k;
        fastcgi_buffer_size 32k;
    }
}

And finally reload nginx: sudo nginx -t && sudo service nginx reload

Download Selfoss

Next we download Selfoss. On the Selfoss home page there is a download button. Copy the destination of that link.
At the time of writing it is on version 2.18 and the download zip is
Replace the url if needed.

sudo wget https://github.com/SSilence/selfoss/releases/download/2.18/selfoss-2.18.zip -O selfoss.zip
sudo unzip selfoss.zip
sudo rm selfoss.zip
sudo chown www-data:www-data /var/www/selfoss.local -R
Screenshot of selfoss empty installation

Now visit the url. You should see an empty -but working- selfoss screen.

Configuration

Although this installation works we are going co configure some aspects of it.

DB connection.

Be default Selfoss uses sqlite. Mysql has a better performance. Instructions to install Mysql are in a separate article.
More detailed instruction are there. So here are the quick instructions to create a database.

Login to mysql as root: sudo mysql -uroot
Create a db, and assign a new user. !Replace the password
You can set your own database name and user.

CREATE DATABASE misc_selfoss;
CREATE USER 'selfoss'@'localhost' IDENTIFIED BY '%%SAFE_PASSWORD%%';
GRANT ALL PRIVILEGES ON `misc_selfoss`.* TO `selfoss`@`localhost`;
FLUSH PRIVILEGES;
EXIT;
Creating the database for Selfoss

Next we set the db connection in the config file: sudo nano config.ini

[globals]
db_type=mysql
db_file=data/sqlite/selfoss.db
db_host=localhost
db_database=misc_feeds
db_username=janwfeeds
db_password=%%SAFE_PASSWORD%%
db_port=
db_prefix=self_

A few cleanup commands afterwards

sudo rm data/sqlite/selfoss.db
sudo chown www-data:www-data /var/www/selfoss.local -R

Password protection

To protect your selfoss installation we will set a password.
Go to your url/password in my case: http://selfoss.local/password

Screenshot of the Seelfoss password generation screen

After go got the hash add it to the config file: sudo nano config.ini

username=yourusername
password=a19fe3f69ec64cded61d0dbc01f0200ab265df0c3a2828d0e53d47bc93032698fbc7862e536c324f47edd6bd1f3e07571cca898fae140394f93632fa9334ad

Other configurations.

There are more configurations. See the official documentation Personally I prefer that items get marked as read when I open them.

auto_mark_as_read=1
homepage=unread

Now you are ready to start using Selfoss. Have fun!