Step 1: Update System Packages
Before installing any components, ensure your Debian system’s package list is up to date and upgrade existing packages to their latest versions. Run the following commands in the terminal:
sudo apt update && sudo apt upgrade -y
This step prevents compatibility issues and ensures you’re working with the most stable software versions.
Step 2: Install Apache Web Server
The LAMP stack requires a web server to serve PHP pages. Install Apache (the most common choice for Debian) using:
sudo apt install apache2 -y
Start the Apache service and enable it to launch on system boot:
sudo systemctl start apache2
sudo systemctl enable apache2
Verify Apache is running by visiting your server’s IP address in a browser— you should see the default Apache welcome page.
Step 3: Install MySQL Database Server
PHP often interacts with databases, so install MySQL (or its drop-in replacement, MariaDB) to store application data. For Debian, MariaDB is recommended due to its compatibility and active maintenance:
sudo apt install mariadb-server -y
Start the MariaDB service and enable it:
sudo systemctl start mariadb
sudo systemctl enable mariadb
Run the security script to harden your installation (e.g., set a root password, remove anonymous users, disable remote root login):
sudo mysql_secure_installation
Follow the on-screen prompts to complete the setup.
Step 4: Install PHP and Essential Extensions
The core of the LAMP stack is PHP. Install PHP along with modules for Apache integration and database connectivity (MySQL). For Debian’s default repositories (as of 2025), use:
sudo apt install php libapache2-mod-php php-mysql -y
php
: Installs the PHP interpreter.libapache2-mod-php
: Enables Apache to process PHP files.php-mysql
: Provides MySQL/MariaDB support for PHP (e.g., mysqli
and PDO
extensions).After installation, restart Apache to load the PHP module:
sudo systemctl restart apache2
This ensures Apache can handle .php
files correctly.
Step 5: Verify PHP Installation
Create a test PHP file in Apache’s default document root (/var/www/html
) to confirm PHP is working. Run:
sudo nano /var/www/html/info.php
Add the following code to the file:
<?php phpinfo(); ?>
Save the file and exit the editor. Open a browser and navigate to http://your_server_ip/info.php
. You should see a detailed PHP information page (e.g., version, loaded extensions, configuration settings). This confirms PHP is installed and accessible via Apache.
Step 6: Install Additional PHP Extensions (Optional but Recommended)
Depending on your application (e.g., WordPress, Laravel), you may need extra PHP modules. Common extensions include:
php-curl
: For HTTP requests.php-gd
: For image manipulation.php-mbstring
: For multibyte string support (e.g., Unicode).php-xml
: For XML processing.php-zip
: For ZIP archive handling.Install them using:
sudo apt install php-curl php-gd php-mbstring php-xml php-zip -y
After installation, restart Apache to apply the changes:
sudo systemctl restart apache2
These extensions are widely used in web applications and prevent errors related to missing functionality.
Step 7: Configure PHP Settings (Optional)
You can customize PHP behavior by editing its configuration file. For Debian with Apache, the main configuration file is located at /etc/php/8.2/apache2/php.ini
(adjust the version number if you installed a different PHP version, e.g., 7.4
).
Common settings to modify include:
memory_limit
: Increase the maximum memory a PHP script can use (e.g., 256M
).max_execution_time
: Extend the maximum execution time for scripts (e.g., 300
seconds).upload_max_filesize
and post_max_size
: Adjust file upload limits (e.g., 64M
each).After making changes, save the file and restart Apache:
sudo systemctl restart apache2
These adjustments optimize PHP for your application’s needs.
Troubleshooting Tips
libapache2-mod-php
package is installed and the Apache PHP module is enabled (sudo a2enmod php8.2
if using PHP 8.2). Restart Apache after enabling the module..php
files. Check the /etc/apache2/mods-enabled/php8.2.conf
file (replace 8.2
with your PHP version) for the SetHandler
directive:<FilesMatch \.php$>
SetHandler application/x-httpd-php
</FilesMatch>
Call to undefined function mysqli_connect()
), install the corresponding PHP module (e.g., sudo apt install php-mysqli
) and restart Apache.