Add project files
This commit is contained in:
20
00_vortrags-files/Dockerfile
Normal file
20
00_vortrags-files/Dockerfile
Normal file
@@ -0,0 +1,20 @@
|
||||
FROM php:8.1-fpm
|
||||
WORKDIR /var/www/html
|
||||
|
||||
COPY composer.json ./
|
||||
|
||||
RUN apt-get update && apt-get install -y \
|
||||
zip \
|
||||
unzip \
|
||||
&& docker-php-ext-install pdo pdo_mysql
|
||||
|
||||
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
|
||||
|
||||
RUN composer install --no-scripts --no-autoloader
|
||||
|
||||
COPY . .
|
||||
|
||||
RUN composer dump-autoload --optimize
|
||||
|
||||
EXPOSE 9000
|
||||
CMD ["php-fpm"]
|
||||
14
00_vortrags-files/composer.json
Normal file
14
00_vortrags-files/composer.json
Normal file
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"name": "example/php-docker-demo",
|
||||
"description": "Eine einfache PHP-Anwendung für Docker",
|
||||
"type": "project",
|
||||
"require": {
|
||||
"php": ">=8.1",
|
||||
"monolog/monolog": "^2.8"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"App\\": "src/"
|
||||
}
|
||||
}
|
||||
}
|
||||
29
00_vortrags-files/nginx.conf
Normal file
29
00_vortrags-files/nginx.conf
Normal file
@@ -0,0 +1,29 @@
|
||||
worker_processes 1;
|
||||
|
||||
events {
|
||||
worker_connections 1024;
|
||||
}
|
||||
|
||||
http {
|
||||
include mime.types;
|
||||
default_type application/octet-stream;
|
||||
|
||||
sendfile on;
|
||||
keepalive_timeout 65;
|
||||
|
||||
server {
|
||||
listen 9090;
|
||||
server_name localhost;
|
||||
root /var/www/html/public;
|
||||
index index.php;
|
||||
|
||||
location ~ \.php$ {
|
||||
fastcgi_pass 127.0.0.1:9000;
|
||||
fastcgi_index index.php;
|
||||
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
|
||||
include fastcgi_params;
|
||||
}
|
||||
}
|
||||
|
||||
include servers/*;
|
||||
}
|
||||
69
00_vortrags-files/public/index.php
Normal file
69
00_vortrags-files/public/index.php
Normal file
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
require __DIR__ . '/../vendor/autoload.php';
|
||||
|
||||
use App\HelloWorld;
|
||||
use App\Database;
|
||||
use Monolog\Logger;
|
||||
use Monolog\Handler\StreamHandler;
|
||||
|
||||
// Logger initialisieren
|
||||
$log = new Logger('app');
|
||||
$log->pushHandler(new StreamHandler('php://stdout', Logger::INFO));
|
||||
|
||||
$log->info('Anwendung gestartet');
|
||||
|
||||
// HelloWorld-Klasse verwenden
|
||||
$hello = new HelloWorld();
|
||||
$message = $hello->getMessage();
|
||||
|
||||
// Datenbankverbindung testen
|
||||
$dbConfig = [
|
||||
'host' => getenv('DB_HOST') ?: 'localhost',
|
||||
'user' => getenv('DB_USER') ?: 'root',
|
||||
'pass' => getenv('DB_PASS') ?: 'secret',
|
||||
'name' => getenv('DB_NAME') ?: 'test'
|
||||
];
|
||||
|
||||
$db = new Database($dbConfig);
|
||||
$dbStatus = $db->testConnection() ? 'erfolgreich' : 'fehlgeschlagen';
|
||||
|
||||
?>
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Docker PHP Demo</title>
|
||||
<style>
|
||||
body { font-family: Arial, sans-serif; margin: 40px; line-height: 1.6; }
|
||||
.container { max-width: 800px; margin: 0 auto; }
|
||||
.status { padding: 10px; margin: 10px 0; border-radius: 4px; }
|
||||
.success { background-color: #d4edda; color: #155724; }
|
||||
.error { background-color: #f8d7da; color: #721c24; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<h1>PHP Docker Demo</h1>
|
||||
<p>Dies ist eine einfache PHP-Anwendung für Docker-Demo.</p>
|
||||
|
||||
<h2>Status</h2>
|
||||
<div class="status success">
|
||||
<p><?php echo $message; ?></p>
|
||||
</div>
|
||||
|
||||
<h2>Datenbankverbindung</h2>
|
||||
<div class="status <?php echo $dbStatus === 'erfolgreich' ? 'success' : 'error'; ?>">
|
||||
<p>Verbindung: <?php echo $dbStatus; ?></p>
|
||||
<p>Host: <?php echo $dbConfig['host']; ?></p>
|
||||
</div>
|
||||
|
||||
<h2>PHP Info</h2>
|
||||
<p>PHP Version: <?php echo phpversion(); ?></p>
|
||||
<p>Loaded Extensions:</p>
|
||||
<ul>
|
||||
<?php foreach(get_loaded_extensions() as $ext): ?>
|
||||
<li><?php echo $ext; ?></li>
|
||||
<?php endforeach; ?>
|
||||
</ul>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
41
00_vortrags-files/src/Database.php
Normal file
41
00_vortrags-files/src/Database.php
Normal file
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
namespace App;
|
||||
|
||||
class Database
|
||||
{
|
||||
private array $config;
|
||||
|
||||
public function __construct(array $config)
|
||||
{
|
||||
$this->config = $config;
|
||||
}
|
||||
|
||||
public function testConnection(): bool
|
||||
{
|
||||
try {
|
||||
$dsn = sprintf(
|
||||
'mysql:host=%s;dbname=%s',
|
||||
$this->config['host'],
|
||||
$this->config['name']
|
||||
);
|
||||
|
||||
$options = [
|
||||
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
|
||||
\PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC,
|
||||
\PDO::ATTR_TIMEOUT => 5,
|
||||
];
|
||||
|
||||
$pdo = new \PDO(
|
||||
$dsn,
|
||||
$this->config['user'],
|
||||
$this->config['pass'],
|
||||
$options
|
||||
);
|
||||
|
||||
return true;
|
||||
} catch (\PDOException $e) {
|
||||
// In einer realen Anwendung würde hier geloggt werden
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
10
00_vortrags-files/src/HelloWorld.php
Normal file
10
00_vortrags-files/src/HelloWorld.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
namespace App;
|
||||
|
||||
class HelloWorld
|
||||
{
|
||||
public function getMessage(): string
|
||||
{
|
||||
return 'Hallo Welt! Die PHP-Anwendung läuft in Docker.';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user