All articles
2 min read

Setting Up LAMP on Raspberry Pi Ubuntu: Apache, MySQL (MariaDB), and PHP

Install a full LAMP stack (Linux, Apache, MariaDB, PHP) on a Raspberry Pi running Ubuntu, then deploy WordPress — step by step from package installation to a working site.

LAMP stands for Linux, Apache, MariaDB, and PHP — the classic web server stack. This guide sets it up on a Raspberry Pi running Ubuntu Server, then deploys WordPress on top of it.

Step 1: Update the System

sudo apt update && sudo apt upgrade -y

Step 2: Install Apache

sudo apt install apache2 -y

Test it by visiting http://<your-pi-ip> from another device on your network. You should see the Apache default page.

Find your Pi's IP address:

hostname -I

The default web root is /var/www/html/. The web server runs as the www-data user.

Step 3: Install PHP

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

Test that PHP is working by creating a test file:

echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php

Visit http://<your-pi-ip>/info.php — you should see the PHP info page. Delete it when done:

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

Restart Apache if PHP isn't executing:

sudo systemctl restart apache2

Step 4: Install MariaDB

sudo apt install mariadb-server -y

Run the secure installation to set a root password and remove test databases:

sudo mysql_secure_installation

When prompted:

  • Set a root password (or press Enter to keep the current one if empty)
  • Remove anonymous users: Yes
  • Disallow root login remotely: Yes
  • Remove test database: Yes
  • Reload privilege tables: Yes

Step 5: Create a WordPress Database

Log in to MariaDB:

sudo mysql -u root -p

Create a dedicated database user — never grant WordPress access to the root account:

CREATE DATABASE wordpress;
CREATE USER 'wpuser'@'localhost' IDENTIFIED BY 'strong_password_here';
GRANT ALL PRIVILEGES ON wordpress.* TO 'wpuser'@'localhost';
FLUSH PRIVILEGES;
EXIT;

Replace strong_password_here with an actual secure password.

Step 6: Download and Install WordPress

cd /var/www/html
sudo wget https://wordpress.org/latest.tar.gz
sudo tar xzf latest.tar.gz
sudo mv wordpress/* .
sudo rm -rf wordpress latest.tar.gz index.html
sudo chown -R www-data:www-data /var/www/html

Step 7: Configure WordPress

Copy the sample config:

sudo cp wp-config-sample.php wp-config.php
sudo nano wp-config.php

Update these values with your database credentials:

define( 'DB_NAME', 'wordpress' );
define( 'DB_USER', 'wpuser' );
define( 'DB_PASSWORD', 'strong_password_here' );
define( 'DB_HOST', 'localhost' );

Also replace the authentication keys and salts. Generate fresh ones at https://api.wordpress.org/secret-key/1.1/salt/.

Step 8: Enable Apache Rewrites (for Permalinks)

sudo a2enmod rewrite
sudo systemctl restart apache2

Edit the default site config to allow .htaccess overrides:

sudo nano /etc/apache2/sites-available/000-default.conf

Inside the <VirtualHost> block, add:

<Directory /var/www/html>
    AllowOverride All
</Directory>

Restart Apache:

sudo systemctl restart apache2

Step 9: Complete WordPress Setup

Visit http://<your-pi-ip> in your browser. The WordPress installation wizard will walk you through:

  1. Site title and admin account creation
  2. Database connection confirmation (already configured)

After completing the wizard, your WordPress site is live.

Conclusion

Your Raspberry Pi now runs a full LAMP stack with WordPress. For a production setup accessible from the internet, add HTTPS via Let's Encrypt (sudo apt install certbot python3-certbot-apache) and configure a domain name pointing to your Pi's public IP.