Setting Up a Raspberry Pi Server with Docker and Apache
Welcome to my step-by-step guide on transforming your Raspberry Pi 4 into a powerful and efficient server! Whether you're a beginner or an experienced developer, this guide will walk you through the entire process using Docker and Apache. Let's dive in and get your server up and running!
Prerequisites
Before we begin, make sure you have a Raspberry Pi 4 with internet access and SSH enabled. If you're not sure how to set this up, you can follow the official Raspberry Pi SSH Guide.
Step 1: Updating Your Raspberry Pi
Start by updating your Raspberry Pi to ensure it's running the latest software:
sudo apt-get update
sudo apt-get upgrade
Regular updates are essential for system security and stability. Never skip this step, especially when setting up a server.
Step 2: Installing Docker
Docker simplifies container management, making it a perfect tool for your Raspberry Pi server. Install it with the following commands:
curl -sSL https://get.docker.com | sh
sudo usermod -aG docker pi
sudo reboot
After the reboot, you should be able to run Docker commands without using "sudo". This makes working with containers easier and more efficient.
Step 3: Automatic Docker Updates
Keeping Docker containers up-to-date is crucial for security. Install Watchtower to automatically update your Docker containers:
docker run -d --name watchtower -v /var/run/docker.sock:/var/run/docker.sock containrrr/watchtower --cleanup
Watchtower will handle automatic updates, ensuring your containers are always running the latest versions.
Step 4: Monitoring with Uptime Kuma
Uptime Kuma is a powerful monitoring tool that helps you keep track of your server's performance. Set it up with Docker:
docker run -d --restart=always -p 3001:3001 -v uptime-kuma:/app/data -v /var/run/docker.sock:/var/run/docker.sock --name uptime-kuma louislam/uptime-kuma:1
Uptime Kuma will allow you to monitor your server's availability, track downtime, and receive alerts when something goes wrong.
Step 5: Setting Up Apache2
Apache2 is one of the most popular web servers in the world. Here's how to install and configure it for your domain:
sudo apt install apache2
sudo nano /etc/apache2/sites-available/example.com.conf
Add the following configuration to your Apache2 site file:
<VirtualHost *:80>
ServerAdmin webmaster@localhost
ServerName example.com
Redirect permanent / https://www.example.com/
</VirtualHost>
<VirtualHost *:443>
ServerAdmin webmaster@localhost
ServerName example.com
DocumentRoot /var/www/example.com/
<Directory /var/www/>
Options FollowSymLinks
AllowOverride None
Require all granted
</Directory>
SSLEngine on
SSLCertificateFile /etc/ssl/example.com/cert.pem
SSLCertificateKeyFile /etc/ssl/example.com/cert.key
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^(.*)$ http://www.example.com$1 [L,R=301]
</VirtualHost>
Enable necessary Apache2 modules and restart the service:
sudo a2enmod rewrite
sudo a2enmod ssl
sudo a2ensite example.com.conf
sudo a2dissite 000-default.conf
sudo systemctl restart apache2
Secure your site with SSL using Certbot:
sudo apt-get install python-certbot-apache
sudo certbot --apache
Step 6: Setting Up a Firewall
Protect your server by setting up a firewall with UFW:
# Install UFW
sudo apt-get install ufw
# Enable UFW to start on boot
sudo systemctl enable ufw
# Allow necessary ports
sudo ufw allow ssh
sudo ufw allow 443
sudo ufw allow 80
sudo ufw allow 139
sudo ufw allow 445
# Enable UFW
sudo ufw enable
A firewall adds an essential layer of security by controlling network traffic. Make sure to only open necessary ports.
Security Best Practices
While the setup above is secure, consider additional security measures:
- Use services (ex. Cloudflare) to prevent brute-force attacks.
- Regularly update your software and review security settings.
- Use SSH keys for a more secure remote login method.
For further reading on security standards, visit the CIS Benchmarks and OWASP Guidelines.
Congratulations! You've successfully set up a Raspberry Pi server with Docker, Apache2, and monitoring tools. Make sure to regularly check on your server and perform necessary maintenance to ensure it continues running smoothly.