Create a LAMP Server on Raspberry Pi
Table of Contents
Setting Up a LAMP Server on Raspberry Pi for Local Development
TL;DR: Turn your Raspberry Pi into a powerful local web development server using Linux, Apache, MySQL, and PHP (LAMP stack). Perfect for testing projects before deploying to production.
Have you ever wanted your own web server for testing projects, but didn’t want to pay for hosting or run a power-hungry computer 24/7? A Raspberry Pi makes the perfect low-cost, energy-efficient solution!
In this guide, I’ll walk you through transforming a basic Raspberry Pi into a complete LAMP development environment. I’ve used this setup for everything from testing WordPress themes to building custom web applications.
Let’s get started!
What You’ll Need
Before diving in, gather these essentials:
- Raspberry Pi (3B, 4, or newer recommended for better performance)
- MicroSD card (16GB+ recommended)
- Power supply matching your Pi model’s requirements
- Network connection (Ethernet preferred for stability, Wi-Fi works too)
- Basic terminal knowledge (we’ll use simple commands)
- Optional: keyboard, mouse, and monitor (only needed for non-headless setup)
Time required: About 1 hour, mostly waiting for installations
The Setup Process
Step 1: Prepare Your Raspberry Pi OS
Let’s start with a fresh Raspberry Pi OS installation:
- Download the Raspberry Pi Imager for your operating system
- Insert your microSD card into your computer
- Launch the Raspberry Pi Imager
- Choose Raspberry Pi OS Lite (64-bit) for a minimal server installation
- Select your microSD card as the destination
- Click the ⚙️ (gear icon) for advanced options:
- Set a meaningful hostname (e.g.,
lampserver
) - Enable SSH for remote access
- Configure your Wi-Fi details if needed
- Create your username and password
- Set a meaningful hostname (e.g.,
- Click Write and wait for the process to complete
- Insert the card into your Pi and power it on
💡 Pro Tip: Using Raspberry Pi OS Lite saves precious resources by excluding the desktop environment. Your server doesn’t need a GUI!
Step 2: Update Your System
First things first—let’s make sure everything’s up to date:
sudo apt update
sudo apt upgrade -y
This might take a few minutes, especially on a fresh installation.
Step 3: Install the Apache Web Server
Apache will handle serving your web pages:
sudo apt install apache2 -y
Once installed, Apache starts automatically. Let’s make sure it’s running correctly:
sudo systemctl status apache2
You should see active (running)
in the output.
To test your web server, open a browser on another device on your network and enter your Pi’s IP address. You’ll find your IP with:
hostname -I
If you see the Apache default page, you’re on the right track! 🎉
Step 4: Add MariaDB (MySQL) Database Server
Next up is our database system:
sudo apt install mariadb-server -y
After installation, secure it with:
sudo mysql_secure_installation
During this security setup:
- Press ENTER for the first question (no root password yet)
- Type Y to set a root password (choose something strong!)
- Answer Y to all remaining security questions
⚠️ Important: Remember your MariaDB root password! You’ll need it for database management.
Step 5: Install PHP
Now for the programming language that powers many websites:
sudo apt install php libapache2-mod-php php-mysql -y
After installation, restart Apache to integrate with PHP:
sudo systemctl restart apache2
Step 6: Test Your Complete LAMP Stack
Let’s verify everything works together. Create a PHP info page:
echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php
Now visit http://[YOUR-PI-IP-ADDRESS]/info.php
in your browser. You should see a detailed page about your PHP configuration.
Step 7: Create a User-Friendly Development Directory
For easier file management, let’s create a directory in your home folder:
mkdir ~/www
sudo ln -s ~/www /var/www/html/dev
sudo chown -R pi:www-data ~/www
sudo chmod -R 750 ~/www
Now you can place files in ~/www
and access them at http://[YOUR-PI-IP-ADDRESS]/dev/
💡 Developer Tip: This setup lets you edit files in your home directory without needing sudo permissions!
Securing Your Server
Even for local development, basic security is important:
1. Keep Your System Updated
Run this regularly:
sudo apt update && sudo apt upgrade -y
2. Hide Server Information
Edit Apache’s security configuration:
sudo nano /etc/apache2/conf-enabled/security.conf
Find and modify these lines:
ServerTokens Prod
ServerSignature Off
Save (Ctrl+O, Enter) and exit (Ctrl+X), then restart Apache:
sudo systemctl restart apache2
3. Clean Up Testing Files
Remove the PHP info file once testing is complete:
sudo rm /var/www/html/info.php
Adding phpMyAdmin (Optional)
Want a graphical interface for managing your databases? Install phpMyAdmin:
sudo apt install phpmyadmin -y
During installation:
- Select apache2 when prompted
- Choose Yes to configure the database
- Enter your MariaDB root password
- Create a strong phpMyAdmin application password
Access phpMyAdmin at http://[YOUR-PI-IP-ADDRESS]/phpmyadmin
Troubleshooting Guide
Running into issues? Here are quick fixes for common problems:
Apache Not Starting?
sudo systemctl status apache2
# If there are errors, check the logs:
sudo tail -n 50 /var/log/apache2/error.log
PHP Files Downloading Instead of Running?
sudo a2enmod php
sudo systemctl restart apache2
Permission Problems?
sudo chown -R www-data:www-data /var/www/html
sudo chmod -R 755 /var/www/html
Database Connection Issues?
sudo systemctl status mariadb
# If it's not running:
sudo systemctl start mariadb
sudo systemctl enable mariadb
What’s Next?
Now that you have your LAMP server running, here are some exciting next steps:
- Set up virtual hosts to run multiple websites
- Implement HTTPS with a self-signed certificate
- Configure automatic backups for your databases
- Install Git for version control of your projects
- Try installing WordPress, Drupal, or other CMS systems
I’ve been using this exact setup for over a year now, and it’s been perfect for testing client projects before deploying them to production servers.
Have you set up a Raspberry Pi LAMP server? What are you building with it? I’d love to hear about your projects in the comments!
Happy coding!
Last updated: March 2025