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.
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.
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.
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.
Create a directory for your site and a small PHP page. The page reveals no server configuration.
Create the Apache configuration:
Paste the following, save with Ctrl+O and Enter, then exit with Ctrl+X:
Enable the site and check the configuration. Continue to reload only if the test reports Syntax OK.
Test this virtual host locally without changing DNS. The expected response is Apache and PHP are working.
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.
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.
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:
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.
Check that the rule allows your actual SSH port before continuing. Then enable UFW and check its status:
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
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.
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.
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
