Build A Lamp Web Server With Wordpress

About the project

Learn to set up a LAMP (Linux, Apache, MySQL, PHP) stack on your Raspberry Pi

Project info

Difficulty: Easy

Platforms: Raspberry Pi

Estimated time: 4 hours

License: Creative Commons Attribution CC BY version 4.0 or later (CC BY 4+)

Story

Set up an Apache web server

Apache is a popular web server application you can install on the Raspberry Pi to allow it to serve web pages.

On its own, Apache can serve HTML files over HTTP. With additional modules it can serve dynamic web pages using scripting languages such as PHP.

Install Apache

  • Open a terminal window by selecting Accessories > Terminal from the menu.
  • Install the apache2 package by typing the following command into the terminal and pressing Enter

  1. sudo apt-get install apache2 -y

Test the web server

By default, Apache puts a test HTML file in the web folder that you will be able to view from your Pi or another computer on your network.

Open the Apache default web page on your Raspberry Pi:

  • Open Chromium by selecting Internet > Chromium Web Browser from the menu.

  • Enter the address http://localhost.

You should see this in your browser window:

This means you have Apache working!

You will also be able to open this web page from any other computer on your network using the IP address of your Raspberry Pi, e.g http://192.168.1.10.

To find out your Raspberry Pi’s IP address, type hostname -I into the terminal window. Your Raspberry Pi’s IP address is a really useful and will allow you to remotely access it.

Changing the default web page

This default web page is just a HTML file on the file system. It is located at /var/www/html/index.html.

  • Navigate to this directory in the terminal and have a look at what’s inside:
  1. cd /var/www/html
  2. ls -al

You should see this in the window:

  1. total 12
  2. drwxr-xr-x 2 root root 4096 Jan 8 01:29 .
  3. drwxr-xr-x 3 root root 4096 Jan 8 01:28 ..
  4. -rw-r--r-- 1 root root 177 Jan 8 01:29 index.html

This shows that there is one file in /var/www/html/ called index.html. . refers to the directory itself /var/www/html, and .. refers to the parent directory /var/www/.

What the columns mean

  1. The permissions of the file or directory
  2. The number of files in the directory (or 1 if it’s a file).
  3. The user that owns the file or directory
  4. The group that owns the file or directory
  5. The size of the file or directory
  6. The date and time of the last modification

As you can see, the html directory and index.html file are both owned by the root user, so you’ll need to use sudo to edit them.

You can edit this file using leafpad:

  1. sudo leafpad index.html

If you make a change to the file, save it, and refresh the browser, you will see your change appear.

Install PHP

PHP is a preprocessor: it’s code that runs when the server receives a request for a web page via a web browser. It works out what needs to be shown on the page, and then sends that page to the browser. Unlike static HTML, PHP can show different content under different circumstances. Other languages are also capable of doing this, but since WordPress is written in PHP, that’s what we need to use this time. PHP is a very popular language on the web: huge projects like Facebook and Wikipedia are written in PHP.

  • Install the PHP package with the following command:
  1. sudo apt-get install php -y

Test PHP

  • Create the file index.php:
  1. sudo leafpad index.php
  • Put some PHP content in it:
  1. <?php echo "hello world"; ?>
  • Save the file.
  • Delete index.html, because it takes precedence over index.php:
  1. sudo rm index.html

  Refresh your browser. You should see “hello world”. This page is not dynamic, but it is still served by PHP.

  1. sudo service apache2 restart
  • Edit index.php to include some dynamic content, for example:
  1. <?php echo date('Y-m-d H:i:s'); ?>
  • Or show your PHP info:
  1. <?php phpinfo(); ?>

Install MySQL

MySQL (pronounced My Sequel or My S-Q-L) is a popular database engine. Like PHP, it’s widely used on web servers, which is why projects like WordPress use it, and why those projects are so popular.

Install the MySQL Server and PHP-MySQL packages by entering the following command into the terminal window:

  1. sudo apt-get install mysql-server php-mysql -y

Now restart Apache:

  1. sudo service apache2 restart

Download WordPress

You can download WordPress from wordpress.org using the wget command. Helpfully, a copy of the latest version of WordPress is always available at wordpress.org/latest.tar.gz, so you can grab the latest version without having to look it up on the website. At the time of writing, this is version 4.5.

What is a .tar.gz file?

In case you’re wondering, .tar.gz stands for ‘gzip-compressed tar archive’. gzip is a tool for compressing files, which means reducing their size so they can be stored or distributed more easily. .tar stands for tarball, which is a computer file format that combines and compresses multiple files. Software is often available for download in .tar.gz format, because downloading a tarball is a lot faster than downloading the non-compressed files.

  • Change directory to /var/www/html/ and delete all the files in the folder.

  1. cd /var/www/html/
  2. sudo rm *
  • Download WordPress using get.
  1. sudo wget http://wordpress.org/latest.tar.gz
  •      Extract the WordPress tarball to get at the WordPress files.
  1. sudo tar xzf latest.tar.gz
  • Move the contents of the extracted wordpress directory to the current directory.
  1. sudo mv wordpress/* .
  • Tidy up by removing the tarball and the now empty wordpress directory.
  1. sudo rm -rf wordpress latest.tar.gz
  • Running the ls or tree -L 1 command now will show you the contents of a WordPress project:
  1. .
  2. ├── index.php
  3. ├── license.txt
  4. ├── readme.html
  5. ├── wp-activate.php
  6. ├── wp-admin
  7. ├── wp-blog-header.php
  8. ├── wp-comments-post.php
  9. ├── wp-config-sample.php
  10. ├── wp-content
  11. ├── wp-cron.php
  12. ├── wp-includes
  13. ├── wp-links-opml.php
  14. ├── wp-load.php
  15. ├── wp-login.php
  16. ├── wp-mail.php
  17. ├── wp-settings.php
  18. ├── wp-signup.php
  19. ├── wp-trackback.php
  20. └── xmlrpc.php
  21.  
  22. 3 directories, 16 files

This is the source of a default WordPress installation. The files you edit to customise your installation belong in the wp-content folder.

  • You should now change the ownership of all these files to the Apache user:
  1. sudo chown -R www-data: .

Set up your WordPress Database

Set up MySQL/MariaDB

To get your WordPress site set up, you need a database. This is where MySQL and MariaDB come in!

  • Run the MySQL secure installation command in the terminal window.
  1. sudo mysql_secure_installation
  • You will be asked Enter current password for root (enter for none): — press Enter.

  • Type in Y and press Enter to Set root password?.
  • Type in a password at the New password:  prompt, and press EnterImportant: remember this root password, as you will need it later to set up WordPress.

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

When complete, you will see the message All done! and Thanks for using MariaDB!.

Create the WordPress database

  • Run mysql in the terminal window:
  1. sudo mysql -uroot -p
  •   Enter the root password you created.

You will be greeted by the message Welcome to the MariaDB monitor.

  • Create the database for your WordPress installation at the MariaDB [(none)]> prompt using:
  1. create database wordpress;

Note the semi-colon ending the statement.

If this has been successful, you should see this:

  1. Query OK, 1 row affected (0.00 sec)

  • Now grant database privileges to the root user. Note: you will need to enter your own password after IDENTIFIED BY.
  1. GRANT ALL PRIVILEGES ON wordpress.* TO 'root'@'localhost' IDENTIFIED BY 'YOURPASSWORD';
  • For the changes to take effect, you will need to flush the database privileges:
  1. FLUSH PRIVILEGES;
  • Exit the MariaDB prompt with Ctrl + D.

WordPress configuration

  • Open the web browser on your Pi and goto http://localhost,  you should see a WordPress page asking to pick your language.

  • Select your language and click Continue.

You will be presented with the WordPress welcome screen.

  • Click the Let’s go! button.

  • Now fill out the basic site information as follows:
  1. Database Name: wordpress
  2. User Name: root
  3. Password: <YOUR PASSWORD>
  4. Database Host: localhost
  5. Table Prefix: wp_
  • Click  Submit to proceed.

  • Click the Run the install button.

Now you’re getting close!

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 have your site set up, you can see the website by visiting your http://localhost/wp-admin.

Log in to WordPress from another computer

To log in from another computer, open a browser and go to http://PI-IP-ADDRESS/wp-admin, using your Pi’s IP address.

You can find your Pi’s IP address using this command:

  1. hostname -I

    

Friendly permalinks

It’s recommended that you change your permalink settings to make your URLs more friendly.

To do this, log in to WordPress and go to the dashboard.

  • Go to Setting, then  Permalinks.
  • Select the  Post name option and click Save Changes.  

You’ll need to enable Apache’s rewrite mod:

  1. sudo a2enmod rewrite

You’ll also need to tell the virtual host serving the site to allow requests to be overwritten.

  • Edit the Apache configuration file for your virtual host:
  1. sudo leafpad /etc/apache2/sites-available/000-default.conf
  • Add the following lines after line 1.
  1. <Directory "/var/www/html">
  2. AllowOverride All
  3. </Directory>
  • Ensure it’s within the <VirtualHost *:80> like so:
  1. <VirtualHost *:80>
  2. <Directory "/var/www/html">
  3. AllowOverride All
  4. </Directory>
  5. ...
  • Save the file and exit.
  • Restart Apache.
  1. sudo service apache2 restart

Customisation

WordPress is very customisable. By clicking your site name in the WordPress banner at the top of the page (when logged you’re in), you’ll be taken to the Dashboard. From there, you can change the theme, add pages and posts, edit the menu, add plugins, and lots more. This is just a taster for getting something interesting set up on the Raspberry Pi’s web server.

What next?

  • Try adding pages and posts to your website.
  • Try installing different themes from the Appearance menu.
  • Try customising your website’s theme, or creating your own.
  • Try using your web server to display useful information for people on your network.

Credits

Photo of Raspberry Pi

Raspberry Pi

Our mission is to put the power of computing and digital making into the hands of people all over the world. We do this so that more people are able to harness the power of computing and digital technologies for work, to solve problems that matter to them, and to express themselves creatively.

   

Leave your feedback...