How to Install LAMP on Ubuntu 26.04 and 24.04

Share

To install a LAMP stack on Ubuntu 26.04 or 24.04, use APT to install Apache, MySQL and PHP from Ubuntu’s repositories. Together, these tools let your Ubuntu computer serve websites and run PHP applications backed by a database.

This guide takes you from installation to a working PHP test, with a separate database user, firewall checks and troubleshooting. If you need help, leave a comment or contact us.

Before you begin
  • Ubuntu 26.04 LTS or 24.04 LTS, Desktop or Server.
  • A user with sudo access and an internet connection.
  • A terminal, or an SSH connection to your server.
  • Backups if the machine already hosts websites or databases. These steps assume a fresh setup.
Your LAMP setup

Apache serves pages, MySQL stores data, and PHP runs application code. Ubuntu provides the Linux foundation.

How to Install LAMP on Ubuntu

The same package names work on both releases. Ubuntu chooses the default versions for your release, so you do not need to add a third-party repository.

1Refresh the package list

Open a terminal and refresh the list of available packages. Enter your Ubuntu user password if sudo asks for it.

Terminal
sudo apt update
2Install Apache, MySQL and PHP

Install the web server, database server, PHP, Apache PHP module and PHP MySQL extension. This also installs PHP for the terminal and curl for the website test below. Review the proposed packages and confirm when prompted.

Terminal
sudo apt install apache2 mysql-server php libapache2-mod-php php-mysql php-cli curl
3Check the services

Both commands should print active. If either does not, see the troubleshooting section.

Terminal
systemctl is-active apache2
systemctl is-active mysql
4Open the Apache test page

On the Ubuntu computer, open http://localhost/ in a browser. You should see the Apache default page. From another computer, use http://SERVER_IP/, replacing SERVER_IP with the server’s reachable address. Run the command below to list its addresses. Remote access also depends on firewalls and network settings.

Terminal
hostname -I

Test PHP in Apache

A terminal version check alone does not prove that Apache can run PHP. This temporary file tests the web server and the MySQL extension together.

1Create the test file

This writes a small file to Apache’s default web directory. If the file or a symbolic link with this name already exists, the command stops without replacing it. Choose another unused filename and use it in the next steps.

Terminal
sudo sh -c 'test ! -e /var/www/html/lamp-check.php && test ! -L /var/www/html/lamp-check.php && printf "%s\n" "<?php echo \"PHP is working. MySQL extension: \" . (extension_loaded(\"mysqli\") ? \"loaded\" : \"missing\");" > /var/www/html/lamp-check.php'
2Open and remove the test

Visit http://localhost/lamp-check.php, or replace localhost with the server address from another computer. You should see PHP is working. MySQL extension: loaded. Then remove the file:

Terminal
sudo rm /var/www/html/lamp-check.php

Your basic LAMP installation is complete if the Apache page and PHP test work. A separate website and a public deployment are optional for local development. For an existing site, the active Apache virtual host (a configuration for a particular website) may use a different document root, the directory containing the site’s files. The default path above applies to the default site.

Set Up a Website

Apache can serve separate sites from their own directories. A virtual host tells Apache which site to serve for a requested hostname. Its document root is the folder containing that site’s files. This example uses lamp.example.com, a placeholder for a domain you control. It does not create DNS records. For a local-only test, you can use the Host header below.

First check that neither example path exists. If the command prints a path, stop and choose unused names instead of overwriting a site. A blank result means neither path was found; the command can return a nonzero status when a path is missing.

Terminal
ls -ld /var/www/lamp.example.com /etc/apache2/sites-available/lamp.example.com.conf 2>/dev/null

Create a directory for your site and a small PHP page. The page reveals no server configuration.

Terminal
sudo mkdir -p /var/www/lamp.example.com/public
printf '%s\n' '<?php echo "Apache and PHP are working.";' | sudo tee /var/www/lamp.example.com/public/index.php > /dev/null

Create the Apache configuration:

Terminal
sudo nano /etc/apache2/sites-available/lamp.example.com.conf

Paste the following, save with Ctrl+O and Enter, then exit with Ctrl+X:

Terminal
<VirtualHost *:80>
    ServerName lamp.example.com
    DocumentRoot /var/www/lamp.example.com/public
    DirectoryIndex index.php index.html
    <Directory /var/www/lamp.example.com/public>
        Options -Indexes
        AllowOverride None
        Require all granted
    </Directory>
    ErrorLog ${APACHE_LOG_DIR}/lamp-error.log
    CustomLog ${APACHE_LOG_DIR}/lamp-access.log combined
</VirtualHost>

Enable the site and check the configuration. Continue to reload only if the test reports Syntax OK.

Terminal
sudo a2ensite lamp.example.com.conf
sudo apache2ctl configtest
Terminal
sudo systemctl reload apache2

Test this virtual host locally without changing DNS. The expected response is Apache and PHP are working.

Terminal
curl -H 'Host: lamp.example.com' http://127.0.0.1/

The default Apache site remains enabled. Visiting the server by IP address can therefore show its default page because that request does not match your new hostname. For a real site, replace the example hostname throughout with your own, point its DNS to your server and configure HTTPS with a trusted certificate before accepting passwords. The example site remains HTTP until you configure TLS.

Create a Database and User

Many PHP apps need their own database. Create a separate user for each app instead of giving an app MySQL root access. The following example creates a local-only user; replace the password with a unique, long password and keep it private. SQL statements may be stored in MySQL client history, so handle that history as sensitive data.

1Open MySQL as the local administrator

Ubuntu normally uses socket authentication for the local MySQL root account: MySQL checks the local operating-system identity over a local socket instead of asking for a separate MySQL password. Use sudo rather than guessing a root password.

Terminal
sudo mysql
2Create the database and account

At the mysql> prompt, enter these statements. Replace the example password before running them. Use a password without a single quote for this example; SQL quotes require special handling.

Terminal
CREATE DATABASE lamp_app CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'lamp_user'@'localhost' IDENTIFIED BY 'replace-with-a-unique-long-password';
GRANT ALL PRIVILEGES ON lamp_app.* TO 'lamp_user'@'localhost';
EXIT;
3Check the account

Enter the password when prompted. A successful connection shows the MySQL prompt. Type EXIT; to leave.

Terminal
mysql -u lamp_user -p lamp_app

The grant gives this account privileges on lamp_app only, not on every database or the whole MySQL server. Use localhost, lamp_app and lamp_user in your application’s database settings, along with the password you chose. Follow the application’s own requirements for PHP extensions and database permissions.

Test PHP and MySQL Together

This checks that PHP can authenticate to the database, not just that its extension is installed. Run it on the server and enter the application user’s password when prompted. The password is not placed in the shell command history. Replace the example database and user if you chose different names.

Terminal
IFS= read -r -s -p 'Database password: ' LAMP_PASSWORD; printf '\n'
printf '%s\n' "$LAMP_PASSWORD" | php -r '$p = rtrim(fgets(STDIN), "\r\n"); try { $db = new mysqli("localhost", "lamp_user", $p, "lamp_app"); echo "Connection successful.\n"; } catch (Throwable $e) { fwrite(STDERR, "Connection failed. Check the user, password and database.\n"); exit(1); }'
unset LAMP_PASSWORD

The password is hidden while you type and is not placed in the command history. This checks PHP from the terminal. The earlier browser test checks PHP through Apache; neither test proves that your web application has its database settings correct. Configure and test the application separately.

Secure and Maintain Your Server

The steps above establish a working stack, not a complete production deployment. Keep Ubuntu and your web apps updated, back up both database and website files, and use HTTPS before sending passwords or other private data over a network.

Check your firewall before changing it:

Terminal
sudo ufw status verbose

If UFW is active and you want other machines to reach the website over HTTP, allow port 80. For HTTPS, allow 443 after configuring a certificate and Apache for TLS. A cloud provider may also have a separate firewall. Do not open MySQL port 3306 to the internet for this local setup.

Terminal
sudo ufw allow 80/tcp

Remote server warning: Before enabling UFW, allow the actual TCP port used by your SSH connection and verify the rule. The example below uses port 22 only; replace 22 with your actual SSH port if it differs. Enabling UFW without the right rule can disconnect you. Keep a provider console available for recovery.

Terminal
sudo ufw allow 22/tcp
sudo ufw status numbered

Check that the rule allows your actual SSH port before continuing. Then enable UFW and check its status:

Terminal
sudo ufw enable
sudo ufw status verbose

For database administration, keep the default local root socket authentication unless you have a specific reason to change it. Review MySQL accounts and remove any test accounts or databases you do not need. Avoid exposing temporary PHP test files.

Troubleshoot LAMP

Issue 1: Apache or MySQL is not running

Check each service and inspect its recent messages. If Apache cannot start because port 80 is already in use, run the port check below after the service commands.

Terminal
sudo systemctl status apache2 --no-pager
sudo journalctl -u apache2 -n 50 --no-pager
sudo systemctl status mysql --no-pager
sudo journalctl -u mysql -n 50 --no-pager

Check which process is listening on port 80:

sudo ss -ltnp '( sport = :80 )'

A LISTEN row can show the process using the port. If the output contains only the header, no TCP listener was found at that moment. Identify the service before changing it: stopping it could take another website or application offline.

Issue 2: The page works locally but not remotely

Check the server address, UFW rules, provider firewall and network routing. A private address is not reachable directly from the public internet. If Apache does not work locally either, check its service status first.

Issue 3: The browser shows PHP code or downloads the file

Remove the test file immediately, especially on a public server. PHP is not being executed by the active site. Check that the Apache PHP module is installed and enabled, and review the Apache configuration before testing again.

Terminal
sudo rm /var/www/html/lamp-check.php
sudo apache2ctl configtest
sudo systemctl status apache2 --no-pager
Issue 4: MySQL says access denied

Check the database name, user, password and host. The example account is limited to localhost. Use sudo mysql for local administration; it is a different login from the application account.

Issue 5: APT reports interrupted configuration

If an install was interrupted, finish configuring unpacked packages, then repair unresolved dependencies. Read any errors before repeating the original install command.

Terminal
sudo dpkg --configure -a
sudo apt --fix-broken install

Remove the LAMP Packages

Removing the stack can break other sites and applications. Back up your databases, configuration and website files first. This command removes the named packages but does not promise to remove every dependency or all database and site data. Review the proposed changes before confirming.

Terminal
sudo apt remove apache2 mysql-server php libapache2-mod-php php-mysql php-cli

Do not run purge or autoremove blindly on a shared server. Check what would be removed and keep backups before deleting any remaining configuration or data.

Helpful Resources

Read the official Ubuntu guides for Apache, MySQL and PHP.

Frequently Asked Questions

What does LAMP stand for?

LAMP stands for Linux, Apache, MySQL and PHP. Together they provide a common foundation for database-backed websites.

Does this work on Ubuntu Desktop as well as Server?

Yes. The packages and terminal commands work on both, provided the system has the required repositories and sudo access.

Which PHP version will I get?

The default PHP version depends on your Ubuntu release and configured repositories. Run php -v to check your installed version, and verify that your application supports it.

Do I need to set a MySQL root password?

Not for the default local Ubuntu setup. You can normally administer MySQL with sudo mysql and create a separate password-protected account for each application.

Why can I see Apache locally but not from another computer?

Check the server IP address, UFW rules, any cloud firewall and network routing. Local success does not mean incoming connections are allowed.

Is LAMP ready for a public website after installation?

The stack can run locally, but a public site also needs appropriate HTTPS, security updates, backups, application configuration and access controls.

Related: How to Optimize Apache | Ubuntu Server Manual | Back Up Files on Ubuntu