Share!

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. You can install LAMP stack on Ubuntu by following any of these methods;

Install Apache, MySQL and PHP in Ubuntu via apt shortcut

There's a shortcut in apt where common installation packages are grouped into tasks. There's a task available for LAMP and could be used to install Apache, MySQL, and PHP in Ubuntu with the following steps;

  1. Update package information.
    $ sudo apt update
  2. Install lamp-server via apt's task.
    $ sudo apt install --assume-yes lamp-server^
  3. apt will install predefined packages for the installation task.
    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 'libnghttp2-14' for task 'lamp-server'
    Note, selecting 'php7.2-common' for task 'lamp-server'
    Note, selecting 'libencode-locale-perl' for task 'lamp-server'
    Note, selecting 'php7.2-cli' for task 'lamp-server'
    Note, selecting 'libwind0-heimdal' for task 'lamp-server'
    Note, selecting 'libsasl2-modules-db' for task 'lamp-server'
    Note, selecting 'mysql-client-5.7' 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 'libevent-core-2.1-6' for task 'lamp-server'
    Note, selecting 'mysql-server-5.7' for task 'lamp-server'
    Note, selecting 'php-common' for task 'lamp-server'
    Note, selecting 'libaprutil1' for task 'lamp-server'
    Note, selecting 'php7.2-mysql' for task 'lamp-server'
    Note, selecting 'libheimntlm0-heimdal' for task 'lamp-server'
    Note, selecting 'mysql-server' for task 'lamp-server'
    Note, selecting 'libcgi-fast-perl' for task 'lamp-server'
    Note, selecting 'libwrap0' for task 'lamp-server'
    Note, selecting 'libhttp-date-perl' for task 'lamp-server'
    Note, selecting 'perl-modules-5.26' for task 'lamp-server'
    Note, selecting 'liblwp-mediatypes-perl' for task 'lamp-server'
    Note, selecting 'libfcgi-perl' for task 'lamp-server'
    Note, selecting 'libheimbase1-heimdal' 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 'php7.2-json' for task 'lamp-server'
    Note, selecting 'php7.2-opcache' 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 'libperl5.26' 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 'mysql-client-core-5.7' for task 'lamp-server'
    Note, selecting 'libsasl2-modules' for task 'lamp-server'
    Note, selecting 'libldap-common' for task 'lamp-server'
    Note, selecting 'php7.2-readline' for task 'lamp-server'
    Note, selecting 'libhcrypto4-heimdal' 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 '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 '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.2' for task 'lamp-server'
    Note, selecting 'liburi-perl' for task 'lamp-server'
    Note, selecting 'mysql-server-core-5.7' for task 'lamp-server'
    libasn1-8-heimdal is already the newest version (7.5.0+dfsg-1).
    libgdbm-compat4 is already the newest version (1.14.1-6).
    libgssapi3-heimdal is already the newest version (7.5.0+dfsg-1).
    libhcrypto4-heimdal is already the newest version (7.5.0+dfsg-1).
    libheimbase1-heimdal is already the newest version (7.5.0+dfsg-1).
    libheimntlm0-heimdal is already the newest version (7.5.0+dfsg-1).
    libhx509-5-heimdal is already the newest version (7.5.0+dfsg-1).
    libkrb5-26-heimdal is already the newest version (7.5.0+dfsg-1).
    libldap-2.4-2 is already the newest version (2.4.45+dfsg-1ubuntu1).
    libldap-common is already the newest version (2.4.45+dfsg-1ubuntu1).
    libnghttp2-14 is already the newest version (1.30.0-1ubuntu1).
    libperl5.26 is already the newest version (5.26.1-6).
    libroken18-heimdal is already the newest version (7.5.0+dfsg-1).
    libsasl2-2 is already the newest version (2.1.27~101-g0780560+dfsg-3ubuntu2).
    libsasl2-modules is already the newest version (2.1.27~101-g0780560+dfsg-3ubuntu2).
    libsasl2-modules-db is already the newest version (2.1.27~101-g0780560+dfsg-3ubuntu2).
    libwind0-heimdal is already the newest version (7.5.0+dfsg-1).
    libwrap0 is already the newest version (7.6.q-27).
    perl is already the newest version (5.26.1-6).
    perl-modules-5.26 is already the newest version (5.26.1-6).
    Suggested packages:
      www-browser apache2-doc apache2-suexec-pristine | apache2-suexec-custom
      php-pear libdata-dump-perl libipc-sharedcache-perl libwww-perl mailx tinyca
      openssl-blacklist
    The following NEW packages will be installed:
      apache2 apache2-bin apache2-data apache2-utils libaio1 libapache2-mod-php
      libapache2-mod-php7.2 libapr1 libaprutil1 libaprutil1-dbd-sqlite3
      libaprutil1-ldap libcgi-fast-perl libcgi-pm-perl libencode-locale-perl
      libevent-core-2.1-6 libfcgi-perl libhtml-parser-perl libhtml-tagset-perl
      libhtml-template-perl libhttp-date-perl libhttp-message-perl libio-html-perl
      liblua5.2-0 liblwp-mediatypes-perl libsodium23 libtimedate-perl liburi-perl
      mysql-client-5.7 mysql-client-core-5.7 mysql-common mysql-server
      mysql-server-5.7 mysql-server-core-5.7 php-common php-mysql php7.2-cli
      php7.2-common php7.2-json php7.2-mysql php7.2-opcache php7.2-readline
      ssl-cert
    0 upgraded, 42 newly installed, 0 to remove and 0 not upgraded.
    Need to get 26.8 MB of archives.
    After this operation, 187 MB of additional disk space will be used.

Install Apache, MySQL and PHP in Ubuntu via tasksel

tasksel is actually the base of the apt's task in the previous installation method. tasksel also provides ncurses interface at the terminal to further ease package installations;

  1. Update package information.
    $ sudo apt update
  2. Install tasksel if it's not already installed.
    $ sudo apt install --assume-yes tasksel

    tasksel is pre-installed in Ubuntu Server but not in most other variants

  3. Run tasksel.
    $ sudo tasksel
  4. Navigate via arrow key to LAMP Server and press <space> to select.
  5. Press <Tab> to navigate to the <Ok> button and press <Enter> to start the installation process.
  6. tasksel will download and install the required packages.

Install Apache, MySQL and PHP in Ubuntu via apt

If you require more granular control of what to and what not to install, you can independently install the packages via apt with the following steps;

  1. Update package information.
    $ sudo apt update
  2. Install Apache.
    $ sudo apt install --assume-yes apache2
  3. Install MySQL server and client.
    $ sudo apt install --assume-yes mysql-server mysql-client
  4. Install PHP.
    $ sudo apt install --assume-yes php php-cli
  5. Install required PHP modules.
    $ sudo apt install --assume-yes php-mysql libapache2-mod-php 

Test if Apache, MySQL and Apache is installed correctly

Verify that the installation was successful with these commands;

  1. Check installed PHP version.
    $ php -version
    PHP 7.2.5-0ubuntu0.18.04.1 (cli) (built: May  9 2018 17:21:02) ( NTS )
    Copyright (c) 1997-2018 The PHP Group
    Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
        with Zend OPcache v7.2.5-0ubuntu0.18.04.1, Copyright (c) 1999-2018, by Zend Technologies
  2. Check Apache and MySQL is running on their default ports.
    $ ss -natp | grep -E ":80|:3306"
    LISTEN  0        80                  127.0.0.1:3306             0.0.0.0:*
    LISTEN  0        128                         *:80                     *:* 
  3. 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" />
  4. Log in to MySQL.
    $ sudo mysql -u root
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 3
    Server version: 5.7.22-0ubuntu18.04.1 (Ubuntu)
    
    Copyright (c) 2000, 2018, 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

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)