LAMP server stack
The abbreviation LAMP stands for Linux, the web server Apache, the database MySQL (or MariaDB) and the scripting language PHP (or Python, Perl).
In contrast to Debian, Ubuntu continues to rely on Oracle’s open source edition of the MySQL server instead of using the MariaDB fork.
Thanks to the magic of the Debian/Ubuntu package manager Apt, a short command line is enough to quickly set up a complete web server stack with PHP as Apache module and an extension for the database server. Apt will automatically install all additionally required packages (including the base package php-common and some common extensions) and ask for a root password for MySQL. A local web server can then be accessed via a web browser at http://localhost.
However, in production one might prefer to install PHP-FPM instead of the PHP module, which performs better by reducing the load on the web server. In this case, a one-line command is not quite enough; the proxy module and the configuration file for PHP-FPM must also be provided to complete the installation.
Installation
Here is how to install the LAMP stack with PHP as Apache module:
sudo apt install php-mysql libapache2-mod-php mysql-server
The more performant variante with PHP-FPM:
sudo apt install php-fpm php-mysql apache2 mysql-server sudo a2enmod proxy_fcgi sudo a2enconf php-fpm systemctl reload apache2
Installing MariaDb for MySQL
Debian installs MariaDB even if the admin types apt install mysql-server
into the console. However, if you want to do without MySQL in Ubuntu, install the package mariadb-server instead of mysql-server.
Installing an older or newer PHP version
If you want to install a PHP version other than the one provided by the distribution maintainers, you can rely on the repositories of developer Ondrej Sury. For Ubuntu, Sury provides a PPA, which can be integrated as follows:
sudo add-apt-repository ppa:ondrej/php sudo apt update
After successful installation of the PPA, you can install other PHP versions by adding the version number to the package names. In the following example, we force PHP 8.2 to replace the PHP version installed by default:
sudo apt install php8.2-fpm php8.2-mysql sudo a2disconf php-fpm sudo a2enconf php8.2-fpm systemctl stop php-fpm systemctl stop php-fpm && systemctl disable php-fpm
First, PHP 8.2 with FPM and the MySQL extension for the new PHP version are installed (this pulls dependencies like php8.2-common; you can install other PHP extensions using the php8.2-extensionname pattern); next, the Apache configuration for the default install of PHP FPM is disabled and the new configuration for PHP 8.2 enabled. Finally, Apache is reloaded to read the new configuration and make it work. It may be necessary to stop old PHP-FPM processes and disable the service so that it won’t be restartet when rebooting.