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
- Move
index.phpor add it to the top of theDirectoryIndexlist in/etc/apache2/mods-enabled/dir.conf. - Add
info.phpfile into your site’s directory and add the following to its content:
<?php
phpinfo();
- Reload Apache service
Initialize MySql server
- Execute the following command and follow the instruction to initialize your MySql installation
sudo mysql_secure_installation
- 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
- Connect to mysql using your username and password and create a
todo_listtable
CREATE TABLE example_database.todo_list (
item_id INT AUTO_INCREMENT,
content VARCHAR(255),
PRIMARY KEY(item_id)
);
- 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();
}