Deploying a LAMP stack (Linux, Apache, MySQL/MariaDB, PHP) on Debian 12 is a popular method for setting up a robust and reliable web server. This guide walks you through the steps required to install and configure a LAMP stack on your Debian 12 system.
Prerequisites
Before you begin, ensure that you have the following:
- A Debian 12 server with a non-root user with sudo privileges.
- A basic understanding of terminal commands.
Step 1: Update the System
First, make sure your system is up-to-date. Open a terminal and run the following commands:
sudo apt update
sudo apt upgrade -y
Step 2: Install Apache
Apache is a widely used web server. Install it using the following command:
sudo apt install apache2 -y
After the installation, enable Apache to start on boot and start the Apache service:
sudo systemctl enable apache2
sudo systemctl start apache2
To verify the installation, open your web browser and visit your server’s IP address. You should see the Apache2 Debian Default Page.
Step 3: Install MySQL/MariaDB
MySQL is a popular database management system. In this guide, we will use MariaDB, a drop-in replacement for MySQL.
Install MariaDB with the following command:
sudo apt install mariadb-server -y
Secure your MariaDB installation by running the security script:
sudo mysql_secure_installation
You will be prompted to set a root password and make several other security-related changes. Follow the prompts to secure your installation.
Step 4: Install PHP
PHP is a server-side scripting language used for web development. Install PHP and the necessary modules with the following command:
sudo apt install php libapache2-mod-php php-mysql -y
To test PHP, create a info.php
file in the web root directory:
sudo nano /var/www/html/info.php
Add the following content to the file:
<?php
phpinfo();
?>
Save and close the file. Now, open your web browser and visit http://your_server_ip/info.php
. You should see the PHP information page, indicating that PHP is working correctly.
Step 5: Adjust Apache Configuration
By default, Apache serves files from /var/www/html
. To prioritize PHP files, modify the Apache configuration file:
sudo nano /etc/apache2/mods-enabled/dir.conf
Move index.php
to the first position in the DirectoryIndex specification:
<IfModule mod_dir.c>
DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm
</IfModule>
Save and close the file, then restart Apache to apply the changes:
sudo systemctl restart apache2
Step 6: Test the LAMP Stack
To ensure everything is working, create a test PHP file:
sudo nano /var/www/html/test.php
Add the following content:
<?php
echo "LAMP stack is installed successfully!";
?>
Save and close the file. Visit http://your_server_ip/test.php
in your web browser. If you see the message “LAMP stack is installed successfully!”, you have successfully deployed a LAMP stack on Debian 12.
Conclusion
You have now installed a LAMP stack on your Debian 12 server. This stack provides a powerful and flexible environment for developing and hosting dynamic websites and applications. From here, you can start deploying your web applications, configuring virtual hosts, and further securing your server.
If you encounter any issues or have questions, feel free to consult the official documentation for Apache, MariaDB, and PHP, or seek help from the Debian and LAMP stack communities.