Saturday, 30 March 2019

How to Host a WordPress Site on Raspberry Pi

wordpress-host-raspberry-pi

If you want the most hassle-free WordPress experience, a managed WordPress host like WP Engine handles all the setup and support so you can focus on your content. It’s what we use to run our own sister sites.

But if you don’t have any funds, or just want to flex your DIY skills, then you can use a Raspberry Pi for your WordPress site.

Here’s how to set up Apache, MySQL, and PHP on your Raspberry Pi and host websites, both locally and on the web.

Why Use WordPress?

If you’re looking for a way to host a website with the minimum of fuss, WordPress isn’t the obvious solution. We’ve already looked at how to configure the Raspberry Pi to host a static or dynamic website (that is, one that relies on standard, prewritten pages, or a site that uses a database to populate pages).

But if you really need to use WordPress, or you’re developing a theme or plugins for a WordPress site, then having a Raspberry Pi with the blogging platform installed and ready to use is a great option.

Which Raspberry Pi Should You Use?

Raspberry Pi 3 and Raspberry Pi 3B+

To install WordPress on a Raspberry Pi, you’ll need to set it up as a LAMP server. With Linux, Apache, MySQL, and PHP installed, your Pi will then be able to handle WordPress (and other website software).

Several models of Raspberry Pi are available. You might have one, several, or none. But which one is the most suitable for running WordPress?

Happily, any version of the Raspberry Pi can be used to host a WordPress site. However, for the best results, we recommend you use a Raspberry Pi 2 or later. Additionally, ensure you’re using a larger SD card—at least 16GB—as storage space is a key requirement for web servers. (Also consider some external storage for the Pi!)

The rest of this tutorial assumes that your Raspberry Pi is powered on and connected to your local network. You should also have SSH configured, for remote command line access.

Step 1: Set Up Apache Web Server

Start off by installing the Apache web server. This is software that lets you serve any type of webpage to any browser. It’s all you need to serve a HTML page, static or dynamically created with PHP.

sudo apt install apache2 -y

Once installed, Apache will drop a test HTML file into the web folder on your Pi. You should test this from another computer (or smartphone) on your network. You’ll need to enter the IP address of the Pi in the address bar of your browser. If you’re using SSH, you’ll know this already; otherwise, enter:

hostname -I

This displays the IP address of your Raspberry Pi. The page you see should be something like this:

Apache server installed on Raspberry Pi

You can also check the web page works from your Raspberry Pi using the http://localhost address.

Step 2: Install PHP on Raspberry Pi

Next, it’s time to install PHP. This is a software pre-processor that enables the serving of server-generated web pages rather than static HTML pages. While an HTML page might be written in its entirety, a PHP page will feature calls to other pages, and the database, to populate it with content.

While other server-side platforms are available (such as ASP), PHP is vital here as it is required for WordPress because WordPress itself is written in PHP.

Install with:

sudo apt install php -y

Once this is done, you should test that PHP works. Change directory to /var/www/html/ like this:

cd /var/www/html/

Here, delete the index.html file (the web page you viewed earlier):

sudo rm index.html

Next, create a new file called index.php (nano is installed by default):

sudo nano index.php

Here, add any (or all) of the following code:

<?php echo "hello world"; ?>
<?php echo date('Y-m-d H:i:s'); ?>
<?php phpinfo(); ?>

In order, these commands display:

  • The phrase “hello world”
  • The current date and time
  • PHP info for the installation

Save the file, then restart Apache:

sudo service apache2 restart

Refresh the page to see the results.

PHP installed and working on Raspberry Pi

PHP and Apache are both working. Now it’s time to install the database software, MySQL.

Step 3: Install MySQL on Raspberry Pi

WordPress (and other dynamically generated website software) requires a database in order to store the content, links to images, and manage user access (among many other things). This project uses a fork of MySQL called MariaDB:

sudo apt install mysql-server php-mysql -y

Once this is installed, you will once again need to restart Apache:

sudo service apache2 restart

As noted, other database options are available for this type of project. However, for the best results, especially if this is your first time configuring a web server, stick with MySQL.

Step 4: Install WordPress on Raspberry Pi

To install WordPress, you’ll first need to install it. Before doing this, however, delete the contents of the /html/ directory:

cd /var/www/html/
sudo rm *

The asterisk wildcard (*) deletes everything in the directory thanks to the rm (remove) command.

Download WordPress with wget

Next, use wget to download the latest version of WordPress:

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

Once downloaded, extract the contents:

sudo tar xzf latest.tar.gz

The wordpress directory should be created, but you want the contents of it in html. While you might move these manually using the desktop user interface, it’s simple to do this from the command line:

sudo mv wordpress/* .

Be sure to include the space and period at the end, which refer to the current directory!

Enter ls to confirm the directory is full of WordPress folders and PHP files:

Move the contents of the WordPress directory

Before proceeding, tidy things up a bit, discarding the downloaded file and the wordpress directory:

sudo rm -rf wordpress latest.tar.gz

Next, set the Apache user as the owner of the directory:

sudo chown -R www-data: .

Step 5: Configure MySQL

To set up the database, you’ll first need to run the installation command:

sudo mysql_secure_installation

You’ll shortly be prompted to set a root password. Follow the onscreen instructions, making sure you keep a note of the password, as it will be required later.

Once this is one, the following prompts will appear:

  • Remove anonymous users
  • Disallow root login remotely
  • Remove test database and access to it
  • Reload privilege tables now

For each of these, tap Y to confirm. When done, the “All done!” message will be displayed.

Step 6: Create the WordPress Database

To install WordPress, you’ll first need to configure the database. Begin by running the mysql command, entering the password from earlier.

sudo mysql -uroot -p

This opens the MariaDB monitor. At the prompt, create the database:

create database wordpress;

Note how all commands within the MariaDB monitor end with a “;”. Next, the root user needs database privileges. Use your own password in place of PASSWORD.

GRANT ALL PRIVILEGES ON wordpress.* TO 'root'@'localhost' IDENTIFIED BY 'PASSWORD';

Follow this by flushing previous database privileges:

FLUSH PRIVILEGES;

Exit the MariaDB database management tool with Ctrl + D.

Step 7: Install and Configure WordPress

Installing WordPress is simple; if you’ve already done it on an existing website, you should know what to do.

Open the website in your browser (as described above). You should see the WordPress Setup screen. Select your language, then Continue, and make a note of what is required: the database name, username, password, host, and the table prefix (this is for the database tables).

If you made it this far, you should have named the database “wordpress”, and have a note of the password. The Username is root, and the host localhost. The Table Prefix is wp_.

Click Submit, then Run the install, and input the Site Title, along with the Username, and Password for your administrator account. Click Install WordPress, and wait as WordPress is (quickly) set up.

To login to your WordPress installation, go to http://localhost/wp-admin.

Installed WordPress site on a Raspberry Pi

At this stage, you have a site you can start using. Our guide to getting started with WordPress will help here. Don’t forget to choose a theme, and a few useful plugins. You’ll also need a method to access the site from the internet.

Access Your WordPress Site From the Internet

As things stand, you’ll probably only be able to access the site from within your home network. To change this, you’ll need a static IP address, and manage port forwarding from your router to your Raspberry Pi.

Static IP addresses are expensive, however, but you can use a dynamic DNS provider instead. This basically links a custom URL to your Raspberry Pi, although such services are often paid. Check our list of the best dynamic DNS providers for full details.

If you run into problems with blank white pages or a 500 internal server error in WordPress, take a look at our helpful guide.

Host a WordPress Site on Raspberry Pi: Success!

You’re all done now, and a WordPress site is running on your Raspberry Pi. As long as the computer remains powered up, the site will continue to be accessible. The process is straightforward too, requiring you to simply:

  • Install Apache, PHP, and MySQL
  • Download and install WordPress
  • Configure the MySQL database
  • Create your WordPress database
  • Configure WordPress
  • Launch the site and access it locally or via the web

If all of this was a bit overwhelming, we don’t blame you. That’s why we recommend using WP Engine to host WordPress sites with zero hassle. They manage all administrative issues so you can focus on your content.

Keep refining your WordPress knowledge with our guide to featured thumbnails and images sizes in WordPress.

Read the full article: How to Host a WordPress Site on Raspberry Pi



from MakeUseOf https://ift.tt/2N2NPnC
via IFTTT

No comments:

Post a Comment