Raspberry Pi: Installing a LAMP Server with WordPress

Wednesday 19th July 2017 3:02pm

If you are looking for a way to make a cheap web server, primarily to be used as a testing environment or to store files, then the Raspberry Pi is perfect for you. What is a Raspberry Pi? It is an inexpensive mini computer, perfect for basic applications. This guide will show you how to take a new Raspberry Pi and turn it in to a web server. To do this we will be installing the following software known collectively as LAMP:

  • Linux (operating system)
  • Apache (web server)
  • MySQL (database)
  • PHP (scripting language)

We will also be installing WordPress a web site content management system; used to create web pages and upload media content.

(Please note that for this article, we will be working in Windows)

Download and Edit the Raspberry Pi Operating System

Below are the steps necessary to install the Raspbian operating system. For a more detailed tutorial click Installing an Operating System (Raspbian).

  1. Obtain a copy of the Raspberry Pi OS (Operating System). There are a few different distributions available, but for this article, we will be using the “Raspbian Jessie Lite” version. After the download completes extract the contents of the zip file by right-clicking the zip file and choosing ‘Extract All’
  2. Write the image to the SD card. To do this, I’m using a tool called the Win32 Disc Imager. A link for this is also available in the sources section. Now open the tool, choose the drive letter representing your SD card. Next select the location of the Raspberry Pi OS image and select ‘Write’.
  3. Enable SSH on the SD card. Using File Explorer open the boot sector on the SD card and create a new file with the name ‘ssh‘ without any file extension (i.e. without .txt). To do this click onto ‘Organize’ and select ‘Folder and Search Options’. Click onto the ‘View’ tab and untick the option ‘Hide extensions for known file types’ and click onto ‘OK’. Right click a blank area of window and choose New>Text Document
  4. Place the SD card into your Raspberry Pi. Remove the SD card from the Windows PC and plug it into the Raspberry Pi. Plug in the network cable and lastly plug in the power adaptor.
  5. Once the operating system finishes loading, you will need to log in. The default username is “pi”, and the default password is “raspberry”.
  6. Start by changing the password. From the command line type:
    sudo passwd pi
    

Enter your new password and then confirm it. Please note, the cursor will not move when typing passwords nervertheless you are entering text.

Run Software Updates

  1. Because you are running a fresh version of Debian, you will need to do some housecleaning, updating, and installing. First, we are going to update the clock, update our sources, then upgrade any pre-installed packages. Type the following at the command line (press return/enter after each line):
    sudo dpkg-reconfigure tzdata
    sudo apt-get update
    sudo apt-get upgrade
    
  2. Set the date and time. From the command line type (replace parts as necessary):
    sudo date –-set="30 December 2013 10:00:00"
    

Keep the Firmware Up To Date

  1. Next, we want to install Hexxeh’s RPI update tool to help keep Raspberry Pi up to date. To do this, run the following commands (press return/enter after each line):
    sudo apt-get install ca-certificates
    sudo apt-get install git-core -y
    sudo wget "http://raw.github.com/Hexxeh/rpi-update/master/rpi-update" -O /usr/bin/rpi-update && sudo chmod +x /usr/bin/rpi-update
    sudo rpi-update
    sudo shutdown –r now
    

Installing the Web Server

  1. To install Apache and PHP, execute the following commands:
    sudo apt-get install apache2 php5 libapache2-mod-php5 php5-gd -y
    
  2. Now restart the service:
    sudo service apache2 restart
    

    Or

    sudo /etc/init.d/apache2 restart
    
  3. Enter the I.P. address of your Raspberry Pi into your web browser. You should see a simple page that says “It Works!”

Install MySQL

  1. To install MySQL, install a few packages with the following command:
    sudo apt-get install mysql-server mysql-client php5-mysql -y
    

Install FTP

  1. We will now install FTP to allow transferring files to and from your Raspberry Pi.
  2. Take ownership of the web root:
    sudo chown pi –R /var/www
    
  3. Next, install vsftpd:
    sudo apt-get install vsftpd -y
    
  4. Edit your vsftpd.conf file:
    sudo nano /etc/vsftpd.conf
    
  5. Make the following changes:
    • anonymous_enable=YES to anonymous_enable=NO
    • Uncomment local_enable=YES and write_enable=YES by deleting the # symbol in front of each line
    • then go to the bottom of the file and add force_dot_files=YES
  6. Now save and exit the file by pressing CTRL-O, CTRL-X.
  7. Now restart vsftpd:
    sudo service vsftpd restart
    
  8. Create a shortcut from the Pi user’s home folder to /var/www:
    ln –s /var/www/ ~/www
    
  9. You can now FTP using the Pi user and access the /var/www folder via a shortcut that should appear on login.

Install WordPress

  1. Navigate to /var/www/html/ and Download WordPress:
    cd /var/www/html/
    sudo rm *
    sudo wget http://wordpress.org/latest.tar.gz
    
  2. Extract WordPress and Delete Tarball:
    sudo tar xzf latest.tar.gz
    sudo mv wordpress/* .
    sudo rmdir wordpress
    sudo rm latest.tar.gz
    
  3. Change ownership of WordPress files to the Apache user:
    sudo chown –R www-data: .
    

Set up WordPress

  1. Setup WordPress database. Run the mysql command in the terminal and provide your login credentials (e.g. username root, password password):
    sudo mysql –uroot -ppassword
    

    Notice that there is no space between –u and username or –p and password

  2. Once connected to MySQL create the database that WordPress installation will use:
    mysql> create database wordpress;
    

Configure WordPress

  1. Discover Home Name. Find out your Raspberry Pi’s IP address to acces it in the browser, so in a terminal type:
    hostname -I
    
  2. Connect to WordPress Welcome Page. Navigate to http://YOUR-IP-ADDRESS e.g. http://192.168.1.5 in your web browser and you should see the WordPress welcome page.
  3. Let’s Go! Click onto the Let’s Go! Button and complete the basic site information:
    Database Name:    wordpress
    User Name:        root
    Password:         YOUR_PASSWORD
    Database Host:    localhost
    Table Presix:     wp_
    

    Click onto Submit to proceed.

  4. Run Install: Click onto the Run the install button. Complete the information: give your site a title, create a username and password and enter you email address. Hit the Install WordPress button, then log in using the account you just created. Open a web browser and navigate to the following web page http://YOUR-IP-ADDRESS/wp-admin.
  5. Friendly Permalinks. It is recommended that you change your permalink settings to make your URL’s more friendly. To do this log into WordPress and go to the Dashboard. Go to Settings>Permalinks. Select the Post name option and click Save Changes. Next we need to enable Apache’s rewrite mod:
    sudo a2enmod rewrite
    
  6. We 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:
    sudo nano /etc/apache2/sites-available/000-default.conf
    
  7. Add the following lines after line 1:
    <Directory "/var/www/html>
      AllowOverride All
    </Directory>
    
  8. Ensuring it’s within the <VirtualHost *:80> like so:
    <VirtualHost *:80>
      <Directory "/var/www/html">
        AllowOverride All
      </Directory>
    ...
    
  9. Next restart Apache:
    sudo service apache2 restart
    

Set up PHP Graphics Library

  1. Setup PHP Graphics. Run the mySQL command in the terminal and provide your login credentials (e.g. username root, password password):
    sudo apt-get install php5-gd
    
  2. Restart Apache Web Server:
    sudo /etc/init.d/apache2 restart
    
  3. Testing PHP. RCreate a new file called test.php and type the following:
    <?php
      Phpinfo();
    ?>
    

    Navigate using a web browser to the following url:

    http://<YOUR-IP-ADDRESS>/test.php
    

Additional Notes

  1. Setup PHP-MyAdmin. Run the following to install PHP-MyAdmin:
    sudo apt-get install phpmyadmin
    
  2. If thumbnails don’t crop. Install imagemagick:
    sudo apt-get install imagemagick
    
  3. Reset IP Address in WordPress. Log into MySQL (with no spaces after –u and –p):
    mysql –<YOUR_USERNAME> -p<YOUR_PASSWORD>
    use wordpress;
    UPDATE wp_options SET option_value = 'http://<YOUR_NEW_IP_ADDRESS>' WHERE option_value = 'http://<YOUR_OLD_IP_ADDRESS>';
    exit
    
  4. If your receive ERROR 1045 (28000): Access denied for user ‘root@localhost’:
    sudo /etc/init.d/mysql stop
    sudo mysqld_safe –skip-grant-tables &
    mysql -uroot
    use mysql;
    UPDATE user SET password='<YOUR_PASSWORD>' WHERE User='root';
    flush privileges;
    quit
    sudo /etc/init.d/mysql stop
    sudo /etc/init.d/mysql start
    
  5. Reset WordPress URL. Add the following lines by editing the functions.php file near the top after the line <?php. After you have managed to get log back into WordPress remove the two lines from the functions.php file:
    update_option('siteurl', 'http://bulis.co.uk');
    update_option('home', 'http://bulis.co.uk');
    
  6. #2002 Cannot log into the MySQL server. This was caused by hackers in Iceland (Hafnarbraut 29, Hólmavík, Iceland) using a Brute Force attack. Fixed by blacklisting the offending IP address between 185.188.204.1 and 185.188.204.22. It turns out that my site was being subjected to a brute force attack that targeted the WordPress xmlrpc.php file. In order to check for this, you should view your apache2 access logs and look for suspicious POST requests. In my case, I found a huge number of POST requests to the /xmlrpc.php file coming from the same range IP addresses (about 23 requests per second):
    sudo /etc/init.d mysql stop
    cd /var/log/apache2/
    cat access.log
    sudo iptables –A INPUT –s 185.188.204.1 –j DROP
    sudo iptables –L INPUT –v –n
    sudo /etc/init.d mysql start
    
FB Like & Share