Raspberry Pi LAMP Server - Complete Guide

Posted
Comments 0

Today I’ll show you how to set up a LAMP (Linux, Apache, MySQL, PHP) server on your Raspberry Pi. You’ll be able to upload files using FTP, manage databases with phpMyAdmin, and I’ll show you how to optionally install WordPress, just for some fun.

Some more information about each package:

  • Linux – In this case we’re using Ubuntu 22.04, but other popular OSes for LAMP stacks are Debian, Fedora and CentOS
  • Apache – The most popular web server software in the world. Apache is the engine that serves pages to your web browser
  • MySQL – A popular database software distribution for storing almost any kind of data (i.e. WordPress data)
  • PHP – A programming language to create dynamic “on the fly” web pages from templates and databases

Contents

Prerequisites

Install Apache Web Server

Login to your Pi either directly or via SSH, then update your cache and upgrade any packages that need upgrading:

sudo apt update && sudo apt upgrade -y

It’s a good idea to reboot after upgrading any packages:

sudo reboot

Let’s install Apache web server:

sudo apt-get install apache2 -y

If you know your RPi’s IP address, enter it inside your web browser address bar. You should see the default “It Works!” Apache welcome page. If you don’t know your RPi’s IP address, use the ip address command to see it (it will be inside the eth0 interface section, see further below):

Default It Works Apache page

Use the ip address command to find the IP address of you RPi (It will be inside the eth0 section):

Use the ip address command to find your Pi's IP address

Enable mod_rewrite

Most content management systems (like WordPress) need the Apache module mod_rewrite enabled (for things like pretty URLs). Let’s edit the Apache config file:

sudo nano /etc/apache2/apache2.conf

Scroll down to the <Directory /var/www/> section and change AllowOverride None to AllowOverride All:

<Directory /var/www/>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
</Directory>

Then save ctrl + o, ENTER and exit nano ctrl + x.

Now let’s enable the module and restart Apache:

sudo a2enmod rewrite
sudo systemctl restart apache2

Install PHP

Let’s install the PHP programming language and the Apache PHP module:

sudo apt-get install php libapache2-mod-php -y

Let’s make sure PHP is working by creating a special file called info.php with some code inside to tell us what extensions are enabled by default in PHP:

sudo nano /var/www/html/info.php

Cope and paste the following code inside your new document:

<?php
        phpinfo();
?>

Then save ctrl + o, ENTER and exit nano ctrl + x.

Enter the IP address of your Pi into your web browser and append it with /info.php:

192.168.1.223/info.php

You should see the PHP information page that shows the details of your PHP installation. It also indicates that PHP is functioning:

PHP Info page

After you’ve finished with the info.php page, make sure to delete it so that nobody can see your servers sensitive information:

sudo rm -rf /var/www/html/info.php

Install MySQL Server

Let’s install the MySQL server database software package:

sudo apt-get install mariadb-server mariadb-client php-mysql -y

Now we run the mysql_secure_installation security script to harden MySQL:

sudo mysql_secure_installation

I will now suggest answers to each question the script will ask you:

  1. Enter current password for root (enter for none):ENTER
  2. Switch to unix_socket authentication [Y/n]N
  3. Change the root password? [Y/n]N
  4. Remove anonymous users? [Y/n]Y
  5. Disallow root login remotely? [Y/n]Y
  6. Remove test database and access to it? [Y/n]Y
  7. Reload privilege tables now? [Y/n]Y

Install phpMyAdmin

Let’s install the phpMyAdmin package so you can easily manage your MySQL databases:

sudo apt-get install phpmyadmin -y

phpMyAdmin will ask several questions during installation. Answer them as follows:

  1. Web server to reconfigure automatically:apache2 (use SPACE BAR to select, TAB, then ENTER)
  2. Configure database for phpmyadmin with dbconfig-common? – Yes (ENTER)
  3. MySQL application password for phpmyadmin: – Enter a strong password, then TAB and ENTER, then confirm password

Enter your Pi’s IP address into your web browser address bar and append it with phpmyadmin then ENTER. You should arrive at the phpMyAdmin login page. Enter username: phpmyadmin and the password that you used during the installation of phpMyAdmin:

192.168.1.223/phpmyadmin
phpMyAdmin login page Inside phpMyAdmin interface

Setup SFTP

By default, the /var/www/ folder is owned by www-data and part of the www-data group, and user pi doesn’t belong to that group, so we need to add pi to the www-data group, then change user/group ownership of the /var/www/html folder recursively:

sudo usermod -a -G www-data pi
sudo chown -R www-data:www-data /var/www

Then we change some default permissions:

sudo find /var/www/ -type d -exec chmod 775 {} \;
sudo find /var/www/ -type f -exec chmod 664 {} \;

Open your preferred FTP client (i.e. FileZilla) and enter the IP address of your Pi into the Host section, then username and password. You’ll get a warning about a security certificate, click Accept. Note that when you connect, you’ll be taken to user:pi home directory, so you’ll need to navigate to the /var/www/html/ directory (see image below using FileZilla).

Navigate to the /var/www/html folder

Install WordPress (Optional)

Before we install WordPress, let’s install some PHP extensions that WordPress requires:

sudo apt install php-curl php-gd php-xml php-xmlrpc php-mbstring php-soap php-intl php-zip -y

Let’s install WordPress. Download the latest package to the /tmp/ directory:

cd /tmp
curl -O https://wordpress.org/latest.tar.gz

Then extract the archive file:

tar xzvf latest.tar.gz

Create a blank .htaccess file inside the new WordPress directory:

touch /tmp/wordpress/.htaccess

Create an upgrade folder so WordPress doesn’t run into any permission errors later:

mkdir /tmp/wordpress/wp-content/upgrade

Create the configuration file by copying the sample config file:

cp /tmp/wordpress/wp-config-sample.php /tmp/wordpress/wp-config.php

Then copy the WordPress folder into your website root folder:

sudo cp -a /tmp/wordpress/. /var/www/html

Create a WordPress Database

Let’s login to MySQL (use your Pi password):

sudo mysql -uroot -p

Create a new user for our WordPress installation (replace MyPassword with a strong password):

CREATE USER 'wpuser'@'localhost' IDENTIFIED BY 'MyPassword';

Create a database for WordPress:

CREATE DATABASE wordpress DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;
GRANT ALL PRIVILEGES ON wordpress.* TO 'wpuser'@'localhost';
FLUSH PRIVILEGES; 
EXIT;

Remove the default index.html page from our Apache installation:

sudo rm -rf /var/www/html/index.html

Obtain new salt codes (copy/paste them into a temporary .txt file as you’ll need them later) OR you can visit the link in your web browser then copy/paste that way:

curl -s https://api.wordpress.org/secret-key/1.1/salt/

OR visit: api.wordpress.org/secret-key/1.1/salt/

Generate a unique set of Salt keys for WordPress

Open the WordPress configuration file and enter your database name, user and password:

sudo nano /var/www/html/wp-config.php
Enter database name, user and password

Using your arrow keys, scroll down to the “Authentication and salt keys” section, and copy and paste the generated values to the corresponding lines inside the ‘put your unique phrase here’ brackets (i.e. “AUTH_KEY” value to “AUTH_KEY” line). You can also just delete the entire section and copy/past the new codes in:

Auth Keys blank

It should look like this but with different codes:

Auth Keys filled

Then save ctrl + o, ENTER and exit nano ctrl + x.

Let’s change ownership

sudo chown -R www-data:www-data /var/www

Then we change some default permissions:

sudo find /var/www/ -type d -exec chmod 775 {} \;
sudo find /var/www/ -type f -exec chmod 664 {} \;

Open a browser tab and enter your Pi’s IP address. You should be served the WordPress website setup page. Enter a unique username and strong password:

WordPress setup page

You can now login to your new WordPress website!

WordPress login page

The WordPress dashboard:

WordPress dashboard

Conclusion

Well I hope everything went smoothly for you and you have a ripper little Raspberry Pi LAMP server! If you had any issues or just want to share your experience, hit me up in the comments or socials/email.

See Also

Further Reading

Author
Categories Raspberry Pi OS

Comments

There are currently no comments on this article.

Comment

Enter your comment below. Fields marked * are required. You must preview your comment before submitting it.