Enhance Docker setup and installation process

- Updated Docker Compose configuration for web, PHP, and database services.
- Added Redis service with health checks and environment variables.
- Improved error handling and user prompts in installation scripts.
- Introduced example environment variables for easier configuration.
- Updated Nginx and PHP Docker images to latest versions.
This commit is contained in:
deffcolony 2025-04-20 22:46:12 +02:00
parent 83b644f0f5
commit d3f36e48f7
8 changed files with 301 additions and 188 deletions

28
.env.example Normal file
View File

@ -0,0 +1,28 @@
# Example environment variables for the vichan imageboard Docker Compose setup.
# Copy this file to `.env` and customize values as needed.
# Do not commit `.env` with sensitive data to version control.
# General Settings
INSTANCE=0
# Web Service (Nginx) Settings
# SSL_CERT_PATH=/path/to/cert.pem
# SSL_KEY_PATH=/path/to/key.pem
# PHP Service Settings
VICHAN_MYSQL__HOST=db
VICHAN_MYSQL__USER=vichan
VICHAN_MYSQL__NAME=vichan
VICHAN_MYSQL__PASSWORD=vichan!
VICHAN_SECURE_LOGIN_ONLY=0
# Redis Settings
VICHAN_REDIS_HOST=redis
VICHAN_REDIS_PORT=6379
VICHAN_REDIS_PASSWORD=redis!
# Database Service (MariaDB) Settings
MYSQL_DATABASE=vichan
MYSQL_USER=vichan
MYSQL_PASSWORD=vichan!
MYSQL_ROOT_PASSWORD=vichan!!

View File

@ -1,18 +1,22 @@
services:
#nginx webserver + php 8.x
web:
build:
context: .
dockerfile: ./docker/nginx/Dockerfile
container_name: vichan_frontend
restart: unless-stopped
ports:
- "9090:80"
# - "9092:443" # optional for SSL
depends_on:
- db
- php
volumes:
- ./local-instances/${INSTANCE:-0}/www:/var/www/html
- ./docker/nginx/vichan.conf:/etc/nginx/conf.d/default.conf
- ./docker/nginx/nginx.conf:/etc/nginx/nginx.conf
- ./docker/nginx/proxy.conf:/etc/nginx/conf.d/proxy.conf
# - ./docker/nginx/ssl:/etc/nginx/ssl # optional For SSL
links:
- php
@ -20,21 +24,48 @@ services:
build:
context: .
dockerfile: ./docker/php/Dockerfile
container_name: vichan_php
restart: unless-stopped
volumes:
- ./local-instances/${INSTANCE:-0}/www:/var/www
- ./docker/php/www.conf:/usr/local/etc/php-fpm.d/www.conf
- ./docker/php/jit.ini:/usr/local/etc/php/conf.d/jit.ini
#MySQL Service
db:
image: mysql:8.0.35
container_name: db
restart: unless-stopped
tty: true
ports:
- "3306:3306"
environment:
MYSQL_DATABASE: vichan
MYSQL_ROOT_PASSWORD: password
VICHAN_MYSQL__HOST: ${VICHAN_MYSQL__HOST}
VICHAN_MYSQL__USER: ${VICHAN_MYSQL__USER}
VICHAN_MYSQL__NAME: ${VICHAN_MYSQL__NAME}
VICHAN_MYSQL__PASSWORD: ${VICHAN_MYSQL__PASSWORD}
VICHAN_SECURE_LOGIN_ONLY: ${VICHAN_SECURE_LOGIN_ONLY}
VICHAN_REDIS_HOST: ${VICHAN_REDIS_HOST}
VICHAN_REDIS_PORT: ${VICHAN_REDIS_PORT}
VICHAN_REDIS_PASSWORD: ${VICHAN_REDIS_PASSWORD}
depends_on:
- db
db:
image: mariadb:latest
container_name: vichan_mariadb
restart: unless-stopped
ports:
- "9091:3306"
environment:
MYSQL_DATABASE: ${MYSQL_DATABASE}
MYSQL_USER: ${MYSQL_USER}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
volumes:
- ./local-instances/${INSTANCE:-0}/mysql:/var/lib/mysql
- ./local-instances/${INSTANCE:-0}/db:/var/lib/mysql
redis:
image: redis:latest
container_name: vichan_redis
restart: unless-stopped
volumes:
- ./local-instances/${INSTANCE:-0}/redis:/data
command: redis-server --requirepass ${VICHAN_REDIS_PASSWORD}
healthcheck:
test: ["CMD", "redis-cli", "-a", "${VICHAN_REDIS_PASSWORD}", "ping"]
interval: 10s
timeout: 5s
retries: 5
start_period: 30s

View File

@ -1,4 +1,4 @@
FROM nginx:1.25.3-alpine
FROM nginx:1.27.5-alpine-slim
COPY . /code
RUN adduser --system www-data \

View File

@ -1,7 +1,7 @@
# Based on https://github.com/dead-guru/devichan/blob/master/php-fpm/Dockerfile
FROM composer:lts AS composer
FROM php:8.1-fpm-alpine
FROM php:8.3-fpm-alpine
RUN apk add --no-cache \
zlib \

View File

@ -1,47 +1,72 @@
<?php
namespace Vichan\Data\Driver;
// Prevent direct access to this file for security
defined('TINYBOARD') or exit;
// Defines the required methods for any cache driver
interface CacheDriver {
public function get(string $key): mixed;
public function set(string $key, mixed $value, mixed $expires = false): void;
public function delete(string $key): void;
public function flush(): void;
}
// Handles caching using Redis, a fast in-memory data store
class RedisCacheDriver implements CacheDriver {
private string $prefix;
private \Redis $inner;
public function __construct(string $prefix, string $host, int $port, ?string $password, string $database) {
// Sets up the Redis connection
public function __construct(string $prefix, string $host, int $port, ?string $password, int $database) {
$this->inner = new \Redis();
$this->inner->connect($host, $port);
if ($password) {
$this->inner->auth($password);
}
if (!$this->inner->select($database)) {
throw new \RuntimeException('Unable to connect to Redis!');
throw new \RuntimeException('Unable to select Redis database ' . $database);
}
$$this->prefix = $prefix;
$this->prefix = $prefix;
}
// Retrieves a value from the cache by key
public function get(string $key): mixed {
$ret = $this->inner->get($this->prefix . $key);
if ($ret === false) {
// Return null if the key doesn't exist
return null;
}
return \json_decode($ret, true);
}
// Stores a value in the cache with an optional expiration time
public function set(string $key, mixed $value, mixed $expires = false): void {
if ($expires === false) {
$this->inner->set($this->prefix . $key, \json_encode($value));
// Convert the value to JSON for storage
$encodedValue = \json_encode($value);
if ($expires === false || !is_numeric($expires) || $expires <= 0) {
// Store the value without an expiration
$this->inner->set($this->prefix . $key, $encodedValue);
} else {
$expires = $expires * 1000; // Seconds to milliseconds.
$this->inner->setex($this->prefix . $key, $expires, \json_encode($value));
// Store the value with an expiration time (in seconds)
$ttl_seconds = (int)$expires;
$this->inner->setex($this->prefix . $key, $ttl_seconds, $encodedValue);
}
}
// Deletes a specific key from the cache
public function delete(string $key): void {
// Remove the key from Redis
$this->inner->del($this->prefix . $key);
}
// Clears all data in the current Redis database
public function flush(): void {
$this->inner->flushDB();
}

View File

@ -387,7 +387,7 @@ if (file_exists($config['has_installed'])) {
CHANGE `theme` `theme` VARCHAR( 40 ) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL ,
CHANGE `name` `name` VARCHAR( 40 ) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL ,
CHANGE `value` `value` TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL ,
DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;") or eror(db_error());
DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;") or error(db_error());
case 'v0.9.6-dev-10':
query("ALTER TABLE `antispam`
CHANGE `board` `board` VARCHAR( 58 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL;") or error(db_error());
@ -680,11 +680,11 @@ function create_config_from_array(&$instance_config, &$array, $prefix = '') {
session_start();
if ($step == 0) {
// Agreeement
// Agreement
$page['body'] = '
<textarea style="width:700px;height:370px;margin:auto;display:block;background:white;color:black" disabled>' . htmlentities(file_get_contents('LICENSE.md')) . '</textarea>
<p style="text-align:center">
<a href="?step=1">I have read and understood the agreement. Proceed to installation.</a>
<button onclick="window.location.href=\'?step=1\'">I have read and understood the agreement. Proceed to installation.</button>
</p>';
echo Element('page.html', $page);
@ -914,15 +914,27 @@ if ($step == 0) {
'config' => $config,
));
} elseif ($step == 2) {
// Basic config
$page['title'] = 'Configuration';
$sg = new SaltGen();
$config['cookies']['salt'] = $sg->generate();
$config['secure_trip_salt'] = $sg->generate();
$config['secure_password_salt'] = $sg->generate();
// Set database configuration from Docker environment variables, leave empty if not found
$config['db'] = array(
'type' => 'mysql', // Default, required for MySQL
'server' => getenv('VICHAN_MYSQL__HOST') !== false ? getenv('VICHAN_MYSQL__HOST') : '',
'database' => getenv('VICHAN_MYSQL__NAME') !== false ? getenv('VICHAN_MYSQL__NAME') : '',
'user' => getenv('VICHAN_MYSQL__USER') !== false ? getenv('VICHAN_MYSQL__USER') : '',
'password' => getenv('VICHAN_MYSQL__PASSWORD') !== false ? getenv('VICHAN_MYSQL__PASSWORD') : '',
);
// Append secure_login_only to $_SESSION['more'] if VICHAN_SECURE_LOGIN_ONLY is set from Docker environment variables
if (getenv('VICHAN_SECURE_LOGIN_ONLY') !== false) {
$secure_login_only = (int)getenv('VICHAN_SECURE_LOGIN_ONLY');
$_SESSION['more'] .= "\n\$config['cookies']['secure_login_only'] = $secure_login_only;";
}
echo Element('page.html', array(
'body' => Element('installer/config.html', array(
'config' => $config,
@ -973,8 +985,6 @@ if ($step == 0) {
echo Element('page.html', $page);
}
} elseif ($step == 4) {
// SQL installation
buildJavascript();
$sql = @file_get_contents('install.sql') or error("Couldn't load install.sql.");
@ -1004,10 +1014,18 @@ if ($step == 0) {
}
$page['title'] = 'Installation complete';
$page['body'] = '<p style="text-align:center">Thank you for using vichan. Please remember to report any bugs you discover. <a href="https://github.com/vichan-devel/vichan/wiki/Configuration-Basics">How do I edit the config files?</a></p>';
$page['body'] = '<p style="text-align:center">Thank you for installing vichan. Please report any bugs you discover. <a href="https://github.com/vichan-devel/vichan/wiki/Configuration-Basics">How do I edit the config files?</a></p>';
// notice and button
$page['body'] .= '<div class="ban"><h2>Next Steps</h2>' .
'<p>You can now log in to the admin panel at <strong>/mod.php</strong> using the default credentials: <strong>Username: admin</strong>, <strong>Password: password</strong>.</p>' .
'<p><strong>Important:</strong> For security, please change the administrator password immediately after logging in.</p>' .
'<p style="text-align:center"><button onclick="window.location.href=\'/mod.php\'">Go to Admin Panel</button></p></div>';
if (!empty($sql_errors)) {
$page['body'] .= '<div class="ban"><h2>SQL errors</h2><p>SQL errors were encountered when trying to install the database. This may be the result of using a database which is already occupied with a vichan installation; if so, you can probably ignore this.</p><p>The errors encountered were:</p><ul>' . $sql_errors . '</ul><p><a href="?step=5">Ignore errors and complete installation.</a></p></div>';
$page['body'] .= '<div class="ban"><h2>SQL errors</h2><p>SQL errors were encountered when trying to install the database. This may be the result of using a database which is already occupied with a vichan installation; if so, you can probably ignore this.</p><p>The errors encountered were:</p><ul>' . $sql_errors . '</ul>' .
'<p style="text-align:center;color:#d00"><strong>Warning:</strong> Ignoring errors is not recommended and may cause installation issues.</p>' .
'<p style="text-align:center"><button onclick="window.location.href=\'?step=5\'">Next</button></p></div>';
} else {
$boards = listBoards();
foreach ($boards as &$_board) {
@ -1024,7 +1042,17 @@ if ($step == 0) {
echo Element('page.html', $page);
} elseif ($step == 5) {
$page['title'] = 'Installation complete';
$page['body'] = '<p style="text-align:center">Thank you for using vichan. Please remember to report any bugs you discover.</p>';
$page['body'] = '<p style="text-align:center">Thank you for installing vichan. Please report any bugs you discover.</p>';
// onboarding notice and button to mod.php
$page['body'] .= '<div class="ban"><h2>Next Steps</h2>' .
'<p>You can now log in to the admin panel at <strong>/mod.php</strong> using the default credentials:</p>' .
'<ul>' .
'<li><strong>Username:</strong> admin</li>' .
'<li><strong>Password:</strong> password</li>' .
'</ul>' .
'<p><strong>Important:</strong> For security, please change the administrator password immediately after logging in.</p>' .
'<p style="text-align:center"><button onclick="window.location.href=\'/mod.php\'">Go to Admin Panel</button></p></div>';
$boards = listBoards();
foreach ($boards as &$_board) {

View File

@ -45,12 +45,13 @@
{% endfor %}
</ul>
{% if errors %}
<p style="text-align:center"><a href="?step=2">Click here to ignore errors and attempt installing anyway (not recommended).</a></p>
<p style="text-align:center;color:#d00"><strong>Warning:</strong> Ignoring these problems is not recommended and may cause installation issues.</p>
<p style="text-align:center"><button onclick="window.location.href='?step=2'">Proceed Anyway</button></p>
{% else %}
<p style="text-align:center"><a href="?step=2">Click here to proceed with installation.</a></p>
<p style="text-align:center"><button onclick="window.location.href='?step=2'">Next</button></p>
{% endif %}
{% else %}
<p>There were no errors or warnings. Good!</p>
<p style="text-align:center"><a href="?step=2">Clik here to proceed with installation.</a></p>
<p style="text-align:center"><button onclick="window.location.href='?step=2'">Next</button></p>
{% endif %}
</div>

View File

@ -14,7 +14,7 @@
<input type="text" id="db_user" name="db[user]" value="{{ config.db.user }}">
<label for="db_pass">Password:</label>
<input type="password" id="db_pass" name="db[password]" value="">
<input type="password" id="db_pass" name="db[password]" value="{{ config.db.password }}">
</fieldset>
<p style="text-align:center" class="unimportant">The following is all later configurable. For more options, <a href="http://tinyboard.org/docs/?p=Config">edit your configuration files</a> after installing.</p>
<fieldset>