A LAMP stack is the classic combination of Linux, the Apache web server, the MySQL database and the PHP language. It runs a large share of PHP applications, from WordPress and Drupal to custom code. In this tutorial you will install Apache, MySQL 8.0 and PHP 8.3 on Ubuntu 24.04, create an Apache virtual host for your own domain and verify that PHP can talk to MySQL.

Prerequisites

To follow this tutorial you need:

  • A server running Ubuntu 24.04 LTS, for example a CubePath VPS, with at least 1 GB of RAM.
  • A non-root user with sudo privileges.
  • Optionally, a domain name with an A record pointing to your server's public IP. This tutorial uses your_domain as a placeholder. If you don't have a domain yet, you can use your_server_ip everywhere instead.

Step 1 - Installing Apache and allowing web traffic

Apache is available in Ubuntu's default repositories. Refresh the package index and install it:

sudo apt update
sudo apt install apache2

The package starts the service and enables it at boot. Confirm that it is running:

sudo systemctl status apache2 --no-pager
● apache2.service - The Apache HTTP Server
     Loaded: loaded (/usr/lib/systemd/system/apache2.service; enabled; preset: enabled)
     Active: active (running) since ...

If you use UFW, Apache registers application profiles for it. Apache opens port 80 only, Apache Full opens ports 80 and 443. Allow SSH first so you don't lock yourself out, then allow Apache Full:

sudo ufw allow OpenSSH
sudo ufw allow "Apache Full"
sudo ufw enable

Check the rules:

sudo ufw status
Status: active

To                         Action      From
--                         ------      ----
OpenSSH                    ALLOW       Anywhere
Apache Full                ALLOW       Anywhere
...

Open http://your_server_ip in your browser. You should see the "Apache2 Default Page". You can also test from the server itself:

curl -sI http://localhost | head -n 1
HTTP/1.1 200 OK

Step 2 - Installing MySQL

Install the MySQL server package. On Ubuntu 24.04 this installs MySQL 8.0:

sudo apt install mysql-server

Verify that the service is active and check the version:

sudo systemctl is-active mysql
mysql --version
active
mysql  Ver 8.0.x-0ubuntu0.24.04.x for Linux on x86_64 ((Ubuntu))

Next, run the included security script. It can enable the password validation component, remove anonymous users, disallow remote root login and remove the test database:

sudo mysql_secure_installation

Answer Y to the questions about removing anonymous users, disallowing remote root login, removing the test database and reloading the privilege tables. On Ubuntu the MySQL root account authenticates through the auth_socket plugin, so the script skips setting a root password. That is expected: only the system root user (or someone using sudo) can log in as MySQL root.

Confirm that you can open the MySQL console:

sudo mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
...
mysql>

Type exit to leave the console.

Step 3 - Installing PHP

Apache runs PHP through the libapache2-mod-php module. Install PHP together with that module and the MySQL extension:

sudo apt install php libapache2-mod-php php-mysql

On Ubuntu 24.04 these meta packages install PHP 8.3. Check the version:

php -v
PHP 8.3.6 (cli) (built: ...) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.3.6, Copyright (c) Zend Technologies
    with Zend OPcache v8.3.6, Copyright (c), by Zend Technologies

Most applications also need a few common extensions. Install the ones used by WordPress, Laravel and similar projects:

sudo apt install php-curl php-gd php-mbstring php-xml php-zip php-intl

Restart Apache so it loads the new modules:

sudo systemctl restart apache2

Step 4 - Creating a virtual host for your domain

Apache serves the default page from /var/www/html. Instead of editing it, create a separate directory and virtual host for your site so you can host more sites on the same server later.

Create the document root and give your user ownership of it:

sudo mkdir -p /var/www/your_domain
sudo chown -R $USER:$USER /var/www/your_domain

Create a new virtual host file:

sudo nano /etc/apache2/sites-available/your_domain.conf

Add the following configuration, replacing your_domain with your domain name:

<VirtualHost *:80>
    ServerName your_domain
    ServerAlias www.your_domain
    DocumentRoot /var/www/your_domain

    <Directory /var/www/your_domain>
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/your_domain_error.log
    CustomLog ${APACHE_LOG_DIR}/your_domain_access.log combined
</VirtualHost>

AllowOverride All lets applications use .htaccess files, which WordPress and many frameworks rely on for pretty URLs. Enable mod_rewrite for the same reason, then enable the new site and disable the default one:

sudo a2enmod rewrite
sudo a2ensite your_domain
sudo a2dissite 000-default

Test the configuration for syntax errors:

sudo apache2ctl configtest
Syntax OK

If the test prints AH00558: Could not reliably determine the server's fully qualified domain name, the configuration is still valid. You can silence the warning by adding ServerName localhost to /etc/apache2/apache2.conf.

Reload Apache to apply the changes:

sudo systemctl reload apache2

Step 5 - Testing PHP processing

Create a small PHP file in the new document root:

nano /var/www/your_domain/info.php

Add this line:

<?php phpinfo();

Visit http://your_domain/info.php. You should see the PHP information page, with Server API set to Apache 2.0 Handler. From the command line you can check it like this:

curl -s http://your_domain/info.php | grep -o 'PHP Version [0-9.]*' | head -n 1
PHP Version 8.3.6

This page exposes details about your server, so delete it once you have confirmed that PHP works:

rm /var/www/your_domain/info.php

Step 6 - Testing the database connection from PHP

To confirm the whole stack works together, create a database and a dedicated user, then connect to it from PHP.

Open the MySQL console:

sudo mysql

Create a database and a user. Replace your_strong_password with a strong password of your own; if you enabled the password validation component in Step 2, weak passwords are rejected:

CREATE DATABASE example_db DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'example_user'@'localhost' IDENTIFIED BY 'your_strong_password';
GRANT ALL PRIVILEGES ON example_db.* TO 'example_user'@'localhost';
EXIT;

Create a test script:

nano /var/www/your_domain/dbtest.php

Add the following code, using the same password:

<?php
$pdo = new PDO(
    'mysql:host=localhost;dbname=example_db;charset=utf8mb4',
    'example_user',
    'your_strong_password',
    [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]
);

echo 'Connected to MySQL ' . $pdo->query('SELECT VERSION()')->fetchColumn() . PHP_EOL;

Request the page:

curl http://your_domain/dbtest.php
Connected to MySQL 8.0.x-0ubuntu0.24.04.x

If you see this line, Apache, PHP and MySQL are working together. Remove the test file, since it contains a password:

rm /var/www/your_domain/dbtest.php

Step 7 - Securing the site with HTTPS (optional)

If your domain already points to the server, you can get a free Let's Encrypt certificate with Certbot. Install Certbot and its Apache plugin:

sudo apt install certbot python3-certbot-apache

Request a certificate for both names. Certbot edits the virtual host and sets up the HTTP to HTTPS redirect for you:

sudo certbot --apache -d your_domain -d www.your_domain

Certbot installs a systemd timer that renews certificates automatically. Test the renewal process with a dry run:

sudo certbot renew --dry-run

Then open https://your_domain to confirm the certificate is served.

Troubleshooting

  • The browser downloads the PHP file instead of running it. The PHP module is not loaded. Run sudo a2enmod php8.3 and sudo systemctl restart apache2.
  • You still see the Apache default page. The default site is still enabled or your ServerName does not match the address you are using. Run sudo a2dissite 000-default, check ServerName and reload Apache.
  • Access denied for user in the PDO test. The user or password does not match what you created. Recreate the user with ALTER USER 'example_user'@'localhost' IDENTIFIED BY 'your_strong_password'; from sudo mysql.
  • Apache fails to start. Read the exact error with sudo journalctl -u apache2 --no-pager -n 50 and check the site configuration with sudo apache2ctl configtest.

Conclusion

You now have a working LAMP stack on Ubuntu 24.04: Apache serving your domain from its own virtual host, MySQL 8.0 secured with mysql_secure_installation, and PHP 8.3 connecting to the database. From here you can deploy a PHP application such as WordPress into /var/www/your_domain, set up regular database backups with mysqldump, or switch Apache to PHP-FPM if you need to run several PHP versions side by side.