A LAMP server runs dynamic websites and web applications by pairing Linux with the Apache web server, the MySQL database and the PHP language. On Ubuntu, installing all three from the standard package archive gives a working stack that serves PHP pages as soon as the packages finish.

The libapache2-mod-php package runs PHP inside Apache, and php-mysql gives PHP its MySQL drivers, so the two need no manual wiring. Each Ubuntu release carries its own versions of all three, while the package names stay the same.

Apache listens on port 80 on every network interface once it starts, so other machines can reach it unless a firewall blocks them. MySQL accepts local connections only, and its root account has no password because access depends on sudo, so applications need their own database user.

Steps to install Apache, MySQL and PHP on Ubuntu:

  1. Update the apt package index.
    $ sudo apt update
  2. Install Apache, MySQL and PHP with the Apache module and the MySQL driver for PHP.
    $ sudo apt install --assume-yes apache2 mysql-server php libapache2-mod-php php-mysql
    ##### snipped #####
    Installing:
      apache2  libapache2-mod-php  mysql-server  php  php-mysql
    ##### snipped #####
    Setting up mysql-server (8.4.11-0ubuntu0.26.04.1) ...
    ##### snipped #####
    Setting up apache2 (2.4.66-2ubuntu2.6) ...
    ##### snipped #####
    Setting up php (2:8.5+99ubuntu1) ...
    ##### snipped #####

    Ubuntu 26.04 LTS installs Apache 2.4, MySQL 8.4 and PHP 8.5.

    Older instructions use the lamp-server^ task instead. On Ubuntu 26.04 LTS, apt was still resolving that task after ten minutes, while this package list covers everything in it and resolves in seconds.

  3. Confirm that Apache and MySQL are running.
    $ systemctl is-active apache2 mysql
    active
    active

    The packages start both services and enable them at boot, so no separate start command is needed.

  4. Run a query as the MySQL root user to confirm that the database accepts logins.
    $ sudo mysql --execute="SELECT VERSION();"
    +-------------------------+
    | VERSION()               |
    +-------------------------+
    | 8.4.11-0ubuntu0.26.04.1 |
    +-------------------------+

    The root account uses auth_socket, so sudo is what lets you in. Running mysql --user=root without sudo returns ERROR 1698 (28000): Access denied, which is expected.

  5. Create a file named phpinfo.php in the Apache web root using your text editor.
    $ sudoedit /var/www/html/phpinfo.php
  6. Enter the following PHP code.
    phpinfo.php
    <?php
    phpinfo();
  7. Exit the editor with your changes saved so sudoedit writes the file to the web root.
  8. Open http://127.0.0.1/phpinfo.php in a web browser.

    On a server with no desktop, open http://<server-ip>/phpinfo.php from another computer instead. If UFW is active on the server, sudo ufw allow Apache lets that computer reach port 80.

  9. Confirm that the page shows the PHP version and Apache 2 Handler as the Server API.

    The phpinfo.php report appears only when Apache hands the file to PHP to run; otherwise the browser shows the raw code or offers a download. The 20-mysqli.ini entry under Additional .ini files parsed shows that PHP has loaded its MySQL driver.

  10. Delete the test file so its server configuration details are not left open to visitors.
    $ sudo rm /var/www/html/phpinfo.php
Share this guide