我在本地工作站上设置了 NixOS 22.11。我正在寻找一种方法来使用 Nix 为 Wordpress 开发创建可自定义且灵活的本地环境。我希望解决方案是“短暂的”(只需将配置放入任何目录并运行)而不修改我的主系统配置(它使用薄片)。我知道像这样的工具:arion,但它在引擎盖下使用
docker-compose
。
我需要一个在轻松设置多个本地 Wordpress 站点方面等同于
docker-compose
的解决方案。但是,它必须使用原生 NixOS 配置和 Nix 语言来设置所需的服务。它应该独立于 docker
或 podman
,但它应该与本地 treafik
子域一起工作。
目前,我正在使用带有本地子域的
traefik
路由。它使用 dnsmasq
配置了 mkcert
和本地信任的证书,如本article 中所述
这是我的
docker-compose.yaml
version: '3'
services:
mariadb:
image: bitnami/mariadb:latest
volumes:
- 'mariadb_data:/bitnami/mariadb'
restart: always
environment:
- MARIADB_ROOT_PASSWORD=wordpress
- MARIADB_DATABASE=wordpress
- MARIADB_USER=wordpress
- MARIADB_PASSWORD=wordpress
networks:
- web
healthcheck:
test: [ 'CMD', '/opt/bitnami/scripts/mariadb/healthcheck.sh' ]
interval: 15s
timeout: 5s
retries: 6
wordpress:
image: wordpress:latest
# command: -H unix:///var/run/docker.sock
ports:
- 8080:80
expose:
- 8080
security_opt:
- no-new-privileges:true
networks:
- web
depends_on:
- mariadb
restart: always
environment:
- WORDPRESS_DB_HOST=mariadb
- WORDPRESS_DB_USER=wordpress
- WORDPRESS_DB_PASSWORD=wordpress
- WORDPRESS_DB_NAME=wordpress
labels:
- traefik.enable=true
- traefik.docker.network=web
- traefik.http.routers.wp-http.entrypoints=web
- traefik.http.routers.wp-http.rule=Host(`wp.docker.localdev`)
- traefik.http.routers.wp-http.middlewares=wp-https
- traefik.http.middlewares.wp-https.redirectscheme.scheme=https
- traefik.http.routers.wp-https.entrypoints=websecure
- traefik.http.routers.wp-https.rule=Host(`wp.docker.localdev`)
- traefik.http.routers.wp-https.tls=true
# - traefik.http.services.wp.loadbalancer.server.port=8080
volumes:
- ./wp-content:/var/www/html/wp-content
volumes:
db_data:
mariadb_data:
driver: local
networks:
web:
external: true
这可能吗?我该如何实现?我正在寻找更多指导:欢迎提供代码框架或什至一般提示之类的东西。
我找到了满足我需求的解决方案。下面的配置对我有用,但它需要一些微调。
我使用了extra-container,它可以像命令式容器一样运行声明式 NixOS 容器,无需系统重建,从任何文件夹。
使用的服务:
.docker.localdev
可能会产生误导,我在这里不使用docker。我只是在我的系统中预先配置了这个名称。wp.nix
{ pkgs, lib, config, ... }:
let
app = "wpdemo";
socket = "/run/phpfpm/${app}.sock";
domain = "localhost";
dataDir = "/var/www/public";
in
{
containers.wp = {
config = {
networking.firewall.enable = false;
security.acme.defaults.email = "[email protected]";
networking.firewall.allowedTCPPorts = [ 80 82 ];
services.traefik = {
enable = true;
staticConfigOptions = {
providers.docker = {
exposedByDefault = false;
};
entryPoints.web.address = ":80";
};
dynamicConfigOptions = {
http.routers.wp = {
rule = "Host(`wp.docker.localdev`)";
entryPoints = [ "web" ];
service = "wp-service";
};
http.services.wp-service.loadBalancer.server.port = 82;
};
};
services.phpfpm.pools.${app} = {
user = app;
settings = {
"listen.owner" = "nginx";
"pm" = "dynamic";
"pm.max_children" = 32;
"pm.max_requests" = 500;
"pm.start_servers" = 2;
"pm.min_spare_servers" = 2;
"pm.max_spare_servers" = 5;
"php_admin_value[error_log]" = "stderr";
"php_admin_flag[log_errors]" = true;
"catch_workers_output" = true;
};
phpEnv."PATH" = lib.makeBinPath [ pkgs.php ];
};
services.mysql = {
enable = true;
package = pkgs.mysql;
};
services.wordpress = {
webserver = "nginx";
sites."wp.docker.localdev" = {
themes =
let
storefrontTheme = pkgs.stdenv.mkDerivation {
name = "storefront-theme";
src = pkgs.fetchurl {
url = "https://downloads.wordpress.org/theme/storefront.4.2.0.zip";
sha256 = "J9q50EhIWflZNhE6gMt4YakFKhIMDORfWwauXHfiQ0A=";
};
nativeBuildInputs = [ pkgs.unzip ];
installPhase = ''
mkdir -p $out; cp -R * $out/
'';
};
in
[ storefrontTheme ];
plugins =
let
woocommercePlugin = pkgs.stdenv.mkDerivation {
name = "woocommerce-plugin";
src = pkgs.fetchurl {
url = "https://downloads.wordpress.org/plugin/woocommerce.7.4.1.zip";
sha256 = "a/I10n+J/mWa55fMsfi54CR2F0FPkDpEjStQiRPSgWY=";
};
nativeBuildInputs = [ pkgs.unzip ];
installPhase = ''
mkdir -p $out; cp -R * $out/
'';
};
in
[ woocommercePlugin ];
extraConfig = ''
define('WP_POST_REVISIONS', 3);
'';
};
};
services.nginx = {
enable = true;
virtualHosts.${domain} = {
listen = [{ addr = "127.0.0.1"; port = 80; }];
serverName = "wp.docker.localdev";
locations."/" = {
root = /var/www/public;
extraConfig = ''
access_log off;
charset utf-8;
etag off;
index index.php;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:${socket};
include ${pkgs.nginx}/conf/fastcgi_params;
include ${pkgs.nginx}/conf/fastcgi.conf;
}
'';
};
};
};
users.users.${app} = {
isSystemUser = true;
createHome = true;
home = dataDir;
group = app;
};
users.groups.${app} = { };
};
};
}
我用命令运行它:
sudo extra-container create --start <<EOF
$(cat wp.nix)
EOF
一般来说它是有效的,但我在
wordpress
服务方面遇到了一个小问题。它应该添加woocommerce
插件和storefront
主题。它们已下载到 nix 商店,但我无法在仪表板中看到它们。
我试图修改这一行以将商店文件夹符号链接到/var/www/public/wp-content/plugins/woocommerce
,但没有成功。
installPhase = ''
mkdir -p $out; cp -R * $out/
ln -s $out/ ${dataDir}/wp-content/plugins/woocommerce
'';
我得到错误:
ln: failed to create symbolic link '/var/www/public/wp-content/plugins/woocommerce': No such file or directory
当从具有硬编码路径的终端运行时,相同的命令可以正常工作并正确创建符号链接:
ln -s /nix/store/b5ir77w3v3p2zjqnlacibm58plj86j4m-woocommerce-plugin/ /var/www/public/wp-content/plugins/woocommerce
如何修复额外的插件和主题,以便它们在 WordPress 仪表板中可见?有什么想法吗?