How To Install LAMP in Ubuntu 18.04 Bionic Beaver (Linux, Apache, MariaDB, PHP)

Objective

Install and configure a basic LAMP server using MariaDB on Ubuntu 18.04 Bionic Beaver.

Distributions

Ubuntu 18.04

Requirements

A working install of Ubuntu 18.04 with root privileges

Conventions

  • # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
  • $ – requires given linux commands to be executed as a regular non-privileged user

Other Versions of this Tutorial

Ubuntu 20.04 (Focal Fossa)

Introduction

The LAMP stack is easily one of the most popular web server stacks in the world, and that’s nothing new. LAMP has been powering a huge portion of the Internet for quite some time.

If you’re an open source purist or you just don’t care for Oracle as a company(a lot of Linux users don’t), you can choose to set up a LAMP server on Ubuntu using MariaDB instead of the traditional MySQL. MariaDB is an open source drop-in replacement that was forked from MySQL several years back. It’s an excellent solution for LAMP setups where you want to avoid Oracle’s open source database.

Installation

Before you begin, you need to install everything for Apache, MySQL, and PHP to all work. There aren’t that many packages, but they’re all important.

$ sudo apt install libapache2-mod-php php-mysql mysql-server apache2 php-curl php-xmlrpc php-intl php-gd

During the installation process, you’ll see be prompted to crate a root user for the database. Choose a strong and memorable password.



Database Setup

Now, you can sign in to your newly created database.

$ mysql -u root -p

Enter the password that you set up during the installation.

You’ll be dropped into the MySQL console. You can do everything that you need from there. Before anything, make your actual database.

mysql> CREATE DATABASE `bionic_lamp`;

Then, make a regular use to run the database.

mysql> CREATE USER `site_admin`@`localhost` IDENTIFIED BY 'your_password';

Finally, grant your new user the privileges to actually use the database.

mysql> GRANT ALL ON bionic_lamp.* TO `site_admin`@`localhost`;

When you’re done, flush the privileges and exit the console.

mysql> FLUSH PRIVILEGES;
mysql> exit;

Apache Setup

Apache sort of works out of the box, but not really. If you want to do anything actually meaningful with it, you need to do a bit of configuration. That configuration will set up virtual hosts, allowing you to run multiple sites from self-contained directories.

First, copy the default configuration file that Apache comes with to a new config for your site.

sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/your-site.com.conf

Then, open that new file using sudo and your preferred text editor. There are actually only a couple of changes that you need to make to it.

Ubuntu Bionic Apache Configuration LAMP

When you first open the file, you’ll notice the main VirtualHost block with a port number. If you want to run on a different port, change that number. Otherwise, leave it the same.

Take a look below. Change the DocumentRoot line to match the location of your site. It’s best to do something like the example below.

DocumentRoot /var/www/html/your-site.com/public_html

Next, create an entry for your server name. This is the URL that Apache will associate with your site. If you’re running locally, localhost is alright. Otherwise, use the base web address.

ServerName your-site.com

If you want Apache to listen for a www too, you can create an alias that tells Apache that it’s the same thing as the server name.

ServerAlias www.your-site.com

When you’re done, save and exit.

Next, it’s probably a good idea to actually create that directory that you specified for Apache.

$ sudo mkdir -p /var/www/html/your-site.com/{public_html,logs}

The last thing you need to do is enable your configuration and disable the default. Start by enabling yours.

$ sudo a2ensite your-site.com.conf

Disable the default.

$ sudo a2dissite 000-default.conf

Reload the Apache configurations for the changes to take effect.

$ sudo systemctl reload apache2


Testing

By default, Apache looks in your document directory for index files. Create a file in /var/www/html/your-site.conf/public_html called index.php. Put the following block of PHP code in the file to test if Apache is successfully interpreting PHP and whether or not it can successfully connect to the MySQL database that you set up.

<?php echo '<h1>Page loaded with PHP!</h1>';
$conn = mysqli_connect('localhost', 'site_admin', 'your_password');

if(!$conn){
	die('<h2>Error: </h2>' . mysqli_connect_error());
}else{
	echo '<h2>Successfully Connected to MySQL Database!</h2>';
}
?>

Navigate to the address that you set up in your configuration using your web browser. Using localhost is much easier for testing, so if you didn’t do that and are having problems, consider doing that, if you’re working locally.

Ubuntu Bionic Running LAMP

You should see a plain white page with the text that you told PHP to echo out. If you’ve configured everything correctly, you’ll get the success message for PHP having been able to connect to your database.

Closing Thoughts

As of now, you have a fully functional LAMP server. It’s nothing fancy, and you should consider security improvements before you host anything in production, but it will work well for hosting everything from simple PHP sites to full PHP-based web applications.