Tutorial – Install WordPress on a Raspberry Pi using Nginx

This is the final part in a 3 part series which has seen us install Nginx and PHP on Raspbian, install and configure MySQL Server, and finally installing WordPress. Let’s get started.

Article Update 20 June 2012 20:55: Please note, since publishing this article, an additional instruction has been added. If you’ve previously used this tutorial, please make sure you install php-apc.

Prerequisites

As with the previous tutorials, I am assuming that you are familiar with and comfortable using the command line interface. I’m also assuming that you’ve followed the previous two tutorials; Install Nginx and PHP on Raspberry Pi and Install MySQL server on Raspbian.

Step 1 – Download WordPress

We’ll switch to using heightened permissions for all of the next. To do this, type:

sudo -i

Navigate to your document root. By default, this is located at /usr/share/nginx/www/:

cd /usr/share/nginx/www/

Remove the default nginx files so that the directory is clean:

rm 50x.html index.html

Download WordPress:

wget http://wordpress.org/latest.tar.gz

Extract WordPress and remove the downloaded file:

tar xzvf latest.tar.gz && rm latest.tar.gz

All the files will extract to a directory called ‘wordpress’ in our document root. We don’t want our users to visit http://mydomain.com/wordpress to be able to see the WordPress website, so we need to move all the files to the document root and remove the ‘wordpress’ directory. To do this, we begin by changing to the ‘wordpress’ directory:

cd wordpress

Let’s move everything to where it needs to be, in the document root:

mv ../ *

So, what does this mean? The first part ‘mv’ is the move command. The next part ‘../’ means move to the parent directory, and finally the asterisk ‘*’ means ‘everything in this directory’. So we’re saying ‘move to the parent directory, everything in this one’. We finish by changing to the document root directory and remove the ‘wordpress’ directory:

cd ../ && rm -rf wordpress

Step 2 – Add some extra packages

We’re almost there. Now, we need to add a couple more packages that WordPress relies on. We do our usual updating and upgrading:

sudo apt-get update && sudo apt-get upgrade

Install any updates which need installing, and finally install Curl, GD image library and SSH2 library:

sudo apt-get install php5-curl php5-gd libssh2-php

To improve the performance of PHP5-FPM, we need to install a PHP cacher:

sudo apt-get install php-apc

Step 3 – Configure WordPress

To start using WordPress, we need to make some changes to the configuration file. To start, create a copy of the sample WordPress configuration file:

cp wp-config-sample.php wp-config.php

Next, edit the file and provide our local database connection details. Start by typing:

nano wp-config.php

Scroll down until you see the database credentials part of the configuration file. You shouldn’t have to scroll too far. Enter the database name, username and password we created in the previous tutorial. Your configuration file should look a little something like this by now:

Install WordPress on Raspberry Pi

Save and exit by pressing CTRL + X on your keyboard. Finally, we navigate to the URL or IP address of the Raspberry Pi. If all goes well, we should see the WordPress setup page:

WordPress setup

Fill in your details, and you’re done with the setup. Once you’ve completed the ‘Information needed’, we will see the success page. Congratulations! You’ve installed WordPress on your Raspberry Pi!

Optional but recommended – Some other configurations

These are additional configuration changes that aren’t essential, but will improve your experience of running WordPress from Nginx on a Raspberry Pi.

Enable pretty permalinks in Nginx

One question I’ve been asked numerous times is “how do I get pretty permalinks to work with WordPress and Nginx?”. This is incredibly straightforward, it requires one tweak of the configuration file. To begin type the following command:

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

Nano editor will launch with the configuration file that we’ve already adjusted in the first tutorial in this three part series. Scroll to the line that reads try_files $uri $uri/ /index.html; as shown in this screenshot:

Configure pretty permalinks in Nginx

Simply edit this line to read the following:

try_files $uri $uri/ /index.php;

This will ensure all requests are sent to index.php (and therefore WordPress) for handling. Just remember to enable pretty permalinks from your WordPress Admin panel.

Set some permissions

These permissions will enable you to upload image files, install plugins and updates. To begin, we need to change the owner of your www directory to www-data as this is the user that Nginx runs as. Assuming you’re using the default root path, type the following:

sudo chown -R www-data:www-data /usr/share/nginx/www

Now we need to set the permissions on the wp-config.php file to make it nice and secure. Be aware, some plugins such as cache plugins require making changes to your wp-config.php file. I would always recommend making these changes yourself.

sudo chmod 644 /usr/share/nginx/www/wp-config.php

Next, let’s change the permissions of your wp-content directory to allow file uploads and plugin installation:

sudo chmod -R 755 /usr/share/nginx/www/wp-content/

Finally, to allow plugins to be installed without the need to install a FTP server, open your config file:

sudo nano /usr/share/nginx/www/wp-config.php

Scroll to the line below define(‘DB_COLLATE’, ”); and add the following line to your configuration:

define('FS_METHOD', 'direct');

Your configuration might look a little something like this now:

Install plugins without FTP wordpress

Save your config file and you’re ready to rock.

Conclusion

So, in the first tutorial we installed and made some basic configurations for Nginx and PHP. In the second tutorial we installed and configured MySQL database server. Then I baked a Pi,  and finally we’ve installed and configured WordPress. That concludes the final part in this tutorial series. Let me know your thoughts, share them below. Thanks for reading, stick the kettle on, mines a tea with no sugar.

Ste

Web Developer living in Manchester, working for Studio Skylab (http://www.studioskylab.com). Views and thoughts are my own.

You may also like...

33 Responses

  1. alongtheway says:

    It worked! Fantastic! I did change

    mv ../ *

    to

    mv * ..

    But everything else seemed to go without a hitch. And thank you for the tips and options at the end! I am having an unreasonable amount of fun doing this. 🙂

  2. alongtheway says:

    So…I know that there are about a million options for getting a domain name, but I am always in the market for recommendations if you have any.

  3. Neil says:

    All worked according to instructions and note from “alongtheway”. Thanks.
    Im having a problem now when, in WP, I want to view my page or site, it always comes back with the PHP config test page from tutorial 1. I suspect this has something to do with the default.php file we created then. Appreciate some assistance please.
    Also, now I have registered a domain name, will it work on my pi or do I have to officially host it first.
    Thank you again.
    Regards
    Neil

  4. Could you guide us through the process of linking the newly bought domain to the raspberry?Bought one from namecheap but i am totally clueless on what to do next!

  5. aran says:

    root@raspberrypi:/usr/share/nginx/www# cp wp-config-sample.php wp-config.php
    cp: cannot stat `wp-config-sample.php’: No such file or directory

    It seems that I cant find the wp-config file… I

    Any help is highly appreciated

  6. michaeldreksler says:

    Great series of tutorials.
    When I got to the starting WordPress stage I got the dreaded “Your PHP installation appears to be missing the MySQL extension which is required.”

    The internet suggested I edit the php.ini to include the line extension=mysql.so, which didn’t make any difference.

    I then looked to see if there was a mysql.so anywhere, and I could not find one other than one under perl5(!).

    I then ran sudo apt-get install php5-mysql and everything then started working.

  7. Alex Zerbach says:

    Hi Stewart,

    I followed your tutorial yesterday and ran into an issue on Step 3 of this tut after configuring the wp-config file. I used the same convention for the DB, User, and of course a custom password. I actually did some reading and made my password more simple for SQL. I tweaked stuff on my own and actually had to reflash my SD card.

    At any rate, I am getting a 403 Forbidden Error after putting in my info on the last step of Step 3, any insight?

  8. Alex Zerbach says:

    Apologies I don’t know where I got the name Stewart from.

  9. Thank you so much for these massively insightful tutorials! We hit one snag but were put right by the helpful comment above by alongtheway, now we’re having problems with pretty permalinks (they’re returning 500 errors, I suspect it’s something to do with overwrites not happening properly but I’m not sure- any advice anyone?)

    Anyway, thank you again!

  10. Darryl Gibbs says:

    Thanks for your great tutorial!! But, when I’m logged into the WP dashboard, ANY and EVERY link/option I click on for settings or posts etc just goes to a blank webpage. Any ideas? Please?

  11. Lukas B says:

    Hi. Thank you very much for the tutorial! I still have a problem.

    After the configuration of wp-config.php, I get the following error message in the webbrowser:

    “Your PHP installation appears to be missing the MySQL extension which is required by WordPress.”

    instead of the WordPress Welcome Page

    Someone has an idea what is wrong? The MySQL server login as a test of the wpuser worked.

  12. thisisole73 says:

    mv ../ * is wrong… but really nice tutorial

  13. Sourc7 says:

    Thank you very much for this.

  14. a says:

    Thanks a lot! I just got a Raspberry Pi, and this was a fun first project 😀

  15. gdelory says:

    Thanks for this series of tuto, it saved me a lot of time and explanations are great!

  16. Arnold says:

    Last time I installed PHPMyadmin and stuff it all was really slow.
    But with the latest image of Raspbian today, everything runs really quite smooth!

    Just sharing that, if someone has issue regarding speed.

    My setup is:
    Raspberry Pi B
    32GB Class 10
    Cable network
    Jessie apt servers

  17. manictechguy says:

    After trying and failing several times to setup a wordpress on my model B Pi, i decided to follow your tutorial (and decided to follow it to the letter)…
    And boy do i wish i’d done so from the getgo, could have saved myself a lot of trouble :-/
    No more errors, no more nonsense, and it all just works now.
    You, Sir, are my new hero 🙂

  18. Martin says:

    Great set of tutorials, I learned quite a bit. There was just enough information to follow along, but still required a fair amount of searching to fully understand the commands and procedures. Very few issues arose during the process. A few notes to anyone else planning on doing this tutorial “not bragging” but I was able to perform the entire 3 part tutorial over SSH, on my Nexus 3, all command line, on a train, in less than 1 week; I hope others take a look. Fun Stuff!!!

  19. Trevor says:

    I was able to setup my server using your tutorials and everything went great! Now as I did further I have found out that the SD card will fail much sooner than normal due to the constant read/writes that the sever does. i’m still working out how to fix it by moving the files to a USB connected HD. In http://www.raspipress.com/2013/05/install-and-run-raspbian-from-a-usb-flash-drive/ you set this up from the get-go, but I wonder if I can change things now? Say, move the /usr/share/nginx/www/ folder to the HD and mount it? Should I also be looking at moving some mySQL folders as well? Sould I just say to heck with it and follow that tutoral to run raspbian from usb?

  20. Peter says:

    Have been working on the same thing recently. Loved making it work but was not impressed with the result. It turned out to be pretty slow. I site with several plugins enables would load a page in 8 seconds plus.
    What is your experience with this subject? Did you solve the speed issue?

    • manictechguy says:

      My experience is that it’s about 2-5 seconds page load on a Raspberry Pi 2, might be worth it to upgrade if you haven’t already.
      It’s also worth it to check around the web for optimisation suggestions for MySQL, Nginx and WordPress, and you may want to check out some of the caching plugins on the WordPress repository, they can really help, if configured the right way.

  21. Bob R says:

    Thank you Thank you for taking the time to do this. You really have a talent for writing tutorials. Everything is easy to understand and follow. Most importantly, it actually works!! Very different from most other tutorials I’ve tried so far.

    I can’t believe I’m finally editing my own wordpress site online. THANKS!

    Please make a tutorial on adding a surveillance cam with web access and you’ll be my all time hero.

  22. Paddy says:

    I used your guide thx. But with this guide for example wp-strava (access via api) or youtube channel viewer plugin doesn’t work – I think its a result of rewrite?

  23. danilofukuoka says:

    Thanks very much! Very well done!

  24. George says:

    I’ve got two little issues with the install. 1. after install and reboot, the site won’t load it’s CSS. 2. I can’t get into wp-admin because of some weird portforwarding it’s doing.
    I think I can fix both, if I could just get into the database, but I can’t seem to find any way to get into the mysql db. Can you help me?

  25. escapologybb says:

    Apart from the mv * ../ command, this has been a brilliant series. Thought you should know, kudos.

  26. Don’t forget, from your first command onward you’re now at root and never actually exit it, so there’s actually no need to keep typing sudo throughout step two at all (or ever, if you don’t reboot and like living dangerously)!
    btw, take another look at a more contemporary Raspian install – I found my nginx serving http root directory was at /var/www/html as it is these days in a normal apache2 install in normal ubuntu etc.

Leave a Reply