LAMP or Linux, Apache, MySQL and PHP is a common stack for hosting websites. It's free, open source, and easily available and configured in most Linux distributions.

There are a few ways to install a LAMP stack on Ubuntu but the easiest is probably by using apt's task. A lamp-server task is available for apt where it could be used to easily install Apache, MySQL, and PHP on Ubuntu with good defaults and minimal efforts.

Steps to install Apache, MySQL and PHP on Ubuntu:

  1. Launch 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 lamp-server via apt's 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. Check installed version 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. Check Apache and MySQL is 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. Check if 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 show installed PHP info.
    $ echo '<?php phpinfo(); ?>' | sudo tee -a /var/www/html/phpinfo.php
  9. Access PHP script using web browser to see if PHP is configured correctly along with more details.

Guide compatibility:

Operating System
Ubuntu 16.04 LTS (Xenial Xerus)
Ubuntu 16.10 (Yakkety Yak)
Ubuntu 17.04 (Zesty Zapus)
Ubuntu 17.10 (Artful Aardvark)
Ubuntu 18.04 LTS (Bionic Beaver)
Ubuntu 18.10 (Cosmic Cuttlefish)
Ubuntu 19.04 (Disco Dingo)
Share this guide!
Discuss the article:

Comment anonymously. Login not required.

Share!