Image root issue in laravel in cPanel - laravel

Images getting stored in outside public folder and in view, getting called from inside public folder. Everything was working fine in local host. After uploading the project in cpanel i took index.php and htaccess outside public folder. How can i store and call them from same folder? Thank you.

When using Laravel all HTTP Requests has to be hit public directory. But since you are using cPanel all Requests hit directly public_html by default.
You tried to overcome it by moving index.php and .htaccess files to public_html from public_html/public and you broke its structure.
If you have the Administrative (eg: root) access on server, you can solve this by editing your Web server (I assume Apache in your case) to point your domain's requests directly to your Laraval application's public directory.
Usually every Web site in cPanel have separate config files. Their paths something like this:
/var/cpanel/userdata/CPANELUSER/WEBSITEDOMAIN.COM and /var/cpanel/userdata/CPANELUSER/WEBSITEDOMAIN.COM_SSL
In this files there is a documentroot field, it points to public_html directory by default:
documentroot: /home/CPANELUSER/public_html
You need to change it and point it to your application's public directory;
documentroot: /home/CPANELUSER/laravel/project/path/public
After you update both of these files, you need to rebuild your Apache config and restart it;
/usr/local/cpanel/scripts/rebuildhttpdconf
/usr/local/cpanel/scripts/restartsrv_httpd
If those 2 lines don't work, you can try this 2;
/scripts/rebuildhttpdconf
/scripts/restartsrv_apache
If everything done well, all HTTP Requests will hit your Laravel application's public directory as expected.
After that; if your uploaded files stores in public storage, Laravel puts them in to storage/app/public and you need a symlink to it from public directory.
Laravel creates it when you run
php artisan storage:link
Now all your application's requests as your.domain/storage/bla.jpg will give a response from storage/app/public/bla.jpg
And if you don't have administrative access to server you can check this tutorial: How to deploy Laravel 8 project on Cpanel shared hosting

Related

Laravel, Using a cPanel shared hosting if possible?

I have a shared hosting with bluehost.com... can I just put my project on that.. like upload public files to public_html and others to root directory and backup MySQL DB and upload it to my shared hosting then edit my .env file... will this actually work with Laravel?
I searched most online resources and it requires ssh access to deploy, but the way I explained should not require that right?
I mean this way you don't have to go throw terminal access right?
I used to do that with WordPress when I want to set everything locally then upload it and edit URLs from the dashboard and stuff...
Yes it would work.
But you have to ensure one thing that, in your cpanel your domain point to the /public folder of laravel project.
Normally cPanel points the http requests to public_html folder, but laravel start from its public folder. You can achieve this by two way.
By pointing your server to straight to public folder
Copying the index.php to the public_html, in that case you have to edit index.php to run the laravel project properly. And I don't recommend this step. Step 1 is lot more easier.

Hosting Laravel 5.7 project in Godaddy server

What will be my .htaccess code? I want to host my laravel project in godaddy server, my project path will be in public_html/coder.com ? In coder.com, i have to keep all my project files and folders. Any idea please someone help me.
You have to keep backend files outside of the public_html folder. The content of the Laravel public folder should be placed inside coder.com folder. That is, you index.php, .htaccess and the rest should be placed inside coder.com folder.
Then, you need to edit index.php to point to the correct locations of vendor and bootstrap folder.
If, say for example, you placed the Laravel backend files in a folder named laravel at the same level as public_html, it should be /../../laravel/vendor/autoload.php and /../../laravel/bootstrap/app.php.
You do not need to edit the content of the default .htaccess file.
But, shared hosting almost always gives you unforeseen troubles. If you do not have shell access, you might need to ask for technical help from GoDaddy team to fix symlink issues if required.
Normally, there will be no issues with folder permissions, if there is you need to make sure that storage and bootstrap/cache is writable. Never ever give 777 permissions; 755 for folder/directories and 644 for files will be good. For this also, since it's shared hosting, you might want to seek technical assistance from GoDaddy.

laravel Asset() function break down after deployment

I developeded a Laravel web app, which works fine and great on local machine but
after going in production on a shared hosting server i have some issues displaying the logo and the website images using the asset() helper function.
However the css and js are accessible and works fine (also using the asset function).
I've deployed the App following the steps bellow:
I created a directory called repositories outside the public_html (the root document of mydomain.com) and i copied inside it directory soukelafalah (contining all the files of my laravel project)
I moved the laravel public directory files into the public_html directory
I edited the index.php in public_html as follow:
require DIR.'./../repositories/SoukElfalah/vendor/autoload.php';
$app = require_once__DIR__.'/../repositories/SoukElfalah/bootstrap/app.php';
I run php artisan storage:link and the link is succufully created
I had run also:
ln -s /home/dtruxpgw/repositories/SoukElfalah/storage/app/public /home/dtruxpgw/public_html/storage
The route and the css is working fine but the logo and the images are not displayed and shows a 404 error as shown in the image below.
So please, can any one point what i am missing?
And when i put asset('/storage/images/thumbs/'.$img_name)}}" am i pointing to the storage directory in the public_html or in the one in the public folder of my laravel app directory.
Thank you in advance.

Laravel 5.1 upload on host

I have used the Laravel 5.1 php framework to build a web application on my local XAMPP server.
Which files or folders should be uploaded on my shared host server?
Should the .env file at root be uploaded?
In this case, if sensitive information such as database connection passwords are in this file, is there a security risk?
All project files should be uploaded under project root \var\www\project (project = all laravel project folder and files and \var\www\project\public is where your domain pointing.
Storage should have write permission.
.env will be under project root and it is not available for public access.
.env won't be accessible for public, if it is then you have a configuration problem with you web server.
Move All the files on xyz(your folder name for which domain/subdomain is mapped) folder
Then follow these steps
Go to public folder
Copy .htacsses file
Paste it in xyz folder
Change server.php to index.php
Enjoy and happy coding
This is working fine with hostgator.in shared hosting.

How to Deploy Laravel 5 on shared cPanel hosting

I've been successful in deploying Laravel 4 applications on shared hosts by simply editing the paths here at /bootstrap/paths.php.
However, Laravel 5 does not have that file. How does Laravel 5 manage paths? I have access to the root directory with public_html. I can upload all the files to root and public directories, but confused where to look for the paths to be maintained?
Laravel 5 is smart enough to detect all it's path. I was missing some of the files during upload. :)
What I did was upload all the files as it is structured and the content of public folder was uploaded into the www directory, that's it!
You can play with creating new Application() in bootstrap/app.php. Its constructor takes argument which tells where is rest of the application related to public/index.php
Currently app.php has
$app = new Illuminate\Foundation\Application(
realpath(__DIR__.'/../')
);
Change the /../ to something else if rest of the application folders for example in ../laravel/ compared to index.html
If you have cPanel in your shared hosting you can try these easier methods before trying to move the Laravel's public folder:
Often you also have an SSH access when you have cPanel on shared hostings, so you can replace the www folder (i.e. the website document root) with a symbolic link to [app]/public, then deploy your Laravel app inside the [app] folder.
If you are deploying on a subdomain, e.g. [app].example.com, usually with cPanel you can choose the document root, then you can simply set it to [app]/public.
I think the two methods above are both easier and safer than moving the public folder. I would keep this as the last option.
Take a look here for some more info: http://blog.netgloo.com/2016/01/29/deploy-laravel-application-on-shared-hosting/

Resources