The following quick tutorial will provide you with the information on how to configure LAMP ( Linux, Apache, MySQL, PHP ) server on the Ubuntu 16.04 Xenial Xerus Linux. This guide consists of the three simple to follow steps: installation, database setup and testing.

Installation

Let's begin by installation of all required packages:
$ sudo apt-get update
$ sudo apt-get install libapache2-mod-php php-mysql mysql-server apache2
The previous commands will install Apache web server, MySQL database, PHP 7 and all required modules. In case the MySQL database was not available on your Ubuntu 16.04 Linux system yet, you will be asked for the administrator's password. Number of php scripts available also require additional modules from which some of them are mentioned below. The following linux command is optional as it will install extra packages only:
$ sudo apt-get install php-curl php-xmlrpc php-intl php-gd
$ sudo systemctl restart apache2.service

Database Setup

Create MySQL database and user. For example below we create a database xenial_lamp and give the new user admin full access to this databases using pass as password> Execute the below commands using mysql root user password configured in the previous step:
$ mysql -u root -p -e "CREATE USER 'admin'@'%' IDENTIFIED BY 'pass';"
$ mysql -u root -p -e "CREATE DATABASE xenial_lamp" 
$ mysql -u root -p -e "GRANT ALL PRIVILEGES ON xenial_lamp.* TO 'admin'@'%' WITH GRANT OPTION;"

Testing

All should now be ready to create our first PHP and MySQL connection. Use your favorite text editor and create new /var/www/html/index.php file with a following content:
<?php
$link = mysqli_connect("localhost", "admin", "pass", "xenial_lamp");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

if ($result = mysqli_query($link, "SELECT DATABASE()")) {
    $row = mysqli_fetch_row($result);
    printf("Default database is %s.\n", $row[0]);
    mysqli_free_result($result);
}

mysqli_close($link);
?>
Test your connection using localhost or remote IP address/hostname of your Ubuntu 16.04 LAMP server:
$ curl -qi http://10.1.1.109/index.php                                                                                                                                             
HTTP/1.1 200 OK
Date: Tue, 31 May 2016 02:35:21 GMT
Server: Apache/2.4.18 (Ubuntu)
Content-Length: 50
Content-Type: text/html; charset=UTF-8

Your are connected to MySQL xenial_lamp database.
Alternatively, point your web browser to LAMP server's IP address or hostname.

Troubleshooting

Uncaught Error: Call to undefined function mysql_connect()
The mysql_connect() method has been deprecated in PHP7, thus you need to use mysqli_connect() instead.
ARE YOU LOOKING FOR A LINUX JOB?
Submit your RESUME, create a JOB ALERT or subscribe to RSS feed on LinuxCareers.com.
LINUX CAREER NEWSLETTER
Subscribe to NEWSLETTER and receive latest news, jobs, career advice and tutorials.
DO YOU NEED ADDITIONAL HELP?
Get extra help by visiting our LINUX FORUM or simply use comments below.

You may also be interested in: