How to Install LAMP Stack on Debian 12 (AWS)

The LAMP stack is a popular web service solution stack, consisting of Linux, Apache, MySQL (or MariaDB), and PHP. In this guide, I will walk you through the process of installing the LAMP stack on Debian 12, a robust and secure distribution, running on an AWS instance. This setup is ideal for hosting websites and web applications on a scalable cloud platform.

Prerequisites

Before we start, ensure you have:

  1. AWS Account: You need access to an AWS account to create an EC2 instance.
  2. Debian 12 EC2 Instance: A running Debian 12 EC2 instance. Choose a t2.micro instance if you are eligible for the free tier.
  3. SSH Access: Ensure you can SSH into your EC2 instance with a key pair (.pem file) provided by AWS.

Step 1: Update the System

Start by connecting to your Debian 12 instance via SSH. Once connected, update your package list and upgrade the installed packages to their latest versions.

sudo apt update && sudo apt upgrade -y

This command updates the list of available packages and their versions, then installs the latest versions of all currently installed packages and dependencies.

Step 2: Install Apache Web Server

Apache is the most widely used web server. To install Apache, run the following command:

sudo apt install apache2 -y

After the installation, you can start the Apache service and enable it to start on boot:

sudo systemctl start apache2
sudo systemctl enable apache2

To verify that Apache is running, enter the public IP address of your EC2 instance in a web browser. You should see the default Apache welcome page.

See also  Mastering Swap Space: A Guide to Viewing, Creating, and Setting in fstab

Step 3: Adjust Firewall Settings

If your instance has UFW (Uncomplicated Firewall) enabled, you need to allow HTTP and HTTPS traffic. Allow Apache through the firewall:

sudo ufw allow 'Apache Full'

If UFW is inactive or not installed, ensure that your AWS security group allows inbound traffic on port 80 (HTTP) and port 443 (HTTPS).

Step 4: Install MySQL (or MariaDB)

For the database server, you can choose between MySQL and MariaDB. MariaDB is a fork of MySQL and is fully compatible. In this guide, we will install MariaDB.

To install MariaDB, run the following command:

sudo apt install mariadb-server mariadb-client -y

After installation, start the MariaDB service and enable it to start on boot:

sudo systemctl start mariadb
sudo systemctl enable mariadb

Next, secure your MariaDB installation by running the security script:

sudo mysql_secure_installation

You will be prompted to set the root password and to answer a series of security questions. It’s recommended to answer Y to all prompts.

Step 5: Install PHP

PHP is the scripting language that processes dynamic content. Install PHP and the necessary extensions using the following command:

sudo apt install php php-mysql libapache2-mod-php php-cli php-cgi php-gd php-xml -y

This command installs PHP along with common extensions that are typically required by various web applications.

Step 6: Configure Apache to Use PHP

To ensure that Apache can serve PHP files, you need to modify the Apache configuration. Open the dir.conf file with a text editor:

sudo nano /etc/apache2/mods-enabled/dir.conf

Look for the following line:

DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm

Ensure that index.php is listed before index.html as shown below:

DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm

Save the file and exit the editor. To apply the changes, restart Apache:

sudo systemctl restart apache2

Step 7: Test PHP Processing

To test if PHP is installed correctly, create a phpinfo() file in the Apache document root directory:

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

Add the following content to the file:

<?php
phpinfo();
?>

Save and exit the file. Now, navigate to http://your_server_ip/info.php in your web browser. You should see the PHP information page, which confirms that PHP is working correctly.

See also  Part 3: Leveraging Auto Scaling for Cost and Performance Optimization on AWS

Step 8: Set Up MySQL Database

To set up a database and user for your web applications, log in to the MariaDB shell:

sudo mysql -u root -p

After entering your root password, create a new database:

CREATE DATABASE mydatabase;

Then create a new user and grant them privileges on the database:

CREATE USER 'myuser'@'localhost' IDENTIFIED BY 'mypassword';
GRANT ALL PRIVILEGES ON mydatabase.* TO 'myuser'@'localhost';
FLUSH PRIVILEGES;
EXIT;

Replace mydatabase, myuser, and mypassword with your desired database name, username, and password.

Step 9: Optimize LAMP Stack for AWS

Since you’re running this LAMP stack on an AWS EC2 instance, it’s important to optimize the stack for performance and security.

1. Optimize Apache Configuration

  • Enable Gzip Compression: Modify the apache2.conf file to enable Gzip compression to reduce the size of your web pages, thus improving load times.
   sudo a2enmod deflate
  • Enable Caching: Use Apache’s mod_cache to cache responses, which will speed up page delivery.
   sudo a2enmod cache
   sudo a2enmod cache_disk
   sudo a2enmod expires
  • Limit Server Signature and Trace Methods: Disable server signatures and trace methods to make your server less prone to attacks.
   sudo nano /etc/apache2/conf-available/security.conf

Set ServerSignature Off and ServerTokens Prod.

2. Secure MySQL/MariaDB

  • Disable Remote Root Login: Prevent root from logging in remotely by editing the MySQL configuration file:
   sudo nano /etc/mysql/mariadb.conf.d/50-server.cnf

Under [mysqld], add:

   bind-address = 127.0.0.1

This binds MySQL to localhost, ensuring it only accepts connections from the server itself.

  • Configure Automatic Backups: Use cron jobs to schedule regular backups of your databases. This ensures data safety in case of failure.
   sudo apt install automysqlbackup
   sudo nano /etc/default/automysqlbackup

Configure the necessary options for daily, weekly, or monthly backups.

See also  Part 7: Automating Server Management with DevOps Tools

3. Use SSL/TLS with Let’s Encrypt

Encrypt the traffic between your server and clients by installing a free SSL certificate from Let’s Encrypt.

sudo apt install certbot python3-certbot-apache -y
sudo certbot --apache

Follow the prompts to obtain and configure your SSL certificate.

Step 10: Automate and Monitor Your LAMP Stack

To ensure your LAMP stack is always running smoothly, consider implementing the following:

  1. Auto-Scaling: Set up AWS Auto Scaling to handle traffic spikes by automatically adding or removing EC2 instances.
  2. Monitoring: Use AWS CloudWatch to monitor the health and performance of your EC2 instances. Set alarms to notify you of any issues.
  3. Security Groups: Regularly review and update your security groups to minimize exposed ports and limit access to your EC2 instance.
  4. Automated Updates: Schedule updates and patches for your Debian system to keep it secure and up-to-date.

Conclusion

Installing a LAMP stack on Debian 12 within an AWS environment provides a robust platform for hosting web applications. By following these steps, you will have a fully functional LAMP stack ready to serve your applications. Additionally, by optimizing and securing your setup, you can ensure it runs efficiently and securely in the cloud.

This guide covered the installation of Apache, MariaDB, and PHP on Debian 12, including configurations specific to AWS environments. Remember to keep your software up-to-date and monitor your system regularly to maintain a secure and reliable server.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.