Article

Install Apache, MySQL and PHP (LAMP) on Ubuntu 20.04 LTS

Last Updated: Fri, Jul 10, 2020
Linux Guides MySQL and MariaDB PHP Popular Server Apps Ubuntu

Introduction

Vultr's One-Click LAMP server is ready to deploy in your customer portal. It's the fastest way to start your web project with Linux, Apache, MySQL/MariaDB, and PHP. Use this installation guide if you prefer to install the applications on a bare Ubuntu 20.04 LTS instance.

1. Deploy Ubuntu Server

Change to your sudo user for the remaining steps.

2. Install Apache

Install and start Apache.

$ sudo apt install apache2 -y
$ sudo systemctl start apache2.service

Enable Apache to start on boot.

$ sudo systemctl enable apache2.service

3. Install MariaDB

Install MariaDB, a drop-in replacement for MySQL.

$ sudo apt install mariadb-server mariadb-client -y

Enable MariaDB to start on boot.

$ sudo systemctl enable mariadb.service

Secure the database. Answer all the security questions as shown.

$ sudo mysql_secure_installation
  • Initially, there is no password for root. Press ENTER.

    Enter current password for root (enter for none):
    
  • Press ENTER to enter a new password.

    Set root password? [Y/n]
    
  • Enter and confirm a root password for MariaDB.

    New password:
    Re-enter new password:
    Password updated successfully!
    
  • Press ENTER to remove the anonymous user.

    Remove anonymous users? [Y/n]
    
  • Press ENTER to disallow remote root logins.

    Disallow root login remotely? [Y/n]
    
  • Press ENTER to remove the test database.

    Remove test database and access to it? [Y/n]
    
  • Press ENTER to reload the privilege tables.

    Reload privilege tables now? [Y/n]
    

This completes the basic security configuration.

4. Install PHP

Install the base PHP package and common extensions.

$ sudo apt install php php-{bcmath,bz2,intl,gd,mbstring,mysql,zip,fpm} -y

Restart Apache to load PHP.

$ sudo systemctl restart apache2.service

5. (Optional) Install SSL Certificate

See our guide to install a free Let's Encrypt SSL certificate on the Ubuntu LAMP stack.

6. Test

Test Apache

To verify Apache is running, navigate to your server's IP address with your web browser. You will see the default Apache2 page. For example (substitute 192.0.2.123 with your server's IP address):

Test PHP

As root, execute the following to create the PHP info page.

# echo '<?php phpinfo(); ?>' > /var/www/html/info.php

To verify PHP is working, navigate to your info page URL. Substitute 192.0.2.123 with your server's IP address.

Test Database

Connect to the database as root.

# mysql -u root -p -h localhost

Create a test user.

> CREATE USER 'test_user'@'localhost' IDENTIFIED BY 'test_pass';

Create a test database.

> CREATE DATABASE test_database;

Grant the test user privileges on the test database.

> GRANT ALL PRIVILEGES ON test_database.* TO 'test_user'@'localhost';

Exit the database client.

> quit

As root, create a file to test the PHP database connection.

# nano /var/www/html/db_test.php

Add the following to the file:

<?php
$conn = new mysqli('localhost', 'test_user', 'test_pass', 'test_database');

if ($conn->connect_error) {
    die("Failed: " . $conn->connect_error);
}
echo "Success";
?>

To verify the database is reachable by PHP, navigate to your page URL. Substitute 192.0.2.123 with your server's IP address.

You should see "Success". This verifies that the complete LAMP stack is functioning properly.

Conclusion

You have successfully installed a LAMP stack on your Ubuntu 20.04 LTS VPS. For more information about LAMP, see the official documentation:

Want to contribute?

You could earn up to $300 by adding new articles