<p>You already know using a non-standard folder structure is going to cause pain, but here we.</p>
<p>If the site is working, but the <code>ddev craft</code> command is not, let's override the command.</p>
<ul>
<li>Add a new file: <code>.ddev/commands/web/craft</code></li>
<li>Add the following to the file</li>
</ul>
<pre class="lang-bash prettyprint-override"><code>#!/bin/bash

## Description: Run Craft CMS command inside the web container
## Usage: craft [flags] [args]
## Example: &quot;ddev craft db/backup&quot; or &quot;ddev craft db/backup ./my-backups&quot; (see https://craftcms.com/docs/4.x/console-commands.html)
## ProjectTypes: craftcms,php
## ExecRaw: true

if [ &quot;${DDEV_PROJECT_TYPE}&quot; != &quot;craftcms&quot; ]; then
    echo &quot;The craft command is only available in the craftcms project type. You can update this in your project's config file, followed by restarting the DDEV project.&quot;
else
    CRAFT_CMD_ROOT=/var/www/html/craft

    cd &quot;${CRAFT_CMD_ROOT}&quot;
    php craft &quot;$@&quot;
fi
</code></pre>
<p>This is a copy of the global command, but will override it because its on the project level.</p>
<ul>
<li>I removed the <code>#ddev-generated</code> comment so DDEV doesn't override it.</li>
<li>hard-coded <code>CRAFT_CMD_ROOT=/var/www/html/craft</code> directory where the &quot;Craft console bootstrap file&quot; (craft) file is. Eg. &quot;/var/www/html/craft/craft&quot;.</li>
</ul>

CC BY-SA 4.0
2024-07-26T06:18:43.653Z
2024-07-26T06:18:43.653Z
tyler36
<p>Change the <a href="https://ddev.readthedocs.io/en/stable/users/configuration/config/#working_dir" rel="nofollow noreferrer"><code>working_dir</code></a> for the <code>web</code> container:</p>
<pre><code>ddev config --web-working-dir=/var/www/html/craft
</code></pre>
<p>Or manually edit <code>.ddev/config.yaml</code>:</p>
<pre class="lang-yaml prettyprint-override"><code>type: craftcms
docroot: public
...
working_dir:
    web: /var/www/html/craft
</code></pre>

CC BY-SA 4.0
2024-07-26T07:58:24.973Z
2024-07-26T07:58:24.973Z
stasadev
<p>regarding DDEV - you can adjust composer root to a subdirectory:</p>
<p><a href="https://ddev.readthedocs.io/en/stable/users/configuration/config/#composer_root" rel="nofollow noreferrer">https://ddev.readthedocs.io/en/stable/users/configuration/config/#composer_root</a></p>
<p>For your errors:</p>
<blockquote>
<p>Craft can’t connect to the database. Check your connection settings.</p>
</blockquote>
<p>DDEV assumes the original folder structure of craftcms and only handles .env automatically in the root folder.</p>
<p>DDEV replaces DB_HOST, PASSWORD, DB NAME etc. with &quot;db&quot; on start automagically, but doesn't know about your /craft/.env file.</p>
<p>You need to do this manually now in your case.</p>
<p>I don't know if it's a good idea to move craftcms to a subdirectory, I would advise against and stick to the official way of doing things.</p>
<p>If you need further help, you can also get support in DDEV discord (<a href="https://discord.gg/5wjP76mBJD" rel="nofollow noreferrer">https://discord.gg/5wjP76mBJD</a>) if it is DDEV-related.</p>
<p>But most likely DDEV team will also advise you to stick to the standard structure to get the most out of craftcms and DDEV - e.g. automatic .env handling for DDEV.</p>
<p>But it can be done of course, I would recommend checking out a .env from a normal craft project started with DDEV, e.g.:</p>
<pre class="lang-bash prettyprint-override"><code># Database connection settings for DDEV
CRAFT_DB_DRIVER=&quot;mysql&quot;
CRAFT_DB_SERVER=&quot;db&quot;
CRAFT_DB_PORT=&quot;3306&quot;
CRAFT_DB_DATABASE=&quot;db&quot;
CRAFT_DB_USER=&quot;db&quot;
CRAFT_DB_PASSWORD=&quot;db&quot;
CRAFT_DB_SCHEMA=public
CRAFT_DB_TABLE_PREFIX=

PRIMARY_SITE_URL=&quot;https://ddev-craftcms-vite.ddev.site&quot;
</code></pre>
<p>Cheers!</p>

CC BY-SA 4.0
2024-07-26T05:49:33.557Z
Composer commands I don't have a problem with, it's Craft CLI commands. I can't change the folder structure, it's the way the clients want their server set up. I'll post on DDEV Discord to see if they can help.
Tyssen
CC BY-SA 4.0
2024-07-26T05:41:29.760Z
2024-07-26T05:41:29.760Z
m_andrasch
<p>I'm working on a Craft site for a company that wanted me to change the usual folder structure of the project. Instead of it being</p>
<pre><code>-config
-storage
-templates
…
-public
</code></pre>
<p>they wanted to group all the folders and files related to Craft itself into a separate folder so the folder structure would be like this</p>
<pre><code>-craft
--config
--storage
--templates
…
-public
</code></pre>
<p>because they have a lot of folders for other things in the root of the server and wanted to be able to easily see which ones were related to Craft.</p>
<p>Everything is working fine except that in my local environment, I'm running DDEV so it looks like this:</p>
<pre><code>-.ddev
-craft
-public
</code></pre>
<p>and to run <code>composer</code> or <code>php craft</code> console commands, I have to <code>cd</code> into the <code>craft</code> folder because that's where the Craft console bootstrap file and <code>composer.json</code> are.</p>
<p><code>composer</code> commands work OK but any <code>php craft</code> commands always return</p>
<blockquote>
<p>Craft can’t connect to the database. Check your connection settings.</p>
</blockquote>
<p>which I think is because the <code>.ddev</code> folder is in the level above.</p>
<p>So I can't currently run any Craft console commands which I'm able to get around most of the time. Plugin updates I can do from the control panel and even migrations seem to complete properly most of the time by just visiting the control panel and then clicking the button to perform database updates.</p>
<p>But now I need to redo a migration that didn't complete properly before and I'm unable to.</p>
<p>So I need advice on how to get <code>php craft</code> commands to work with the way I have the project structured. Or else how I can get the migration I missed to run without using the CLI.</p>

<migration><upgrades><craft5><ddev>
DDEV and Craft project structure problem
# DDEV and Craft project structure problem

(Asked by: username_0 on 2024-07-26)

I'm working on a Craft site for a company that wanted me to change the usual folder structure of the project. Instead of it being
-config
-storage
-templates
…
-public

they wanted to group all the folders and files related to Craft itself into a separate folder so the folder structure would be like this
-craft
--config
--storage
--templates
…
-public

because they have a lot of folders for other things in the root of the server and wanted to be able to easily see which ones were related to Craft.
Everything is working fine except that in my local environment, I'm running DDEV so it looks like this:
-.ddev
-craft
-public

and to run composer or php craft console commands, I have to cd into the craft folder because that's where the Craft console bootstrap file and composer.json are.
composer commands work OK but any php craft commands always return

Craft can’t connect to the database. Check your connection settings.

which I think is because the .ddev folder is in the level above.
So I can't currently run any Craft console commands which I'm able to get around most of the time. Plugin updates I can do from the control panel and even migrations seem to complete properly most of the time by just visiting the control panel and then clicking the button to perform database updates.
But now I need to redo a migration that didn't complete properly before and I'm unable to.
So I need advice on how to get php craft commands to work with the way I have the project structured. Or else how I can get the migration I missed to run without using the CLI.

**Tags:** migration, upgrades, craft5, ddev

---

## 3 answers

### Answer by username_1 on 2024-07-26. Score: 3 | Accepted answer

You already know using a non-standard folder structure is going to cause pain, but here we.
If the site is working, but the ddev craft command is not, let's override the command.

Add a new file: .ddev/commands/web/craft
Add the following to the file

#!/bin/bash

## Description: Run Craft CMS command inside the web container
## Usage: craft [flags] [args]
## Example: "ddev craft db/backup" or "ddev craft db/backup ./my-backups" (see https://craftcms.com/docs/4.x/console-commands.html)
## ProjectTypes: craftcms,php
## ExecRaw: true

if [ "${DDEV_PROJECT_TYPE}" != "craftcms" ]; then
    echo "The craft command is only available in the craftcms project type. You can update this in your project's config file, followed by restarting the DDEV project."
else
    CRAFT_CMD_ROOT=/var/www/html/craft

    cd "${CRAFT_CMD_ROOT}"
    php craft "$@"
fi

This is a copy of the global command, but will override it because its on the project level.

I removed the #ddev-generated comment so DDEV doesn't override it.
hard-coded CRAFT_CMD_ROOT=/var/www/html/craft directory where the "Craft console bootstrap file" (craft) file is. Eg. "/var/www/html/craft/craft".


---

### Answer by username_2 on 2024-07-26. Score: 3

Change the working_dir for the web container:
ddev config --web-working-dir=/var/www/html/craft

Or manually edit .ddev/config.yaml:
type: craftcms
docroot: public
...
working_dir:
    web: /var/www/html/craft


---

### Answer by username_3 on 2024-07-26. Score: 1

regarding DDEV - you can adjust composer root to a subdirectory:
https://ddev.readthedocs.io/en/stable/users/configuration/config/#composer_root
For your errors:

Craft can’t connect to the database. Check your connection settings.

DDEV assumes the original folder structure of craftcms and only handles .env automatically in the root folder.
DDEV replaces DB_HOST, PASSWORD, DB NAME etc. with "db" on start automagically, but doesn't know about your /craft/.env file.
You need to do this manually now in your case.
I don't know if it's a good idea to move craftcms to a subdirectory, I would advise against and stick to the official way of doing things.
If you need further help, you can also get support in DDEV discord (https://discord.gg/5wjP76mBJD) if it is DDEV-related.
But most likely DDEV team will also advise you to stick to the standard structure to get the most out of craftcms and DDEV - e.g. automatic .env handling for DDEV.
But it can be done of course, I would recommend checking out a .env from a normal craft project started with DDEV, e.g.:
# Database connection settings for DDEV
CRAFT_DB_DRIVER="mysql"
CRAFT_DB_SERVER="db"
CRAFT_DB_PORT="3306"
CRAFT_DB_DATABASE="db"
CRAFT_DB_USER="db"
CRAFT_DB_PASSWORD="db"
CRAFT_DB_SCHEMA=public
CRAFT_DB_TABLE_PREFIX=

PRIMARY_SITE_URL="https://ddev-craftcms-vite.ddev.site"

Cheers!

> **Comments:**
> **username_0**: Composer commands I don't have a problem with, it's Craft CLI commands. I can't change the folder structure, it's the way the clients want their server set up. I'll post on DDEV Discord to see if they can help.  

---

