In Part 1, you learned how a Raspberry Pi can function as a web server, installed Apache, created your first website and explored the differences between static and dynamic websites. While serving HTML pages is an excellent starting point, many modern websites rely on databases, scripting languages and content management systems that generate pages dynamically. This allows websites to provide user accounts, comments, search functions, online stores and countless other interactive features.
A Raspberry Pi is more than capable of running these technologies for development, learning and even small production websites. By combining Apache, PHP and MariaDB, you create what is commonly known as a LAMP stack, one of the most widely used web hosting environments in the world. This section explains how these components work together, how to install them, and how to prepare your Raspberry Pi for hosting websites that can eventually be accessed from outside your home network.
Understanding Dynamic Websites
Unlike static websites that simply return HTML files stored on disk, dynamic websites generate pages every time a visitor requests them.
Suppose someone visits a blog homepage. Rather than reading a single HTML file, the web server may:
- Execute PHP code.
- Query a database.
- Retrieve recent articles.
- Check whether the visitor is logged in.
- Display personalised menus.
- Generate HTML before sending it back to the browser.
The visitor never sees this processing. From their perspective, the page loads normally, but behind the scenes multiple software components work together.
The typical flow looks like this:
Browser Request
│
Apache Web Server
│
PHP Interpreter
│
MariaDB Database
│
Website Files
│
Generated HTML
│
Browser
Understanding this workflow makes troubleshooting much easier later when individual components need maintenance.
Installing MariaDB
MariaDB is one of the most popular open-source relational database servers and is fully compatible with most software designed for MySQL.
Its job is to store structured information, including:
- User accounts
- Password hashes
- Blog posts
- Product catalogues
- Comments
- Configuration settings
- Website statistics
Install MariaDB using:
sudo apt install mariadb-server -y
After installation, start the service if necessary.
sudo systemctl enable mariadb
sudo systemctl start mariadb
You can verify the database server is running with:
sudo systemctl status mariadb
A healthy installation reports the service as active and running.
Securing MariaDB
Before creating databases, run the built-in security configuration utility.
sudo mysql_secure_installation
The script guides you through several important security settings.
Typical recommendations include:
- Set a strong administrator password.
- Remove anonymous users.
- Disable remote root logins.
- Remove test databases.
- Reload privilege tables.
These simple steps significantly improve the security of your server before it becomes accessible from other devices.
Creating Your First Database
Once MariaDB is running, connect to the database shell.
sudo mysql
Create a simple database.
CREATE DATABASE website;
Create a dedicated user.
CREATE USER 'websiteuser'@'localhost' IDENTIFIED BY 'StrongPassword';
Grant permissions.
GRANT ALL PRIVILEGES ON website.* TO 'websiteuser'@'localhost';
Apply the changes.
FLUSH PRIVILEGES;
Finally, exit.
EXIT;
Using dedicated database accounts rather than the administrator account is considered best practice because it limits the impact of configuration mistakes or compromised applications.
Installing Additional PHP Modules
Dynamic websites frequently require additional PHP extensions.
Install common modules using:
sudo apt install php-mysql php-gd php-curl php-mbstring php-xml php-zip -y
These provide functionality including:
- Database access
- Image processing
- File uploads
- Character encoding
- XML parsing
- Archive handling
Many web applications expect these modules to be available.
Restarting Apache
Whenever new PHP modules are installed, restart Apache so it loads the updated configuration.
sudo systemctl restart apache2
Restarting only takes a few seconds and ensures all newly installed extensions become available.
Hosting WordPress
WordPress is the world’s most widely used content management system and is an excellent way to learn dynamic website hosting.
It combines:
- PHP
- MariaDB
- HTML
- CSS
- JavaScript
into an easy-to-manage publishing platform.
Once installed, new pages and blog posts can be created through a browser without editing HTML files directly.
A Raspberry Pi 4 or Raspberry Pi 5 can comfortably host small WordPress websites with dozens or even hundreds of pages, particularly when traffic levels remain moderate.
WordPress Directory Structure
After installation, a typical WordPress site contains folders such as:
wp-admin
wp-content
wp-includes
Each has a specific purpose.
The wp-content directory contains:
- Themes
- Plugins
- Uploaded images
- User-generated media
This folder becomes the primary location for customisation.
Understanding Virtual Hosts
Apache can host multiple websites simultaneously using Virtual Hosts.
Instead of serving a single website, Apache examines the requested domain name and directs visitors to the correct website directory.
For example:
site1.local
site2.local
portfolio.local
Each website can have:
- Independent document roots
- Separate log files
- Different PHP versions
- Individual SSL certificates
- Unique configuration files
This feature allows one Raspberry Pi to host several websites without them interfering with each other.
Organising Website Files
As multiple websites are added, keeping files organised becomes increasingly important.
A common directory structure might be:
/var/www/
├── blog
├── company
├── portfolio
├── development
└── testing
Each website contains its own:
- HTML files
- PHP scripts
- Images
- CSS
- JavaScript
- Configuration files
Keeping websites separate simplifies backups and maintenance.
File Permissions
Linux permissions play an important role in website security.
Apache needs permission to read website files but should not necessarily have unrestricted write access.
Typical ownership may look like:
www-data
for web server processes.
Correct permissions reduce the risk of accidental file modification or malicious code execution.
Giving Your Raspberry Pi a Static IP Address
A web server should always have a predictable IP address.
If your router automatically assigns different addresses after each reboot, users may no longer be able to reach the server.
For example:
Instead of changing between:
192.168.1.32
192.168.1.74
192.168.1.91
configure the Raspberry Pi to always use:
192.168.1.50
This consistency makes router configuration, DNS records and remote access significantly easier.
Many home routers allow DHCP reservations based on the Raspberry Pi’s MAC address.
Finding Your Current IP Address
To display the current address:
hostname -I
Example:
192.168.1.50
Record this address before configuring router settings.
Domain Names
Although IP addresses work, most visitors expect memorable domain names.
Instead of:
http://192.168.1.50
users prefer:
mywebsite.example
A domain name acts as a friendly alias that ultimately resolves to the IP address of your Raspberry Pi.
When hosting only within your home network, local DNS solutions or hosts file entries can also provide friendly names.
Dynamic Public IP Addresses
Many residential broadband providers assign dynamic public IP addresses.
This means your external address may change periodically.
If someone bookmarks your website’s IP address, it could stop working after the provider assigns a new one.
Dynamic DNS services solve this by automatically updating DNS records whenever your public IP changes.
This allows visitors to continue reaching your Raspberry Pi even if the underlying IP address changes.
Understanding Port Forwarding
By default, your home router blocks incoming internet connections.
Port forwarding tells the router where to send website traffic.
For example:
Incoming HTTP traffic:
Internet
↓
Router
↓
Port 80
↓
Raspberry Pi
HTTPS works similarly using port 443.
Without port forwarding, external visitors cannot reach your Raspberry Pi even if Apache is running correctly.
Understanding NAT
Home routers use Network Address Translation (NAT).
Internally your Raspberry Pi might have:
192.168.1.50
Externally your broadband connection has one public IP shared by all devices.
NAT translates incoming traffic and forwards it to the correct internal device based on configured rules.
Understanding NAT helps explain why port forwarding is necessary for public web hosting.
Firewall Considerations
Linux firewalls help protect your server by restricting incoming connections.
Typical rules allow:
- HTTP
- HTTPS
- SSH
while blocking unnecessary ports.
Keeping only essential services accessible greatly reduces the server’s attack surface.
Using HTTPS
Modern websites should use encrypted connections whenever possible.
HTTPS encrypts communication between browsers and servers.
Benefits include:
- Improved privacy
- Better authentication
- Protection against interception
- Increased visitor confidence
Without HTTPS, usernames, passwords and other sensitive information could potentially be intercepted on insecure networks.
Even small personal websites benefit from encrypted connections.
Understanding SSL Certificates
SSL certificates verify the identity of your website.
When visitors connect securely, the certificate assures their browser that it has reached the intended server.
Browsers then establish encrypted communication before exchanging website data.
Certificates eventually expire and require renewal, making automation an important part of long-term server administration.
Testing from Another Device
After configuring your web server, always test from another computer or smartphone.
Check that:
- Pages load correctly.
- Images display.
- CSS styles apply.
- JavaScript functions normally.
- Forms submit successfully.
- PHP executes correctly.
- Database connections succeed.
Testing from multiple devices often reveals networking or browser-specific issues that are not obvious when testing locally.
Common Problems
New Raspberry Pi web administrators frequently encounter similar issues during setup.
Some common examples include:
403 Forbidden
Usually caused by incorrect file permissions or Apache configuration.
404 Not Found
The requested file does not exist or the document root is incorrect.
500 Internal Server Error
Typically indicates a PHP error or configuration problem.
Database Connection Failed
Usually caused by incorrect usernames, passwords or database permissions.
Apache Won’t Start
Often caused by configuration syntax errors or another application already using the required ports.
Understanding these common problems makes troubleshooting considerably less intimidating.
Best Practices for Internet Hosting
Before making your Raspberry Pi accessible from the internet:
- Keep Raspberry Pi OS fully updated.
- Use strong passwords.
- Disable unused services.
- Limit SSH access where possible.
- Use HTTPS instead of plain HTTP.
- Regularly back up your website and database.
- Monitor storage usage.
- Check server logs frequently.
- Keep WordPress themes and plugins updated if used.
Small security improvements applied consistently provide much greater protection than relying on a single defensive measure.
Conclusion
By combining Apache, PHP and MariaDB, your Raspberry Pi becomes capable of hosting fully dynamic websites ranging from simple blogs to sophisticated web applications. Features such as Virtual Hosts, static IP addresses and proper database management allow a single Raspberry Pi to support multiple independent websites while remaining organised and maintainable.
Preparing your server for internet access also introduces important networking concepts including port forwarding, NAT, HTTPS and firewall configuration. Although these technologies may initially seem complex, understanding how they interact is essential for anyone serious about self-hosting.
In Part 3, you’ll learn how to optimise performance, host multiple sites efficiently, use Docker containers, implement monitoring and backups, improve reliability, and troubleshoot real-world hosting problems commonly encountered on Raspberry Pi web servers.
