Linux Containers

⏱ 10 mins remaining

Run a PHP application in LAMP

Before we start using our application let’s start by running a php application in our LAMP stack.

Configuring modphp and testing it

  1. Move index.php or add it to the top of the DirectoryIndex list in /etc/apache2/mods-enabled/dir.conf.
  2. Add info.php file into your site’s directory and add the following to its content:
<?php
phpinfo();
  1. Reload Apache service

Initialize MySql server

  1. Execute the following command and follow the instruction to initialize your MySql installation
sudo mysql_secure_installation
  1. Connect to mysql using root
CREATE DATABASE your-database;
CREATE USER 'your_username'@'%' IDENTIFIED BY 'your_password';
GRANT ALL ON your_database.* TO 'your_user'@'%';
exit;

Creating the Database schema

  1. Connect to mysql using your username and password and create a todo_list table
CREATE TABLE example_database.todo_list (
    item_id INT AUTO_INCREMENT,
    content VARCHAR(255),
    PRIMARY KEY(item_id)
);
  1. Insert some data into the table
INSERT INTO example_database.todo_list (content) VALUES ("My first important item");

Deploy our application

Copy the following code into index.php in your site directory

<?php
$user = "your_user";
$password = "your_password";
$database = "your_database";
$table = "todo_list";

try {
  $db = new PDO("mysql:host=localhost;dbname=$database", $user, $password);
  echo "<h2>TODO</h2><ol>";
  foreach($db->query("SELECT content FROM $table") as $row) {
    echo "<li>" . $row['content'] . "</li>";
  }
  echo "</ol>";
} catch (PDOException $e) {
    print "Error!: " . $e->getMessage() . "<br/>";
    die();
}