Now that we have a server running Nginx, php7.0-fpm with MariaDB we can install WordPress, which will act as our content management system (CMS) and provide a framework for the website.
Install WordPress
Instlaling WordPress is pretty straightforward. Let’s make sure we’re in the correct directory:
1 | cd /var/www/html/ |
Next let’s download the latest version of WordPress to this location:
1 | sudo wget http://wordpress.org/latest.tar.gz |
Now let’s upzip this file and move its contents to the current directory. Then we’ll do a little cleanup and remove the empty directory as well as the zipped file we just downloaded:
1 2 3 | sudo tar xzf latest.tar.gz sudo mv wordpress/* . sudo rm -rf wordpress latest.tar.gz |
WordPress Installed
Next, we need to add a robots.txt file. This file informs web bots of what files and directores they are or are not allowed to crawl and index. A lot has changed since the early days of the web and it used to be good practice to restrict access to sensitive parts of a website in the robots.txt file. Nowadays though Google actually fully renders each page of our sites and if we block bots from crawling certain areas of our site files we run the risk of blocking access to important CSS or Javascript, giving the webcrawler a potentially bad experience and having a negative impact on our SEO. Because of this, I now keep a very lean and clean robots.txt file for my sites:
1 | sudo nano robots.txt |
Add the follow lines:
1 2 | # This space intentionally left (mostly) blank User-Agent: * |
Press Control-x to save file and exit the editor.
Setting the right permissions and ownership
Currently the WordPress files and folders we extracted belong to user ‘nobody’. We need to set the proper ownership and permissions in the html folder so that Nginx can access them and work its magic.
1 2 3 4 5 6 7 8 | # Set the correct ownership for all files sudo chown -R www-data: . # Change folder permissions sudo find . -type d -exec chmod 775 {} \; # Change file permissions sudo find . -type f -exec chmod 664 {} \; # No one should be able to access wp-config.php sudo chmod 660 wp-config.php |
Setting up a WordPress database
Now that we have WordPress’ file system squared away we need to create a database for it to use. Login to mariaDB with your root username and password:
1 | mysql -u root -p |
Create your database:
1 | CREATE DATABASE your_database_name; |
Now for security, we need to create a new user for the database so that we’re not using root with our WordPress application
1 2 3 4 5 | CREATE USER 'your_wordpressuser'@'localhost' IDENTIFIED BY ''; grant all privileges on wordpress_blog1.* to your_wordpressuser@localhost with grant option; grant all privileges on wordpress_blog2.* to your_wordpressuser@localhost with grant option; FLUSH TABLES; exit |
Now let’s test our connection
1 | mysql -u your_wordpressuser -p |
If you’re, great! If you were greeted with a login error check your credentials and try again…
I’m going to assume you made it. Let’s get outta here:
1 | exit |
Launch web browser and navigate to your Pis IP address. You should be greeted with a WordPress installation and configuration page!
Follow the instructions on the page to enter your database info (make sure you’re using the connection for your_wordpressuser we just created and not root) and in just a few clicks your WordPress site will be up and running. We’re not quite done just yet though.
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://[your pi’s IP address]/wp-admin. To see the website, go to http://[your pi’s IP address].
We have to tweak our Nginx configuration to handle permalink and URL rewrites. Luckily it’s not terribly difficult.
WordPress Permalinks on Nginx
WordPress works well with Apache web server straight out of the box when it comes to permalinks by utilizing mod_rewrites in the .htaccess file. However, since we’re running Nginx and it doesn’t use an .htaccess file we need to modify our site’s configuration file for Nginx:
1 | sudo nano /etc/nginx/sites-available/default |
Find the location / block, and comment out the try_files directive and add the following just below it:
1 | try_files $uri $uri/ /index.php?$args; |
What this does is tells Nginx to look for a file at the requested URL ($uri), if none exists check for a directory ($uri/). If neither exists then redirect to /index.php passing the query string arguments as parameters.
Save the file, then text Nginx to make sure everything is good and finally reload the configuration settings.
Test the settings:
1 | sudo nginx -t |
Reload Nginx:
1 | sudo service nginx reload |
WordPress is up and running and all pages should load correctly (no 404 or 502 errors when navigating from the home page). Next step is to point a domain to the Pi to make it visible to the world!