From cbda27a044d39bb509f1a907abaa821d42984f18 Mon Sep 17 00:00:00 2001 From: adjoly Date: Sat, 15 Feb 2025 19:22:32 +0100 Subject: [PATCH] =?UTF-8?q?=E3=80=8C=F0=9F=8F=97=EF=B8=8F=E3=80=8D=20wip:?= =?UTF-8?q?=20wordpress=20nearly=20working=20need=20to=20fix=20title=20set?= =?UTF-8?q?=20and=20admin=20name=20and=20pass?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Makefile | 5 +-- srcs/cmd/mariadb/entrypoint/entrypoint.go | 3 +- srcs/cmd/wordpress/entrypoint/entrypoint.go | 10 +++--- .../nginx/templates/testing.conf.template | 26 ++++++++++++++ .../configs/nginx/templates/www.conf.template | 2 +- srcs/docker-compose.yml | 21 +++++++---- srcs/docker/wordpress/Dockerfile | 16 ++++----- srcs/docker/wordpress/wp-config-docker.php | 35 +++++++++++++++++++ 8 files changed, 95 insertions(+), 23 deletions(-) create mode 100644 srcs/configs/nginx/templates/testing.conf.template diff --git a/Makefile b/Makefile index 30c4585..9f4d643 100644 --- a/Makefile +++ b/Makefile @@ -33,10 +33,11 @@ stop: docker compose -f $(DOCKER_CONTEXT)docker-compose.yml stop clean: stop - docker system prune + docker system prune -f fclean: clean - docker system prune -a + docker system prune -af + docker volume prune -af clean-db: docker stop inception-db diff --git a/srcs/cmd/mariadb/entrypoint/entrypoint.go b/srcs/cmd/mariadb/entrypoint/entrypoint.go index 9e3f5dd..59ebaf4 100644 --- a/srcs/cmd/mariadb/entrypoint/entrypoint.go +++ b/srcs/cmd/mariadb/entrypoint/entrypoint.go @@ -123,7 +123,7 @@ func configureMariaDB(rootPassword, user, password, database string) { CREATE DATABASE IF NOT EXISTS %s; CREATE USER IF NOT EXISTS '%s'@'%%'; GRANT ALL PRIVILEGES ON %s.* TO '%s'@'localhost' IDENTIFIED BY '%s'; - GRANT ALL PRIVILEGES ON %s.* TO '%s'@'%%'; + GRANT ALL PRIVILEGES ON %s.* TO '%s'@'%%' IDENTIFIED BY '%s'; FLUSH PRIVILEGES; `, env.EscapeEnv(rootPassword), @@ -134,6 +134,7 @@ func configureMariaDB(rootPassword, user, password, database string) { env.EscapeEnv(password), env.EscapeEnv(database), env.EscapeEnv(user), + env.EscapeEnv(password), )) // Capture standard output and error diff --git a/srcs/cmd/wordpress/entrypoint/entrypoint.go b/srcs/cmd/wordpress/entrypoint/entrypoint.go index 4405e8e..d1f3e55 100644 --- a/srcs/cmd/wordpress/entrypoint/entrypoint.go +++ b/srcs/cmd/wordpress/entrypoint/entrypoint.go @@ -49,25 +49,24 @@ func main() { makeFpmConf() - _, err := os.ReadFile("/usr/src/wordpress/wp-config.php") + _, err := os.ReadFile("/var/www/wordpress/wp-config.php") if err != nil { _log.Log("note", "Configuring wordpress...") - content, err := os.ReadFile("/usr/src/wordpress/wp-config-docker.php") + content, err := os.ReadFile("/var/www/wordpress/wp-config-docker.php") if err != nil { log.Fatal(err) } res := bytes.Replace([]byte(content), []byte("put your unique phrase here"), []byte(pass.GenStrPass(32)), -1) - if err := os.WriteFile("/usr/src/wordpress/wp-config.php", res, 0660); err != nil { + if err := os.WriteFile("/var/www/wordpress/wp-config.php", res, 0660); err != nil { log.Fatal(err) } - cmd := exec.Command("chown", "www-data:www-data", "/usr/src/wordpress/wp-config.php") + cmd := exec.Command("chown", "www-data:www-data", "/var/www/wordpress/wp-config.php") cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr if err := cmd.Run(); err != nil { log.Fatal(err) } - _log.Log("note", "wp configured") } } @@ -75,6 +74,7 @@ func main() { cmd := exec.Command(args[1], args[2:]...) cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr + cmd.Env = os.Environ() cmd.Stdin = os.Stdin if err := cmd.Run(); err != nil { fmt.Printf("Error running MariaDB: %v\n", err) diff --git a/srcs/configs/nginx/templates/testing.conf.template b/srcs/configs/nginx/templates/testing.conf.template new file mode 100644 index 0000000..7e5686e --- /dev/null +++ b/srcs/configs/nginx/templates/testing.conf.template @@ -0,0 +1,26 @@ +server { + listen 8443 ssl; + + server_name _; + + root /var/www/wordpress; + index index.php; + + ssl_certificate $NGINX_SSL_CERT_FILE; + ssl_certificate_key $NGINX_SSL_KEY_FILE; + ssl_protocols TLSv1.2 TLSv1.3; + ssl_session_timeout 10m; + + keepalive_timeout 60; + + location / { + try_files $${q}uri $${q}uri/ =404; + } + + location ~ \.php$ { + fastcgi_pass $NGINX_PHP_HOST:9000; + fastcgi_index index.php; + fastcgi_split_path_info ^(.+\.php)(/.+)$; + include fastcgi.conf; + } +} diff --git a/srcs/configs/nginx/templates/www.conf.template b/srcs/configs/nginx/templates/www.conf.template index 7aa0ec9..b5dfcab 100644 --- a/srcs/configs/nginx/templates/www.conf.template +++ b/srcs/configs/nginx/templates/www.conf.template @@ -3,7 +3,7 @@ server { server_name adjoly.42.fr www.adjoly.42.fr; - root /var/www/html; + root /var/www/wordpress; index index.php; ssl_certificate $NGINX_SSL_CERT_FILE; diff --git a/srcs/docker-compose.yml b/srcs/docker-compose.yml index 43f77ee..67bdb12 100644 --- a/srcs/docker-compose.yml +++ b/srcs/docker-compose.yml @@ -23,15 +23,15 @@ services: - NGINX_SSL_CERT_FILE=/etc/nginx/ssl/adjoly-wp.crt depends_on: wordpress-php: - condition: service_started + condition: service_healthy db: - condition: service_started + condition: service_healthy volumes: - - wp-site:/var/www/html + - wp-site:/var/www/wordpress - ./configs/nginx/templates:/etc/nginx/templates - ./configs/nginx/entry:/docker-entrypoint.d ports: - - "443:443" + - "8443:8443" restart: unless-stopped wordpress-php: @@ -43,14 +43,23 @@ services: - inception depends_on: db: - condition: service_started + condition: service_healthy environment: + - WORDPRESS_DB_NAME=knl + - WORDPRESS_DB_USER=kanel + - WORDPRESS_DB_PASSWORD=alpine + - WORDPRESS_DB_HOST=db + - WORDPRESS_ADMIN_EMAIL=contact@kanel.ovh + - WORDPRESS_ADMIN_USER=kanel + - WORDPRESS_ADMIN_PASS=alpine + - WORDPRESS_SITE_TITLE='Kanel supremacy' + - WORDPRESS_SEARCH_ENGINE_VISIBILITY=false - PHP_MEMORY_LIMIT="512M" - PHP_MAX_UPLOAD="50M" - PHP_PORT=9000 - TZ=Europe/Paris volumes: - - wp-site:/var/www/html + - wp-site:/var/www/wordpress restart: unless-stopped db: diff --git a/srcs/docker/wordpress/Dockerfile b/srcs/docker/wordpress/Dockerfile index 1c5b036..72a4a77 100644 --- a/srcs/docker/wordpress/Dockerfile +++ b/srcs/docker/wordpress/Dockerfile @@ -11,7 +11,7 @@ COPY go.mod /build/go.mod COPY cmd /build/cmd COPY internal /build/internal COPY docker/wordpress/www-docker.conf /www-docker.conf -COPY docker/wordpress/wp-config-docker.php /usr/src/wordpress/wp-config-docker.php +COPY docker/wordpress/wp-config-docker.php /var/www/wordpress/wp-config-docker.php RUN apk add --no-cache go curl php84-mysqli php84-fpm tzdata fcgi \ && cd /build \ @@ -22,9 +22,9 @@ RUN apk add --no-cache go curl php84-mysqli php84-fpm tzdata fcgi \ && rm -Rf /build \ && mkdir -p /usr/src/wordpress -VOLUME /usr/src/wordpress +VOLUME /var/www/wordpress -WORKDIR /usr/src +WORKDIR /var/www RUN version='6.7.1' \ && curl -o wordpress.tar.gz -fL "https://wordpress.org/wordpress-$version.tar.gz" \ @@ -32,17 +32,17 @@ RUN version='6.7.1' \ && rm -rf /var/cache/apk/* \ && tar -xzvf wordpress.tar.gz \ && rm wordpress.tar.gz \ - && chown -R www-data:www-data /usr/src/wordpress \ + && chown -R www-data:www-data /var/www/wordpress \ && mkdir wp-content \ - && for dir in /usr/src/wordpress/wp-content/*/ cache; do \ + && for dir in /var/www/wp-content/*/ cache; do \ dir="$(basename "${dir%/}")"; \ mkdir "wp-content/$dir"; \ done \ - && chown -R www-data:www-data wp-content \ - && chmod -R 1777 wp-content + && chown -R www-data:www-data /var/www/wordpress/wp-content \ + && chmod -R 1777 /var/www/wordpress/wp-content ENTRYPOINT [ "/docker-entrypoint" ] -WORKDIR /usr/src/wordpress +WORKDIR /var/www/wordpress STOPSIGNAL SIGQUIT EXPOSE 9000 diff --git a/srcs/docker/wordpress/wp-config-docker.php b/srcs/docker/wordpress/wp-config-docker.php index 00b4ece..e42bfae 100644 --- a/srcs/docker/wordpress/wp-config-docker.php +++ b/srcs/docker/wordpress/wp-config-docker.php @@ -64,6 +64,13 @@ define( 'DB_CHARSET', getenv_docker('WORDPRESS_DB_CHARSET', 'utf8') ); /** The database collate type. Don't change this if in doubt. */ define( 'DB_COLLATE', getenv_docker('WORDPRESS_DB_COLLATE', '') ); +/** Site url, user and pass */ +define( 'WP_ADMIN_USER', getenv_docker('WORDPRESS_ADMIN_USER', 'admin')); +define( 'WP_ADMIN_PASS', getenv_docker('WORDPRESS_ADMIN_PASS', 'password123')); +define( 'WP_ADMIN_EMAIL', getenv_docker('WORDPRESS_ADMIN_EMAIL', 'admin@example.com')); +define( 'WP_SITE_TITLE', getenv_docker('WORDPRESS_SITE_TITLE', 'My WordPress Site')); +define( 'WP_SEARCH_ENGINE_VISIBILITY', getenv_docker('WORDPRESS_SEARCH_ENGINE_VISIBILITY', false)); + /**#@+ * Authentication unique keys and salts. * @@ -137,3 +144,31 @@ if ( ! defined( 'ABSPATH' ) ) { /** Sets up WordPress vars and included files. */ require_once ABSPATH . 'wp-settings.php'; + +/** + * Custom script to set up admin user + */ +function setup_admin_user() { + global $wpdb; + + $user = WP_ADMIN_USER; + $pass = WP_ADMIN_PASS; + $email = WP_ADMIN_EMAIL; + + if ( ! username_exists( $user ) && ! email_exists( $email ) ) { + $user_id = wp_create_user( $user, $pass, $email ); + $user = new WP_User( $user_id ); + $user->set_role( 'administrator' ); + } +} +add_action( 'init', 'setup_admin_user' ); + +/** + * Custom script to set up search engine visibility + */ +function setup_search_engine_visibility() { + if ( get_option( 'blog_public' ) === '1' ) { + update_option( 'blog_public', !WP_SEARCH_ENGINE_VISIBILITY ); + } +} +add_action( 'init', 'setup_search_engine_visibility' );