How to Install LAMP Stack on Ubuntu 18.04 Server/Desktop

This tutorial is going to show you how to install LAMP stack on Ubuntu 18.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.

Prerequisites

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

If you are looking for a VPS (Virtual Private Server), then you can register an account at DigitalOcean via this special link to get $50 free credit. (For new users only). If you are already a DigitalOcean user, then you can register an account on Vultr via this special link to get $50 free credit (for new users only).

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 for free.

Step 1: Update Software Packages

Before we install the LAMP stack, it’s a good idea to update repository and software packages. Run the following command on your Ubuntu 18.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)
  Drop-In: /lib/systemd/system/apache2.service.d
           └─apache2-systemd.conf
   Active: active (running) since Sat 2018-09-08 10:46:05 UTC; 3min 37s ago
 Main PID: 1610 (apache2)
    Tasks: 55 (limit: 505)
   CGroup: /system.slice/apache2.service
           ├─1610 /usr/sbin/apache2 -k start
           ├─1612 /usr/sbin/apache2 -k start
           └─1613 /usr/sbin/apache2 -k start

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.29 (Ubuntu)
Server built: 2018-06-27T17:05:04

Now type in the public IP address of your Ubuntu 18.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 18.04 computer, then type 127.0.0.1 or localhost in the browser address bar.

ubuntu 18.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 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

Step 3: Install MariaDB Database Server

MariaDB is a drop-in replacement for MySQL. Enter the following command to install it on Ubuntu 18.04.

sudo apt install mariadb-server mariadb-client

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

systemctl status mariadb

Sample output:

 mariadb.service - MariaDB 10.1.34 database server
   Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset: enabled)
   Active: active (running) since Sat 2018-09-08 11:13:27 UTC; 21s ago
     Docs: man:mysqld(8)
           https://mariadb.com/kb/en/library/systemd/
 Main PID: 3473 (mysqld)
   Status: "Taking your SQL requests now..."
    Tasks: 27 (limit: 505)
   CGroup: /system.slice/mariadb.service
           └─3473 /usr/sbin/mysqld

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

sudo systemctl start mariadb

To enable MariaDB to automatically start at system 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 18.04 lamp 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. (Note that the letter Y is capitalized, which means it’s the default answer.)

ubuntu 18.04 lamp stack mariadb server

By default, the MaraiDB 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

Output:

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

Step 4: Install PHP7.2

At the the time of this writing, PHP7.2 is the latest stable version of PHP and has a minor performance edge over PHP7.1. Enter the following command to install PHP7.2.

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

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

sudo a2enmod php7.2

sudo systemctl restart apache2

Check PHP version information.

php --version

Output:

PHP 7.2.7-0ubuntu0.18.04.2 (cli) (built: Jul  4 2018 16:55:24) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.2.7-0ubuntu0.18.04.2, Copyright (c) 1999-2018, 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 sever-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 18.04 lamp php7.2

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 PHP7.2 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.2 module.

sudo a2dismod php7.2

Install PHP-FPM.

sudo apt install php7.2-fpm

Enable proxy_fcgi and setenvif module.

sudo a2enmod proxy_fcgi setenvif

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

sudo a2enconf php7.2-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 to FPM/FastCGI, which means Apache web server will pass PHP requests to PHP-FPM.

ubuntu 18.04 lamp php-fpm

Congrats! You have successfully installed LAMP stack (Apache, MariaDB and PHP7.2) on Ubuntu 18.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

So you just learned how to install LAMP stack on Ubuntu 18.04. What next? You may also want to install phpMyAdmin alongside your LAMP stack.

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: 25 Average: 5]

22 Responses to “How to Install LAMP Stack on Ubuntu 18.04 Server/Desktop

  • Amazing, thank you!

    • This is the only help I found that got PHP actually working. Thanks very much for the great work!!

  • Hi,
    Thank you very much the material, very helpful. One last thing I am stuck on, the joomla http://localhost/joomla/installation/index.php is displaying “Error”. I don’t know where I have gone wrong, if you can kindly help I will be grateful.

    • Xiao Guo An (Admin)
      5 years ago

      You can check the Nginx error log to see what went wrong.

  • CarlosEAM
    5 years ago

    Straight to the point and worked perfectly! Thank you.
    Installed on Ubuntu 18.04 LTS

  • Thanks for this awesome tutorial! Installed the LAMP stack within 15 minutes on Ubuntu 18.04.

    I’m stuck on the correct file permissions though. If I download my projects to the /var/www/html/ folder using sudo, I can set them to www-data user. But then I can’t open the folder using for instance Visual Studio Code, because my user doesn’t have permission to the www-data files and folders.

    Any clue how to set them properly without having to change the files and folders permissions to 777?

    • Bit late, but you’d have to create a group and put both your user and the www-data user in the group, then give permissions to the group, chown -r www-data:your-group-name and then set the permissions to 776 or 771, depending on what you want guest to be able to do.

  • very helpful!

  • Could i use the same methods above for installation in linux lite 4.4 (based on ubuntu 18.04, debian) too ?

  • Aleksandr
    5 years ago

    Thanks, man!

  • Comprehensive. Well written for clarity of understanding. Added extras to the installation. Install went perfect and obtained the intended results. Great job on this tutorial.

  • Wesley Santos
    4 years ago

    Hello how are you?
    Do you have a tutorial for deploying wordpress on LAMP server with Ubuntu Server 18.04?

  • May I ask how to make sure we install latest packages? Thanks

  • Obrigado! Ajudou muito.

  • Just used this today, works great!

  • I am stack, am using Ubuntu 20.04 LTS. Am getting enable to locate package php7.1 errors at Step 4: Install PHP7.2

    anyone who knows what i am doing wrong?
    thanks in advance

  • You’re killing it! This was a 15 min install and config…Google Instance…Thanks so much!

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