What's the reason to put all files from public to root directory of applications?
public folder issue / .env file related issue
Hello , i want to know two things one things is
- in laravel project run without public directory
- when i hit .env / composer.json / package.json / robots.txt all file then show all information how to hide or what the procedure for this kind of problem
thank you
As the previous answer said it would be good to know
-
What is the problem of having a public folder? I'm kinda assuming that it might have something to do with shared hosting? :D
-
Set correct permissions on the file, are you using CPanel or are administrating some kind of linux based or windows machine?
- No, don't. That is the only dir that
shouldbe accessible (/public) to the public. That's why it's called public. Everything else should not be accessible, like going up a dir and being able to access .env. - Simply set your webservers DocumentRoot to the /public dir of laravel. That's how it's supposed to be, and you won't be able to read anything outside of the public dir. That's why it's called public. It's the only thing that should be exposed.
You'll have to figure out how to do that for whatever system/webserver you're using. There's lots of info on the net about setting up all webservers and the DocumentRoot.
If shared hosting here is an excellent guide from @snapey
http://novate.co.uk/deploy-laravel-5-on-shared-hosting-from-heart-internet/
I use this guide and it works perfect.
@EDIM - ok i want to use local server , its happens there also ..
@CRONIX - but without public folder , if i route this link then show all information
http://localhost/laravel/laravel-one/.env
and
http://localhost/laravel/laravel-one/composer.json
and
http://localhost/laravel/laravel-one/package.json
all files are open -- :(
@TOWHID - You don't have to move anything. You have to change settings of root directory to be /var/www/your-app/public, usually cPanels provide this in UI, it's very easy to change it.
Sorry above guide did not help, it does work:
Ok image:

https://i.imgur.com/Oo6k4Fp.jpg
laravel54up is main laravel above htdocs in wamp, shared hosting, xampp etc
laravel54 is public under htdocs.
I know of no other way to explain, thought a picture would help.
I was hoping the guide from @snapey would have helped.
public_html htdocs www all the same depending on host
what the procedure for this kind of problem
Sorry if you do not understand.
@SERGIU17 - ok - tell me first in local server how to do that ?
Because you haven't set the DocumentRoot to be /public. You need to learn how to set your webserver up.
Google whatever your webserver is plus document root. "nginx document root", "apache document root", or if you're using a control panel with your host, check how in the control panel, or with your host directly.
You will want to set up a virtualhost as well (so you can set document root up per domain), so you can access each project with it's own domain, like http://domain1.test, http://domain2.text etc, instead of /localhost/laravel/domain1, /localhost/laravel/domain2, etc.
Right now you have it setup so everything is being run off of a single domain, which isn't how you'd normally set up a website.
You'll want to read up a lot more on how to set up a virtual host for whatever it is you are using.
What did you use to set up your webserver? What webserver is it? Is it a WAMP type package?
I give up.
@CRONIX - Xamp server i am using
@JLRDW - ok i will check it and i will confirm -- is it work me or not , thank you .
Check the xamp docs. You have a lot of reading to do. You need to create a virtual host for each domain (each laravel project), and then properly set the document root for each domain. Google "xamp virtual host" for more info how to set it all up.
https://www.cloudways.com/blog/configure-virtual-host-on-windows-10-for-wordpress/
https://www.wpwhitesecurity.com/multiple-websites-xampp/
The above talks about creating wordpress sites, but that doesn't matter. It's the same for laravel. You just need to enter the correct DocumentRoot and ServerName for each domain, and then also add it to your hosts file.
I haven't used xamp for about 10 years, so can't really assist with this.
@CRONIX - ok i am trying your instruction, if i have any update i will inform you thanks
@CRONIX - hello , i created as like your instruction
http://laravel-one.localhost/.env
but its same result all files are open :( all arefile open .env ,package.Jason and composer.jasson
We can only guess as to what you did since you didn't show your code/config. I assume you did not set it up correctly, or it would work. You've also been messing around with your files this whole time trying to get things to work in different ways. You may (probably) have broken it elsewhere when you were doing that. I really don't know what you've done or how you've set it up. There could be several places where you've broken it, and finding that will be quite a challenge, especially when you don't show us what you've done.
DocumentRoot should be /your/path/to/laravel/public. public needs to be the last part.
@CRONIX -
<VirtualHost *:80>
DocumentRoot "c:/xampp/htdocs/laravel/laravel-one"
ServerName laravel-one.localhost
<Directory "c:/xampp/htdocs/laravel/laravel-one">
</Directory>
</VirtualHost>
and
#
127.0.0.1 localhost
::1 localhost
127.0.0.1 laravel-one.localhost
Did you read my last post, specifically the last sentence? Your DocumentRoot does not have /public
That's the same thing you posted before. I don't know what to tell you. You still haven't added /public to the end of DocumentRoot I've stated it 3 times now...
Hold on.. maybe I can help here.
From Laravel documentation:
After installing Laravel, you should configure your web server's document / web root to be the public directory. The index.php in this directory serves as the front controller for all HTTP requests entering your application.
Thread recap:
Simply set your webservers DocumentRoot to the /public dir of laravel
You have to change settings of root directory to be /var/www/your-app/public
Because you haven't set the DocumentRoot to be /public
DocumentRoot should be /your/path/to/laravel/public. public needs to be the last part.
Your DocumentRoot does not have /public
You still haven't added /public to the end of DocumentRoot
You might need to add public to your DocumentRoot?
@TALINON - I think one of the problems that will come up, is setting up stuff one way in development and then having to set it up a different way if using shared hosting.
I try to match development and shared hosting environments.
Anyway this will be my last response here I promise.
The guide I shared is not from me it is just one that I use that works for both shared and local development.
@TOWHID - Do it like this:
DocumentRoot "c:/xampp/htdocs/laravel/laravel-one/public"
ServerName laravel-one.localhost
<Directory "c:/xampp/htdocs/laravel/laravel-one">
</Directory>
</VirtualHost>
the public was missing and that is why you NEED to have a public folder in your application. c:/xampp/htdocs/laravel/laravel-one/public
@cronix last night i was set as like your instructon
just type mistake and this is the code
<VirtualHost *:80>
DocumentRoot "c:/xampp/htdocs/laravel/laravel-one/public"
ServerName laravel-one.localhost
<Directory "c:/xampp/htdocs/laravel/laravel-one">
</Directory>
</VirtualHost>
and this time is not any file show its show ----- 404 error when i want to route
laravel-one.localhost/.env
and other's thing now tell me about shared hosting - local server solve my problem as like your instruction ........ i think this is my best answer for any local server ...
@TALINON - thanks for your instruction as like many way - its really helpful to me - :)
If you think what he wrote was helpful, I'd urge you to read peoples posts a bit closer. His entire post was just quoting what everyone else had already said about adding /public to DocumentRoot, but you kept ignoring.
If you get en error for .env now, that's good. Does everything else work now? It's not clear by your post if things are all working now.
All this was covered once already, and good links given:
https://laracasts.com/discuss/channels/laravel/how-to-transfer-local-server-to-live-server-site
OP had trouble with .env still being readable, after it was shown how to hide .env.
You asked snapey there to give you a link as how to do it.
He gave http://novate.co.uk/deploy-laravel-5-on-shared-hosting-from-heart-internet/
The exact link I gave.
Sorry, I am confused on your confusion.
@CRONIX - i am not ignoring the points i just don't understand what all are said yes now everything is ok local server public folder directly rout and root folder not a single file open thank you .
Please or to participate in this conversation.