
This simple tutorial will allow you to run your WordPress site on a Raspberry Pi.
WordPress is a very popular open-source blogging platform and content management system (CMS). It is easy to set up and has an active developer community building websites, creating themes, and plugins for others to use.
While it is easy to get a hosting package through a one-click WordPress setup, you can also simply set up your own hosting package via the command line on a Linux server, and the Raspberry Pi is a pretty good way to try it out while learning something along the way.
A commonly used web stack consists of four components: Linux, Apache, MySQL, and PHP. Here is what you need to know about each of them.
Linux
The operating system running on the Raspberry Pi is Raspbian, a well-optimized Linux distribution based on Debian for running on Raspberry Pi hardware. You have two options: the desktop version or the lite version. The desktop version has a familiar desktop environment and a lot of educational software and programming tools, such as the LibreOffice suite, Minecraft, and a web browser. The lite version does not have a desktop environment, so it only has the command line and some essential software.
This tutorial can be used on both versions, but if you are using the lite version, you will need another computer to access your site.
Apache
Apache is a popular web server application you can install on your Raspberry Pi to serve your web pages. By itself, Apache can serve static HTML files over HTTP. With additional modules, it can also serve dynamic web pages using scripting languages like PHP.
Installing Apache is very simple. Open a terminal window and enter the following command:
sudo apt install apache2 -y
Apache has a default test file placed in a web directory that you can access from your computer or other computers on your network. Just open a web browser and enter the address <http://localhost>
. Or (especially if you are using Raspbian Lite) replace localhost
with your Raspberry Pi’s IP address. You should see content like this in your browser window:
This means your Apache is up and running!
This default webpage is just a file in your filesystem. It is located at /var/www/html/index.html
. You can use the Leafpad[1] text editor to write some HTML to replace the content of this file.
cd /var/www/html/
sudo leafpad index.html
Save and close Leafpad, then refresh the webpage to see your changes.
MySQL
MySQL (pronounced “my S-Q-L” or “my sequel”) is a very popular database engine. Like PHP, it is widely used in web services, which is why projects like WordPress choose it and why these projects are so popular.
Enter the following command in a terminal window to install the MySQL service (LCTT translation note: actually installing the MySQL branch MariaDB):
sudo apt-get install mysql-server -y
WordPress uses MySQL to store posts, pages, user data, and many other content types.
PHP
PHP is a preprocessor: it runs code on the server in response to web browser requests. It solves the content that needs to be displayed on the webpage and then sends those pages to the browser. Unlike static HTML, PHP can display different content under different circumstances. PHP is a very popular language on the web; many projects like Facebook and Wikipedia are written in PHP.
Install PHP and the MySQL plugin:
sudo apt-get install php php-mysql -y
Delete index.html
and then create index.php
:
sudo rm index.html
sudo leafpad index.php
Add the following content:
<?php phpinfo(); ?>
Save, exit, refresh your webpage. You will see the PHP status page:
WordPress
You can use the wget
command to download WordPress from wordpress.org[2]. The latest WordPress can always be found at wordpress.org/latest.tar.gz[3], so you can fetch these files directly without checking the website, and the current version is 4.9.8.
Make sure you are in the /var/www/html
directory, then delete everything inside:
cd /var/www/html/
sudo rm *
Use wget
to download WordPress, extract its contents, and move the extracted WordPress directory’s contents to the html
directory:
sudo wget http://wordpress.org/latest.tar.gz
sudo tar xzf latest.tar.gz
sudo mv wordpress/* .
You can now delete the compressed file and the empty wordpress
directory:
sudo rm -rf wordpress latest.tar.gz
Run ls
or tree -L 1
to show the contents included in the WordPress project:
.
βββ index.php
βββ license.txt
βββ readme.html
βββ wp-activate.php
βββ wp-admin
βββ wp-blog-header.php
βββ wp-comments-post.php
βββ wp-config-sample.php
βββ wp-content
βββ wp-cron.php
βββ wp-includes
βββ wp-links-opml.php
βββ wp-load.php
βββ wp-login.php
βββ wp-mail.php
βββ wp-settings.php
βββ wp-signup.php
βββ wp-trackback.php
βββ xmlrpc.php
3 directories, 16 files
This is the default installation source for WordPress. In the wp-content
directory, you can edit your custom installation.
You should now change the ownership of all files to the Apache running user www-data
:
sudo chown -R www-data: .
WordPress Database
To set up your WordPress site, you need a database. Here, we are using MySQL.
Run the MySQL secure installation command in the terminal window:
sudo mysql_secure_installation
You will be asked a series of questions. There was no password set originally, but you should set one in the next step. Make sure you remember the password you enter, as you will need it to connect to your WordPress later. Press Enter to confirm all the following questions.
When it is finished, you will see the message “All done!” and “Thanks for using MariaDB!”.
Run the mysql
command in the terminal window:
sudo mysql -uroot -p
Enter the root password you created (LCTT translation note: this is not the Linux system root password, it is the MySQL root password). You will see the welcome message “Welcome to the MariaDB monitor.” At the prompt “MariaDB [(none)] >”, use the following command to create a database for your WordPress installation:
create database wordpress;
Note the semicolon at the end of the statement; if the command executes successfully, you will see the following prompt:
Query OK, 1 row affected (0.00 sec)
Grant database privileges to the root user by entering the password at the bottom of the statement:
GRANT ALL PRIVILEGES ON wordpress.* TO 'root'@'localhost' IDENTIFIED BY 'YOURPASSWORD';
To make the changes take effect, you need to flush the database privileges:
FLUSH PRIVILEGES;
Press Ctrl+D
to exit the MariaDB prompt and return to the Bash shell.
WordPress Configuration
On your Raspberry Pi, open a web browser and enter http://localhost
in the address bar. Choose a language you want to use in WordPress, then click “Continue”. You will see the WordPress welcome screen. Click the “Letβs go!” button.
Fill in the basic site information as follows:
Database Name: wordpress
User Name: root
Password: <YOUR PASSWORD>
Database Host: localhost
Table Prefix: wp_
Click “Submit” to continue, then click “Run the install”.
Fill in the following format: set a title for your site, create a username and password, and enter your email address. Click the “Install WordPress” button, and then log in using the account you just created. You are now logged in, and your site is set up. You can enter http://localhost/wp-admin
in the browser address bar to view your site.
Permalinks
It is a good idea to change your permalink settings to make your URLs more friendly.
To do this, first log in to your WordPress and go to the dashboard. Go to “Settings”, then “Permalinks”. Choose the “Post name” option, then click “Save Changes”. You also need to enable Apacheβs rewrite
module.
sudo a2enmod rewrite
You also need to tell the virtual hosting service to allow rewrite requests. Edit the Apache configuration file for your virtual host:
sudo leafpad /etc/apache2/sites-available/000-default.conf
Add the following content after the first line:
<Directory "/var/www/html">
AllowOverride All
</Directory>
Make sure there is content like this <VirtualHost *:80>
:
<VirtualHost *:80>
<Directory "/var/www/html">
AllowOverride All
</Directory>
...
Save this file, then exit, and restart Apache:
sudo systemctl restart apache2
Whatβs next?
WordPress is highly customizable. Click on your site name at the top banner of the site to enter the dashboard. Here you can modify themes, add pages and posts, edit menus, add plugins, and many other things.
Here are some interesting things you can try on your Raspberry Pi web service:
Donβt forget, the Raspberry Pi is a Linux computer. You can also use the same structure to install WordPress on servers running Debian or Ubuntu.
via: https://opensource.com/article/18/10/setting-wordpress-raspberry-pi
Author: Ben Nuttall[5] Edited by: lujun9972 Translator: dianbanjiu Proofread by: wxy
This article is originally compiled by LCTT and proudly presented by Linux China