Hosting WordPress on Raspberry Pi Part 3 – Setup WordPress

wordpress-setup-page

We now have a server running Nginx, php7-fpm with MariaDB, now it is time to install WordPress, and how to handle WordPress permalink for Nginx.

Install WordPress

We are ready to install WordPress, which is quite straightforward. First we want to delete the index.php that we previously created for testing php activation as well as the default Nginx index.html file before downloading WordPress.

cd /var/www/html/
sudo rm index.*
sudo wget https://wordpress.org/latest.tar.gz

Upzip the file which will create a folder called wordpress, and we want to move all the content to the current directory, and then delete the empty director left over as well as the downloaded source file.

sudo tar xzf latest.tar.gz
sudo mv wordpress/* .
sudo rm -rf wordpress latest.tar.gz
wordpress-installed
WordPress Installed

Next, we'd want to add a robots.txt file to inform crawlers not to crawl certain directories, create the file using:

sudo nano robots.txt

Input the follow lines:

User-agent: *
Disallow: /wp-admin/
Allow: /wp-admin/admin-ajax.php
Disallow: /wp-includes/
Allow: /wp-includes/js/*
Disallow: /admin/

Press Control-x to save file and exit the editor.

The files downloaded belong to user ‘nobody’, we should change the ownership of the html directory to Nginx user www-data:

sudo chown -R www-data: .

Setup a WordPress database

We can now create a database which will be used by WordPress. Login to mariaDB with username and password:

mysql -u username -p

Create a database and then exit the mariaDB.

CREATE DATABASE wordpress_on_p3;
exit

Launch web browser and navigate to http://192.168.0.101. You should see a WordPress weclome page.

wordpress-welcome-page
WordPress Welcome Page

Click the Let's go! button.
Now fill out the basic site information as follows:

wordpress-database-connection
WordPress Database Connection

and click Submit to proceed and click the Run the install button.

Fill out the information: give your site a title, create a username and password and enter your email address. Hit the Install WordPress button, then log in using the account you just created.

Now you're logged in and see the Dashboard of WordPress. To administrate the WordPress site and log in again in future, go to http://192.168.0.101/wp-admin. To see the website, go to http://192.168.0.101.
Further customisation of WordPress are quite straightforward and therefore we will not discuss here.

WordPress Permalinks on Nginx

WordPress generally works out-of-the box on Nginx until when trying to customise the permalinks. When WordPress run on Apache, with mod_rewrite server directive, WordPress will automatically add the required rewrite rules to .htaccess file for permalinks to work. But for Nginx, when WordPress detects that mod_rewrite is not loaded (which is the case with Nginx), it falls back to using PATHINFO permalinks, which inserts an extra ‘index.php’ in front. This can be solved by using the ‘Custom Structure’ option to remove the index.php.

wordpress-permalinks-setting
WordPress permalinks setting

Then we will have to setup the re-write rule manually.

sudo nano /etc/nginx/sites-available/default

Scroll down to find the location / section, and comment out (add a # in front of the line) the original try_files directive and add a new try_files directive as following line.

# try_files $uri $uri/ =404;
try_files $uri $uri/ /index.php?$args;

What this means is that Nginx will check for the existence of a file at the URL ($uri), then for a directory ($uri/). If it doesn’t find a directory or a file, it will redirect to /index.php passing the query string arguments as parameters.

It should look like this after the edits :

nginx-redirect-rule
Nginx Redirect rule

Save the file and reload the configuration settings.

sudo service nginx reload

We now have the WordPress web site setup and be able to create and view contents within our local environment. It is time to create content, and then get a domain name and publish the site for everyone to access it.

Related Topic

If you are going to write your blog frequently, sooner or later, the SD card will be running out of space with all the images and articles, you might want to consider to mount a hard disk to Raspberry Pi as root file system eventually, but this is not required immediately, let's continue to setup the server for now...

10 comments by readers

  1. I am getting the following error: Your PHP installation appears to be missing the MySQL extension which is required by WordPress.
    Could you help me?

    1. What is the version of your PHP and WordPress? This Error is created by the WP core file /wp-includes/load.php under the function wp_check_php_mysql_versions(). What happened is PHP7 no longer support mysql and switched to mysqli, if you follow my description part2 to install the php7, this should not happened. Unless you had an really old WordPress installed (which I think is unlikely), so that leaves the possibility that some themes or plugins that you are using could still using mysql.

      1. I will just get a fresh image of Raspbian and try again. I’ll let you know if I get stuck again. thanks!:D

        1. Worked flawlessly. Something probably went wrong on the other installation. Just as a piece of information to other ppl: my php.ini file was empty when I edited it with nano.

  2. I everything works great until the end when i try to add a new plugin or a new theme and i get the following error message:
    “An unexpected error occurred. Something may be wrong with WordPress.org or this server’s configuration. If you continue to have problems, please try the support forums.”
    Any idea on what might be the problem?

    1. By the way i am working on a rapsberry pi 3 / raspbian-buster-lite / php 7.3 fpm.
      Also i had tried another guide with apache server in the past and worked just fine with adding plugins/themes but i wanted to try nginx server this time.

    2. Check your /var/log/nginx/error.log and /var/log/php-7.3.log to see what exactly the error is. Also try to turn off all the plugins, and turning it back on one by one to see if there is any plugins caused it. Personally, I have not test the php 7.3 yet.

      1. From /var/log/nginx/error.log i am getting these errors:

        2020/05/18 18:08:26 [notice] 8430#8430: signal process started
        2020/05/18 19:03:21 [error] 456#456: *48 FastCGI sent in stderr: "PHP message: WordPress database error Unknown column 'wp_on_p3_' in 'field list' for query SELECT wp_on_p3_" while reading response header from upstream, client: 192.168.2.10, server: _, request: "POST /wp-admin/setup-config.php?step=2 HTTP/1.1", upstream: "fastcgi://unix:/run/php/php7.3-fpm.sock:", host: "192.168.2.20", referrer: "http://192.168.2.20/wp-admin/setup-config.php?step=1"
        2020/05/18 19:06:49 [error] 456#456: *66 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 192.168.2.10, server: _, request: "GET /wp-admin/ HTTP/1.1", upstream: "fastcgi://unix:/run/php/php7.3-fpm.sock", host: "192.168.2.20", referrer: "http://192.168.2.20/wp-login.php"
        2020/05/18 19:09:18 [error] 456#456: *89 FastCGI sent in stderr: "PHP message: PHP Warning:  Invalid argument supplied for foreach() in /var/www/html/wp-admin/includes/theme.php on line 557" while reading response header from upstream, client: 192.168.2.10, server: _, request: "POST /wp-admin/admin-ajax.php HTTP/1.1", upstream: "fastcgi://unix:/run/php/php7.3-fpm.sock:", host: "192.168.2.20", referrer: "http://192.168.2.20/wp-admin/theme-install.php?browse=featured"
        

        and from “var/log/php-7.3-fpm.log” i am getting these messages:

        [18-May-2020 18:06:11] NOTICE: fpm is running, pid 7704
        [18-May-2020 18:06:11] NOTICE: ready to handle connections
        [18-May-2020 18:06:11] NOTICE: systemd monitor interval set to 10000ms
        [18-May-2020 18:06:22] NOTICE: Terminating ...
        [18-May-2020 18:06:22] NOTICE: exiting, bye-bye!
        [18-May-2020 18:06:22] NOTICE: fpm is running, pid 8391
        [18-May-2020 18:06:22] NOTICE: ready to handle connections
        [18-May-2020 18:06:22] NOTICE: systemd monitor interval set to 10000ms
        [18-May-2020 18:46:43] NOTICE: Terminating ...
        [18-May-2020 18:46:43] NOTICE: exiting, bye-bye!
        [18-May-2020 18:46:55] NOTICE: fpm is running, pid 446
        [18-May-2020 18:46:55] NOTICE: ready to handle connections
        [18-May-2020 18:46:55] NOTICE: systemd monitor interval set to 10000ms
        

        I will try to find a way to use php 7.0 fpm like your example maybe that will solve the issue… Thank you for your fast response. 😀

        1. Your nginx error suggesting that you have mismatching database prefix:

          "PHP message: WordPress database error Unknown column 'wp_on_p3_' in 'field list'...
          

          You must be missed some steps along the way of setting up the database/WordPress. Fix your database prefix or change your config.php to match the database prefix you used.

          Don’t try to use php7.0, a) it is deprecated and don’t try to download from some unknown source. b) your error has nothing to do with php version.

Comments are closed.