How to Install LAMP Stack on Ubuntu 20.04 Server/Desktop

This tutorial is going to show you how to install LAMP stack on Ubuntu 20.04 LTS. A software stack is a set of software tools bundled together. LAMP stands for Linux, Apache, MariaDB/MySQL, and PHP, all of which are open source and free to use. It is the most common software stack that powers dynamic websites and web applications. Linux is the operating system; Apache is the web server; MariaDB/MySQL is the database server and PHP is the server-side scripting language responsible for generating dynamic web pages.

Requirements

To follow this tutorial, you need an Ubuntu 20.04 OS running on your local computer or on a remote server.

If you are looking for a virtual private server (VPS), I recommend Kamatera VPS, which features:

  • 30 days free trial.
  • Starts at $4/month (1GB RAM)
  • High-performance KVM-based VPS
  • 9 data centers around the world, including the United States, Canada, UK, Germany, The Netherlands, Hong Kong, and Isreal.

Follow the tutorial linked below to create your Linux VPS server at Kamatera.

Once you have a VPS running Ubuntu 20.04, follow the instructions below.

And if you need to set up LAMP stack with a domain name, I recommend buying domain names from NameCheap because the price is low and they give whois privacy protection free for life.

Step 1: Update Software Packages

Before we install the LAMP stack, it’s a good idea to update the repository and software packages. Run the following commands on your Ubuntu 20.04 OS.

sudo apt update

sudo apt upgrade

Step 2: Install Apache Web Server

Enter the following command to install Apache Web server. The apache2-utils package will install some useful utilities like Apache HTTP server benchmarking tool (ab).

sudo apt install -y apache2 apache2-utils

After it’s installed, Apache should be automatically started. Check its status with systemctl.

systemctl status apache2

Sample output:

 apache2.service - The Apache HTTP Server
     Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
     Active: active (running) since Sat 2020-04-11 11:31:31 CST; 2s ago
       Docs: https://httpd.apache.org/docs/2.4/
    Process: 53003 ExecStart=/usr/sbin/apachectl start (code=exited, status=0/SUCCESS)
   Main PID: 53011 (apache2)
      Tasks: 55 (limit: 19072)
     Memory: 6.4M
     CGroup: /system.slice/apache2.service
             ├─53011 /usr/sbin/apache2 -k start
             ├─53012 /usr/sbin/apache2 -k start
             └─53013 /usr/sbin/apache2 -k start

Hint: If the above command doesn’t quit immediately, you can press Q key to gain back control of the terminal.

If it’s not running, use systemctl to start it.

sudo systemctl start apache2

It’s also a good idea to enable Apache to automatically start at system boot time.

sudo systemctl enable apache2

Check Apache version:

apache2 -v

Output:

Server version: Apache/2.4.41 (Ubuntu)
Server built:   2020-03-05T18:51:00

Now type in the public IP address of your Ubuntu 20.04 server in the browser address bar. You should see the “It works!” Web page, which means Apache Web server is running properly. If you are installing LAMP on your local Ubuntu 20.04 computer, then type 127.0.0.1 or localhost in the browser address bar.

ubuntu-20.04-lamp-stack

If the connection is refused or failed to complete, there might be a firewall preventing incoming requests to TCP port 80. If you are using iptables firewall, then you need to run the following command to open TCP port 80.

sudo iptables -I INPUT -p tcp --dport 80 -j ACCEPT

If you are using the UFW firewall, then run this command to open TCP port 80.

sudo ufw allow http

Now we need to set www-data (Apache user) as the owner of document root (otherwise known as web root). By default it’s owned by the root user.

sudo chown www-data:www-data /var/www/html/ -R

By default, Apache uses the system hostname as its global ServerName. If the system hostname can’t be resolved in DNS, then you will probably see the following error after running sudo apache2ctl -t command.

AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1. Set the 'ServerName' directive globally to suppress this message

To solve this problem, we can set a global ServerName in Apache. Use the Nano command-line text editor to create a new configuration file.

sudo nano /etc/apache2/conf-available/servername.conf

Add the following line in this file.

ServerName localhost

Save and close the file. To save a file in Nano text editor, press Ctrl+O, then press Enter to confirm. To exit, press Ctrl+X. Then enable this config file.

sudo a2enconf servername.conf

Reload Apache for the change to take effect.

sudo systemctl reload apache2

Now if you run the sudo apache2ctl -t command again, you won’t see the above error message.

Step 3: Install MariaDB Database Server

MariaDB is a drop-in replacement for MySQL. It is developed by former members of MySQL team who are concerned that Oracle might turn MySQL into a closed-source product. Enter the following command to install MariaDB on Ubuntu 20.04.

sudo apt install mariadb-server mariadb-client

After it’s installed, MariaDB server should be automatically started. Use systemctl to check its status.

systemctl status mariadb

Output:

 mariadb.service - MariaDB 10.3.22 database server
     Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset: enabled)
     Active: active (running) since Fri 2020-04-10 14:19:16 UTC; 18s ago
       Docs: man:mysqld(8)
             https://mariadb.com/kb/en/library/systemd/
   Main PID: 9161 (mysqld)
     Status: "Taking your SQL requests now..."
      Tasks: 31 (limit: 9451)
     Memory: 64.7M
     CGroup: /system.slice/mariadb.service
             └─9161 /usr/sbin/mysqld

If it’s not running, start it with this command:

sudo systemctl start mariadb

To enable MariaDB to automatically start at boot time, run

sudo systemctl enable mariadb

Now run the post-installation security script.

sudo mysql_secure_installation

When it asks you to enter MariaDB root password, press Enter key as the root password isn’t set yet. Then enter y to set the root password for MariaDB server.

ubuntu-20.04-install-LAMP-stack-MariaDB

Next, you can press Enter to answer all remaining questions, which will remove anonymous user, disable remote root login and remove test database. This step is a basic requirement for MariaDB database security. (Notice that Y is capitalized, which means it is the default answer. )

Install-LAMP-stack-on-Ubuntu-20.04-MariaDB-Database-server

By default, the MariaDB package on Ubuntu uses unix_socket to authenticate user login, which basically means you can use username and password of the OS to log into MariaDB console. So you can run the following command to login without providing MariaDB root password.

sudo mariadb -u root

To exit, run

exit;

Check MariaDB server version information.

mariadb --version

As you can see, we have installed MariaDB 10.3.22.

mariadb  Ver 15.1 Distrib 10.3.22-MariaDB, for debian-linux-gnu (x86_64) using readline 5.2

Step 4: Install PHP7.4

At the time of this writing, PHP7.4 is the latest stable version of PHP and has a minor performance edge over PHP7.3. Enter the following command to install PHP7.4 and some common PHP modules.

sudo apt install php7.4 libapache2-mod-php7.4 php7.4-mysql php-common php7.4-cli php7.4-common php7.4-json php7.4-opcache php7.4-readline

Enable the Apache php7.4 module then restart Apache Web server.

sudo a2enmod php7.4

sudo systemctl restart apache2

Check PHP version information.

php --version

Output:

PHP 7.4.3 (cli) (built: Mar 26 2020 20:24:23) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.3, Copyright (c), by Zend Technologies

To test PHP scripts with Apache server, we need to create a info.php file in the document root directory.

sudo nano /var/www/html/info.php

Paste the following PHP code into the file.

<?php phpinfo(); ?>

To save a file in Nano text editor, press Ctrl+O, then press Enter to confirm. To exit, press Ctrl+X.  Now in the browser address bar, enter server-ip-address/info.php. Replace server-ip-address with your actual IP. If you follow this tutorial on your local computer, then type 127.0.0.1/info.php or localhost/info.php.

You should see your server’s PHP information. This means PHP scripts can run properly with Apache web server.

ubuntu 20.04 LAMP PHP7.4

How to Run PHP-FPM with Apache

There are basically two ways to run PHP code with Apache web server:

  • Apache PHP module
  • PHP-FPM.

In the above steps, the Apache PHP7.4 module is used to handle PHP code, which is usually fine. But in some cases, you need to run PHP code with PHP-FPM instead. Here’s how.

Disable the Apache PHP7.4 module.

sudo a2dismod php7.4

Install PHP-FPM.

sudo apt install php7.4-fpm

Enable proxy_fcgi and setenvif module.

sudo a2enmod proxy_fcgi setenvif

Enable the /etc/apache2/conf-available/php7.4-fpm.conf configuration file.

sudo a2enconf php7.4-fpm

Restart Apache for the changes to take effect.

sudo systemctl restart apache2

Now if you refresh the info.php page in your browser, you will find that Server API is changed from Apache 2.0 Handler to FPM/FastCGI, which means Apache web server will pass PHP requests to PHP-FPM.

ubuntu-20.04-lamp-php-fpm

Congrats! You have successfully installed LAMP stack (Apache, MariaDB and PHP7.4) on Ubuntu 20.04. For your server’s security, you should delete info.php file now to prevent prying eyes.

sudo rm /var/www/html/info.php

Next Step

I hope this tutorial helped you install LAMP stack on Ubuntu 20.04 LTS. You may want to install phpMyAdmin, which is a free and open-source web-based database management tool.

You can also install WordPress on top of the LAMP stack to create your own website or blog.

As always, if you found this post useful, then subscribe to our free newsletter to get more tips and tricks. Take care 🙂

Rate this tutorial
[Total: 30 Average: 5]

45 Responses to “How to Install LAMP Stack on Ubuntu 20.04 Server/Desktop

  • Good!

    • Chris Lee
      2 years ago

      Xiao, your content is head and shoulders above every resource I’ve encountered…I can’t thank you, enough, for your efforts.

  • Kabeer Ahmed SAyeed
    4 years ago

    The tutorial “How to Install LAMP Stack on Ubuntu 20.04 Server/Desktop” was very useful. I was sble to install the LAMP stack without any problems. This is a prelude to installing OpenDocMan which is the next step to be performed. Would you happen to have a tutorial for doing this?

    Thank you very much for your turorial. I plan to make a small donation to you.

  • Thank you very much. Very helpful and successful for me…..(Y)

  • can you do the same for MEAN stack?

  • Great tutorial, works every time. Could do with a section on changing the apache config to put index.PHP before index.html.

    Cheers,

  • Osmar German Monteagudo Castro
    4 years ago

    Thanks for the tutorial, i was able to install and the FPM was a interesant option i,m going to use it. thanks a lot

  • Brandon Mac
    4 years ago

    Hey there thanks for the wonderful tutorial such a great tech write-up. Question for ya hope you can help do I just built my server and it’s showing fingerprint key information for SSH on boot up with cloud-init in the terminal like it’s trying to connect why is this happening and what is it trying to connect to ? I would like to disable it if it’s not necessary for a lamp server and to have remote access via ssh? Thanks for your help.

  • neeraj agarkar
    4 years ago

    Thanks, this was very helpful!

  • Great instructions! Everything installed without a hitch. Thank you!

  • Thank you very much.. Very helpful tutorial!!!

  • Scotty Paul
    4 years ago

    Good stuff! Installation on AWS EC2 went without a hitch.

  • GREAT! Running on GCP with no worries at all! KUDOS!

  • Hi,

    Very good tutorial.
    Do you have also done something about security of the LAMP Server (Fail2ban, ect…) ?

    Best regards,

  • Best tutorial congrats and thank you

  • Whoop after many install attempts and wrong advice finally a site that puts it in easy to follow instructions, installed without any hassle. might just donate some cash as i was on the threshold of giving ubuntu up and revert to ‘you know who’ operating system lol Very good advice on here, can’t go wrong 😉

  • Hi,

    Very good tutorial.

  • Garry Yu
    4 years ago

    Is there a mysql installation guide instead of MariaDB?

  • Frederic
    4 years ago

    Hi, well it seems I’ve broke something
    Maria DB doesn’t start

    $ systemctl status mariadb

    mariadb.service: Main process exited, code=exited, status=1/FAILURE
    mariadb.service: Failed with result 'exit-code'.
    Failed to start MariaDB 10.3.22 database server.
    

    $ sudo systemctl start mariadb

    Job for mariadb.service failed because the control process exited with error code.
    See "systemctl status mariadb.service" and "journalctl -xe" for details.
    

    $ journalctl -xe

    Sep 06 21:30:36 vps.mapom.fr postfix/smtpd[365545]: connect from unknown[45.142.120.215]
    Sep 06 21:30:36 vps.mapom.fr mysqld[369940]: 2020-09-06 21:30:36 0 [Note] /usr/sbin/mysqld (mysqld 10.3.22-MariaDB-1ubuntu1) starting as process 369940 ...
    Sep 06 21:30:37 vps.mapom.fr audit[369940]: AVC apparmor="DENIED" operation="sendmsg" info="Failed name lookup - disconnected path" error=-13 profile="/usr/sbin/mysqld" name="run/systemd/no>
    Sep 06 21:30:37 vps.mapom.fr kernel: audit: type=1400 audit(1599420637.165:94): apparmor="DENIED" operation="sendmsg" info="Failed name lookup - disconnected path" error=-13 profile="/usr/s>
    Sep 06 21:30:37 vps.mapom.fr audit[369940]: AVC apparmor="DENIED" operation="sendmsg" info="Failed name lookup - disconnected path" error=-13 profile="/usr/sbin/mysqld" name="run/systemd/no>
    Sep 06 21:30:37 vps.mapom.fr kernel: audit: type=1400 audit(1599420637.389:95): apparmor="DENIED" operation="sendmsg" info="Failed name lookup - disconnected path" error=-13 profile="/usr/s>
    Sep 06 21:30:37 vps.mapom.fr systemd[1]: mariadb.service: Main process exited, code=exited, status=1/FAILURE
    
    • Xiao Guoan (Admin)
      4 years ago

      Check MariaDB error log /var/log/mysql/error.log.

      • Frederic
        4 years ago

        Hi Xiao, I’ve finally reinstalled my server, MariaDB was confliction with a previous MySQL install and I was unable to clear properly the error.

  • Kuldeep Singh
    4 years ago

    Great tutorial.
    Thanks!

  • There is an easier way to do most of this: install tasksel.

    sudo apt install tasksel
    sudo tasksel

    Select “LAMP server” and click OK.

  • A great tutorial. I wish others were as clear and comprehensive as this.

  • Tri Ari
    4 years ago

    Thanks for the tutorial, you save my life 😀

  • Larry R Trask
    3 years ago

    This is fantastic. This tutorial made setting up my LAMP stack very easy. Thanks so much for taking the time to write this.

  • Jorgen Henningsen
    3 years ago

    I get this error: “Module php7.4-fpm does not exist” when I try: sudo a2enmod php7.4-fpm. Up to this everything runs without errors.
    Any suggestion?

    • Xiao Guoan (Admin)
      3 years ago

      Enable the Apache PHP module.

      sudo a2enmod php7.4

      Disable the Apache PHP module.

      sudo a2dismod php7.4

      Enable PHP-FPM for Apache

      sudo a2enconf php7.4-fpm

      Disable PHP-FPM for Apache.

      sudo a2disconf php7.4-fpm
  • Jorgen Henningsen
    3 years ago

    Super…. thanks

  • Javier Eraso
    3 years ago

    Very good and straightforward tutorial. Thanks. Saves a lot of time.

  • Gaudnik Wojciech
    3 years ago

    PHP-fpm
    OS: Ubuntu 20.10 groovy

    You must run:
    systemctl restart php7.4-fpm.service

  • Greger Leijonhufvud
    3 years ago

    How can you use systemctl on wsl2 when it is not supported?

    greger@Friherr:~$ systemctl
    System has not been booted with systemd as init system (PID 1). Can't operate.
    Failed to connect to bus: Host is down
    • Xiao Guoan (Admin)
      3 years ago

      If you use Windows Subsystem for Linux (WSL), you need to use the traditional service command like so:

      Apache

      sudo service apache2 start
      
      sudo service apache2 status

      MariaDB

      sudo service mariadb start
      
      sudo service mariadb status

      PHP-FPM

      sudo service php7.4-fpm start
      
      sudo service php7.4-fpm status
  • Thanks for the info and have set up to receive your “Linux Tutorials”.
    Also sent you email on that donation method you have up.

    • Xiao Guoan (Admin)
      3 years ago

      Hello Mike, thanks for your donation 🙂

  • Gustavo
    3 years ago

    Hi,
    I am new to Linux and ubuntu server. I follow our tutorial, which is great. But I was never able to access 127.0.0.1, neither local host. I can only access the php page (to confirm it is working) and apache2 page, using my lan server address 192.168.xx.xx.
    So what I am doing wrong? Is there any report/log that I can generate to make sure I follow all the steps?

    Regards,
    Gustavo

  • Hello .
    I went through all the steps but to check
    php.info
    This shows the web page.

    Not Found

    The requested URL was not found on this server.
    Apache / 2.4.41 (Ubuntu) Server at aa.aa.aa.aa Port 80

  • Hammond X. Gritzkofe
    3 years ago

    Boss! Elegant! Just the right information level, and well presented.

  • Jennifer
    2 years ago

    Great tutorial. Please could you update when Ubuntu Server 22.04 is released?

  • Script Nerdz
    2 years ago

    Do you have a tutorial coming soon to support new Ubuntu server 22.04 LTS ? Also what about PHP should we still be using 7.4 for Nextcloud as this link shows that Nextcloud is supporting newer PHP https://docs.nextcloud.com/server/latest/admin_manual/installation/system_requirements.html

    Could you update tutorial for these items as I tried to change the commands from php7.4 to 8.0 or 8.1 and it gives errors?

    Do you have tutorial on scripting this or would it be something you would put together? Hyper-V building out the VM machine deploying Ubuntu and LAMP with NextCloud? Just an idea I know its alot of work.

  • Saurav
    1 year ago

    Great tutorial. VERY USEFUL. thanks…

  • I copy/pasted each command into my bash window on the latest Peppermint Linux 11 (based on Debian), and it worked like a charm. Many thanks for such very clear instructions. I had always used tasksel on Ubuntu, but that was not available in Peppermint.
    I am using this on a stand-alone PC for development purposes, and for security, do not want apache to respond to requests from outside of the localhost, so I added the following refinement to /etc/apache2/ports.conf
    Listen 127.0.0.1:80

Leave a Comment

  • Comments with links are moderated by admin before published.
  • Your email address will not be published.
  • Use <pre> ... </pre> HTML tag to quote the output from your terminal/console.
  • Please use the community (https://community.linuxbabe.com) for questions unrelated to this article.
  • I don't have time to answer every question. Making a donation would incentivize me to spend more time answering questions.

The maximum upload file size: 2 MB. You can upload: image. Links to YouTube, Facebook, Twitter and other services inserted in the comment text will be automatically embedded. Drop file here