Error establishing a database connection - Bedrock Wordpress on host server

Hi, uploaded a bedrock wordpress site to a hosting through ftp. But all I get is the famous database connection error. *spoileralert: I know very little of bedrock, but helping out a friend in need who got a site made through bedrock.

I’ve defined the following inside the .env file that exist in my root subfolder (I run a test on this site on my sub-domain):

DEFINE: Basic variables

WP_ENV=‘production’
WP_HOST=‘test.mysite.com
PROJECT_NAME=‘testproject’
WP_PROTOCOL=‘https’

DEFINE: Database config

DB_HOST=‘localhost’
DB_NAME=‘my_db_name’
DB_USER=‘my_db_username’
DB_PASSWORD=‘password’
DB_PREFIX=‘wp_’

The root inside my hosting folder looks like this:
https://drive.google.com/file/d/1VhRboYYrke3IwQ0mR96evHaND7AI1PSl/view?usp=sharing
(I changed the name of the ‘web’ folder to ‘public_html’, in order to get the right url through htaccess.)

It is almost like the hosting server does not recognize the .env file. Usually it plays with wp-config.php in order to fetch the db-details. I’ve struggled with this thing in 3 days now, and pulling out my hair.

If I am short with details, I will gladly share more if needed!

Any help is highly appreciated! Thanks.

In your screenshot it looks like .env is named ”env” (without the .). I would assume that the error would be something else if the .env-file is not found (on my phone so no way to test right now) but could that maybe be it?

If not, I would make sure that the data from the .env-file is read correctly by dumping values in config/application.php.

I would also make sure that I can connect to the db by placing a call to mysqli_connect() with the credentials ”hardcoded” in public_html/index.php .

Also, if the site is set up as a multisite and you dont have any tables in the db, WP may show the db-connection error which is misleading. In that case you may want to prefill the db with a dump from your local environment.

1 Like

You are an absolute wonder. Can you believe it was only the dot that made the difference? How did I not see it my self? Blind as a blind mule! Thanks.

The only part now is since I am not able to update wordpress, plugins or theme through the admin-panel, because the makers of the site made it so. How would you suggest the best way to update/add new plugins to the site? And update Wordpress? Can I simply add the updated/new plugins inside the folder: /web/app/plugins ? And do the same with a new wordpress release? (/web/wp) And replace the files there with the new update?

Thanks a lot friend!

Bedrock should be used to manage updates (that’s largely the point of Bedrock). See: https://roots.io/docs/bedrock/master/composer/#plugins

I’d recommend setting up a deployment process to simplify updates to the site.

Yes I understand, and I will definitely look into Bedrock when I have the time. But as of now, being thrown into this project with no understanding of Bedrock, I need to know a safe and easy work-around. Do you get me?

I absolutely agree with alwaysblank that Composer and an automatic deploy process is the way to go. I personally use Deployer for which I have shared my setup for here. There’s also Trellis which I am not using (because of the way the company where I work do things) but I think a lot of people here would recommend it :slight_smile:

But I can also see the need for a quick fix and I think there are a cpl of ways to do it (test at your own risk):

If you are able to run composer on the server, you could update versions in composer.json and then run composer update there. If you have a local setup you should first do the updates there using Composer and make sure everything works and then you can upload composer**.lock** to the server and run composer install. Maybe also add some or all of the flags --verbose --prefer-dist --no-progress --no-interaction --no-dev --optimize-autoloader --no-suggest.

If using Composer is not an option, but you still want to keep anyone from updating using the admin area, you can go ahead and manually add/replace plugins in web/app/plugins and core in web/wp.

If you also/instead want to allow updates/installs from the admin area: in config/environments/production.php (and staging.php if you are using a staging environment), set DISALLOW_FILE_MODS to true. I do believe that this also enables the file editor and other maybe-not-so-good-but-default-in-wordpress stuff in the admin area.


If you wont be using Composer, and later on decide to start using it, you need to make sure to match the list in composer.json with the installed plugins and versions.

Yes I agree. This is not a sustainable option in the future, but thanks for thinking outside of the box. :slight_smile:

If I could add the menuitem “Plugins” back into the Admin menu, I would be happy. For some reasons they have removed it. Any clue if there is an easy way of getting it back?

I don’t have a “production.php” file, but I do have an application.php file there instead where I am able to change the following:
// SET: Custom
Config::define(‘AUTOMATIC_UPDATER_DISABLED’, true);
Config::define(‘DISABLE_WP_CRON’, env(‘DISABLE_WP_CRON’) ?: false);
Config::define(‘DISALLOW_FILE_EDIT’, false);
Config::define(‘DISALLOW_FILE_MODS’, false);

I’ve set both disallow_file_edit and _file_mods to false, because if not, I will not be able to edit/add files? Or am I thinking wrong?

No need to reply. I discovered they had a plugin called “Wordklapp” removing and altering the admin area. And so all I needed to do was change the line about “plugins.php” and I got it up and running. I can now see the plugins the site has, update the plugins and add new ones without problem. (So far at least I think its no problem).