DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon 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
VGSources
Blog

How to Uninstall a LAMP Stack From Ubuntu Without Losing Your Data

A careful Ubuntu LAMP uninstall starts with package inventory and verified backups, then uses simulated APT transactions, deliberate remove-versus-purge choices, cautious autoremove, and post-removal checks.
Length10 min Posted Quest giverVGSources Team
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To uninstall a LAMP stack safely, first identify the installed Apache, PHP, and MySQL or MariaDB packages, export databases, copy your website and custom configuration, simulate the APT transaction, and only then remove or purge the packages. Package removal and deletion of application data are separate decisions: apt remove normally leaves package configuration, while apt purge removes that configuration too. Neither command is a substitute for deciding what happens to databases, uploads, TLS keys, or files under /var/www/.

The commands below apply to supported Ubuntu releases using APT. Package names vary by release and by whether the database is MySQL or MariaDB, so do not copy a broad removal command until the inventory step shows what is actually installed.

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.

What a LAMP uninstall actually removes

LAMP stands for Linux, Apache HTTP Server, MySQL or MariaDB, and PHP. Linux is the operating system, not a package you remove as part of this procedure. The uninstall decision concerns the other three layers and any optional tools installed with them, such as PHP modules, database clients, or phpMyAdmin.

Action What it normally does When to choose it
apt remove Removes package files while normally retaining that package’s configuration files. You may reinstall the software and want customized settings available for recovery.
apt purge Removes package files and the package configuration files managed by APT. You want a clean package-level slate and have copied any settings you need.
apt autoremove Removes packages APT considers automatically installed and no longer required. Only after reviewing a simulation; the proposed list can include software unrelated to the web stack.
dpkg -r Directly removes a package without resolving the dependency graph. Not the normal workflow. Canonical warns that direct dpkg removal can leave dependent packages broken.

Configuration files and user-created data are different categories. A purge can remove Apache, PHP, or database configuration owned by packages, but it does not automatically make a considered decision about every website file, upload, virtual-host directory, database dump, or manually created key on the server. Treat those items as data that must be backed up and reviewed independently.

1. Inventory the Ubuntu release and installed components

Run the inventory before stopping services or changing packages. The first command records the release; the second lists installed (ii) and residual-configuration (rc) entries for common LAMP names; the third shows packages that APT considers manually installed.

. /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 accepts package-name and regular-expression matching. An ii line means the package is installed. An rc line means the package itself is removed but its configuration remains; that is useful evidence when deciding whether a later purge is appropriate. A blank result does not prove that no web stack exists: a service might have been installed manually, under a versioned package name, in a container, or from a source build.

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.

Recognize versioned PHP packages

Current Ubuntu releases can install a default php metapackage alongside versioned packages such as php8.3 and modules such as php8.3-mysql. Include only names shown by the inventory. Removing the unversioned metapackage alone may leave the versioned interpreter and modules in place.

Distinguish MySQL from MariaDB

MySQL and MariaDB are alternative database families for this purpose. Remove the family that is installed; do not assume both exist. Look for mysql-server, mysql-client, and mysql-common, or their mariadb-* equivalents. A client library used by another application may remain even after the server is removed.

2. Back up databases, sites, and custom configuration

Do this before any purge. Keep the backup on a different disk or host when possible, and test that you can read the files. A package transaction can be reversed more easily than an accidentally deleted database or private key.

Export every database

Use the dump utility provided by the installed database family and credentials that can read all databases. For MySQL, an example full logical export is:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
sudo mysqldump --all-databases --single-transaction --routines --events --triggers > ~/mysql-all-databases.sql

For MariaDB, use the corresponding mariadb-dump utility when it is installed:

sudo mariadb-dump --all-databases --single-transaction --routines --events --triggers > ~/mariadb-all-databases.sql

These commands create logical SQL backups; verify that the output is non-empty and readable, and protect it because it can contain credentials or personal data. If a dump reports permission or locking errors, resolve those before uninstalling. For a large or write-heavy production database, schedule a maintenance window and use the database vendor’s documented backup and restore procedure rather than assuming a quick dump is sufficient.

Copy website files and web-server configuration

Inspect the document root, virtual hosts, rewrite rules, certificates, and PHP settings. Typical locations include:

  • /var/www/ for site code, uploads, and generated assets;
  • /etc/apache2/ for Apache sites, modules, ports, and enabled configuration;
  • /etc/php/ for PHP version and module configuration;
  • the database data directory reported by the installed database package.

For example, archive the web and configuration trees while preserving ownership and permissions:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
sudo tar --xattrs --acls -czf ~/lamp-files-and-config-$(date +%F).tar.gz 
  /var/www /etc/apache2 /etc/php

Do not recursively delete these directories merely because they are common LAMP paths. Another application may use them, and a virtual host may point outside /var/www. List contents, identify owners, and record any bind mounts or symlinks first. Keep the backup until the replacement service is running and the sites and databases have been restored successfully.

3. Stop services and inspect dependencies

Stop whichever database and web services exist so files are not changing during the transaction. The || true suffix lets the sequence continue when a particular service name is absent.

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

Check the output of systemctl status if a stop fails. A service can have a different unit name, be supervised by another process manager, or have been installed outside APT. Also inspect what depends on the packages before removing them. An application may require PHP modules, an Apache module, or a database client even if it does not look like a traditional LAMP site.

Check for listening services

Before removal, this command helps reveal listeners that are not explained by the package list:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
ss -ltnp | grep -E ':80|:443|:3306' || true

Port 80 or 443 usually indicates an HTTP or HTTPS service; port 3306 commonly indicates MySQL-compatible networking. A listener is evidence that something is still running, not proof of which package supplied it.

4. Simulate the package transaction

Ubuntu’s default APT behavior installs recommended dependencies, and dependency cleanup can affect more than the package named on the command line. Always preview a broad operation and read every proposed removal.

Broad preview for a complete stack removal

The following simulation is intentionally broad. Replace the names with the packages found in your inventory, and add versioned PHP packages or phpmyadmin only when they are installed and genuinely part of the stack:

apt-get -s purge apache2 'php*' 'libapache2-mod-php*' 
  mysql-server mysql-client mysql-common 
  mariadb-server mariadb-client mariadb-common

The quoted wildcards are passed to APT rather than expanded by the shell. They are convenient but can match a PHP module used by another application. If the simulated list includes software you still need, narrow the command to explicit package names and simulate again.

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

Minimal explicit removal

For a machine whose inventory shows Apache, the PHP metapackage and Apache PHP module, and MySQL, a narrower purge looks like this:

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

Use mariadb-server mariadb-client in place of the MySQL server and client when MariaDB is installed. Include additional versioned PHP packages only after confirming them. APT accepts multiple package names in one transaction, so you can review one coherent proposed change instead of removing components blindly one at a time.

5. Choose remove or purge deliberately

Use remove when configuration may be needed again

sudo apt remove ... removes the packaged software while normally retaining customized package configuration. This is the recovery-friendly choice when you are testing another web server, troubleshooting a broken installation, or expect to reinstall the same components.

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

Retained configuration is not a backup of your website or database. It is only a reason to avoid re-creating package settings from memory.

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

Use purge for a clean package slate

sudo apt purge ..., or adding --purge to an APT removal command, also removes package configuration files. Canonical’s Ubuntu Server documentation describes this as the option that removes package configuration; Ubuntu security guidance explains that ordinary removal keeps configuration as a data-loss avoidance measure because administrators may have customized it.

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

Purging can erase customized Apache virtual-host settings, PHP settings, or database configuration that you did not copy. Review your archive before confirming the interactive prompt.

6. Clean orphaned dependencies only after review

Once the explicit packages are gone, APT may identify dependencies that were installed automatically and are no longer needed. Simulate the cleanup first:

apt-get -s autoremove --purge

Read the complete list. Reject the transaction if it contains a library, interpreter, client, monitoring agent, or other package required by an application you are keeping. If the list is correct, run:

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

APT retains packages marked manually installed. If a dependency is important to another workload, mark it manually before cleanup with sudo apt-mark manual package-name, then simulate again. Do not treat the absence of a package from the original purge command as proof that it is safe to autoremove.

7. Decide what happens to remaining data

Package cleanup and a full wipe are separate stages. After the packages are removed, inspect:

  • site code, uploads, and logs under /var/www/;
  • virtual-host files and TLS certificates under or referenced by /etc/apache2/;
  • PHP configuration under /etc/php/;
  • the database data directory and any socket, dump, or backup locations;
  • cron jobs, systemd units, environment files, and deployment scripts that refer to the stack.

If you are migrating to another server, retain these items and restore them according to the replacement service’s ownership and permission requirements. If the goal is permanent destruction, confirm the backup, the retention policy, and that no unrelated application uses the directory before deleting anything. Never use an unreviewed command such as rm -rf /var/www or a database-data-directory deletion as a shortcut for package removal.

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

8. Verify that the stack is gone

Verification should cover package state, residual configuration, service health, network listeners, and the transaction log.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
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

Interpret the results

  • No matching ii entries means those packages are not installed according to dpkg. Remaining rc entries indicate package configuration still exists and can be purged if you no longer need it.
  • apt list '?config-files' can expose residual configuration even when the main package is gone.
  • systemctl --failed shows failed units, but a clean result does not prove that a manually installed process is absent.
  • A listener on ports 80, 443, or 3306 means some process remains. Identify it with the process information shown by ss and investigate its installation source.
  • /var/log/dpkg.log records package actions. The final lines provide an audit trail for what was removed or purged and when.

Common failure modes and recovery

APT proposes removal of an unrelated application

Cancel the prompt. Re-run the command with explicit package names, inspect reverse dependencies, and avoid autoremove until the dependency is marked manual or the consuming application is intentionally retired.

A package name cannot be found

Do not substitute a guessed name. Package names differ between Ubuntu releases and database families. Re-run the inventory, use the exact installed names, and remove only packages that APT reports as installed.

PHP is still present after removing php

The default metapackage may have been removed while versioned interpreters or modules remain. Search the complete dpkg -l output for php, identify which packages another application needs, and simulate a targeted removal.

Apache still answers requests

Check the listener with ss -ltnp, inspect the owning process, and check for a differently named service or manual installation. Stopping apache2 cannot stop a separate web server.

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.

The database will not dump

Keep the service running, correct the credentials or privileges, and retry the export. Do not purge the server while the only copy of the data is inaccessible. For a production database, take a verified backup through the database administrator’s established process.

Residual rc entries remain

Those entries represent removed packages whose configuration files remain. If you have archived the settings and no longer need them, purge the exact package names and verify again. Residual entries alone do not indicate that the service is running.

When a partial uninstall is the better choice

You do not have to remove the entire stack. Removing Apache may make sense when another web server will serve the site. Removing PHP may be appropriate when the machine will host static files or a different runtime. Removing only the database server can preserve Apache and PHP for an application that will connect to a remote database. In each case, keep client libraries and modules required by software that remains, and simulate the smaller transaction before confirming it.

Final checklist

  • Recorded the Ubuntu release and exact installed package names.
  • Determined whether the database is MySQL or MariaDB.
  • Exported databases and verified the dump files.
  • Copied website files, virtual-host settings, PHP settings, certificates, and other custom configuration.
  • Stopped the services that actually exist and checked for listeners.
  • Simulated the purge or removal and rejected unrelated dependency changes.
  • Chose remove or purge based on whether package configuration must survive.
  • Simulated and reviewed autoremove --purge before running it.
  • Deleted user data only after a separate, explicit decision.
  • Verified package states, failed units, listening ports, and /var/log/dpkg.log.

Following this order gives you a package-level uninstall without confusing it with data destruction. Your databases and website files survive as long as you preserve them deliberately and avoid deleting their storage paths until the migration or replacement has been verified.

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

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 quests from Patch Notes

  1. How to Set Up a RedM (RDR2) Server in 2026: License Key, txAdmin and server.cfg, Step by StepBlog11min
  2. Best RedM (RDR2) Server Hosting in 2026: Comparing Four Hosts on Slots, Memory and PriceBlog10min
  3. How to Host a Mindustry Server in 2026: server-release.jar, Port 6567 and the Commands That MatterBlog7min
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Crashes, No Sound, or Screen Glitches?Free driver 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.