Skip to content

Commit

Permalink
added apache-local example
Browse files Browse the repository at this point in the history
  • Loading branch information
jleonard99 committed Oct 21, 2024
1 parent d3a9936 commit ec7cde7
Show file tree
Hide file tree
Showing 9 changed files with 618 additions and 4 deletions.
14 changes: 14 additions & 0 deletions site/apache-php-local-mysql/.env-sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Copy this file to .env IN THE SAME FOLDER as your docker-compose.yml
# Make sure that ".env" string is in your .gitignore
# This is OK for development ONLY. Don't do this for production!
# For production you would "docker swarm secrets" or AWS secrets or other, based on your hosting service

DOCKER_DB_HOST=cmsc508.com
DOCKER_DB_USER=24fa_...
DOCKER_DB_PASS=Shout4_..._JOY
DOCKER_DB_DBNAME=24fa_hr_24fa_...

## this is used for the local database root password.

DOCKER_DB_ROOT_PASSWORD=MyRootPassword44

34 changes: 34 additions & 0 deletions site/apache-php-local-mysql/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Apache-PHP docker-compose example

This example is primarily for development.

In this example, we use docker and docker-compose to locally host an apache/php server
and local web page. Further, the database is running locally in a container, initialized with the DDL-SQL file found
in the .db folder. Finally, a phpmyadmin container is running to provide management of the database.

*./site* - this folder contains the web site. For this example, it's a simple PHP page that lists table in a web site.

*./db* - this folder contains the SQL file used to initialize the database. It is run once when the container is created.

*./docker-apache-php* - a docker file that creates the necessary apache-php image from the docker registry.

*./docker-compose.yml* - a docker compose file that create the docker-apache-php image, remaps our *./site* folder
to the web server root, and connects our local port 8080 to the web server port 80 inside the container.

*./env-sample* - a sample .env file. Copy this file to `.env` and edit it as necessary.

## Create a local .env file

Docker compose can only load environment variables from a `.env` file stored IN THE SAME FOLDER as `docker-compose.yml`. To
load everything properly, you need to copy the `.env-sample` file to `.env` and then edit the `.env` file as appropriate.

Make sure that `.env` is listed in your `.gitignore` so that any `.env` files are NOT stored in your repo.

## Running the example

```shell
docker-compose up -d
```

If all goes well, you can visit: <http://localhost:8080> to see a listing tables in the HR database. `phymyadmin` will be available at <http://localhost:8081>.

409 changes: 409 additions & 0 deletions site/apache-php-local-mysql/db/my-ddl.sql

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions site/apache-php-local-mysql/docker-apache-php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FROM php:apache

# Install the mysqli extension
RUN docker-php-ext-install mysqli && docker-php-ext-enable mysqli

47 changes: 47 additions & 0 deletions site/apache-php-local-mysql/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@

services:
web:
build:
context: .
dockerfile: docker-apache-php
container_name: apache-php-container
ports:
- "8080:80" # Map port 8080 on the host to port 80 in the container
environment:
- MYSQL_HOST=db
- MYSQL_USER=${DOCKER_DB_USER}
- MYSQL_PASSWORD=${DOCKER_DB_PASS}
- MYSQL_DATABASE=${DOCKER_DB_DBNAME}
volumes:
- ./site:/var/www/html
depends_on:
- db

db:
image: mysql:8.0
container_name: mysql-container
restart: always
environment:
MYSQL_ROOT_PASSWORD: ${DOCKER_DB_ROOT_PASSWORD}
MYSQL_USER: ${DOCKER_DB_USER}
MYSQL_PASSWORD: ${DOCKER_DB_PASS}
MYSQL_DATABASE: ${DOCKER_DB_DBNAME}
ports:
- "3306:3306" # MySQL port
volumes:
- ./db:/docker-entrypoint-initdb.d # Mount the folder containing SQL files

phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: phpmyadmin-container
restart: always
ports:
- "8081:80" # Expose phpMyAdmin on port 8081
environment:
PMA_HOST: db
MYSQL_ROOT_PASSWORD: ${DOCKER_DB_ROOT_PASSWORD} # For access to phpMyAdmin
depends_on:
- db

volumes:
mysql_data:
34 changes: 34 additions & 0 deletions site/apache-php-local-mysql/site/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
// Database connection parameters
$servername = getenv('MYSQL_HOST'); // Docker service name for the MySQL container
$username = getenv('MYSQL_USER');
$password = getenv('MYSQL_PASSWORD');
$dbname = getenv('MYSQL_DATABASE');

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

// Query to get all tables in the database
$sql = "SHOW TABLES";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
echo "<h1>Tables in the database:</h1>";
echo "<ul>";
// Output each table name
while($row = $result->fetch_array()) {
$table_name = $row[0];
echo "<li><a href='show-table.php?table=$table_name'>$table_name</a></li>";
}
echo "</ul>";
} else {
echo "No tables found in the database.";
}

$conn->close();
?>
57 changes: 57 additions & 0 deletions site/apache-php-local-mysql/site/show-table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
// Check if the table name is provided as a query parameter
if (!isset($_GET['table'])) {
die("No table specified.");
}

$table_name = $_GET['table'];

// Database connection parameters

$servername = getenv('MYSQL_HOST'); // Docker service name for the MySQL container
$username = getenv('MYSQL_USER');
$password = getenv('MYSQL_PASSWORD');
$dbname = getenv('MYSQL_DATABASE');


// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

// Query to retrieve the contents of the specified table
$sql = "SELECT * FROM `$table_name`";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
echo "<a href='/'>[home]</a><br/>";
echo "<h1>Contents of table: $table_name</h1>";
echo "<table border='1'>";

// Output table headers
$fields = $result->fetch_fields();
echo "<tr>";
foreach ($fields as $field) {
echo "<th>" . htmlspecialchars($field->name ?? '') . "</th>";
}
echo "</tr>";

// Output table rows
while($row = $result->fetch_assoc()) {
echo "<tr>";
foreach ($row as $value) {
echo "<td>" . htmlspecialchars($value ?? '') . "</td>";
}
echo "</tr>";
}

echo "</table>";
} else {
echo "No records found in table: $table_name";
}

$conn->close();
?>
9 changes: 9 additions & 0 deletions site/apache-php-remote-mysql/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ all the tables in the HR database.
*./docker-compose.yml* - a docker compose file that create the docker-apache-php image, remaps our *./site* folder
to the web server root, and connects our local port 8080 to the web server port 80 inside the container.

*./env-sample* - a sample .env file. Copy this file to `.env` and edit it as necessary.

## Create a local .env file

Docker compose can only load environment variables from a `.env` file stored IN THE SAME FOLDER as `docker-compose.yml`. To
load everything properly, you need to copy the `.env-sample` file to `.env` and then edit the `.env` file as appropriate.

Make sure that `.env` is listed in your `.gitignore` so that any `.env` files are NOT stored in your repo.

## Running the example

```shell
Expand Down
13 changes: 9 additions & 4 deletions site/index.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,15 @@ At completion of this tutorial you should have a website and database running on
The web site contains a bunch of docker examples, as listed below. These links take you
directly to the GITHUB repo and the respective README files.

* [nginx-static-example](https://github.com/vcu-ssg/ssg-quarto-docker-tutorial/tree/main/site/nginx-static-example)
* [nginx-static-volume](https://github.com/vcu-ssg/ssg-quarto-docker-tutorial/tree/main/site/nginx-static-volume)
* [apache-php-remote-mysql](https://github.com/vcu-ssg/ssg-quarto-docker-tutorial/tree/main/site/apache-php-remote-mysql)
* [production-example-1](https://github.com/vcu-ssg/ssg-quarto-docker-tutorial/tree/main/site/production-example-1)
* [nginx-static-example](https://github.com/vcu-ssg/ssg-quarto-docker-tutorial/tree/main/site/nginx-static-example) - creates a simple, static web server with the html file stored inside the container.

* [nginx-static-volume](https://github.com/vcu-ssg/ssg-quarto-docker-tutorial/tree/main/site/nginx-static-volume) - creates a simple web server container with the html files stored on your local disk.

* [apache-php-remote-mysql](https://github.com/vcu-ssg/ssg-quarto-docker-tutorial/tree/main/site/apache-php-remote-mysql) - creates a local apache web server serving html/php pages from your local disk, connected to cmsc508.com using your username and password.

* [apache-php-local-mysql](https://github.com/vcu-ssg/ssg-quarto-docker-tutorial/tree/main/site/apache-php-local-mysql) - creates a local apache setb server serving html/php pages from your local disk, connected to a locally running mysql DB and locally running phpmyadmin.

* [production-example-1](https://github.com/vcu-ssg/ssg-quarto-docker-tutorial/tree/main/site/production-example-1) - creates a localling running nginx load balancer and reverse proxy, apache-php server, mysql database and phpmyadmin all talking together.

## Other info

Expand Down

0 comments on commit ec7cde7

Please sign in to comment.