The LAMP stack is a widely-used solution for web development, combining Linux, Apache, MySQL, and PHP. Each component serves a distinct purpose: Apache handles web requests, MySQL manages databases, and PHP processes server-side scripts. Installing this stack on Ubuntu is straightforward, thanks to the apt package manager, which simplifies the process.

Ubuntu offers a streamlined method to set up the LAMP stack using tasksel, which includes a predefined task called lamp-server. This task installs Apache, MySQL, and PHP with default configurations, ensuring compatibility and reducing manual configuration work. This approach minimizes potential issues and provides a reliable environment for hosting web applications.

Once the LAMP stack is installed, Apache serves web pages, MySQL manages database operations, and PHP generates dynamic content. This combination is effective for a wide range of web development needs, providing a stable and efficient platform for building and running web applications on Ubuntu.

Steps to install LAMP on Ubuntu:

  1. Launch the terminal.
  2. Update apt's package list.
    $ sudo apt update
    [sudo] password for user: 
    Hit:1 http://jp.archive.ubuntu.com/ubuntu focal InRelease
    Get:2 http://jp.archive.ubuntu.com/ubuntu focal-updates InRelease [111 kB]  
    Get:3 http://jp.archive.ubuntu.com/ubuntu focal-backports InRelease [98.3 kB]  
    Get:4 http://security.ubuntu.com/ubuntu focal-security InRelease [107 kB]
    Get:5 http://jp.archive.ubuntu.com/ubuntu focal-updates/main amd64 DEP-11 Metadata [196 kB]
    Get:6 http://jp.archive.ubuntu.com/ubuntu focal-updates/universe amd64 DEP-11 Metadata [177 kB]
    Get:7 http://jp.archive.ubuntu.com/ubuntu focal-updates/multiverse amd64 DEP-11 Metadata [2,468 B]
    Get:8 http://jp.archive.ubuntu.com/ubuntu focal-backports/universe amd64 DEP-11 Metadata [1,972 B]
    Get:9 http://security.ubuntu.com/ubuntu focal-security/main amd64 DEP-11 Metadata [21.2 kB]
    Get:10 http://security.ubuntu.com/ubuntu focal-security/universe amd64 DEP-11 Metadata [36.0 kB]
    Fetched 751 kB in 2s (308 kB/s)             
    Reading package lists... Done
    Building dependency tree       
    Reading state information... Done
    All packages are up to date.
  3. Install the LAMP stack using the tasksel utility.
    $ sudo apt install --assume-yes lamp-server^
    Reading package lists... Done
    Building dependency tree       
    Reading state information... Done
    Note, selecting 'libgssapi3-heimdal' for task 'lamp-server'
    Note, selecting 'libhttp-message-perl' for task 'lamp-server'
    Note, selecting 'php7.4-cli' for task 'lamp-server'
    Note, selecting 'libnghttp2-14' for task 'lamp-server'
    Note, selecting 'libencode-locale-perl' for task 'lamp-server'
    Note, selecting 'libwind0-heimdal' for task 'lamp-server'
    Note, selecting 'mecab-utils' for task 'lamp-server'
    Note, selecting 'libsasl2-modules-db' for task 'lamp-server'
    Note, selecting 'libcurl4' for task 'lamp-server'
    Note, selecting 'libldap-2.4-2' for task 'lamp-server'
    Note, selecting 'libapache2-mod-php' for task 'lamp-server'
    Note, selecting 'libssh-4' for task 'lamp-server'
    Note, selecting 'libevent-core-2.1-7' for task 'lamp-server'
    Note, selecting 'php-common' for task 'lamp-server'
    Note, selecting 'libaprutil1' for task 'lamp-server'
    Note, selecting 'libbrotli1' for task 'lamp-server'
    Note, selecting 'php7.4-json' for task 'lamp-server'
    Note, selecting 'mysql-client-8.0' for task 'lamp-server'
    Note, selecting 'libheimntlm0-heimdal' for task 'lamp-server'
    Note, selecting 'mysql-server' for task 'lamp-server'
    Note, selecting 'mysql-server-8.0' for task 'lamp-server'
    Note, selecting 'libcgi-fast-perl' for task 'lamp-server'
    Note, selecting 'libhttp-date-perl' for task 'lamp-server'
    Note, selecting 'perl-modules-5.30' for task 'lamp-server'
    Note, selecting 'liblwp-mediatypes-perl' for task 'lamp-server'
    Note, selecting 'libfcgi-perl' for task 'lamp-server'
    Note, selecting 'libmecab2' for task 'lamp-server'
    Note, selecting 'libheimbase1-heimdal' for task 'lamp-server'
    Note, selecting 'mecab-ipadic-utf8' for task 'lamp-server'
    Note, selecting 'libcgi-pm-perl' for task 'lamp-server'
    Note, selecting 'libaprutil1-dbd-sqlite3' for task 'lamp-server'
    Note, selecting 'libaio1' for task 'lamp-server'
    Note, selecting 'libsasl2-2' for task 'lamp-server'
    Note, selecting 'libio-html-perl' for task 'lamp-server'
    Note, selecting 'ssl-cert' for task 'lamp-server'
    Note, selecting 'apache2-data' for task 'lamp-server'
    Note, selecting 'php7.4-opcache' for task 'lamp-server'
    Note, selecting 'libperl5.30' for task 'lamp-server'
    Note, selecting 'libapr1' for task 'lamp-server'
    Note, selecting 'libaprutil1-ldap' for task 'lamp-server'
    Note, selecting 'libhtml-tagset-perl' for task 'lamp-server'
    Note, selecting 'librtmp1' for task 'lamp-server'
    Note, selecting 'libsasl2-modules' for task 'lamp-server'
    Note, selecting 'mysql-client-core-8.0' for task 'lamp-server'
    Note, selecting 'php7.4-mysql' for task 'lamp-server'
    Note, selecting 'libldap-common' for task 'lamp-server'
    Note, selecting 'libhcrypto4-heimdal' for task 'lamp-server'
    Note, selecting 'php7.4-readline' for task 'lamp-server'
    Note, selecting 'liblua5.2-0' for task 'lamp-server'
    Note, selecting 'mysql-common' for task 'lamp-server'
    Note, selecting 'libsodium23' for task 'lamp-server'
    Note, selecting 'libhtml-template-perl' for task 'lamp-server'
    Note, selecting 'mecab-ipadic' for task 'lamp-server'
    Note, selecting 'libtimedate-perl' for task 'lamp-server'
    Note, selecting 'libroken18-heimdal' for task 'lamp-server'
    Note, selecting 'apache2-bin' for task 'lamp-server'
    Note, selecting 'perl' for task 'lamp-server'
    Note, selecting 'libasn1-8-heimdal' for task 'lamp-server'
    Note, selecting 'libkrb5-26-heimdal' for task 'lamp-server'
    Note, selecting 'php7.4-common' for task 'lamp-server'
    Note, selecting 'libjansson4' for task 'lamp-server'
    Note, selecting 'libgdbm-compat4' for task 'lamp-server'
    Note, selecting 'apache2' for task 'lamp-server'
    Note, selecting 'php-mysql' for task 'lamp-server'
    Note, selecting 'apache2-utils' for task 'lamp-server'
    Note, selecting 'libhx509-5-heimdal' for task 'lamp-server'
    Note, selecting 'libhtml-parser-perl' for task 'lamp-server'
    Note, selecting 'libapache2-mod-php7.4' for task 'lamp-server'
    Note, selecting 'liburi-perl' for task 'lamp-server'
    Note, selecting 'mysql-server-core-8.0' for task 'lamp-server'
    ##### snipped
    Suggested packages:
      apache2-doc apache2-suexec-pristine | apache2-suexec-custom php-pear
      libipc-sharedcache-perl mailx tinyca
    The following NEW packages will be installed:
      apache2 apache2-bin apache2-data apache2-utils libaio1 libapache2-mod-php
      libapache2-mod-php7.4 libapr1 libaprutil1 libaprutil1-dbd-sqlite3
      libaprutil1-ldap libcgi-fast-perl libcgi-pm-perl libevent-core-2.1-7
      libfcgi-perl libhtml-template-perl liblua5.2-0 libmecab2 mecab-ipadic
      mecab-ipadic-utf8 mecab-utils mysql-client-8.0 mysql-client-core-8.0
      mysql-server mysql-server-8.0 mysql-server-core-8.0 php-common php-mysql
      php7.4-cli php7.4-common php7.4-json php7.4-mysql php7.4-opcache
      php7.4-readline
    0 upgraded, 34 newly installed, 0 to remove and 0 not upgraded.
    Need to get 36.2 MB of archives.
    After this operation, 273 MB of additional disk space will be used.
    ##### snipped
  4. Verify the installation of Apache, MySQL, and PHP.
    $ apt list apache2 mysql-server libapache2-mod-php
    Listing... Done
    apache2/focal,now 2.4.41-4ubuntu3 amd64 [installed]
    apache2/focal 2.4.41-4ubuntu3 i386
    libapache2-mod-php/focal,focal,now 2:7.4+75 all [installed]
    mysql-server/focal-updates,focal-updates,focal-security,focal-security,now 8.0.21-0ubuntu0.20.04.4 all [installed]
  5. Ensure Apache and MySQL services are running.
    $ ss -natp | grep -E ":80|:3306\ "
    LISTEN  0        151            127.0.0.1:3306           0.0.0.0:*              
    LISTEN  0        511                    *:80                   *:*
  6. Confirm Apache is serving the default web page.
    $ curl -s 127.0.0.1 | head
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
      <!--
        Modified from the Debian original for Ubuntu
        Last updated: 2016-11-16
        See: https://launchpad.net/bugs/1288690
      -->
      <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  7. Log in to MySQL to verify its functionality.
    $ sudo mysql -u root
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 8
    Server version: 8.0.21-0ubuntu0.20.04.4 (Ubuntu)
    
    Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
    
    Oracle is a registered trademark of Oracle Corporation and/or its
    affiliates. Other names may be trademarks of their respective
    owners.
    
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    
    mysql> 

    MySQL is by default configured with passwordless root account

  8. Create a PHP script to test PHP configuration.
    $ echo '<?php phpinfo(); ?>' | sudo tee -a /var/www/html/phpinfo.php
  9. Access the PHP script in a web browser to confirm correct PHP setup.

This guide is tested on Ubuntu:

Version Code Name
22.04 LTS Jammy Jellyfish
23.10 Mantic Minotaur
24.04 LTS Noble Numbat
Discuss the article:

Comment anonymously. Login not required.