Actualiser sk1/README.md

This commit is contained in:
Antonin Filippi 2025-04-29 21:45:58 +00:00
parent 6753ae36ae
commit 85b680d0f4

View File

@ -1,199 +1,196 @@
# PHP + PostgreSQL Visit Counter # PHP + PostgreSQL Visit Counter
This project implements a PHP web application for counting visits, backed by a PostgreSQL database. It is containerized using Docker and Docker Compose, and provides automated deployment scripts (to Plesk via Git) as well as deployment cleanup scripts. This project implements a PHP web application for counting visits, backed by a PostgreSQL database. It is containerized using Docker and Docker Compose, and provides automated deployment scripts (to Plesk via Git) as well as deployment cleanup scripts.
--- ---
## Table of Contents ## Table of Contents
1. [Overview](#overview) 1. [Overview](#overview)
2. [Features](#features) 2. [Features](#features)
3. [Prerequisites](#prerequisites) 3. [Prerequisites](#prerequisites)
4. [Installation & Configuration](#installation--configuration) 4. [Installation & Configuration](#installation--configuration)
- [Project Structure](#project-structure) - [Project Structure](#project-structure)
- [Environment Variables](#environment-variables) - [Environment Variables](#environment-variables)
- [Docker and Docker Compose](#docker-and-docker-compose) - [Docker and Docker Compose](#docker-and-docker-compose)
- [Prepare Script (`prepare-app.sh`)](#prepare-script-prepare-appsh) - [Prepare Script (`prepare-app.sh`)](#prepare-script-prepare-appsh)
5. [Usage](#usage) 5. [Usage](#usage)
- [Running Locally](#running-locally) - [Application Entry Point](#application-entry-point)
- [Application Entry Point](#application-entry-point) - [Table Reset (`drop.php`)](#table-reset-dropphp)
- [Table Reset (`drop.php`)](#table-reset-dropphp) 6. [Deployment to Plesk](#deployment-to-plesk)
6. [Deployment to Plesk](#deployment-to-plesk) 7. [Deployment Cleanup (`remove-app.sh`)](#deployment-cleanup-remove-appsh)
7. [Deployment Cleanup (`remove-app.sh`)](#deployment-cleanup-remove-appsh) 8. [Detailed File Structure](#detailed-file-structure)
8. [Detailed File Structure](#detailed-file-structure) 9. [Troubleshooting](#troubleshooting)
9. [Contributing](#contributing)
10. [Troubleshooting](#troubleshooting) ---
11. [License](#license)
## Overview
---
This simple application counts the number of visits to a web page. On each page load, a new entry is inserted into the `visitors` table in PostgreSQL, and the total count is displayed.
## Overview
A button allows to choose between light and dark themes via JavaScript and `localStorage`.
This simple application counts the number of visits to a web page. On each page load, a new entry is inserted into the `visitors` table in PostgreSQL, and the total count is displayed.
The project includes:
A button allows to choose between light and dark themes via JavaScript and `localStorage`.
- A **PostgreSQL** container with automatic table initialization via `db.sql`.
The project includes: - A **PHP 8.2 + Apache** container serving the application.
- A **Dockerfile** to build the web image (with the `pdo_pgsql` extension installed).
- A **PostgreSQL** container with automatic table initialization via `db.sql`. - A **docker-compose.yml** to orchestrate both services.
- A **PHP 8.2 + Apache** container serving the application. - A **prepare-app.sh** script for force-pushing deployments to Plesk via Git.
- A **Dockerfile** to build the web image (with the `pdo_pgsql` extension installed). - A **remove-app.sh** script for cleaning up the remote deployment.
- A **docker-compose.yml** to orchestrate both services.
- A **prepare-app.sh** script for force-pushing deployments to Plesk via Git. ---
- A **remove-app.sh** script for cleaning up the remote deployment.
## Features
---
- Automatically inserts a row into the `visitors` table on each load (`INSERT INTO visitors DEFAULT VALUES`).
## Features - Automatically creates the table if it doesnt exist (`CREATE TABLE IF NOT EXISTS visitors …`).
- Displays the visit counter.
- Automatically inserts a row into the `visitors` table on each load (`INSERT INTO visitors DEFAULT VALUES`). - Light/dark theme toggle, persisted in `localStorage`.
- Automatically creates the table if it doesnt exist (`CREATE TABLE IF NOT EXISTS visitors …`). - Automated Git deployments (Plesk).
- Displays the visit counter. - Remote cleanup of pushed content.
- Light/dark theme toggle, persisted in `localStorage`.
- Automated Git deployments (Plesk). ---
- Remote cleanup of pushed content.
## Prerequisites
---
- **Docker** (>= 20.x)
## Prerequisites - **Docker Compose** (>= 1.27.x)
- **Git** (for the deployment scripts)
- **Docker** (>= 20.x) - Access to a Plesk instance with a Git-configured repository (for `prepare-app.sh` and `remove-app.sh`)
- **Docker Compose** (>= 1.27.x)
- **Git** (for the deployment scripts) ---
- Access to a Plesk instance with a Git-configured repository (for `prepare-app.sh` and `remove-app.sh`)
## Installation & Configuration
---
### Project Structure
## Installation & Configuration
```text
### Project Structure ├── db.sql # SQL script to initialize the `visitors` table
├── Dockerfile # Builds the PHP + Apache + pdo_pgsql image
```text ├── docker-compose.yml # Docker orchestration (db + web)
├── db.sql # SQL script to initialize the `visitors` table ├── prepare-app.sh # Deployment script to Plesk
├── Dockerfile # Builds the PHP + Apache + pdo_pgsql image ├── remove-app.sh # Remote cleanup script
├── docker-compose.yml # Docker orchestration (db + web) ├── index.php # Main page (insertion + count + front-end)
├── prepare-app.sh # Deployment script to Plesk └── drop.php # Script to drop the `visitors` table
├── remove-app.sh # Remote cleanup script ```
├── index.php # Main page (insertion + count + front-end)
└── drop.php # Script to drop the `visitors` table ### Environment Variables
```
In `docker-compose.yml`, for the **db** service:
### Environment Variables
```yaml
In `docker-compose.yml`, for the **db** service: POSTGRES_DB: visitors
POSTGRES_USER: visitor_user
```yaml POSTGRES_PASSWORD: secret
POSTGRES_DB: visitors ```
POSTGRES_USER: visitor_user
POSTGRES_PASSWORD: secret And for the **web** service:
```
```yaml
And for the **web** service: DB_HOST: db
DB_PORT: 5432
```yaml DB_NAME: visitors
DB_HOST: db DB_USER: visitor_user
DB_PORT: 5432 DB_PASS: secret
DB_NAME: visitors ```
DB_USER: visitor_user
DB_PASS: secret
```
### Prepare Script (`prepare-app.sh`)
This script initializes the Git repo (if needed), adds a `tuke` remote pointing to the Plesk instance, commits all changes, then force-pushes the branch (`master` or `main`).
### Prepare Script (`prepare-app.sh`)
```bash
This script initializes the Git repo (if needed), adds a `tuke` remote pointing to the Plesk instance, commits all changes, then force-pushes the branch (`master` or `main`). chmod +x prepare-app.sh
./prepare-app.sh
```bash ```
chmod +x prepare-app.sh
./prepare-app.sh
``` ---
## Usage
---
## Usage ### Application Entry Point
`index.php` performs:
### Application Entry Point
1. Connects to PostgreSQL via PDO (`pgsql:host=$dbHost;port=$dbPort;dbname=$dbName`).
`index.php` performs: 2. Runs an embedded SQL migration to create the `visitors` table if it doesnt exist.
3. Inserts a new row:
1. Connects to PostgreSQL via PDO (`pgsql:host=$dbHost;port=$dbPort;dbname=$dbName`). ```php
2. Runs an embedded SQL migration to create the `visitors` table if it doesnt exist. $pdo->exec("INSERT INTO visitors DEFAULT VALUES");
3. Inserts a new row: ```
```php 4. Retrieves the total count:
$pdo->exec("INSERT INTO visitors DEFAULT VALUES"); ```php
``` $count = $pdo->query("SELECT COUNT(*) FROM visitors")->fetchColumn();
4. Retrieves the total count: ```
```php 5. Generates the HTML/CSS/JS to display the counter and manage the theme.
$count = $pdo->query("SELECT COUNT(*) FROM visitors")->fetchColumn();
``` ### Table Reset (`drop.php`)
5. Generates the HTML/CSS/JS to display the counter and manage the theme.
The script `drop.php` connects to the same database and runs:
### Table Reset (`drop.php`)
```php
The script `drop.php` connects to the same database and runs: $pdo->exec("DROP TABLE IF EXISTS visitors");
echo "Table visitors dropped.";
```php ```
$pdo->exec("DROP TABLE IF EXISTS visitors");
echo "Table visitors dropped."; On error it returns HTTP 500 and displays the exception message.
```
---
On error it returns HTTP 500 and displays the exception message.
## Deployment to Plesk
---
To deploy automatically:
## Deployment to Plesk
1. Configure `GIT_REMOTE_URL` and `BRANCH` in `prepare-app.sh`.
To deploy automatically: 2. Make it executable and run:
```bash
1. Configure `GIT_REMOTE_URL` and `BRANCH` in `prepare-app.sh`. chmod +x prepare-app.sh
2. Make it executable and run: ./prepare-app.sh
```bash ```
chmod +x prepare-app.sh This force-pushes the branch to Plesks `httpdocs`, updating the remote site.
./prepare-app.sh
``` ---
This force-pushes the branch to Plesks `httpdocs`, updating the remote site.
## Deployment Cleanup (`remove-app.sh`)
---
To clear the remote content without touching the local repository:
## Deployment Cleanup (`remove-app.sh`)
1. Make it executable:
To clear the remote content without touching the local repository: ```bash
chmod +x remove-app.sh
1. Make it executable: ```
```bash 2. Run:
chmod +x remove-app.sh ```bash
``` ./remove-app.sh
2. Run: ```
```bash This clones the remote into a temp directory, removes all tracked files, commits, and force-pushes, then cleans up.
./remove-app.sh
``` ---
This clones the remote into a temp directory, removes all tracked files, commits, and force-pushes, then cleans up.
## Detailed File Structure
---
- **docker-compose.yml**: Coordinates two services:
## Detailed File Structure - **db** (PostgreSQL 13) initialized via `./db.sql`.
- **web** (PHP 8.2 + Apache with pdo_pgsql) linked to `db`, exposing port 8080.
- **docker-compose.yml**: Coordinates two services: - **Dockerfile**: Base `php:8.2-apache`, installs `libpq-dev` and `pdo_pgsql`, copies `src/` to `/var/www/html/`.
- **db** (PostgreSQL 13) initialized via `./db.sql`. - **db.sql**: SQL to auto-create the `visitors` table.
- **web** (PHP 8.2 + Apache with pdo_pgsql) linked to `db`, exposing port 8080. - **prepare-app.sh** & **remove-app.sh**: Bash scripts for Git-based deployment and cleanup on Plesk.
- **Dockerfile**: Base `php:8.2-apache`, installs `libpq-dev` and `pdo_pgsql`, copies `src/` to `/var/www/html/`. - **index.php**: Main page logic (connect, migrate, insert, count, render).
- **db.sql**: SQL to auto-create the `visitors` table. - **drop.php**: Drops the `visitors` table.
- **prepare-app.sh** & **remove-app.sh**: Bash scripts for Git-based deployment and cleanup on Plesk.
- **index.php**: Main page logic (connect, migrate, insert, count, render). ---
- **drop.php**: Drops the `visitors` table.
## Troubleshooting
---
- **PostgreSQL connection error**: Ensure the `db` service is running; check logs:
## Troubleshooting ```bash
docker-compose logs db
- **PostgreSQL connection error**: Ensure the `db` service is running; check logs: ```
```bash - **Table not created**: Make sure `db.sql` is mounted under `/docker-entrypoint-initdb.d/` and that the `db_data` volume is empty (initialization only runs on a fresh volume).
docker-compose logs db - **Git push rejected**: Verify remote URL and credentials in `prepare-app.sh`.
```
- **Table not created**: Make sure `db.sql` is mounted under `/docker-entrypoint-initdb.d/` and that the `db_data` volume is empty (initialization only runs on a fresh volume).
- **Git push rejected**: Verify remote URL and credentials in `prepare-app.sh`.