A LAMP stack is one of the most popular web development environments, consisting of Linux, Apache, MySQL, and PHP. This powerful combination provides everything you need to host dynamic websites and web applications. In this guide, we’ll walk through installing a complete LAMP stack on Ubuntu 24.04.

Prerequisites

Before we begin, ensure you have:

Update Your System

Start by updating your package lists and upgrading existing packages:

sudo apt update && sudo apt upgrade -y

Install Apache Web Server

Apache is the web server component of our LAMP stack. Install it with:

sudo apt install apache2 -y

After installation, start and enable Apache to run at boot:

sudo systemctl start apache2
sudo systemctl enable apache2

Verify that Apache is running:

sudo systemctl status apache2

Test your Apache installation by opening a web browser and navigating to http://your-server-ip or http://localhost if installing locally. You should see the Apache2 default page.

Configure the Firewall

If you have UFW (Uncomplicated Firewall) enabled, allow HTTP and HTTPS traffic:

sudo ufw allow 'Apache Full'

Install MySQL Database Server

MySQL will serve as our database management system. Install it with:

sudo apt install mysql-server -y

Start and enable MySQL:

sudo systemctl start mysql
sudo systemctl enable mysql

Secure MySQL Installation

Run the security script to improve MySQL’s security:

sudo mysql_secure_installation

This script will guide you through:

  • Setting up the validate password plugin (optional)
  • Setting a root password
  • Removing anonymous users
  • Disabling remote root login
  • Removing the test database
  • Reloading privilege tables

Create a MySQL User (Optional)

For better security practices, create a dedicated MySQL user instead of using root:

sudo mysql -u root -p

In the MySQL prompt, run:

CREATE USER 'your_username'@'localhost' IDENTIFIED BY 'your_strong_password';
GRANT ALL PRIVILEGES ON *.* TO 'your_username'@'localhost' WITH GRANT OPTION;
FLUSH PRIVILEGES;
EXIT;

Install PHP

PHP is the scripting language that will process dynamic content. Ubuntu 24.04 comes with PHP 8.3 by default. Install PHP along with common extensions:

sudo apt install php libapache2-mod-php php-mysql php-cli php-curl php-json php-mbstring php-xml php-zip -y

Configure PHP

Edit the PHP configuration file to optimize performance:

sudo nano /etc/php/8.3/apache2/php.ini

Consider adjusting these settings based on your needs:

  • upload_max_filesize = 64M
  • post_max_size = 64M
  • memory_limit = 256M
  • max_execution_time = 300

Test PHP Processing

Create a test PHP file to verify everything is working correctly:

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

Add the following content:

<?php
phpinfo();
?>

Restart Apache to apply the PHP configuration changes:

sudo systemctl restart apache2

Visit http://your-server-ip/info.php in your browser. You should see a detailed PHP information page showing your PHP version and configuration.

Important: Remove this test file after verification for security reasons:

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

Test Database Connectivity

Create a simple PHP script to test the connection between PHP and MySQL:

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

Add this content (replace with your actual database credentials):

<?php
$servername = "localhost";
$username = "your_username";
$password = "your_password";

try {
    $pdo = new PDO("mysql:host=$servername", $username, $password);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "Connected successfully to MySQL";
} catch(PDOException $e) {
    echo "Connection failed: " . $e->getMessage();
}
?>

Visit http://your-server-ip/test_db.php to test the connection. Remember to delete this file afterward:

sudo rm /var/www/html/test_db.php

Step 7: Configure Virtual Hosts (Optional)

For hosting multiple websites, set up virtual hosts. Create a directory for your site:

sudo mkdir -p /var/www/your-domain.com/public_html
sudo chown -R www-data:www-data /var/www/your-domain.com/public_html
sudo chmod -R 755 /var/www/your-domain.com

Create a virtual host configuration file:

sudo nano /etc/apache2/sites-available/your-domain.com.conf

Add this configuration:

<VirtualHost *:80>
    ServerAdmin admin@your-domain.com
    ServerName your-domain.com
    ServerAlias www.your-domain.com
    DocumentRoot /var/www/your-domain.com/public_html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Enable the site and restart Apache:

sudo a2ensite your-domain.com.conf
sudo systemctl restart apache2

Additional Security Considerations

Disable Server Signature

Hide Apache version information by editing the Apache configuration:

sudo nano /etc/apache2/conf-available/security.conf

Set:

ServerTokens Prod
ServerSignature Off

Enable SSL (Recommended)

For production environments, install SSL certificates using Let’s Encrypt:

sudo apt install certbot python3-certbot-apache -y
sudo certbot --apache -d your-domain.com -d www.your-domain.com

Troubleshooting Common Issues

Apache won’t start: Check if another service is using port 80:

sudo netstat -tlnp | grep :80

PHP not processing: Ensure the PHP module is enabled:

sudo a2enmod php8.3
sudo systemctl restart apache2

Permission issues: Set proper ownership for web directories:

sudo chown -R www-data:www-data /var/www/html

Conclusion

You now have a fully functional LAMP stack running on Ubuntu 24.04! This setup provides a solid foundation for developing and hosting PHP-based web applications. Remember to keep your system updated regularly and follow security best practices, especially if this server will be accessible from the internet.

Your LAMP stack includes Apache 2.4, MySQL 8.0, and PHP 8.3, giving you access to the latest features and security improvements. From here, you can install popular content management systems like WordPress, develop custom PHP applications, or experiment with various web technologies.