Sorry if my question is dumb, but i can't find any help on this. I got a Laravel 5 application testing report recommending to remove the hard coded credentials from app/config/database.php. But where else do i save the DB credentials and how?
There is a .env file in laravel project root. You should add credentials there.
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=
DB_USERNAME=
DB_PASSWORD=
And in database.php -
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
For better understanding, read Laravel official doc.
You can store these type of informations in your environment (.env) file.
This file exists in your project root, if it doesn't exist, you can create it manually or make a copy of .env.example file if it exists.
An example data storage:
//.env file
DB_NAME="project_db"
And to use this variable in any other file of your laravel project:
ENV('DB_NAME') //returns "project_db"
Simple as that.
Note .env files are depending on the environment project is currently using and might have to change on different environments. Usually .env files are not being stored in repositories and are not deploying to a new environment with whole project.
Related
I am trying to change the Laravel configuration variables based on environment, but I do not know how.
Code to get config is
Config::get( 'contants.api_url' );
I am trying to have a separate results for the live environment and the development environment.
Real life scenario
I need to override some config files, so everybody who is using the development environment, has the same base configuration. I cannot use the .env file for this, because it is not shared between the team and everybody has their own version of it.
What did I try:
I read here Laravel 5 configuration - environments and overriding that you can create subfolders within your /config folder, that match the environment, to override the default config files. Alas it did not work for me and the Laravel 5.4 documentation on the subject matter (https://laravel.com/docs/5.4/configuration#retrieving-environment-configuration) did not speak about this option, thus I believe it might have been deprecated.
One option could be to use a service provider. If you're only going to be overriding one or two values then you might as well just use your AppServiceProvider, otherwise I would suggest creating a new service (e.g. ConfigServiceProvider).
In the boot() method of your service provider you could add something like:
if (app()->environment() === 'local') {
config()->set('contants.api_url', 'http://example.com');
}
To change the configuration variables based on environment, this will do:
Example file: config/database.php
'mysql' => [
'driver' => mysql,
'url' => env('DATABASE_URL'),
'host'=> (env('APP_ENV') === 'local' ? env('DB_HOST_LOCAL') : env('DB_HOST_PRODUCTION')),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'default'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', ''),
'options' => (env('APP_ENV') === 'local' ? [
'some_option' => env('SOME_ENV')
] : []),
],
Tested and works on Laravel 5.8.
Following this Setup Laravel 5 Application without command-line access
I have a question about second criteria.
I'm using free hosting and I have the "htdocs" folder that is basically my "public_html"(I think so), because the displayed "index.html" file that comes by default is in it. So, following the second criteria from the above topic, I need to change the "htdocs" for "laravel_folder/public", correct? If so, how can I do this by .htaccess, please?
I just deployed a Laravel 5.7 app on 000webhost which is free hosting website.
First of all, you have to generate a key for your app and erase the cache.
Use the command: php artisan key: generate
Then php artisan config: clear
and finally php artisan cache: clear.
Now you take the folder / root directory or where you have your app, you have to compress it in a zip file.
Create a new folder in the root of the File Manager, for example a folder called App.
Upload the zip file to the App directory on https://files.000webhost.com/ by clicking on the "Upload Files" button on top right.
When it's done, you have to click on the zip file and select "Extract". Also make sure to write "." to extract the contents directly into App.
Make sure all the files are in App just like that.
Move all the files in the public folder (App/public) to the public_html folder.
Now that we have everything, we are going to configure what is necessary.
A previous step is to create a new database for them you will have to go to the section "Manage databases" and create one. Then manage the base in phpmyadmin and import the database of your project / application.
Once this step is completed, the following is:
1.-Configure index.php, in the directory public_html/index.php, you must add the App folder to the addresses of autoload.php and app.php.
Change require __DIR__.'/../vendor/autoload.php';
by require __DIR__.'/../App/vendor/autoload.php';
Change $app = require_once __DIR__.'/../bootstrap/app.php';
by $app = require_once __DIR__.'/../App/bootstrap/app.php';
Below this line add the following
$app->bind('path.public', function() {
return base_path().'/public_html';
});
2.- Go to the .env file and modify the host, the database, the user and the password with the values of the database that we made in the previous step. Copy the APP_KEY also, we will use it later.
3.- In config/app.php, find the line : 'debug' => env('APP_DEBUG', false),
and change the value to true :
'debug' => env('APP_DEBUG', true),
find tehe line: 'key' => env('APP_KEY'),
and add a comma followed by base64_decode ('copy the key that was in the .env file here').
For example:
'key' => env('APP_KEY',base64_decode('AsAAAAA+AWERSDFT4Y123Ywpj123PNaleLdPwcd0000=')),
4.- In the same directory but in the database.php file (config/database.php),
find:
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
Add this line after host: 'options' => [PDO::ATTR_EMULATE_PREPARES => true,],
Modify the host, database, user, and password with the values in the database as in step 1.
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'options' => [PDO::ATTR_EMULATE_PREPARES => true,],
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'databaseNamehere'),
'username' => env('DB_USERNAME', 'userNamehere'),
'password' => env('DB_PASSWORD', 'passwordHere'),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
Now we can go to the address of the server and you can see your app running correctly.
I hope it helps you, greetings.
No, there you need to copy all the files inside the public folder (~/MyAwesomeProject/public/) into the public_html folder (~/public_html).
This blog article will help you to deploy Laravel project on Shared Hosting Server Using cPanel Options:
Laravel: Deploy Laravel Projects On Shared Hosting
From memory of working on shared hosting this is going to be difficult. What you could do, assuming FTP access, is upload everything to your root level, and then in the Laravel configurations you can change the name of your public directory, so to htdocs
Though I think you might struggle with some of Laravels features without having command line access
I'm assuming you're developing for a production environment, if so you can get a cheap VPS or Droplet where you could effectively utilise Laravel alternatively VirtualBox or something for development would work well
Laravel quite heavily relies on Artisan (their Cli interface) as well as various tools within there (such as Migrate which works with Eloquent to deploy tables) so I think you might struggling using this on a shared box.
The reason for all of this reliance is because Laravel was built for Enterprise usage, where generally shared boxes aren't utilised.
I think there are some hosts out there who will give you command line access, however.
If you need to use a different MVC framework without having command line access CodeIgniter is a good one
I just deployed a Laravel app on 000webhost which is free hosting website. I faced some problems, but here's how I solved them:
1- Go to your app's root directory (where app, public, resources and other directories exist) and zip them all into a one zip file (it is important for it to be zip not rar).
2- Upload the zip file to the public_html directory on https://files.000webhost.com/ by clicking on the "Upload Files" button on top right.
3- It will take some time to upload. When it's done, you have to click on the zip file and select "Extract". Also make sure to write "." to extract the contents directly into public_html.
4- Now if you go to the URL 000webhost provides for your site, let's call it $url, then add /public to it which is where your entry point index.php file resides, you'd suppose to see the homepage of your site: $url/public . But you won't.
5- For some reason I didn't understand, the .env file which is a shortcut of your config files in the config directory could not be read. So, you'll have to update your config files, mainly config/app.php and config/database.php.
6- The first problem you'll have is the key issue. In config/app.php, find the line :
'key' => env('APP_KEY'),
and replace env('APP_KEY') with the actual key that you see in you .env file (in case it is empty for you, you'll have to generate a key by sending the command on your computer php artisan key:generate)
7- If you are using a database (such as MySQL), then you'll have to also go to config/database.php and replace all values of env with actual values, mainly these values :
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
P.S: there should be NO env variable whatsoever. Remove all envvariables that you don't use and replace the rest with real values.
I couldn't figure out a way to do virtual host for the free version (maybe you can configure a vhost for the paid version..). So, you'll have to remember that the url provided by 000webhost should be appended by /public (I say this again because you might have to change your navbar links as well by applying this logic).
And voila! It should work now. Well, in case you face any more issues, there's 2 things you can really do to figure them out:
1- In config/app.php, change the debug value from false to true to see the errors instead of a "whoops" message :
'debug' => env('APP_DEBUG', true),
2- You can check the storage/logs/laravel.log file to see the errors you are getting.
Good luck!
Sometimes (about every 20 request) I get this error. But the next (next second), the same request, is fine. I dont know why it failed the first one. Sometimes i can get another error :
No supported encrypter found. The cipher and / or key length are invalid.
My .env database parameters are fine.
I have generated a key using php artisan key:generate
This key is in my .env file under a APP_KEY key
My config/app.php has a key 'key' => env('APP_KEY'), 'cipher' => 'AES-256-CBC'
does anyone have ANY idea how this can happen?
Just cache your config using
php artisan config:cache
Don't forget to do this every time after setting your .env file.
I had this exact same problem for the past few days and I think I solved it:
The settings in .env are not always used for some reason or other and occasionally Laravel will just use the default settings in config/app.php and config/database.php.
config/app.php:
'key' => env('APP_KEY', 'SomeRandomString'),
'cipher' => 'AES-256-CBC',
Change the 'SomeRandomString' to the generated key from your .env
config/database.php
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
Change localhost, database, username, password to your actual settings from the .env. This example is for MySQL if you use another database, change those variables instead.
There might be a better solution (more secure?) but this is what so far kept the error from showing up.
You have to delete bootstrap/cache/config.php
I just experienced this after merging in changes from git without updating the .env file.
Basically, the person changed the code so that the application required these keys in the env file:
DB_SOME_DATABASE=something
DB_SOME_USERNAME=something
DB_SOME_PASSWORD=something
But I still had the old credentials in there, so it "looked correct", but Laravel was throwing this error that made it look like the env file wasn't being used.
This would indicate that, if you see this, check very closely if anything is spelled wrong in your .env file that would cause Laravel to attempt to use the default values (ie: look in the folder config/database.php where you see 'forge').
It may not be a typo. Someone may have added a database connection and maybe you haven't updated your .env file yet.
I created a new user on my Macbook Air (OS X Version 10.11.2). The new user has full administrator access. I'm using MAMP. The MySQL port is set to 3306. I can connect to MySQL with everything else just fine. All of my Laravel projects stopped connecting to MySQL. When i try to use
php artisan migrate
I get:
[PDOException]
SQLSTATE[HY000] [2002] Connection refused
Here is the database portion of my .env:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=store
DB_USERNAME=clerk
DB_PASSWORD=inventory
I have tried using the root user and password. I have tried using different ports. I have tried switching the host to localhost. I have verified that the username and password are correct, and that the user has access to the database.
Here is the MySQL portion of my config/database.php file:
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'store'),
'username' => env('DB_USERNAME', 'clerk'),
'password' => env('DB_PASSWORD', 'inventory'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
I have been searching for a few days, and i can't seem to find the answer. My old Laravel 5.2 projects won't connect either. I'm pretty sure that this was caused by creating a new user, but i don't know enough about MySQL to figure out the problem.
Having followed troubleshooting processes as I've suggested in comments below your question, all should be working as expected.
Should that not be the case, you must be missing something; ... a PATH issue I presume.
I suggest you export your database, uninstall your MySQL, then download a new one at here.
Follow the approach elaborated here for a possible fix.
With this, your challenge will surely be addressed.
Try changing your db_host = localhost:3306
You define an engine = null. The engine is used inside the MySqlGrammar.php specifically when compiling a create query as far as I know. I have only seen null engines for VIEWS.
Try the following config:
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'store'),
'username' => env('DB_USERNAME', 'clerk'),
'password' => env('DB_PASSWORD', 'inventory'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
],
You did mention it in your question and take it that the following works from a terminal
mysql -uclerk -pinventory -h127.0.0.1 -P3306 store
Could be that it needs to be
mysql -uclerk -pinventory -hlocalhost -P3306 store
Something else you can check with just an info.php file in your project to double check that everything seems fine with the reference to PDO as a module.
<?php
phpinfo();
Then also play around with just a little PDO php script to see if your connection is working properly. Something like:
<?php
$servername = "127.0.0.1";
$username = "clerk";
$password = "inventory";
try {
$conn = new PDO("mysql:host=$servername;port=3306;dbname=store;", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
} catch (PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
Something else you can also check inside a dummy route what env('DB_DATABASE') etc gives you.
Updated things to check 2016.09.15
To make sure that env() is fetching the correct information include the route below and then include the output of the following:
Route::get('dbtest', function () {
$config = [
'host' => env('DB_HOST'),
'port' => env('DB_PORT'),
'database' => env('DB_DATABASE'),
'username' => env('DB_USERNAME'),
'password' => env('DB_PASSWORD'),
];
dd($config);
});
Then also access mysql as the root user and include the output of the select statement:
mysql> select host, user, password, authentication_string, password_expired from mysql.user;
As you are using MAMP make sure to use the mysql commandline tool from /Applications/MAMP/Library/bin/mysql
It's been solved. After a few hours of pulling my hair out, i decided to take #nyedidikeke advice and re-install mysql. I was using MAMP, and I have been wanting to stop using it for a while. I uninstalled it, and installed mysql through Homebrew. Everything is working just fine, thanks for all your help!
This should be really easy, but it's not. Here is the default settings for a MySQL database in Laravel 5.
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
I ran php artisan env on the production server and got production. I set my environment variables with the correct keys on Forge. But when I run artisan on the production server, I get an error: 'Access denied for user 'forge'#'localhost' (using password: NO)' The reason I think it's saying password: NO is that an empty string is the default for DB_PASSWORD if the environment variable is not set.
I think what is happening is that Forge is not reading my environment variables and is using the defaults specified. If I change the values of my environment variables, I get the same error message, which seems to confirm this. I have restarted the Digital Ocean production server, thinking that would force the server to read the variables. But nada. Do I need to do something else in Forge to get it to read the variables. Jeffrey Way's Laracast doesn't indicate that I need to.
What's going on here?
Laravel 5 is not yet in beta.
Taylor has already mentioned in a recent podcast that he'll need to make changes to Forge to accommodate Laravel 5 environmental settings.
I think from memory he mentioned the ability to just edit the .env file from Forge.
For now - you should just put all your settings into a .env file in the root of your web server and it will work.
In case anyone else finds this and is still experiencing it, the .env on both Laravel 5 and Forge now work. You must however make sure your database credentials are correct. This should work for most of the defaults as of the writing of this post (Laravel 5.2)
DB_HOST=localhost
DB_DATABASE=database_name
DB_USERNAME=forge
DB_PASSWORD=the_provided_password