This document is free text: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or any later version.
This document is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see
Sources:
Digital Ocean
geeksforgeeks
sudo apt update
sudo apt install --yes apache2
sudo apt install --yes mariadb-server
The following command makes some fine tunes regarding Mariadb security.
sudo mysql_secure_installation
You will be asked some questions.
Enter current password for root (enter for none):
There is no password yet, so press enter.
The next 2 questions
Switch to unix_socket authentication [Y/n]
and
Change the root password? [Y/n]
are about securing root account. In Ubuntu and Debian root account is already protected, so you can answer n.
For the next questions you can select default answers.
sudo apt install --yes php libapache2-mod-php php-mysql
Depending on the PHP code, you may need some more PHP library packages.
For example, Wordpress needs the following packages:
sudo apt install --yes php-curl php-gd php-mbstring php-xml php-xmlrpc \
php-soap php-intl php-zip
sudo systemctl restart apache2
We'll create a test database, a table in that database, add some rows to the table on Mariadb. We will also create a test PHP file with the PHP code to retrieve the data from the database and display it as HTML.
Connect to Mariadb shell
sudo mariadb
Create mysampledb database, connect to it, create a table, fill the table, create a user with the access permission to that database and the table.
Run on Mariadb shell.
CREATE DATABASE mysampledb;
USE mysampledb;
CREATE TABLE Employees (Name char(15), Age int(3), Occupation char(15));
INSERT INTO Employees VALUES ('Joe Smith', '26', 'Ninja');
INSERT INTO Employees VALUES ('John Doe', '33', 'Sleeper');
INSERT INTO Employees VALUES ('Mariadb Server', '14', 'RDBM');
GRANT ALL ON mysampledb.* TO 'appuser'@'localhost' IDENTIFIED BY 'password';
exit
sudo nano /var/www/html/test.php
Fill as below
<?php
$mycon = new mysqli("localhost", "appuser", "password", "mysampledb");
if ($mycon->connect_errno)
{
echo "Connection Error";
exit();
}
$mysql = "SELECT * FROM Employees";
$result = ($mycon->query($mysql));
$rows = [];
if ($result->num_rows > 0)
{
$rows = $result->fetch_all(MYSQLI_ASSOC);
}
?>
<!DOCTYPE html>
<html>
<body>
<table>
<thead>
<tr>
<th>Name</th>
<th>Age</th>
<th>Occupation</th>
</tr>
</thead>
<tbody>
<?php
if(!empty($rows))
foreach($rows as $row)
{
?>
<tr>
<td><?php echo $row['Name']; ?></td>
<td><?php echo $row['Age']; ?></td>
<td><?php echo $row['Occupation']; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
</body>
</html>
<?php
mysqli_close($conn);
?>
Now, from your workstation's browser, load the page (replace srv with your server's IP or name:
http:/srv/test.php