A Complete Guide for Installing and Securing LAMP Stacks on Debian 12 - Part 1
Introduction
Welcome to our complete guide on installing, configuring, and securing a LAMP (Linux, Apache, MySQL, PHP) stack on Debian 12 (Bookworm). This series is designed to take you through the entire process of setting up a robust, secure, and efficient web server environment using the latest Debian release.
The guide is divided into four parts, each focusing on a crucial aspect of the setup process:
Part 1: Setting Up Debian 12 on AWS EC2 (this tutorial)
- Creating an AWS account
- Launching a Debian 12 EC2 instance
- Configuring security groups
- Allocating an Elastic IP
- Connecting to the instance
- Initial server setup and security measures
Part 2: Installing and Configuring LAMP Stack Components
- Installing Apache web server
- Setting up MariaDB (MySQL) database
- Installing PHP and essential modules
- Configuring Apache with PHP
- Basic optimizations for each component
Part 3: Implementing Advanced Security Measures
- Installing and configuring SSL certificates with Let's Encrypt
- Hardening Apache configurations
- Securing PHP settings
- Enhancing MariaDB security
- Setting up fail2ban and modsecurity for intrusion prevention
- Configuring and optimizing firewalls
Part 4: Maintenance, Backups, and Performance Tuning
- Establishing comprehensive backup routines
- Setting up automated security updates
- Implementing log rotation and management
- Performance tuning for Apache, PHP, and MariaDB
- Monitoring system health and performance
- Best practices for ongoing maintenance
Part 5 BONUS: Installing and Working with Docker
- Installing Docker on Debian 12
- Basic Docker commands and concepts
- Creating and managing Docker containers
- Working with Docker images
- Setting up Docker Compose
- Containerizing a simple web application
- Docker networking basics
- Docker volume management
- Docker security best practices
- Integrating Docker with your LAMP stack
- Monitoring and logging Docker containers
Setting up a Debian 12 instance on AWS EC2.
In this initial section, we'll walk you through the process of setting up a Debian 12 instance on Amazon Web Services (AWS) Elastic Compute Cloud (EC2).
This will serve as the foundation for our LAMP stack, which we'll build and secure in the subsequent parts of this series.
Prerequisites
- An AWS account
- Basic familiarity with AWS console
- Understanding of basic Linux commands
Step 1: Create an AWS Account and Log In
- Go to https://aws.amazon.com/
- Click "Create an AWS Account" in the top right corner
- Follow the prompts, providing necessary information including email, password, and payment method
- Once the account is created, return to https://aws.amazon.com/
- Click "Sign In to the Console" in the top right corner
- Enter your email and password to log in
Step 2: Choose AWS Region
- After logging in, locate the region dropdown in the top right corner of the AWS Management Console
- Click the dropdown and select the region closest to you or your target audience
Note: Choosing a nearby region can help reduce latency for your users
Step 3: Navigate to EC2 Dashboard
- In the AWS Management Console, click on "Services" in the top left corner
- In the search bar that appears, type "EC2"
- Click on "EC2" in the search results to open the EC2 dashboard
Step 4: Launch a Debian 12 EC2 Instance
- On the EC2 dashboard, click the orange "Launch instance" button
- In the "Name and tags" section, enter a name for your instance (e.g., "Debian12-LAMP-Server")
- In the "Application and OS Images" section:
- Click on "Quick Start"
- In the search bar, type "Debian"
- Look for "Debian 12 (Bookworm)" in the Community AMIs
- Ensure it says "Free tier eligible" next to it
- Select this AMI
- In the "Instance type" section:
- Select "t2.micro" (This should be pre-selected as it's free tier eligible)
- In the "Key pair (login)" section:
- Click "Create new key pair"
- Enter a name for your key pair (e.g., "Debian12-LAMP-keypair")
- For "Key pair type", select "RSA"
- For "Private key file format", select ".pem"
- Click "Create key pair"
- The key pair file will automatically download. Keep this file safe and don't lose it
- In the "Network settings" section:
- Keep "Create security group" selected
- Ensure "Allow SSH traffic from" is checked and set to "Anywhere"
- Check "Allow HTTPS traffic from the internet"
- Check "Allow HTTP traffic from the internet"
- In the "Configure storage" section:
- Keep the default 8 GB gp2 (SSD) root volume
- Review all settings, then click "Launch instance" in the right sidebar
Step 5: Allocate and Associate an Elastic IP Address
Note: Elastic IP addresses are not part of the free tier. They're free when associated with a running instance but incur a small hourly cost when not associated.
- In the EC2 dashboard's left sidebar, under "Network & Security", click on "Elastic IPs"
- Click the orange "Allocate Elastic IP address" button
- In the "Allocate Elastic IP address" page:
- For "Network border group", keep your current region selected
- For "Public IPv4 address pool", keep "Amazon's pool of IPv4 addresses" selected
- Click "Allocate"
- You'll see a success message. Click "Close"
- In the Elastic IPs list, select your newly created Elastic IP
- Click "Actions" at the top, then "Associate Elastic IP address"
- In the "Associate Elastic IP address" page:
- For "Resource type", select "Instance"
- For "Instance", select your Debian 12 instance from the dropdown
- For "Private IP address", keep the pre-filled address
- Check "Allow this Elastic IP address to be reassociated"
- Click "Associate"
Step 6: Connect to Your Debian 12 Instance Using Termius
- Download and install Termius from https://termius.com/ if you haven't already
- Open Termius
- Click on "New Host" (usually a "+" icon)
- In the "New Host" window, enter the following details:
- Label: Give your instance a name (e.g., "Debian12-LAMP-Server")
- Address: Enter your instance's Elastic IP (the one you just associated)
- Username: admin
- In the "Authentication" section:
- Click "New Key"
- Click "Import" and select the .pem file you downloaded when creating the key pair
- Give the key a name (e.g., "Debian12-LAMP-key")
- Click "Save" for the key
- Back in the "New Host" window, select the key you just imported
- Click "Save" to save the host configuration
- In the hosts list, double-click on the newly created host to connect
- If prompted about the authenticity of the host, click "Continue"
You should now be connected to your Debian 12 instance via SSH.
Step 7: Initial System Update
Once connected to your instance, run the following commands:
sudo apt update
sudo apt upgrade -y
This will update the package lists and upgrade all installed packages to their latest versions.
Step 8: Install ZRAM for Swap
ZRAM provides a more efficient alternative to traditional swap files. To set it up:
- Install zram-tools:
sudo apt install zram-tools -y
- The default configuration should work well for most use cases. However, if you want to adjust settings:
sudo nano /etc/default/zramswap
You can modify settings like PERCENT (percentage of RAM to use for ZRAM) or PRIORITY (swap priority).
- Enable and start the ZRAM service:
sudo systemctl enable zramswap
sudo systemctl start zramswap
- Verify ZRAM is working:
swapon -s
You should see a /dev/zram0 device listed.
Step 9: Install Basic Utilities
Install some common tools that will be useful for system management:
sudo apt update
sudo apt install htop neovim wget curl syslog-ng net-tools -y
This installs:
- htop: an interactive process viewer
- neovim: an improved version of the vim text editor
- wget: a utility for retrieving files using HTTP, HTTPS, and FTP
- curl: a tool for transferring data using various protocols
- syslog-ng: a flexible and scalable system logging application
- net-tools: includes important networking tools like netstat
Conclusion
You have now successfully:
1. Set up a free tier eligible Debian 12 instance on AWS EC2
2. Associated an Elastic IP address for consistent access
3. Connected to your instance using Termius
4. Performed initial system updates
5. Configured ZRAM for efficient memory management
6. Installed essential system utilities
You have now successfully set up a free tier eligible Debian 12 instance on AWS EC2 with an Elastic IP address, basic security measures, and system preparations. The Elastic IP ensures your instance maintains the same public IP address even after stops and starts, which is crucial for consistent access, especially for web servers. In the next part of this guide, we'll install and configure the core components of our LAMP stack.
What's Coming in Part 2
In the next part of our series, we'll dive into installing and configuring the core components of our LAMP stack:
- Apache web server
- MariaDB (MySQL) database
- PHP and essential modules
We'll also cover basic optimizations and initial security measures for each component.
Latest Comments
Sign in to add a commentNo comments yet. Be the first to comment!