-1

To create my test sites, I wanted to run a command like:

ddev config --database=mysql:5.7 --docroot=wwwroot --php_version=7.4 --webserver_type=apache-fpm --timezone=America/Chicago --create-docroot

I was hoping this command would take the hostname from the directory it is ran from according to https://ddev.readthedocs.io/en/stable/users/configuration/config_yaml/.

Under the name option it says "If this option is omitted, the project will take the name of the enclosing directory."

How would I also get it to include the www. version of the hostname from the directory it is ran from?

I see that you can enable * wildcard entries, but I really only want the www. version.

Is that possible?

gmclelland
  • 187
  • 3

2 Answers2

0

I think this might be related, but I figured out a way to run parallel project instances without having to manually override the site name configuration item every time.

In my case, I want to run a local docs site which is static, alongside a WordPress instance. A couple of things I had to do:

  1. Create a custom web entrypoint that copied a template nginx conf file for the static site into /etc/nginx/sites-enabled. The template was passed through envsubst in order to replace the server name with the actual generated site name from DDEV_SITENAME environment variable.
  2. Override some Docker Compose configuration in order to get around DDEV's refusal to allow dynamic hostnames based on the calculated site name.

.ddev/docker-compose.router.yaml:

services:
  web:
    environment:
      DDEV_HOSTNAME: ${DDEV_SITENAME}.ddev.site,sphinx-${DDEV_SITENAME}.ddev.site
      VIRTUAL_HOST: ${DDEV_SITENAME}.ddev.site,sphinx-${DDEV_SITENAME}.ddev.site
    external_links:
      - ddev-router:sphinx-${DDEV_SITENAME}.ddev.site

.ddev/nginx_templates/sphinx.conf.template


server {
    server_name sphinx-${DDEV_SITENAME}.ddev.site;
    root /var/www/html/sphinx/build/html;

    listen 80;
    listen 443 ssl;

    ssl_certificate /etc/ssl/certs/master.crt;
    ssl_certificate_key /etc/ssl/certs/master.key;

    include /etc/nginx/monitoring.conf;

    index index.htm index.html;

    # Disable sendfile as per https://docs.vagrantup.com/v2/synced-folders/virtualbox.html
    sendfile off;
    error_log /dev/stdout info;
    access_log /var/log/nginx/access.log;

    location / {
        try_files $uri $uri/ =404;
    }

    # Expire rules for static content
    # Media: images, icons, video, audio, HTC
    location ~* \.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc)$ {
        access_log off;
    }

    # Prevent clients from accessing hidden files (starting with a dot)
    # This is particularly important if you store .htpasswd files in the site hierarchy
    # Access to `/.well-known/` is allowed.
    # https://www.mnot.net/blog/2010/04/07/well-known
    # https://tools.ietf.org/html/rfc5785
    location ~* /\.(?!well-known\/) {
        deny all;
    }

    # Prevent clients from accessing to backup/config/source files
    location ~* (?:\.(?:bak|conf|dist|fla|in[ci]|log|psd|sh|sql|sw[op])|~)$ {
        deny all;
    }

    include /etc/nginx/common.d/*.conf;
}

.ddev/web-entrypoint.d/nginx-templates.sh

#!/bin/bash

envsubst '$DDEV_SITENAME' < /mnt/ddev_config/nginx_templates/sphinx.conf.template > /etc/nginx/sites-enabled/sphinx.conf

The obvious caveat is that by doing this you are saying you are going to manually control what the additional_hostnames configuration would otherwise provide. Personally I find that if I'm running parallel project instances, you can't use additional_hostnames anyway with how its currently implemented, so its fine. I mandate on all of my DDEV projects that usage of additional_hostnames is disallowed because parallel instance support is required.

Lester Peabody
  • 1,868
  • 3
  • 20
  • 42
-1

Yes, you can use ddev config --additional-hostnames=www.<projectname>, and no, I don't think you can get www.<directoryname> automatically when omitting the name in the .ddev/config.yaml

So either use ddev config --additional-hostnames="*.<projectname>" or ddev config --additional-hostnames="www.<projectname> or add to the config.yaml

additional-hostnames: ["*.<yourprojectname>"]

or

additional_hostnames: ["www.<yourprojectname>"]
rfay
  • 9,963
  • 1
  • 47
  • 89
  • Thanks rfay, but I was really hoping to have one command that doesn't change that I can just copy and paste to spin up a new dev environment. No biggie, I'm still loving ddev! – gmclelland Nov 07 '22 at 02:31
  • Why don't you name your directory `www.projectname` to make this work out? – rfay Nov 07 '22 at 16:54
  • 1
    I typically need both the bare domain and the www. version when developing websites. I usually test to make sure the redirecting from non-www to www is working correctly with Apache and the CMS(Processwire). – gmclelland Nov 07 '22 at 17:25
  • This also doesn't work if you want to run multiple instances of the same project side-by-side. The goal is to not have to explicitly set a site name configuration value every time you run a parallel project. I found a workaround, albeit incredibly hacky... see my answer. – Lester Peabody Aug 01 '23 at 22:33