Home UbuntuHow to Install a LAMP Stack on Ubuntu 24.04

How to Install a LAMP Stack on Ubuntu 24.04

by Knowledge Base Linux
How to Install a LAMP Stack — How to Install a LAMP Stack on Ubuntu 24.04

A LAMP stack on Ubuntu 24.04 is still the most common foundation for hosting dynamic websites and web applications on a Linux server. LAMP stands for Linux, Apache, MySQL and PHP: four proven open-source components that together serve millions of WordPress, WooCommerce, Laravel and custom PHP sites. In this guide you will install each component on Ubuntu 24.04 LTS, secure the database, open the firewall and verify that PHP is processed correctly. By the end, your server will be ready to host a real website.

What Is Included in a LAMP Stack on Ubuntu 24.04? – How to Install a LAMP Stack

Each letter of the stack has a specific job. Linux (Ubuntu 24.04 LTS) is the operating system. Apache is the web server that receives HTTP requests and serves files. MySQL stores your data in relational tables. PHP is the programming language that generates dynamic pages and talks to the database. Ubuntu 24.04 ships Apache 2.4.58, MySQL 8.0 and PHP 8.3 in its default repositories, so you can build the entire stack with apt and receive security updates from Canonical for years.

Prerequisites – How to Install a LAMP Stack

  • A server or VPS running a fresh Ubuntu 24.04 LTS.
  • A user account with sudo privileges.
  • A domain name pointing to your server if you plan to go live (optional for testing).
  • Basic familiarity with the Linux command line and SSH.

Step 1: Update the System

Start by refreshing the package index and applying pending updates so you install the newest available versions:

sudo apt update && sudo apt upgrade -y

Step 2: Install Apache

Install the Apache web server with:

sudo apt install apache2 -y

If UFW is enabled, allow web traffic through the firewall. The Apache Full profile opens both port 80 (HTTP) and port 443 (HTTPS):

sudo ufw allow 'Apache Full'
sudo systemctl status apache2

Now open http://your_server_ip in a browser. You should see the Apache2 Ubuntu default page, which confirms the web server is running. If you have not configured the firewall yet, our guide to configuring UFW on Ubuntu walks through the rules in detail.

Step 3: Install MySQL

Next, install the MySQL database server:

sudo apt install mysql-server -y
sudo systemctl start mysql
sudo systemctl enable mysql

Run the included security script to set a root password policy, remove anonymous users and delete the test database:

sudo mysql_secure_installation

Answer Y to the main hardening questions. When it finishes, log in to test the server:

sudo mysql -e "SELECT VERSION();"
Testing a LAMP stack on Ubuntu 24.04 in a web browser

Step 4: Install PHP and Required Modules

Install PHP 8.3, the Apache PHP module and the extensions most applications need:

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

Restart Apache so it loads the PHP module:

sudo systemctl restart apache2
php -v

Step 5: Test PHP Processing

Create a small test file in the web root to confirm Apache hands PHP files to the interpreter:

sudo tee /var/www/html/test.php > /dev/null <<'EOF'
<?php
echo 'PHP version: ' . phpversion();
EOF

Visit http://your_server_ip/test.php. If the page displays the PHP version, for example PHP version: 8.3.6, the stack works end to end. Delete the test file afterwards so visitors cannot probe your setup:

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

Step 6: Create a Virtual Host (Recommended)

Instead of using the default site, create a dedicated virtual host for your domain. Create the document root and a test page:

sudo mkdir -p /var/www/example.com
sudo chown -R $USER:$USER /var/www/example.com
echo "<h1>It works!</h1>" | sudo tee /var/www/example.com/index.html

Create the configuration file /etc/apache2/sites-available/example.com.conf:

<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example.com
    ErrorLog ${APACHE_LOG_DIR}/example_error.log
    CustomLog ${APACHE_LOG_DIR}/example_access.log combined
</VirtualHost>

Enable the site, disable the default one and reload Apache:

sudo a2ensite example.com.conf
sudo a2dissite 000-default.conf
sudo apachectl configtest
sudo systemctl reload apache2

Managing LAMP Services Day to Day

All three daemons are managed by systemd, so the same commands work on any modern Ubuntu system. Use sudo systemctl restart apache2 after changing Apache configuration files, and sudo systemctl reload apache2 when you only changed a virtual host, because reload applies the new settings without dropping active connections. MySQL is controlled with sudo systemctl restart mysql, and its status and recent errors are visible through sudo systemctl status mysql and the error log at /var/log/mysql/error.log. Apache keeps two main logs per virtual host, an access log and an error log, under /var/log/apache2/. When a website suddenly returns a 500 error, the Apache error log is almost always the fastest place to find the PHP fatal error or permission problem behind it. Finally, remember to enable both services at boot with sudo systemctl enable apache2 mysql so the stack comes back automatically after a reboot or a power event.

Troubleshooting Common LAMP Problems

Apache fails to start. Run sudo apachectl configtest to find syntax errors and sudo ss -tlnp | grep :80 to check whether another process (often Nginx) already occupies port 80.

PHP files download instead of executing. The PHP module is not enabled. Run sudo a2enmod php8.3 and restart Apache.

MySQL root login is denied from PHP. Ubuntu’s MySQL root account uses auth_socket by default. Create a dedicated user for your application instead:

sudo mysql -e "CREATE USER 'appuser'@'localhost' IDENTIFIED BY 'StrongPass123!'; GRANT ALL PRIVILEGES ON appdb.* TO 'appuser'@'localhost'; FLUSH PRIVILEGES;"

Conclusion

Your LAMP stack on Ubuntu 24.04 is now installed and verified: Apache serves pages, MySQL stores data securely and PHP 8.3 processes dynamic code. The natural next steps are to protect the server with Fail2Ban and to add HTTPS once your domain resolves to the server. For deeper configuration options, consult the official Ubuntu Server documentation.

You may also like

KbLinux

Typically replies within a day

Hello, Welcome to the site. Please click below button for chatting me through Telegram.