The Linux, Apache, MySQL, and PHP (LAMP) stack is a popular open-source solution for building dynamic websites and web applications. This stack combines the Linux operating system, Apache web server, MySQL database management system, and PHP scripting language, providing a powerful and cost-effective environment for web development. Each component is highly configurable and robust, offering the flexibility needed to meet diverse web development needs.

On Ubuntu, setting up a LAMP stack is streamlined using the apt package manager. Ubuntu offers a lamp-server task, a meta-package that installs Apache, MySQL, and PHP with default configurations optimized for easy setup. This task handles the installation of necessary packages and dependencies, simplifying the process and ensuring a reliable environment for your applications. Leveraging Ubuntu’s tasksel utility, users can quickly deploy a LAMP stack, reducing the complexity typically associated with manual installation and configuration.

Once installed, the LAMP stack on Ubuntu provides a solid platform for developing and hosting web applications. Apache serves as the web server, handling HTTP requests and delivering web pages. MySQL manages the backend database, storing and retrieving data as required by the applications. PHP processes server-side scripts embedded within HTML, enabling dynamic content generation. This combination ensures efficient handling of web requests and database interactions, forming a strong foundation for web development projects.

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 server using the apt task.
    $ 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 installed versions 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 are running on their default ports.
    $ 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 page correctly.
    $ 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.
    $ 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 display PHP configuration info.
    $ echo '<?php phpinfo(); ?>' | sudo tee -a /var/www/html/phpinfo.php
  9. Access the PHP script in a web browser to verify PHP is configured correctly.

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.