Installing laravel on free hosting - php

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!

Related

Laravel 5.4 environment based config files

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.

Remove hardcoded Mysql Credentials in Laravel Framework

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.

Laravel 5 error SQLSTATE[HY000] [1045] Access denied for user 'forge'#'localhost' (using password: NO)

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.

Forge not reading my Laravel 5 environment variables

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

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. :)

Categories