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

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

To uninstall a LAMP stack, first identify the Apache, PHP, and MySQL-or-MariaDB packages actually installed, back up databases and website files, preview the APT transaction, and then remove or purge only the packages you no longer need. APT removes software; it does not automatically make the separate decision to erase your databases, uploads, TLS keys, or web-root contents.

What uninstalling a LAMP stack actually means

LAMP is a combination of Linux, Apache HTTP Server, MySQL or MariaDB, and PHP. On Ubuntu, Linux is the operating system itself, so a normal LAMP removal removes the server applications and their packages—not Ubuntu. Apache, the database server, PHP runtimes, modules, and related clients may have been installed at different times and may have different package names.

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

There are two separate jobs:

  • Package removal: uninstall the software managed by APT and optionally remove its package configuration.
  • Application-data cleanup: decide whether to retain or delete databases, site files, uploads, certificates, logs, and custom configuration.

Keeping those decisions separate prevents the most common mistake: assuming that apt purge is a complete wipe of every file created by a website or database administrator. Conversely, manually deleting directories before making a backup can destroy data that a replacement server still needs.

Before you remove anything: inventory the installation

Do not begin with a copied command that assumes a particular Ubuntu release or database. Ubuntu may provide an unversioned php metapackage alongside versioned PHP modules, and a machine normally has either the MySQL family or the MariaDB family rather than both. A package may also have been installed for another application that is not part of the website you intend to retire.

Record the Ubuntu release

. /etc/os-release
printf '%sn' "$PRETTY_NAME"

The release affects the default PHP version, package dependencies, service behavior, and package names. Save this output with your change notes, especially if you are preparing a rebuild on another Ubuntu release.

List installed and residual LAMP packages

dpkg -l | grep -E '^(ii|rc)  (apache2|php|libapache2-mod-php|mysql|mariadb)'
apt-mark showmanual | grep -E 'apache2|php|mysql|mariadb'

In dpkg -l output, ii means the package is installed. rc identifies a package whose software has been removed while its configuration files remain. The second command shows packages marked as manually installed; that distinction matters when you later consider autoremove.

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

Read the complete package names from the output. You may see entries such as:

  • apache2, apache2-bin, apache2-data, and enabled Apache modules;
  • php, a versioned package such as php8.x, libapache2-mod-php, and extensions such as database, XML, JSON, or GD modules;
  • MySQL packages such as mysql-server, mysql-client, and mysql-common;
  • or MariaDB packages such as mariadb-server, mariadb-client, and mariadb-common.

Do not treat a wildcard result as proof that every matching package belongs to this stack. A PHP module can be used by a command-line application, a monitoring tool, or another virtual host.

Check services and listening ports

systemctl status apache2 --no-pager
systemctl status mysql --no-pager
systemctl status mariadb --no-pager
ss -ltnp | grep -E ':80|:443|:3306' || true

Some status commands will report that a unit does not exist; that is useful information, not an error requiring repair. Port 80 or 443 can be occupied by a different web server, container, or manually installed binary. Port 3306 can likewise belong to a database installed outside the package set you are about to remove.

Back up databases, websites, and custom settings

Make a recoverable backup before stopping or purging services. Package configuration and user-created data have different lifecycles, and neither should be assumed to contain the other.

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

Export every database you need

Use the database tools available on the machine to create logical dumps of application databases, users, and—where appropriate—stored routines or events. Record the database engine and the dump command options used. A dump that can be imported into a replacement server is more useful than a directory copy whose files depend on a particular engine version.

Verify that the dump files are readable and stored outside any directory you plan to remove. Protect them because they can contain credentials, personal information, and session data. If the site is still receiving writes, put it into maintenance mode or schedule a short outage so the export represents a consistent application state.

Copy the web document root and uploads

Inspect common locations such as /var/www/, but do not assume every virtual host uses the default directory. Apache virtual-host definitions identify each DocumentRoot; copy the files, ownership, permissions, symbolic links, and hidden files that the application needs. Include user-uploaded media, generated assets, deployment files, and any local environment files that are not stored in version control.

Save Apache, PHP, and TLS configuration

Review and copy the relevant parts of:

  • /etc/apache2/, including virtual hosts, enabled sites, modules, rewrite rules, and proxy settings;
  • /etc/php/, including the PHP version directories and per-SAPI settings;
  • TLS certificate and key locations, if HTTPS is configured;
  • scheduled jobs, service overrides, deployment scripts, and firewall rules that your application depends on.

Do not publish private keys or database dumps in a publicly readable backup location. Preserve ownership information and note which files were generated by a package versus created by an administrator.

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

Inspect the database data directory without deleting it

The database package reports or documents its data directory, which is often under /var/lib/. The exact path and contents depend on the engine and version. Treat it as live database storage, not as a disposable cache. A raw copy made while the server is running may be inconsistent; prefer a logical export, or follow the database engine’s documented cold-backup procedure after a clean shutdown.

Stop the services and check dependencies

Stopping services prevents new requests and reduces the chance that files change while APT is processing the transaction. Stop whichever units exist:

sudo systemctl stop apache2 2>/dev/null || true
sudo systemctl stop mysql 2>/dev/null || true
sudo systemctl stop mariadb 2>/dev/null || true

Stopping a unit does not erase its enablement, package, or data. It simply makes the removal window predictable. If another service needs Apache, PHP, or a database client library, identify that dependency before proceeding.

Inspect package relationships

apt-cache depends apache2 php mysql-server mariadb-server 2>/dev/null

This displays declared relationships for the names supplied. It is not a complete dependency graph for every installed package, so also read the transaction preview in the next step. Pay particular attention to PHP modules, database client libraries, and packages used by monitoring, control-panel, or deployment software.

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

Choose remove or purge

APT action What it does When it fits Main caution
apt remove Removes the packaged software while normally retaining package configuration files. You may reinstall the same package and want customized settings available for recovery. Residual configuration can remain visible as rc package state.
apt purge Removes the package and its package-managed configuration files. You want a clean package-level reinstall or no longer need the service configuration. Customized Apache or PHP package configuration can be lost; save it first.
apt autoremove --purge Removes automatically installed dependencies that APT now considers unnecessary, and purges their configuration. After the explicit removal, when you have reviewed every proposed dependency removal. It can remove software that another task still needs if it was incorrectly or no longer marked manual.

Ubuntu’s package-management guidance describes retaining configuration after a normal removal as a data-loss-avoidance measure. Adding --purge removes package configuration as well. Neither operation is a substitute for deciding what happens to user-created web and database data.

Preview and remove the explicit stack packages

Always simulate first. The simulation produces APT’s proposed transaction without changing the system, allowing you to catch an unexpectedly broad removal.

Broad preview for a machine whose inventory is understood

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

The quoted PHP wildcards are convenient for a complete PHP-family review, but they may match packages used outside the website. Supplying both MySQL and MariaDB names is also intentionally broad: remove the family that the inventory shows, not both by habit. Narrow the command before confirming if the simulation proposes unrelated packages.

Minimal removal using the installed top-level names

For a conventional Apache, PHP, and MySQL installation, a targeted command might be:

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

For MariaDB, substitute mariadb-server and mariadb-client. Add a versioned PHP package or phpmyadmin only when the inventory confirms it is installed and it is genuinely part of the stack being retired. APT will show the final package list and ask for confirmation; read it rather than accepting automatically.

Retain configuration instead

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

Use the MariaDB names when MariaDB is installed. This option is useful when you are testing a replacement or expect to reinstall the same package and want its configuration files retained. It does not keep the running service, and it does not guarantee that every dependency remains installed.

Why not make dpkg -r the primary workflow?

Direct dpkg removal does not resolve dependencies in the way APT does. Removing a package with it can leave dependent packages installed but broken. Use APT for the transaction, and use dpkg -l afterward to inspect package state. If a package was installed manually outside APT, document that fact and remove it using its own installation method rather than guessing.

Clean orphaned dependencies cautiously

apt-get -s autoremove --purge

Read the simulation line by line. Ubuntu marks many dependencies as automatically installed because another package requested them. Once that parent package is gone, APT may propose them for removal. A package marked manually installed is normally retained, even if it originally arrived as a dependency.

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.

Only after confirming that the list contains no required runtime, client library, backup utility, or unrelated application should you run:

sudo apt autoremove --purge

If the proposed list is too broad, cancel the operation and mark a required package as manual with sudo apt-mark manual package-name. Then simulate again. Do not use autoremove as a substitute for understanding the explicit package list.

Decide whether to delete data and leftover directories

A package purge can remove package-owned configuration, but it is not a universal “factory reset.” Website files, uploads, certificates, database dumps, and administrator-created configuration may remain. That is a safety feature: deleting a package should not silently destroy a site’s content.

If you intend to keep the data, move it to a protected backup or replacement server and leave the original directories untouched until the new service is working. If you intend a full wipe, inspect each path and confirm its ownership before deleting it. Typical paths to review include:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  • /var/www/ for document roots and uploaded content;
  • /etc/apache2/ and /etc/php/ for retained or custom settings;
  • the database data directory reported by the installed engine;
  • certificate, key, log, spool, and scheduled-job locations used by the application.

Do not recursively remove an entire parent directory merely because it has a familiar name. The same directory can contain data for multiple virtual hosts or services. Confirm your backups, stop the relevant service, inspect contents with ownership and timestamps, and obtain a second review for production systems before irreversible deletion.

Verify that the LAMP packages and services are gone

Verification should prove both what APT removed and what is still listening on the host.

Inspect package state and residual configuration

dpkg -l | grep -E 'apache2|php|mysql|mariadb' || true
apt list '?config-files' 2>/dev/null | grep -E 'apache2|php|mysql|mariadb' || true

The first command can still show packages that belong to another application, so interpret each line rather than demanding an empty result. The second looks for package configuration files retained after removal. Residual entries are not proof that a service is running.

Check failed units and listening ports

systemctl --failed
ss -ltnp | grep -E ':80|:443|:3306' || true

No expected Apache or database listener should remain if those services were the only owners of the ports. If a port is still open, identify the process shown by ss; it may be a container, a different web server, a manually installed binary, or a service with a different unit name.

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.

Review the package transaction log

grep -E ' (apache2|php|mysql|mariadb)' /var/log/dpkg.log | tail -n 50

APT and dpkg record package actions in /var/log/dpkg.log. The log helps establish which packages were removed, purged, or left in a residual state and is useful when troubleshooting an interrupted transaction.

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

Troubleshoot common removal problems

APT proposes removal of an unrelated application

Cancel the transaction. Re-run the simulation with only the installed top-level packages you confirmed. Inspect reverse dependencies and mark any package that must remain as manually installed. Do not approve a large autoremove merely because the command completed without an error.

A package says it is not installed

That name may be a metapackage, a versioned variant, or the wrong database family. Return to dpkg -l and use the exact installed name. An absent mysql-server does not prove there is no database; MariaDB or a manually installed server may be present.

Apache or the database still answers requests

Check ss -ltnp and the process ID, then map that process to its service or package. Stop the correct unit and inspect whether the software was installed by a container runtime, source build, snap, or a separate repository. Removing Ubuntu packages cannot remove software that APT never managed.

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

Configuration files remain after removal

That is expected after apt remove. If you have a verified backup and want package-level configuration removed, purge the residual package names shown by dpkg -l. Review each file first; administrator customizations may be needed for migration.

The operation was interrupted

Do not delete directories to force completion. Read the APT and dpkg output, check the transaction log, and repair package configuration using the normal APT recovery procedure for your Ubuntu release. Once the package database is consistent, re-run a simulation before attempting another removal.

Removal checklist

  • Recorded the Ubuntu release and exact Apache, PHP, and database packages.
  • Confirmed whether the database is MySQL or MariaDB, rather than assuming.
  • Exported required databases and verified the dump files.
  • Copied document roots, uploads, virtual-host files, PHP settings, and TLS material.
  • Stopped whichever Apache and database services actually exist.
  • Simulated the explicit removal and reviewed every proposed package.
  • Chose remove or purge based on whether package configuration must survive.
  • Simulated autoremove --purge before accepting dependency cleanup.
  • Deleted user data only after a separate, intentional full-wipe decision.
  • Verified package state, failed units, listening ports, and /var/log/dpkg.log.

Frequently asked questions

Will apt purge delete my website files?

Not normally. Purge targets package-managed files and configuration. Website files, uploads, and other administrator-created data can remain, but you must inspect the actual paths rather than relying on that expectation.

Should I remove MySQL and MariaDB together?

No. Identify the installed database family and remove that family. Removing names for both engines makes the command broader than necessary and can create an avoidable data or dependency risk.

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

Do I need to uninstall PHP if another application uses it?

No. Remove PHP only when no remaining application requires the runtime or its modules. A shared PHP module or client library can be a dependency of software unrelated to the website you are retiring.

Is deleting /var/lib/mysql required?

No. It is a separate, irreversible data decision. Keep it when the databases may be needed, and delete it only after verified exports and a deliberate full-wipe review.

Why does dpkg -l still show an entry after uninstalling?

An rc entry means the package files are gone but configuration remains. Purging that package removes the retained package configuration, subject to your backup and review.

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.

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