Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docker #6

Open
wants to merge 21 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
756708b
Remove -x settings from siikr.conf
alekratz Feb 9, 2024
2ef6883
docker: add Dockerfile and supervisord.conf
alekratz Feb 23, 2024
0065b7d
docker: Rename siikr.conf -> siikr.example.env
alekratz Feb 23, 2024
8b95507
Move Dockerfile -> docker/php-fpm.Dockerfile, and add DB configuratio…
alekratz Feb 23, 2024
01bdc9e
Add docker-compose, postgres.Dockerfile, create_database.sh script, a…
alekratz Feb 23, 2024
f422ea5
Add siikr.env to root .gitignore
alekratz Feb 23, 2024
4c666e0
docker: Add DB host configuration and get_db() function
alekratz Feb 23, 2024
b5a8309
Add postgres_data/ directory to .gitignore
alekratz Feb 24, 2024
0e2d88e
docker: Add nginx to the PHP FPM container
alekratz Feb 24, 2024
3636659
Add siikr/internal/disks.php file creation to php-fpm.Dockerfile
alekratz Feb 24, 2024
d1a5266
Fix a couple of small bugs in index.php and show_page.php
alekratz Feb 24, 2024
c0d575b
fix(docker): Docker works now
meyerkev Aug 20, 2024
734c0ed
siikrweb must exist as a functional entity
meyerkev Aug 20, 2024
548cab5
One quick doc line
meyerkev Aug 20, 2024
7950608
fix(postgres): Removing the postgres superuser password
meyerkev Aug 20, 2024
0ecd316
Made auth an entrypoint script
meyerkev Aug 20, 2024
155aabd
fixes: Using postgres 14 and proper db connections
meyerkev Aug 20, 2024
bb12812
fix(php): Database connections now work
meyerkev Aug 20, 2024
4919abf
Fixed extension installs
meyerkev Aug 20, 2024
6c7fb2f
Fixing get_db()
meyerkev Aug 20, 2024
598d13c
Adding two helper functions for databases
meyerkev Aug 20, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ siikr/dev/**
siikr/auth/**
siikr/internal/disks.php
siikr/management/archive_blogs.php
siikr.env

postgres_data/
49 changes: 49 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
DOCKER_REPO?=meyerkev248/siikr

reset:
$(MAKE) build
$(MAKE) down
$(MAKE) up

reset-clean:
$(MAKE) build
$(MAKE) clean
$(MAKE) up

.PHONY: build
build:
docker-compose build

.PHONY: up
up:

docker-compose up -d

.PHONY: down
down:
docker-compose down

.PHONY: clean
clean:
docker-compose down -v

.PHONY: logs
logs:
docker-compose logs -f

.PHONY: docker-push
docker-push:
@IMAGES="my-postgres:latest my-php-fpm:latest"; \
TAG=$$(git rev-parse --short HEAD); \
if [ -n "$$(git status --porcelain)" ]; then \
TAG="$$TAG-dirty"; \
fi; \
for IMAGE in $$IMAGES; do \
docker tag $$IMAGE ${DOCKER_REPO}:$$TAG; \
echo "Pushing $$IMAGE to ${DOCKER_REPO}:$$TAG"; \
docker push ${DOCKER_REPO}:$$TAG; \
done

.PHONY: debug
debug:
docker-compose exec php bash
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,20 @@ IYKYK.
Best done on a clean Ubuntu or debian server.

0. clone this repo.
1. Fill out (at a minimum) the first three lines in `siikr.conf`
1. Copy `siikr.example.env` to `siikr.env` and fill out (at a minimum) the first three lines.
2. If not already in a terminal, switch to one and run `chmod +x setup_simple.sh`
3. Then run `sudo ./setup_simple.sh` and follow the prompts.
4. Pray.
5. Submit an issue if it fails.

# Docker installation

Docker gives you a clean server, but the install may be a little more involved.

0. clone this repo.
1. Make sure you have both Docker and `docker-compose` installed.
1. TODO: Document how to setup the API key sanely
2. Copy `siikr.example.env` to `siikr.env` and fill out (at a minimum) the first three lines while noticing the hardcoded postgres user siikrweb.
3. `make build up && open localhost:8080 && make logs` - This will follow logs in the terminal and open the browser window
4. You'll need to do some magic with DDNS for callback reasons in your router or via some other mechanism. This is _not_ documented.

43 changes: 43 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

version: '3'

services:
postgres:
build:
context: .
dockerfile: docker/postgres.Dockerfile
image: my-postgres:latest
env_file:
- siikr.env
volumes:
- postgres_data:/var/lib/postgresql/data
- ./siikr/siikr_db_setup.sql:/docker-entrypoint-initdb.d/01_siikr_db_setup.sql
healthcheck:
test: ["CMD", "pg_isready", "-U", "postgres"]
interval: 10s
retries: 5
start_period: 30s
timeout: 5s

php:
build:
context: .
dockerfile: docker/php-fpm.Dockerfile
image: my-php-fpm:latest
ports:
- "8080:80"
env_file:
- siikr.env
depends_on:
postgres:
condition: service_healthy
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:80"]
interval: 1m30s
timeout: 30s
retries: 5
start_period: 30s

volumes:
postgres_data:

18 changes: 18 additions & 0 deletions docker/create_database.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/bash
set -eo pipefail

script_path="$(realpath "$0")"
script_dir="$(realpath "$(dirname "$script_path")"/..)"

if [[ ! -f "$script_dir/siikr.env" ]]; then
echo "Could not find $script_dir/siikr.env. Make sure to copy siikr.example.env and fill it out with your deploy information."
exit 1
fi

source "$script_dir/siikr.env"

docker-compose up -d postgres
echo "Waiting for the database to be created and to come online..."
sleep 3
docker-compose exec -T -u postgres postgres psql -U "$POSTGRES_USER" -d "$POSTGRES_DB" < "$script_dir/siikr/siikr_db_setup.sql"
#docker-compose down postgres
16 changes: 16 additions & 0 deletions docker/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash

if [ -f siikr.env ]; then
export $(cat siikr.env | xargs)
fi

cat > /var/www/html/auth/credentials.php <<EOF
<?php
\$api_key = '$tumblr_API_consumer_key';
\$db_name = '$POSTGRES_DB';
\$db_user = '$POSTGRES_USER';
\$db_pass = '$POSTGRES_PASSWORD';
\$db_host = 'postgres';
EOF

exec "$@"
23 changes: 23 additions & 0 deletions docker/nginx-siikr.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
server {
listen 80 default_server;
server_name _;

root /var/www/html;

index index.html index.php;


location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}

location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass 127.0.0.1:9000;
}

error_log /dev/stdout;
access_log /dev/stdout;
}
1 change: 1 addition & 0 deletions docker/php-fpm-siikr.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[www]
65 changes: 65 additions & 0 deletions docker/php-fpm.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
FROM php:8.2-fpm

# Install dependencies
RUN apt-get update && \
apt-get install -y libpq-dev hunspell hunspell-en-us libzmq5-dev supervisor nginx gcc git

# Compile and install php-zmq
#
# PHP-ZMQ has some ... interesting issues with the Docker container.
# * There does not appear to be a package for it(?) and it doesn't come installed
# with PHP.
# * The package available on PECL is about 8 years old and the repo is abandoned.
# So we have to compile it from source. However, this is not that painful. Just
# annoying that we have to do it ourselves.

# TODO: make this into a script and just copy it over rather than having this
# all embedded in the Dockerfile
# TODO: choose a specific tag or commit to clone from rather than using master,
# for reproducible builds

RUN cd /tmp && \
meyerkev marked this conversation as resolved.
Show resolved Hide resolved
git clone "https://github.com/zeromq/php-zmq" && \
cd php-zmq && \
phpize && \
./configure && \
make && make install && \
cd .. && rm -rf php-zmq

# Enable PHP extensions
RUN docker-php-ext-configure pgsql && \
docker-php-ext-install pgsql && \
docker-php-ext-enable zmq

# Add php-fpm config
COPY docker/php-fpm-siikr.conf /usr/local/etc/php-fpm.d

# Add nginx config
COPY docker/nginx-siikr.conf /etc/nginx/sites-available/default

# Initialize supervisord
RUN mkdir -p /var/log/supervisor
COPY docker/supervisord.conf /etc/supervisor/conf.d/supervisord.conf

WORKDIR /var/www/html

# TODO: set up php-fpm config like the setup_simple.sh script does

# Clean out /var/www/html and then copy siikr
RUN rm /var/www/html/* -rf
COPY siikr/. /var/www/html

# Create database configuration
RUN mkdir -p /var/www/html/auth


RUN cat > /var/www/html/internal/disks.php <<EOF
<?php
\$db_disk = '$pg_disk';
EOF

COPY docker/entrypoint.sh /entrypoint.sh

ENTRYPOINT [ "/entrypoint.sh" ]

CMD ["/usr/bin/supervisord"]
4 changes: 4 additions & 0 deletions docker/postgres.Dockerfile
meyerkev marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ARG POSTGRES_VERSION=14
FROM postgres:$(POSTGRES_VERSION)-bookworm
RUN apt-get update && \
apt-get -y install hunspell hunspell-en-us postgresql-$(POSTGRES_VERSION)-pgvector
35 changes: 35 additions & 0 deletions docker/supervisord.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
[supervisord]
user=root
nodaemon=true
logfile=/dev/null
logfile_maxbytes=0

[program:php-fpm]
command=php-fpm
user=root
autostart=true
autorestart=true
# Redirect stderr to stdout, and log to stdout
stdout_logfile=/dev/fd/1
stdout_logfile_maxbytes=0
redirect_stderr=true

[program:msgrouter]
command=php /var/www/html/routing/msgRouter.php
user=root
autostart=true
autorestart=true
# Redirect stderr to stdout, and log to stdout
stdout_logfile=/dev/fd/1
stdout_logfile_maxbytes=0
redirect_stderr=true

[program:nginx]
command=nginx -g 'daemon off;'
user=root
autostart=true
autorestart=true
# Redirect stderr to stdout, and log to stdout
stdout_logfile=/dev/fd/1
stdout_logfile_maxbytes=0
redirect_stderr=true
7 changes: 6 additions & 1 deletion setup_simple.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ set -eo pipefail
script_path="$(realpath "$0")"
script_dir="$(dirname "$script_path")"

source "$script_dir/siikr.conf"
if [[ ! -f "$script_dir/siikr.env" ]]; then
echo "Could not find $script_dir/siikr.env. Make sure to copy siikr.example.env and fill it out with your deploy information."
exit 1
fi

source "$script_dir/siikr.env"

update_install_packages() {
local confirm
Expand Down Expand Up @@ -81,6 +85,7 @@ set_credentials_file() {
\$db_name = '$siikr_db';
\$db_user = '$pg_user';
\$db_pass = '$pg_pass';
\$db_host = '$pg_host';
EOF

cat > "$script_dir/siikr/internal/disks.php" << EOF
Expand Down
11 changes: 0 additions & 11 deletions siikr.conf

This file was deleted.

27 changes: 27 additions & 0 deletions siikr.example.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/bash

tumblr_API_consumer_key="PUT YOUR KEY HERE"
pg_pass="PUT YOUR DB PASSWORD HERE"
# If using Docker, this should be set to /var/lib/postgresql/data
pg_disk="PUT THE MOUNTPOINT OF WHEREVER THE DISK DRIVE STORING THE DB DATA IS HERE"
document_root="/var/www/html/" #location you want to install siikr into
php_dir="/usr/bin/php" #location of your active php install
php_user="www-data" #the user your php server runs as
pg_user="siikrweb" #postgres username that database access will be performed as.
siikr_db="siikrdb" #name of siikr database to create

# These are Docker-specific environment variables, don't touch these.
POSTGRES_USER="$pg_user"
POSTGRES_PASSWORD="$pg_pass"
POSTGRES_DB="$siikr_db"

# If you're using Docker, follow these
# These are Docker-specific environment variables, don't touch these.
# https://github.com/containers/podman-compose/issues/718
# POSTGRES_USER="$pg_user"
# POSTGRES_PASSWORD="$pg_pass"
# POSTGRES_DB="$siikr_db"

POSTGRES_USER=siikrweb
POSTGRES_PASSWORD="testtesttest"
POSTGRES_DB=siikrdb
2 changes: 1 addition & 1 deletion siikr/get_tags.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
require_once 'internal/globals.php';
$blog_uuid = $_GET["blog_uuid"];
try {
$db = new PDO("pgsql:dbname=$db_name", "www-data", null);
$db = get_db();
$blog_info = (object)[
"valid" => false,
"blog_uuid" => $blog_uuid,
Expand Down
2 changes: 1 addition & 1 deletion siikr/index.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
require_once "squid_game.php";
if($_GET['lemmein']==true) {
if(isset($_GET['lemmein'])) {
require_once "show_page.php";
} else if($squid_game_warn == false && $squid_game == true) {
require_once "maintenance.php";
Expand Down
3 changes: 1 addition & 2 deletions siikr/internal/archive.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
require_once 'globals.php';

$archiver_uuid = uuid_create(UUID_TYPE_RANDOM);
$userInfo = posix_getpwuid(posix_geteuid());
$db = new PDO("pgsql:dbname=$db_name", $userInfo["name"], null);
$db = get_db();

require_once 'lease.php';

Expand Down
Loading