I have a Heroku app where I'm hosting my Laravel app. I started the development initially with MySQL, so I wanted to continue doing so using Amazon's RDS service. I create the instance there and managed to successfully connect via my MySQL client, the console etc.
The problem is that the Laravel app can't connect the database after numerous desperate attempts for me to fix it. I have found some articles suggesting the use of DATABASE_URL environment variable is mandatory, so I added it via the Heroku app settings. It looks like so:
mysql://myusername:mypass#myhostnamefromamazon/mydb?sslca=/app/storage/certs/amazon-rds-ca-cert.pem
I found this solution on Heroku's website. I have placed the amazon-rds-ca-cert.pem file on my Laravel's storage folder, like so: /app/storage/certs/amazon-rds-ca-cert.pem
This didn't solve my issue, so then I kept looking and found a Stackoverflow question which had this issue on Lumen. I adjusted my config/database.php according to the answer, but it's still not working for me!
<?php
$credentials = get_db_credentials();
$config = [
'default' => env('DB_CONNECTION', 'mysql'),
'connections' => [
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', $credentials->host),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', $credentials->database),
'username' => env('DB_USERNAME', $credentials->username),
'password' => env('DB_PASSWORD', $credentials->password),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
],
];
if (env('APP_ENV') == 'production') {
$config['connections']['mysql']['options'] = [PDO::MYSQL_ATTR_SSL_CA => '../storage/certs/amazon-rds-ca-cert.pem'];
}
return $config;
The get_db_credentials() function simply parses the DATABASE_URL environment variable.
The exact exception that I get is:
[2018-10-25 19:32:16] production.ERROR: SQLSTATE[HY000] [2002] Connection timed out {"exception":"[object] (Doctrine\\DBAL\\Driver\\PDOException(code: 2002): SQLSTATE[HY000] [2002] Connection timed out at /tmp/build_05920c42a6de0a378402b798320d3f04/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php:50
I'm totally lost on this and unsure how to proceed.
Your AWS Security Groups must permit traffic from Heroku's IP address range.
https://devcenter.heroku.com/articles/amazon-rds
You must grant Heroku dynos access to your RDS instance. The recommended way to do this is to configure the RDS instance to only accept SSL-encrypted connections from authorized users and configure the security group for your instance to permit ingress from all IPs, eg 0.0.0.0/0.
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.
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!
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
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. :)