1

Novice dev here, really struggling to get my local setup functioning. After a couple of days of wrestling with Apache config settings, I finally have Craft connecting via a VHOST setup. The problem – 

Craft can’t connect to the database with the credentials in craft/config/db.php.

This should all be tried and tested as it came from my lead dev on this project and had been working, so it is likely something silly I have done in the set up. I just can't figure out what.

Thus far, I have downloaded the repo, installed all dependencies and have successfully got the local server running with npm run start. I've configured a Virtual Host and that's all running fine. I downloaded a copy of the production db from the live site and uploaded it to my local db (connect via Sequel Pro). Both db's have the same name.

My db.php file is:

return array(

// The database server name or IP address. Usually this is 'localhost' or '127.0.0.1'.
'server' => $_SERVER['DATABASE_HOST'],

// The name of the database to select.
'database' => $_SERVER['DB_NAME'],

// The database username to connect with.
'user' => 'root',

// The database password to connect with.
'password' => $_SERVER['DB_PASSWORD'],

// The prefix to use when naming tables. This can be no more than 5 characters.
'tablePrefix' => 'craft',

);

My VHOST is:

<VirtualHost *:80>


SetEnv DATABASE_HOST localhost
  SetEnv DB_NAME dbName
  SetEnv DB_PASSWORD dbPassword
  SetEnv DEV_MODE true
  SetEnv SITE_URL sitename.local
  SetEnv DB_PORT 3306
  DocumentRoot "/Users/username/Sites/sitename/public"
  ServerName www.sitename.local
  ServerAlias www.sitename.local
  ErrorLog "/Users/username/Sites/sitename/error_log"
  CustomLog "/Users/username/Sites/sitename/access_log" common
  <Directory "/Users/username/Sites/sitename/public">
    Options Indexes FollowSymLinks Includes ExecCGI
    AllowOverride All
    Require all granted
  </Directory>
</VirtualHost>

I'm stumped. The only think I have been able to find on similar threads seems to point to db version issues (which might be the case but I don't know how to check).

Does anyone have any ideas?

  • In db.php, before the return array( bit, if you put die($_SERVER['DATABASE_HOST']);, do you get the value you expect? – Brad Bell May 3 '19 at 0:50
  • No I don't think so (unless I am doing something wrong). Doing that seems to loop me. I got: This page isn’t working www.sitename.local redirected you too many times. Try clearing your cookies. ERR_TOO_MANY_REDIRECTS – – J. Ktz May 6 '19 at 22:53
1

Rather then passing the database details in virtualhost. You should put the database server details in db.php file as below.

DB.php file below:

<?php

/**
 * Database Configuration
 *
 * All of your system's database configuration settings go in here.
 * You can see a list of the default settings in craft/app/etc/config/defaults/db.php
 */

return array(

    // The database server name or IP address. Usually this is 'localhost' or '127.0.0.1'.
    'server' => '127.0.0.1',

    // The database username to connect with.
    'user' => 'root',

    // The database password to connect with.
    'password' => '',

    // The name of the database to select.
    'database' => 'db_name',

    // The prefix to use when naming tables. This can be no more than 5 characters.
    'tablePrefix' => 'craft',

);

Your VHost file should be like below:

#
<VirtualHost *:80>
    ServerName crafty-coffee.local
    DocumentRoot "c:/wamp64/www/crafty-coffee/httpdocs"
    <Directory  "c:/wamp64/www/crafty-coffee/">
        Options +Indexes +Includes +FollowSymLinks +MultiViews
        AllowOverride All
        Require local
    </Directory>
</VirtualHost>

host file (C:\Windows\System32\drivers\etc) :

127.0.0.1   crafty-coffee.local
::1 crafty-coffee.local

This works perfectly for me. After this restart your Sequel Pro.

  • Thanks for the reply. I had tried this before, but sadly it didn't work. I just tried again and still a no go. Makes me feel like I have the db creds wrong, but I don't think I do. – J. Ktz May 3 '19 at 20:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.