Redirect to public/ in php artisan serve - php

I have a laravel application that I made changes to the folder structure, I have been working with laragon and everything works fine but on my local environment and live server.
However, when i user php artisan serve, the generated url doesn't load assets in the public folder.
E.g
https://livewebsite.com // [live server] works.
https://myproject.local // [laragon] works.
http://localhost/myproject // [laragon /xampp] works
http://127.0.0.1:8000 // artisan [doesn't load assets, assets in the public directory returns 404
//e.g http://127.0.0.1:8000/public/assets/themes/cryptic/style/bgs.css would return 404
Changes I made,
|-core
|-public
|-.htaccess
|-index.php
I have moved all laravel core folders and files to the core folder,
moved the .htaccess and index.php to the root directory.
From my debugging so far, I understand that laravel built in server redirects requests to the public folder where the index.php file is located.
What changes can I make so that the request is not sent to the public folder,
i.e http://127.0.0.1:8000/public/assets/themes/cryptic/style/bgs.css should just be allowed to be without be redirected.
The entire thing works on various environments except when I'm using laravel built-in PHP server with the artisan command.
EDIT
Folder structure
|-project
|-core
|-app
|-bootstrap
|-config
|-database
|-lang
|-etc
|-public
|-assets
|-test-folder
|-img.png
|-.htacess
|-index.php
in this setup, laravel base_path() is project/core . This where all laravel core files and folders are stored.
Now I have folder test-folder with an image img.png . If i go to http://localhost/project/new-folder/img.png it will return the image but if i go http://127.0.0.1:8000/new-folder/img.png will return 404. If i use a live server and go http://livewebsite.com/new-folder/img.png , it loads.

I can't comment due to reputation, but what you're doing might be unsafe!
This way all your project files will be public, which can expose credentials and exposes Composer packages to the public as well. This means any PHP file in any Composer package can be executed, which can lead to remote code execution, which has happened before here and is described here.

Related

Issue with Storage on shared hosting

I am deploying my first project on a shared hosting.
I followed this tutorial to deploy the website and turn the public folder into the public_html folder of my hosting plan.
When I upload an image from my website (with storeAs() method), the file is uploaded in the private/storage folder, not the public one (where I would like).
The asset() function try to display the image from public_html/storage.
What can I do ?
Thank you :)
My files are like this :
private/
- app/
- bootstrap/
- config/
- database/
- resources/
- routes/
- storage/
- tests/
- vendor/
public_html/
- css/
- js/
- images/
- js/
- storage/
(Laravel 8, trying to be hosted on Hostinger)
The tutorial : https://dev.to/pushpak1300/deploying-laravel7-app-on-shared-hosting-hostinger-31cj
Why does this problem happen?
If you implement and need storage matter in your project then you need to link storage folder to the public folder for security reason and to make the URL simple, clean and readable by the users, so when you upload the laravel project to a server, you need to run the following command line to complete the linking process:
php artisan storage:link
and since you host the laravel project to a shared server, so you don’t have access to a terminal to run the above-mentioned command line . so I’ll show you a way to do that manually without using SSH or the terminal.
Steps to fix the problem
1- At localhost server-side (before uploading the project to the server), make sure that you have done filesystem configuration, then run the following command line:
php artisan storage:link
after that you will get a message “The links have been created.” and that is ok for now.
2- Upload the project to the server, the storage link will not work as you expected, so we need to start fixing this.
3- Go to the public folder you will find a folder called “storage”, delete it.
4- Go to routes folder and open “web.php” file, then copy & paste the following code at the top of the file:
Route::get('/linkstorage', function () {
Artisan::call('storage:link') // this will do the command line job
});
Don’t forget to save the changes.
Also, you can do the same process via “api.php” and using requests tools like postman.
5- Now we just need to run this code, so we need to do the GET request by entering the following URL at browser search bar:
“https://www.your-domain.com/public/index.php/linkstorage”
or
“https://www.your-domain.com/public/linkstorage”
this request will run the above code “Artisan::call('storage:link')” which is, in turn, will run the command line
PHP artisan storage: link
now you can go to the public folder and you will see the “storage” folder created again and marked as a shortcut folder and that means the public storage folder now is linked to the storage folder of the project.
6- Now if you upload or create a file to the storage folder, then it will appear in the public/storage folder too, now you can hit the URL of the file at the search bar of the browser and you will get the file successfully :).
Ok guys i found the solution !
Here is how to deploy your Laravel project on one shared hosting using public_html folder : https://dev.to/pushpak1300/deploying-laravel7-app-on-shared-hosting-hostinger-31cj
This will work but you can have a problem (like me) with the storeAs() method if you deal with file uploads.
To solve this problem, I edited the MyProject/config/filesystem.php by adding this in the available disks :
'public_folder' => [
'driver' => 'local',
'root' => 'PATH',
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
]
Replace PATH with the complete path to your public storage (for example /home/you/domains/mydomain.com/public_html/storage/).
Now, open the controller which manage file upload and edit it like this :
request()->image->storeAs('/uploads', 'filename.png', 'public_folder');
This will upload the request()->image in public_html/storage/uploads/filename.png.
I hope it will help you if you had the same problem as me.

Laravel 5.7 Nova - The Mix manifest does not exist (on server)

I set up laravel Nova locally last night, tested it, used it, everything works great on my local machine.
Uploaded it to the server, and everything works great except when i head to /nova which rightfully redirects me to /nova/login but then presents me with this error
The Mix manifest does not exist. (View: /home/loanappboi/nova/resources/views/auth/layout.blade.php)
i'm using shared hosting, and i uploaded the files to the root, and uploaded the public folder content, into public_html (i did the necessary file updates to reflect this change)
What could be the cause of this?
i need help
I got this same error. Running php artisan nova:publish fixed it for me. You need to have the nova resources generated.
mix() helper function use /public/manifest-json.js
Set it in config
mix.setPublicPath('public_html/')
OR
use link to public
ln -s ./public ./public_html
For Apache Options FollowSymLinks
Make sure that you are adding '/vendor' in your .gitignore and not 'vendor'. Adding just 'vendor' will ignore all folders named vendor in your codebase and nova resources are in the vendor folder in public folder.

HTTP 500 Error in Symfony 3

I created an app on my localhost using symfony 3. Stuff works perfectly well on my local host. Now my main problem is moving it to my shared hosting (cPanel). Moved the files to the public_html folder, then I moved the .exe, bin, src, var, etc folders to the parent folder (The folder containing the public_html folder) while leaving the 'web' folder in the public_html. I then moved everything from the 'web' folder (Which is inside the public_html folder) into the public_html folder. Making me have the following directory structure:
cpanel-root-directory<br>
---/app<br>
---/bin<br>
---/src<br>
---/tests<br>
---/var<br>
---/vendor<br>
---/public_html<br>
------/bundles<br>
------/css<br>
------/dist<br>
------/fonts<br>
------/img<br>
------/js<br>
------/plugins<br>
------/uploads<br>
------.htaccess<br>
------app.php<br>
------app_dev.php<br>
------apple-touch-icon.png<br>
------config.php<br>
------robots.txt<br>
<br>
When I go to the domain name, I keep getting this annoying HTTP 500 error. Then on both localhost and on the server (at different times), I created a fresh new symfony project, dumped it in the server and got the "Welcome to Symfony 3" page quite alright. But, when I tried putting my controller files, and going to the routes defined in them (I use annotations in the Sensio bundle to define routes), I get that HTTP 500 error. Then, in the default controller that works, I try defining a new action 'testAction'. When I go to the route for the test action, I still get the HTTP 500 error, but going to the '/' route, I see the symfony welcome page correctly.
My clients have seen that the stuff works perfectly well on my localhost, but now moving it to the server is the issue.
If you can (have access), just keep your web dir, and then make public_html a symlink to your web dir.
If you need to change the name of your "web" dir, you need to follow this instructions:
http://symfony.com/doc/current/configuration/override_dir_structure.html
You should check this paragraph from Symfony documentation
This part seems to describe exactly your problem:
Some shared hosts have a public_html web directory root. Renaming your web directory from web to public_html is one way to make your Symfony project work on your shared host. Another way is to deploy your application to a directory outside of your web root, delete your public_html directory, and then replace it with a symbolic link to the web in your project.
Eh, I think that's because, now, you are online, so you need to clear the cache for the prod environment:
$ php bin/console cache:clear --env=prod
But you say you are on a shared hosting, so unless you have some available tools to access the terminal (and run the above command), then you need to manually delete the var/cache/prod directory if present.
Or if this doesn't work, I usually, don't upload the var/cache + var/logs + var/sessions directories at all, but create them manually as empty ones, and then let Symfony do its job with them by default.
LE
Maybe you have another problem, and the one with the cache you've solve it already.
So, to check if there is something wrong with your app, enable the dev mode ( app_dev.php ), by modifying, locally, your public_html/app_dev.php file, and comment out these lines:
if (isset($_SERVER['HTTP_CLIENT_IP'])
|| isset($_SERVER['HTTP_X_FORWARDED_FOR'])
|| !(in_array(#$_SERVER['REMOTE_ADDR'], ['127.0.0.1', '::1']) || php_sapi_name() === 'cli-server')
) {
header('HTTP/1.0 403 Forbidden');
exit('You are not allowed to access this file. Check '.basename(__FILE__).' for more information.');
}
And then, copy this file over the one on the server, and then access a route with app_dev.php in front, and see if there is an error (eg):
http://www.domain.com/app_dev.php/existing_route
After you are done, don't forget to uncomment the code inside app_dev.php,and re-upload the file on the server.

move symfony from root directory to public_html

I have a symfony2 project that somehow was running fine while it was outside the public_html directory, but now it's not working (the path is not allowed anymore, apparently).
Now I need to move the symfony2 directory into public_html, but when I do, the website is still not working (the page is blank).
I modified app.php so the include path is the correct one, and I also modified the projectConfiguration class so it has the right WebDir. What am I missing?
alternatively, how can I make it work outside the public_html directory?
for the record, I'm completely new to symfony.
The cache was the culprit.
I cleaned symfony's cache (removed all the contents residing in the cache folder) and the website came back to normal.
thanks to fos.alex for the comment/answer.

Install a Laravel 4 Application in the Public directory of a site?

Is there anyway possible to install a Laravel PHP application into the same directory?
What I mean is as I see it, you have to install the whole Laravel application 1 directory under the the main Public directory.
I know that you can change the Public directory to anything in the settings but the server my client has, they really need for everything to live in 1 directory.
Current Folder Structure....
app/
bootstrap/
vendor/
public/
artison
server.php
If this was all dropped into a Public directory on the server, then you would have to access the application/website at the domain www.domain.com/public/
I am hoping there is a way to put all these files/folders into a public or www htdocs type folder and still access the application at the root.
I know the main reason to keep the PHP files out of the Public accessible folder is for security but in my clients situation this is not possible the way there server is configured.
Surely there is a way to do this?
Try putting contents of public in same directory as other folders, and then edit your index.php previously in public to look something like this
require __DIR__./bootstrap/autoload.php';
$app = require_once __DIR__.'/bootstrap/start.php';
I haven't tried this. Just came to my mind right now
Anyway, I'm doing this by keeping app, boootstrap, vendor in / of my hosting, and contents of public are in my public_html. It works fine, and I guess it could be a security plus.

Categories