Skip to content

Instantly share code, notes, and snippets.

@vonKrafft
Created March 18, 2019 21:02
Show Gist options
  • Save vonKrafft/18019dedb49eae01035489bbb9ff856c to your computer and use it in GitHub Desktop.
Save vonKrafft/18019dedb49eae01035489bbb9ff856c to your computer and use it in GitHub Desktop.
Serveur Web avec Docker : Nginx, PHP et PostgreSQL (https://vonkrafft.fr/console/serveur-web-docker-nginx-php-postgres/)
#!/bin/bash
mkdir -p docker-web/www # Webroot dans lequel sera stocké le contenu statique du site
mkdir -p docker-web/log # Pour enregistrer les journaux de Nginx
mkdir -p docker-web/data # Pour stocker le contenu de la base de données
echo "<?php phpinfo();" > docker-web/www/index.php
docker-compose up -d
version: '3'
services:
web-nginx:
image: nginx:stable-alpine
container_name: web-nginx
volumes:
- "./docker-web/www:/usr/share/nginx/html:ro"
- "./docker-web/log:/var/log/nginx"
- "./docker-web/nginx.conf:/etc/nginx/nginx.conf:ro"
ports:
- "127.0.0.1:80:80"
web-php:
build: ./docker-web/.build-php
container_name: web-php
volumes:
- "./docker-web/www:/script:ro"
web-pgsql:
image: postgres:alpine
container_name: web-pgsql
volumes:
- "./docker-web/data:/var/lib/postgresql/data"
environment:
POSTGRES_USER: foobar
POSTGRES_PASSWORD: foobar
POSTGRES_DB: foobar
FROM php:fpm-alpine
RUN apk update && apk add --no-cache \
postgresql-dev \
&& docker-php-ext-install -j$(nproc) pgsql \
&& docker-php-ext-install -j$(nproc) pdo_pgsql
<?php
$dbconn = pg_connect('host=web-pgsql port=5432 dbname=foobar user=foobar password=foobar')
or die('Could not connect');
echo '<pre>' . var_export(pg_version($dbconn), true) . '</pre>';
pg_close($dbconn);
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
keepalive_timeout 65;
server_tokens off;
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.php index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
root /usr/share/nginx/html;
include fastcgi_params;
fastcgi_pass web-php:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /script$fastcgi_script_name;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment