Access OpenShift environment vars with Laravel 4 - php

I'm trying to use OPENSHIFT environment variables in my Laravel 4 application, but it doesn't seem to work! I read the following question: Laravel 4 accessing environment variables
But in OnpenShift I don't have access to server config.
I always used
$path = $_ENV['OPENSHIFT_DATA_DIR'] . 'uploads/thumbs/';
But $_ENV and getEnv() always returns me nothing.
How can I get that?

You could try using the Laravel 4 quickstart https://www.openshift.com/quickstarts/laravel-4-on-openshift

The PHP command is getenv('my_var_name'), which should work fine. See PHP Environment Variables in the OpenShift Developer Portal for more info.

I need to set fallback values. then I can keep my local settings. So I do this
'mysql' => array(
'driver' => 'mysql',
'host' => getenv('OPENSHIFT_MYSQL_DB_HOST') ? getenv('OPENSHIFT_MYSQL_DB_HOST') :'127.0.0.1',
'port' => getenv('OPENSHIFT_MYSQL_DB_PORT') ? getenv('OPENSHIFT_MYSQL_DB_PORT') :'3306',
'database' => 'mydatabase',
'username' => 'admintest',
'password' => 'asd123asd',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),

Related

Laravel database configuration for local and remote environment

I just have a database issue on my project/app/config/database.php. I want to my application can change its database when detecting its database environment. For example, if application is develop in local database, the database setting are all local parameter. If it detect that the database is on the remote server, it changes into remote database settings.
'mysql' => array(
'driver' => 'mysql',
'host' => $_ENV['DB_HOST'],
'database' => $_ENV['DB_NAME'],
'username' => $_ENV['DB_USER'],
'password' => $_ENV['DB_PASSWORD'],
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => 'laravel_',
),
I've solved the problem, however, not by adding local folder in to config folder as the document said.
I check the article Working with Configuration in Laravel 4, it is useful if you use .env.local.php to protect you local database configuration.

Laravel Forge detecting environment variables in only 3 of 4 deployed sites

they are all configured identically.
the environment is definitely set to production.
the database credentials do not get read at all - it wants to use ''#localhost password: NO as the default.
i set
'mysql' => array(
'driver' => 'mysql',
'host' => getenv('DB_HOST'),
'database' => getenv('DB_NAME'),
'username' => getenv('DB_USERNAME'),
'password' => getenv('DB_PASSWORD'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
in all 4 of the sites app/config/database.php, and i set the related environment variables in forge. i have tried leaving the environment field blank, and also typing in 'production'. i also tried using $_ENV['environmentvariable'] in database.php.
this gives me a different error of Undefined Index DB_HOST. so clearly the env vars aren't getting read.
i've taken everything down and re-created the repo and the laravel server many times. all 3 other sites are configured identically. it detects the environment in bootstrap/start with
$env = $app->detectEnvironment(function()
{
return getenv('ENV') ?: 'development';
});
i appreciate any help guys. it works fine on homestead btw.
edit: i can ssh into forge and do whatever i want in any of the sites or databases as well, except in the problem one any php artisan command fails with the 'Access Denied for ''#localhost password: NO', even if i run it with --env="production".
I am not entirely sure if you are using .env files at root to specify different environment vars, but after going through this myself I realized that while when environment is 'local' it reads '.env.local.php', when environment is 'production', it wants '.env.php'.
It's in the docs, but I forget almost every time. I'm hoping that typing this out will help. :)

Environment variables in database.php

I have added DB credentials as environment variables (using nginx), so that i can use them like so:
return array(
'connections' => array(
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => getenv('DB_NAME'),
'username' => getenv('DB_USER'),
'password' => getenv('DB_PASS'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
)
)
);
The problem is that when I use artisan the environment variables do not seem to be available, so when i run migrate or seed i get errors.
Is there a way around that or should i just write my DB credentials directly in my config file ?
To edit my previous answer (sorry for misunderstandig):
Yes, environment variables are created by server, so they can't be reached or modified from CLI. Before deploying, server is generating those variables, so they can be "injected" into application at runtime.
I am thinking that it is maybe possible to reach those variables through remote Laravel package and SSH ? For example php artisan tail command is reading locally errors from the server side.

Using Google Cloud SQL with dev_appserver.py and Laravel (PHP)

I am having issues trying to work with a Cloud SQL instance and Laravel. I was able to do local dev work on a Cloud SQL instance with Python but I can't seem to get it with PHP.
Here is the error I get:
SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/cloudsql/project:instance-db'
Here is my app/config/database.php:
<?php
return array(
'fetch' => PDO::FETCH_CLASS,
'default' => 'mysql',
'connections' => array(
'mysql' => array(
'driver' => 'mysql',
'unix_socket' => '/cloudsql/my-project:instance-id',
'host' => '',
'database' => 'my_database',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
),
'migrations' => 'migrations',
);
It all works when deployed just not locally.
You cannot connect to CloudSQL from your local machine right now. For testing you should use a local MySQL instance the CloudSQL instance when running in production.
You should be able to assign an IP address to your Cloud SQL instance and allow your own network to access the instance in the management console.
Assigning an IP address will cost extra.

cakephp hosting "Cake is NOT able to connect to the database."

beginner level stuff-
-i recently hosted my cakephp project on 000webhost.com
-it shows the cake homepage
-but at the same time shows
Cake is NOT able to connect to the database.
Datasource class a2952772 could not be found.
-i have tried editing root/app/config/database.php number of times.
-i have created mysql database on 000webhost.com
i guess there is some problem with
$datasource in
public $default = array(
'datasource' => '????',
'persistent' => false,
'host' => 'mysql11.000webhost.com',
'login' => '*****',
'password' => '*****',
'database' => 'db1',
'prefix' => '',
//'encoding' => 'utf8',
);
if anybody has hosted and configured databases on 000webhost.com ,plz help...
You need to specify a valid datasource, such as 'Database/Mysql'. Also check the CakePHP database configuration section.

Categories