DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowHalloween DealsAmazon USHalloween week deals: useful picks before month-endAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run Scan×
Skip to content
World desk10 min

How to Uninstall a LAMP Stack From Ubuntu Safely

A careful Ubuntu LAMP uninstall starts with package and database identification, not a one-line purge. Back up databases and web files, preview APT’s transaction, choose remove or purge deliberately, review autoremove, and verify that no service or configuration remains unexpectedly.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To uninstall a LAMP stack, first identify which Apache, PHP, and MySQL-or-MariaDB packages are actually installed; export every database and copy the site and custom configuration; simulate the APT transaction; then stop services, remove or purge the selected packages, review orphan cleanup, and verify that no web or database service remains.

LAMP means Linux, Apache, MySQL or MariaDB, and PHP. Ubuntu package names differ by release and by database choice, so a command copied from another server can remove too little, fail to match anything, or affect software used by another application. Package removal is also separate from deleting your websites, uploads, database files, TLS keys, and other user-created data.

As an Amazon Associate I earn from qualifying purchases.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Decide what “uninstall LAMP” means on this machine

There are three different jobs people call an uninstall:

  • Stop the stack: leave packages and data installed but stop Apache and the database service.
  • Remove software: uninstall packaged programs while normally retaining their package configuration files.
  • Clean-slate removal: purge package configuration and, only as a separate deliberate action, remove application data that you have backed up and no longer need.

Do not assume that purging packages deletes every website or database. Files under /var/www/, database storage, virtual-host content, uploads, certificates, and hand-edited settings may be user data or may be retained for recovery. Conversely, a broad dependency cleanup can remove software that another application still needs. Treat package removal and data destruction as separate approvals.

Canonical’s Ubuntu security guidance, updated May 1, 2026, describes the default behavior this way: “By default, removal of a package will not delete its configuration files, as a data-loss avoidance strategy, as these may have been customised by you.” Ubuntu package-management guidance updated June 26, 2026, states that adding --purge to apt remove removes package configuration files as well.

Inventory the release and installed components

Run the inventory before stopping or deleting anything. The release string helps you interpret versioned package names, while the package queries distinguish installed packages (ii) from removed packages whose configuration remains (rc).

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
. /etc/os-release
printf '%sn' "$PRETTY_NAME"
dpkg -l | grep -E '^(ii|rc)  (apache2|php|libapache2-mod-php|mysql|mariadb)'
apt-mark showmanual | grep -E 'apache2|php|mysql|mariadb'

dpkg -l is an inventory, not a removal command. Ubuntu supports filtering its package listing by package name or regular expression. The apt-mark showmanual result shows packages explicitly marked as manually installed; this matters later when APT decides which dependencies are eligible for autoremove.

Map the names you find

Stack part Names you may see What to confirm
Web server apache2 and related Apache modules Whether Apache is the server you intend to remove, and whether another site depends on it.
PHP runtime php, versioned packages such as php8.x, and libapache2-mod-php The installed default metapackage, the actual versioned modules, and extensions used by other applications.
Database Either the MySQL family or the MariaDB family, including server, client, and common packages Which family is installed. Do not remove both families merely because both names appear in a generic command.
Optional tools For example, phpmyadmin if it appears in your inventory Whether it is part of this stack and safe to remove, rather than a tool still used elsewhere.

Current Ubuntu releases can install an unversioned php metapackage alongside versioned PHP modules. Package names can therefore differ even between two machines running the same broad Ubuntu release. Use the names returned by the inventory instead of guessing a version.

Back up databases, sites, and custom configuration

Make backups before any purge or autoremove. Keep them on storage that will survive the package transaction, and do not delete the originals until a replacement service has been installed and tested.

Export each database

List the databases through the client supplied by the installed MySQL-compatible server, then export the databases your applications require. A typical logical export uses the MySQL-compatible dump utility:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
mysqldump -u root -p --all-databases > all-databases.sql

On installations that provide MariaDB’s client tools, the equivalent utility may be named mariadb-dump:

mariadb-dump -u root -p --all-databases > all-databases.sql

Use the utility and authentication method configured on your server; a root account may authenticate through a local socket rather than a password. For a production system, verify that the dump completes without errors and that its file is readable by the account performing the backup. If you need per-database files, export each application database separately and record its database user, character-set expectations, and connection settings.

Copy website and configuration files

Inspect the usual locations before copying:

  • /var/www/ for document roots, uploads, and application files.
  • /etc/apache2/ for virtual hosts, enabled modules, and Apache settings.
  • /etc/php/ for PHP versions, module settings, and php.ini files.
  • The database data directory reported by the installed database package, rather than assuming a fixed path.

Preserve file ownership, permissions, hidden files, deployment scripts, scheduled jobs, TLS private keys, and environment files. A recursive copy into a backup location is only useful if that location has sufficient space and access controls for secrets. Do not recursively delete any of these paths until you have inspected their contents and confirmed that no unrelated application uses them.

Stop services and inspect dependencies

Stopping services prevents requests and database writes while packages are being changed. Service names vary, so the following attempts to stop each possible database service and ignores the error for a name that is not present.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
sudo systemctl stop apache2 2>/dev/null || true
sudo systemctl stop mysql 2>/dev/null || true
sudo systemctl stop mariadb 2>/dev/null || true
apt-cache depends apache2 php mysql-server mariadb-server 2>/dev/null

The dependency query is informative; it does not alter the system. Check application documentation, systemd units, container definitions, and deployment scripts for software that still expects Apache, PHP modules, a database client library, or a local database socket. If another application shares any of those packages, remove only the components that are genuinely obsolete.

Choose remove or purge

Use plain removal when you may reinstall the stack and want package configuration retained. Use purge when you intentionally want APT to remove the selected packages’ configuration files as well. Neither choice is permission to erase website or database data.

APT action Effect When it fits Main risk
apt remove Removes packaged software and normally keeps its package configuration files. You may reinstall, need to preserve customized settings, or want a reversible first step. Old configuration can remain and influence a later reinstall.
apt purge Removes the package and its package-managed configuration files. You want a clean package-level reset after recording settings. Customized package configuration is discarded; user data still needs a separate decision.
apt autoremove --purge Removes packages APT considers automatically installed and no longer required, purging their configuration. After reviewing a simulated transaction and confirming every listed package is disposable. It can include dependencies needed by another program if package markings or assumptions are wrong.

Ubuntu recommends using APT for dependency-aware removal. Canonical’s package-management documentation says, “Uninstalling packages using dpkg, is NOT recommended in most cases.” Direct dpkg -r removal does not resolve dependencies and can leave dependent packages broken.

Preview the transaction before confirming it

APT accepts multiple package names in one command. Preview the exact operation first, especially if you use wildcards. Quoted patterns prevent the shell from expanding them against filenames in your current directory.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
apt-get -s purge apache2 'php*' 'libapache2-mod-php*' 
  mysql-server mysql-client mysql-common 
  mariadb-server mariadb-client mariadb-common

This broad example is only a preview. Replace it with the packages shown by your inventory. Include versioned PHP packages or phpmyadmin only when they are installed and truly belong to the stack. Review every removal, including libraries and applications that are not named in your command. If the simulation proposes removing a package used by another service, stop and narrow the list.

A safer approach is to name only installed top-level packages. For a MySQL-family installation, the command might look like this:

sudo apt purge apache2 php libapache2-mod-php mysql-server mysql-client

For a MariaDB-family installation, substitute the MariaDB server and client names that your inventory returned. Do not include both database families unless your inspection proves that both are installed and neither is needed.

Remove the selected packages

Retain configuration with remove

If preserving package configuration is intentional, run the reviewed package list with remove:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
sudo apt remove apache2 php libapache2-mod-php mysql-server mysql-client

Use the MariaDB package names instead when MariaDB is installed. Read APT’s confirmation screen; never accept a transaction whose removal list differs from the reviewed simulation. A plain remove is often the better first pass on a server that may be restored.

Delete package configuration with purge

For a package-level clean slate, run the reviewed list with purge:

sudo apt purge apache2 php libapache2-mod-php mysql-server mysql-client

Purging removes package configuration files, including customized settings managed by those packages. Save virtual-host definitions, PHP settings, module choices, and database configuration before confirming. If you selected a versioned PHP module in the simulation, include that exact installed name rather than assuming the unversioned php metapackage covers it.

Handle automatically installed dependencies cautiously

After the explicit removal succeeds, simulate orphan cleanup:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
apt-get -s autoremove --purge

Read the complete proposed list. Ubuntu’s default APT behavior installs recommended dependencies, and packages marked as automatically installed can later become autoremove candidates when their original dependent package is gone. A package marked manually installed is retained. If the list contains a runtime, library, client, or tool that another application still needs, cancel the operation and mark that package manual before trying again:

sudo apt-mark manual package-name

Only after the simulation contains exclusively unwanted packages should you run:

sudo apt autoremove --purge

Run a new simulation after any change in package markings. Do not treat autoremove as a harmless finishing command; it is a second transaction with its own removal set.

Decide separately whether to delete leftover data

Package removal and data destruction are different decisions. Purging Apache does not tell you whether an application’s document root, uploads, certificates, or virtual-host content should disappear. Removing a database package does not by itself establish that every database file is gone or that deleting those files is safe.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For a clean reinstall, inspect the contents and ownership of each leftover directory, compare them with your backup, and confirm that no unrelated service references them. Keep the database dump and site backup until the replacement stack can serve the site and connect to its restored database. If the goal is only to change web servers or PHP versions, preserve the data and remove only the packages that block that change.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Verify that the stack is gone

Verification should cover package state, retained configuration, service health, listening ports, and the transaction log.

dpkg -l | grep -E 'apache2|php|mysql|mariadb' || true
apt list '?config-files' 2>/dev/null | grep -E 'apache2|php|mysql|mariadb' || true
systemctl --failed
ss -ltnp | grep -E ':80|:443|:3306' || true
grep -E ' (apache2|php|mysql|mariadb)' /var/log/dpkg.log | tail -n 50
Check How to interpret it Next action
Package listing No ii entries means those packages are not currently installed. An rc entry means the package is removed but configuration remains. Use the retained-configuration list to decide whether those files should be purged.
APT config-file query Entries identify package configuration files that survived a plain remove. Confirm they are not needed, then purge the corresponding packages if appropriate.
systemctl --failed Shows failed units, which may include a service left in a broken state rather than cleanly removed. Inspect the unit name and logs before deleting files or retrying APT.
Listening ports Ports 80 and 443 indicate an HTTP/HTTPS listener; 3306 commonly indicates a MySQL-compatible listener. Identify the owning process. It may come from a different package, container, source build, or manually installed service.
/var/log/dpkg.log Records the package removal transaction and helps establish what APT actually changed. Use the timestamped entries when diagnosing a partial or unexpected removal.

A port check is complementary to package inspection. A listener can remain when it was installed by another package, launched from a container, built from source, or started by a manually created systemd unit.

Troubleshoot common failure cases

The simulation wants to remove too much

Cancel the command. Identify which proposed package another application needs, then rerun the simulation with a narrower explicit list. Check manual-install markings and dependency relationships with apt-cache depends. Do not approve a large wildcard purge merely because it includes the packages you recognize.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

APT reports interrupted configuration

Do not start deleting package directories manually. First inspect the error output and the recent entries in /var/log/dpkg.log. Resolve the reported package-state problem using APT’s normal recovery path, then rerun the simulation so you know what transaction will occur. Directly removing files can leave the package database inconsistent.

Configuration files remain after remove

This is expected for plain removal. The rc state and apt list '?config-files' query show retained package configuration. If you have copied any settings you need and genuinely want them gone, purge the affected package; otherwise retain them for a possible reinstall.

Apache or the database still listens

Use ss -ltnp to identify the process owning ports 80, 443, or 3306. Check the process’s package, systemd unit, container runtime, or source-install location. Removing the LAMP packages cannot stop a separately installed web server or database.

A reinstall behaves differently

Review whether the previous operation was remove or purge. Retained configuration can restore old module settings, virtual hosts, or PHP defaults. If you need a clean package configuration, back up the retained files and purge deliberately; if you need the old settings, keep them and restore data separately.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

A safe decision checklist

  • Record the Ubuntu release and the exact installed Apache, PHP, and database packages.
  • Confirm whether the database is MySQL-family or MariaDB-family; do not assume both.
  • Export databases and verify the dump files.
  • Copy /var/www/, Apache and PHP configuration, certificates, and other application-specific files.
  • Stop whichever Apache and database services actually exist.
  • Simulate the explicit removal and inspect every package APT proposes to change.
  • Choose remove for retained package configuration or purge for a package-level clean slate.
  • Simulate autoremove --purge separately and reject unrelated removals.
  • Delete user data only after a backup, content review, and explicit clean-wipe decision.
  • Verify package states, retained configuration, failed units, listening ports, and /var/log/dpkg.log.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.