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
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.
Click the Let's go!
button.
Now fill out the basic site information as follows:
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.
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 :
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...
Works flawlessly as expected, thanks!
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?
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.I will just get a fresh image of Raspbian and try again. I’ll let you know if I get stuck again. thanks!:D
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.
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?
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.
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.From /var/log/nginx/error.log i am getting these errors:
and from “var/log/php-7.3-fpm.log” i am getting these messages:
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. 😀
Your nginx error suggesting that you have mismatching database prefix:
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.