diff --git a/README.md b/README.md index af05681c..351c8edb 100644 --- a/README.md +++ b/README.md @@ -23,8 +23,9 @@ We provide simple and intuitive production setup with prebuilt Frappe and ERPNex Also, there's docs to help with deployment: -- [setup options](docs/setup-options.md), -- in cluster: +- [Setup options](docs/setup-options.md) +- Examples: + - [Single Server](docs/single-server-example.md) - [Docker Swarm](docs/docker-swarm.md), - [Kubernetes (frappe/helm)](https://helm.erpnext.com), - [site operations](docs/site-operations.md). diff --git a/docs/compose/compose.custom-domain-ssl.yaml b/docs/compose/compose.custom-domain-ssl.yaml new file mode 100644 index 00000000..5aa1d0ea --- /dev/null +++ b/docs/compose/compose.custom-domain-ssl.yaml @@ -0,0 +1,5 @@ +services: + custom-domain: + labels: + - traefik.http.routers.${ROUTER}.entrypoints=http,https + - traefik.http.routers.${ROUTER}.tls.certresolver=le diff --git a/docs/compose/compose.custom-domain.yaml b/docs/compose/compose.custom-domain.yaml new file mode 100644 index 00000000..5746a325 --- /dev/null +++ b/docs/compose/compose.custom-domain.yaml @@ -0,0 +1,31 @@ +version: "3.3" + +services: + custom-domain: + image: caddy:2 + command: + - caddy + - reverse-proxy + - --to + - frontend:8080 + - --from + - :2016 + labels: + - traefik.enable=true + - traefik.docker.network=traefik-public + - traefik.http.services.${ROUTER?ROUTER not set}.loadbalancer.server.port=2016 + - traefik.http.routers.${ROUTER}.service=${ROUTER} + - traefik.http.routers.${ROUTER}.entrypoints=http + - traefik.http.routers.${ROUTER}.rule=Host(${SITES?SITES not set}) + - traefik.http.middlewares.${ROUTER}.headers.customrequestheaders.Host=${BASE_SITE?BASE_SITE not set} + - traefik.http.routers.${ROUTER}.middlewares=${ROUTER} + networks: + - traefik-public + - bench-network + +networks: + traefik-public: + external: true + bench-network: + name: ${BENCH_NETWORK?BENCH_NETWORK not set} + external: true diff --git a/docs/compose/compose.mariadb-shared.yaml b/docs/compose/compose.mariadb-shared.yaml new file mode 100644 index 00000000..29e7404b --- /dev/null +++ b/docs/compose/compose.mariadb-shared.yaml @@ -0,0 +1,27 @@ +version: "3.3" + +services: + database: + container_name: mariadb-database + image: mariadb:10.6 + restart: unless-stopped + healthcheck: + test: mysqladmin ping -h localhost --password=${DB_PASSWORD:-changeit} + interval: 1s + retries: 15 + command: + - --character-set-server=utf8mb4 + - --collation-server=utf8mb4_unicode_ci + - --skip-character-set-client-handshake + - --skip-innodb-read-only-compressed + environment: + MYSQL_ROOT_PASSWORD: ${DB_PASSWORD:-changeit} + volumes: + - ${HOME}/data/mariadb:/var/lib/mysql + networks: + - mariadb-network + +networks: + mariadb-network: + name: mariadb-network + external: false diff --git a/docs/compose/compose.multi-bench-ssl.yaml b/docs/compose/compose.multi-bench-ssl.yaml new file mode 100644 index 00000000..73e6becd --- /dev/null +++ b/docs/compose/compose.multi-bench-ssl.yaml @@ -0,0 +1,5 @@ +services: + frontend: + labels: + - traefik.http.routers.${ROUTER}.entrypoints=http,https + - traefik.http.routers.${ROUTER}.tls.certresolver=le diff --git a/docs/compose/compose.multi-bench.yaml b/docs/compose/compose.multi-bench.yaml new file mode 100644 index 00000000..ee2e17ee --- /dev/null +++ b/docs/compose/compose.multi-bench.yaml @@ -0,0 +1,46 @@ +services: + frontend: + networks: + - traefik-public + - mariadb-network + - bench-network + labels: + - traefik.enable=true + - traefik.docker.network=traefik-public + - traefik.http.services.${ROUTER?ROUTER not set}.loadbalancer.server.port=8080 + - traefik.http.routers.${ROUTER}.service=${ROUTER} + - traefik.http.routers.${ROUTER}.entrypoints=http + - traefik.http.routers.${ROUTER}.rule=Host(${SITES?SITES not set}) + configurator: + networks: + - mariadb-network + backend: + networks: + - mariadb-network + websocket: + networks: + - mariadb-network + scheduler: + networks: + - mariadb-network + queue-default: + networks: + - mariadb-network + queue-short: + networks: + - mariadb-network + queue-long: + networks: + - mariadb-network + redis: + networks: + - mariadb-network + +networks: + traefik-public: + external: true + mariadb-network: + external: true + bench-network: + name: ${ROUTER} + external: false diff --git a/docs/compose/compose.traefik-ssl.yaml b/docs/compose/compose.traefik-ssl.yaml new file mode 100644 index 00000000..6a31998f --- /dev/null +++ b/docs/compose/compose.traefik-ssl.yaml @@ -0,0 +1,45 @@ +services: + traefik: + labels: + # https-redirect middleware to redirect HTTP to HTTPS + # It can be re-used by other stacks in other Docker Compose files + - traefik.http.middlewares.https-redirect.redirectscheme.scheme=https + - traefik.http.middlewares.https-redirect.redirectscheme.permanent=true + # traefik-http to use the middleware to redirect to https + - traefik.http.routers.traefik-public-http.middlewares=https-redirect + # traefik-https the actual router using HTTPS + # Uses the environment variable DOMAIN + - traefik.http.routers.traefik-public-https.rule=Host(`${TRAEFIK_DOMAIN}`) + - traefik.http.routers.traefik-public-https.entrypoints=https + - traefik.http.routers.traefik-public-https.tls=true + # Use the special Traefik service api@internal with the web UI/Dashboard + - traefik.http.routers.traefik-public-https.service=api@internal + # Use the "le" (Let's Encrypt) resolver created below + - traefik.http.routers.traefik-public-https.tls.certresolver=le + # Enable HTTP Basic auth, using the middleware created above + - traefik.http.routers.traefik-public-https.middlewares=admin-auth + command: + # Enable Docker in Traefik, so that it reads labels from Docker services + - --providers.docker=true + # Do not expose all Docker services, only the ones explicitly exposed + - --providers.docker.exposedbydefault=false + # Create an entrypoint http listening on port 80 + - --entrypoints.http.address=:80 + # Create an entrypoint https listening on port 443 + - --entrypoints.https.address=:443 + # Create the certificate resolver le for Let's Encrypt, uses the environment variable EMAIL + - --certificatesresolvers.le.acme.email=${EMAIL:?No EMAIL set} + # Store the Let's Encrypt certificates in the mounted volume + - --certificatesresolvers.le.acme.storage=/certificates/acme.json + # Use the TLS Challenge for Let's Encrypt + - --certificatesresolvers.le.acme.tlschallenge=true + # Enable the access log, with HTTP requests + - --accesslog + # Enable the Traefik log, for configurations and errors + - --log + # Enable the Dashboard and API + - --api + ports: + - 443:443 + volumes: + - ${HOME}/data/traefik/certificates:/certificates diff --git a/docs/compose/compose.traefik-swarm.yaml b/docs/compose/compose.traefik-swarm.yaml new file mode 100644 index 00000000..cf310165 --- /dev/null +++ b/docs/compose/compose.traefik-swarm.yaml @@ -0,0 +1,7 @@ +version: "3.3" + +services: + traefik: + command: + # Enable Docker Swarm mode + - --providers.docker.swarmmode diff --git a/docs/compose/compose.traefik.yaml b/docs/compose/compose.traefik.yaml new file mode 100644 index 00000000..1aff7ed4 --- /dev/null +++ b/docs/compose/compose.traefik.yaml @@ -0,0 +1,46 @@ +version: "3.3" + +services: + traefik: + image: "traefik:v2.6" + labels: + # Enable Traefik for this service, to make it available in the public network + - traefik.enable=true + # Use the traefik-public network (declared below) + - traefik.docker.network=traefik-public + # admin-auth middleware with HTTP Basic auth + # Using the environment variables USERNAME and HASHED_PASSWORD + - traefik.http.middlewares.admin-auth.basicauth.users=admin:${HASHED_PASSWORD:?No HASHED_PASSWORD set} + # Uses the environment variable TRAEFIK_DOMAIN + - traefik.http.routers.traefik-public-http.rule=Host(`${TRAEFIK_DOMAIN:?No TRAEFIK_DOMAIN set}`) + - traefik.http.routers.traefik-public-http.entrypoints=http + # Use the special Traefik service api@internal with the web UI/Dashboard + - traefik.http.routers.traefik-public-http.service=api@internal + # Enable HTTP Basic auth, using the middleware created above + - traefik.http.routers.traefik-public-http.middlewares=admin-auth + # Define the port inside of the Docker service to use + - traefik.http.services.traefik-public.loadbalancer.server.port=8080 + command: + # Enable Docker in Traefik, so that it reads labels from Docker services + - --providers.docker=true + # Do not expose all Docker services, only the ones explicitly exposed + - --providers.docker.exposedbydefault=false + # Create an entrypoint http listening on port 80 + - --entrypoints.http.address=:80 + # Enable the access log, with HTTP requests + - --accesslog + # Enable the Traefik log, for configurations and errors + - --log + # Enable the Dashboard and API + - --api + ports: + - 80:80 + volumes: + - /var/run/docker.sock:/var/run/docker.sock:ro + networks: + - traefik-public + +networks: + traefik-public: + name: traefik-public + external: false diff --git a/docs/single-server-example.md b/docs/single-server-example.md new file mode 100644 index 00000000..73da4ddf --- /dev/null +++ b/docs/single-server-example.md @@ -0,0 +1,288 @@ +### Single Server Example + +In this use case we have a single server with a static IP attached to it. It can be used in scenarios where one powerful VM has multiple benches and applications or one entry level VM with single site. For single bench, single site setup follow only up to the point where first bench and first site is added. If you choose this setup you can only scale vertically. If you need to scale horizontally you'll need to backup the sites and restore them on to cluster setup. + +We will setup the following: + +- Install docker and docker compose v2 on linux server. +- Install traefik service for internal load balancer and letsencrypt. +- Install MariaDB with containers. +- Setup project called `erpnext-one` and create sites `one.example.com` and `two.example.com` in the project. +- Setup project called `erpnext-two` and create sites `three.example.com` and `four.example.com` in the project. + +Explanation: + +Single instance of **Traefik** will be installed and act as internal loadbalancer for multiple benches and sites hosted on the server. It can also load balance other applications along with frappe benches, e.g. wordpress, metabase, etc. We only expose the ports `80` and `443` once with this instance of traefik. Traefik will also take care of letsencrypt automation for all sites installed on the server. _Why choose Traefik over Nginx Proxy Manager?_ Traefik doesn't need additional DB service and can store certificates in a json file in a volume. Traefik will also be used in swarm setup keeping things consistent for understanding. + +Single instance of **MariaDB** will be installed and act as database service for all the benches/projects installed on the server. + +Each instance of ERPNext project (bench) will have its own redis, socketio, gunicorn, nginx, workers and scheduler. It will connect to internal MariaDB by connecting to MariaDB network. It will expose sites to public through Traefik by connecting to Traefik network. + +### Install Docker + +Easiest way to install docker is to use the [convenience script](https://docs.docker.com/engine/install/ubuntu/#install-using-the-convenience-script). + +```shell +curl -fsSL https://get.docker.com | bash +``` + +Note: The documentation assumes Ubuntu LTS server is used. Use any distribution as long as the docker convenience script works. If the convenience script doesn't work, you'll need to install docker manually. + +### Install Compose V2 + +Refer [original documentation](https://docs.docker.com/compose/cli-command/#install-on-linux) for updated version. + +```shell +DOCKER_CONFIG=${DOCKER_CONFIG:-$HOME/.docker} +mkdir -p $DOCKER_CONFIG/cli-plugins +curl -SL https://github.com/docker/compose/releases/download/v2.2.3/docker-compose-linux-x86_64 -o $DOCKER_CONFIG/cli-plugins/docker-compose +chmod +x $DOCKER_CONFIG/cli-plugins/docker-compose +``` + +### Prepare + +Clone `frappe_docker` repo for the needed YAMLs and change the current working director of you shell to the cloned repo. + +```shell +git clone https://github.com/frappe/frappe_docker +cd frappe_docker +``` + +Create configuration and resources directory + +```shell +mkdir ~/gitops +``` + +The `~/gitops` directory will store all the resources that we use for setup. We will also keep the environment files in this directory as there will be multiple projects with different environment variables. You can create a private repo for this directory and track the changes there. + +### Install Traefik + +Basic Traefik setup using docker compose. + +Create a file called `traefik.env` in `~/gitops` + +```shell +echo 'TRAEFIK_DOMAIN=traefik.example.com' > ~/gitops/traefik.env +echo 'EMAIL=admin@example.com' >> ~/gitops/traefik.env +echo 'HASHED_PASSWORD='$(openssl passwd -apr1 changeit | sed 's/\$/\\\$/g') >> ~/gitops/traefik.env +``` + +Note: + +- Change the domain from `traefik.example.com` to the one used in production. DNS entry needs to point to the Server IP. +- Change the letsencrypt notification email from `admin@example.com` to correct email. +- Change the password from `changeit` to more secure. + +env file generated at location `~/gitops/traefik.env` will look like following: + +```env +TRAEFIK_DOMAIN=traefik.example.com +EMAIL=admin@example.com +HASHED_PASSWORD=$apr1$K.4gp7RT$tj9R2jHh0D4Gb5o5fIAzm/ +``` + +Deploy the traefik container with letsencrypt SSL + +```shell +docker compose --project-name traefik \ + --env-file ~/gitops/traefik.env \ + -f docs/compose/compose.traefik.yaml \ + -f docs/compose/compose.traefik-ssl.yaml up -d +``` + +This will make the traefik dashboard available on `traefik.example.com` and all certificates will reside in `/data/traefik/certificates` on host filesystem. + +For LAN setup deploy the traefik container without overriding `docs/compose/compose.traefik-ssl.yaml`. + +### Install MariaDB + +Basic MariaDB setup using docker compose. + +Create a file called `mariadb.env` in `~/gitops` + +```shell +echo "DB_PASSWORD=changeit" > ~/gitops/mariadb.env +``` + +Note: + +- Change the password from `changeit` to more secure. + +env file generated at location `~/gitops/mariadb.env` will look like following: + +```env +DB_PASSWORD=changeit +``` + +Note: Change the password from `changeit` to more secure one. + +Deploy the mariadb container + +```shell +docker compose --project-name mariadb --env-file ~/gitops/mariadb.env -f docs/compose/compose.mariadb-shared.yaml up -d +``` + +This will make `mariadb-database` service available under `mariadb-network`. Data will reside in `/data/mariadb`. + +### Install ERPNext + +#### Create first bench + +Create second bench called `erpnext-one` with `one.example.com` and `two.example.com` + +Create a file called `erpnext-one.env` in `~/gitops` + +```shell +cp example.env ~/gitops/erpnext-one.env +sed -i 's/DB_PASSWORD=123/DB_PASSWORD=changeit/g' ~/gitops/erpnext-one.env +sed -i 's/DB_HOST=/DB_HOST=mariadb-database/g' ~/gitops/erpnext-one.env +sed -i 's/DB_PORT=/DB_PORT=3306/g' ~/gitops/erpnext-one.env +echo 'ROUTER=erpnext-one' >> ~/gitops/erpnext-one.env +echo "SITES=\`one.example.com\`,\`two.example.com\`" >> ~/gitops/erpnext-one.env +echo "BENCH_NETWORK=erpnext-one" >> ~/gitops/erpnext-one.env +``` + +Note: + +- Change the password from `changeit` to the one set for MariaDB compose in the previous step. + +env file is generated at location `~/gitops/erpnext-one.env`. + +Create a yaml file called `erpnext-one.yaml` in `~/gitops` directory: + +```shell +docker compose --project-name erpnext-one \ + --env-file ~/gitops/erpnext-one.env \ + -f compose.yaml \ + -f overrides/compose.erpnext.yaml \ + -f overrides/compose.redis.yaml \ + -f docs/compose/compose.multi-bench.yaml \ + -f docs/compose/compose.multi-bench-ssl.yaml config > ~/gitops/erpnext-one.yaml +``` + +For LAN setup do not override `compose.multi-bench-ssl.yaml`. + +Use the above command after any changes are made to `erpnext-one.env` file to regenerate `~/gitops/erpnext-one.yaml`. e.g. after changing version to migrate the bench. + +Deploy `erpnext-one` containers: + +```shell +docker compose --project-name erpnext-one -f ~/gitops/erpnext-one.yaml up -d +``` + +Create sites `one.example.com` and `two.example.com`: + +```shell +# one.example.com +docker compose --project-name erpnext-one exec backend \ + bench new-site one.example.com --mariadb-root-password changeit --install-app erpnext --admin-password changeit +``` + +You can stop here and have a single bench single site setup complete. Continue to add one more site to the current bench. + +```shell +# two.example.com +docker compose --project-name erpnext-one exec backend \ + bench new-site two.example.com --mariadb-root-password changeit --install-app erpnext --admin-password changeit +``` + +#### Create second bench + +Setting up additional bench is optional. Continue only if you need multi bench setup. + +Create second bench called `erpnext-two` with `three.example.com` and `four.example.com` + +Create a file called `erpnext-two.env` in `~/gitops` + +```shell +curl -sL https://raw.githubusercontent.com/frappe/frappe_docker/main/example.env -o ~/gitops/erpnext-two.env +sed -i 's/DB_PASSWORD=123/DB_PASSWORD=changeit/g' ~/gitops/erpnext-two.env +sed -i 's/DB_HOST=/DB_HOST=mariadb-database/g' ~/gitops/erpnext-two.env +sed -i 's/DB_PORT=/DB_PORT=3306/g' ~/gitops/erpnext-two.env +echo "ROUTER=erpnext-two" >> ~/gitops/erpnext-two.env +echo "SITES=\`three.example.com\`,\`four.example.com\`" >> ~/gitops/erpnext-two.env +echo "BENCH_NETWORK=erpnext-two" >> ~/gitops/erpnext-two.env +``` + +Note: + +- Change the password from `changeit` to the one set for MariaDB compose in the previous step. + +env file is generated at location `~/gitops/erpnext-two.env`. + +Create a yaml file called `erpnext-two.yaml` in `~/gitops` directory: + +```shell +docker compose --project-name erpnext-two \ + --env-file ~/gitops/erpnext-two.env \ + -f compose.yaml \ + -f overrides/compose.erpnext.yaml \ + -f overrides/compose.redis.yaml \ + -f docs/compose/compose.multi-bench.yaml \ + -f docs/compose/compose.multi-bench-ssl.yaml config > ~/gitops/erpnext-two.yaml +``` + +Use the above command after any changes are made to `erpnext-two.env` file to regenerate `~/gitops/erpnext-two.yaml`. e.g. after changing version to migrate the bench. + +Deploy `erpnext-two` containers: + +```shell +docker compose --project-name erpnext-two -f ~/gitops/erpnext-two.yaml up -d +``` + +Create sites `three.example.com` and `four.example.com`: + +```shell +# three.example.com +docker compose --project-name erpnext-two exec backend \ + bench new-site three.example.com --mariadb-root-password changeit --install-app erpnext --admin-password changeit +# four.example.com +docker compose --project-name erpnext-two exec backend \ + bench new-site four.example.com --mariadb-root-password changeit --install-app erpnext --admin-password changeit +``` + +#### Create custom domain to existing site + +In case you need to point custom domain to existing site follow these steps. +Also useful if custom domain is required for LAN based access. + +Create environment file + +```shell +echo "ROUTER=custom-one-example" > ~/gitops/custom-one-example.env +echo "SITES=\`custom-one.example.com\`" >> ~/gitops/custom-one-example.env +echo "BASE_SITE=one.example.com" >> ~/gitops/custom-one-example.env +echo "BENCH_NETWORK=erpnext-one" >> ~/gitops/custom-one-example.env +``` + +Note: + +- Change the file name from `custom-one-example.env` to a logical one. +- Change `ROUTER` variable from `custom-one.example.com` to the one being added. +- Change `SITES` variable from `custom-one.example.com` to the one being added. You can add multiple sites quoted in backtick (`) and separated by commas. +- Change `BASE_SITE` variable from `one.example.com` to the one which is being pointed to. +- Change `BENCH_NETWORK` variable from `erpnext-one` to the one which was created with the bench. + +env file is generated at location mentioned in command. + +Generate yaml to reverse proxy: + +```shell +docker compose --project-name custom-one-example \ + --env-file ~/gitops/custom-one-example.env \ + -f docs/compose/compose.custom-domain.yaml \ + -f docs/compose/compose.custom-domain-ssl.yaml config > ~/gitops/custom-one-example.yaml +``` + +For LAN setup do not override `compose.custom-domain-ssl.yaml`. + +Deploy `erpnext-two` containers: + +```shell +docker compose --project-name custom-one-example -f ~/gitops/custom-one-example.yaml up -d +``` + +### Site operations + +Refer: [site operations](./site-operations.md)