| .. | ||
| db.sql | ||
| docker-compose.yml | ||
| Dockerfile | ||
| drop.php | ||
| index.php | ||
| prepare-app.sh | ||
| README.md | ||
| remove-app.sh | ||
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.
Table of Contents
- Overview
- Features
- Prerequisites
- Installation & Configuration
- Usage
- Deployment to Plesk
- Deployment Cleanup (
remove-app.sh) - Detailed File Structure
- Troubleshooting
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.
A button allows to choose between light and dark themes via JavaScript and localStorage.
The project includes:
- A PostgreSQL container with automatic table initialization via
db.sql. - A PHP 8.2 + Apache container serving the application.
- A Dockerfile to build the web image (with the
pdo_pgsqlextension installed). - 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
visitorstable on each load (INSERT INTO visitors DEFAULT VALUES). - Automatically creates the table if it doesn’t exist (
CREATE TABLE IF NOT EXISTS visitors …). - Displays the visit counter.
- Light/dark theme toggle, persisted in
localStorage. - Automated Git deployments (Plesk).
- Remote cleanup of pushed content.
Prerequisites
- Docker (>= 20.x)
- Docker Compose (>= 1.27.x)
- Git (for the deployment scripts)
- Access to a Plesk instance with a Git-configured repository (for
prepare-app.shandremove-app.sh)
Installation & Configuration
Project Structure
├── db.sql # SQL script to initialize the `visitors` table
├── Dockerfile # Builds the PHP + Apache + pdo_pgsql image
├── docker-compose.yml # Docker orchestration (db + web)
├── prepare-app.sh # Deployment script to Plesk
├── 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:
POSTGRES_DB: visitors
POSTGRES_USER: visitor_user
POSTGRES_PASSWORD: secret
And for the web service:
DB_HOST: db
DB_PORT: 5432
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).
chmod +x prepare-app.sh
./prepare-app.sh
Usage
Application Entry Point
index.php performs:
- Connects to PostgreSQL via PDO (
pgsql:host=$dbHost;port=$dbPort;dbname=$dbName). - Runs an embedded SQL migration to create the
visitorstable if it doesn’t exist. - Inserts a new row:
$pdo->exec("INSERT INTO visitors DEFAULT VALUES"); - Retrieves the total count:
$count = $pdo->query("SELECT COUNT(*) FROM visitors")->fetchColumn(); - Generates the HTML/CSS/JS to display the counter and manage the theme.
Table Reset (drop.php)
The script drop.php connects to the same database and runs:
$pdo->exec("DROP TABLE IF EXISTS visitors");
echo "Table visitors dropped.";
On error it returns HTTP 500 and displays the exception message.
Deployment to Plesk
To deploy automatically:
- Configure
GIT_REMOTE_URLandBRANCHinprepare-app.sh. - Make it executable and run:
chmod +x prepare-app.sh ./prepare-app.sh
This force-pushes the branch to Plesk’s httpdocs, updating the remote site.
Deployment Cleanup (remove-app.sh)
To clear the remote content without touching the local repository:
- Make it executable:
chmod +x remove-app.sh - Run:
./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:
- db (PostgreSQL 13) initialized via
./db.sql. - web (PHP 8.2 + Apache with pdo_pgsql) linked to
db, exposing port 8080.
- db (PostgreSQL 13) initialized via
- Dockerfile: Base
php:8.2-apache, installslibpq-devandpdo_pgsql, copiessrc/to/var/www/html/. - db.sql: SQL to auto-create the
visitorstable. - 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
visitorstable.
Troubleshooting
- PostgreSQL connection error: Ensure the
dbservice is running; check logs:docker-compose logs db - Table not created: Make sure
db.sqlis mounted under/docker-entrypoint-initdb.d/and that thedb_datavolume is empty (initialization only runs on a fresh volume). - Git push rejected: Verify remote URL and credentials in
prepare-app.sh.