Laravel "public" folder on a cloud server: is it safe to leave it in public_html? - laravel

we recently contracted a cloud server (NOT shared hosting). We are going to proceed to install Laravel in it. I would like to know if it is safe to leave the content of the "public" folder inside "public_html" and move the rest of the app one level up or if it is necessary to also move the public folder outside of public_html (this last option is complicated to set due to restrictions of the server). Thanks in advance!

It is completly fine to upload a Laravel project like that, in my experience the folder structure i used was...
| public_html
| index.php
| ... etc
| laravel-app
| app
| migrations
| ... etc
While my public_html/index.php file had this config.
require __DIR__ . '/../../laravel-app/vendor/autoload.php';
$app = require_once __DIR__ . '/../laravel-app/bootstrap/app.php';
With this structure your are not compromising user information by not setting any info in the public folder.
What is more, by doing this you take away from git structure all the files in public folder, so any hand made change made to the files won't affect your git repo.
Of course, when you modify any of the files (js, css, index, etc) you need to manually move them.
I made a video not so long ago (it is in spanish) but the goal is to load a Laravel project to cPanel, the point is that i made a script that moved the /public files to /public_html with every git push to master, maybe you can do something similar.
Video: https://www.youtube.com/watch?v=a5cyTpQMqi0

if in the files in the public folder are not stored confidential info such as passwords and usernames related to any component of your website it can be fine

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.

How to run Multiple codeigniter application in single shared hosting server?

I am using shared hosing for Codeigniter application for deployment.
How can i run staging url on live server?
1) domain.com (Working)
2) domain.com/staging/ (Not working, showing 404 error)
I created staging folder under public_html directory, now put All CI code in staging folder, and tried to run the application with URL domain.com/staging/ but it redirect on domain.com/404
I expect to run domain.com for users and domain.com/staging for development. (Staging URL i will use for development and after that i will place the same code for live URL.)
This is most easily done by using a subdomain for the staging version, i.e. staging.doman.com. The folder where you put the CI files isn't super important as the subdomain will be set to use the directory you choose.
You will either need to have complete control over the server or have a service provider that allows you to set up subdomains.
- Expanded Answer -
If you cannot set up a subdomain then your next best option is to use the advice in the documentation on Managing you Applications. It isn't required to use the file structure shown there. In your case try the following.
Create a folder on the same level as application named staging.
Into that folder copy all the folders and files normally found in /application.
Add all application folders and files required by your site. The files in these folders are those that make up the application you are staging.
Make a copy of index.php (the file at the root of the public folder, i.e. domain.com/index.php) and name it staging.php. Both index.php and staging.php will be in the same folder.
Edit staging.php and change the value of $application_folder to be the absolute path to the staging folder. For example:
$application_folder = '/path/to/domain.com/public_folder/staging';
The path above is an example. You must replace it with the actual path in your server. If you have no idea what the full and absolute path is you can use
$application_folder = dirname(__FILE__).'/staging';
Browse to the URL domain.com/staging.php and you should get your default controller's output. To test that you are actually seeing the staging version of the page temporarily change something in the view file so there can be no doubt.
You will go to other controllers by using the URL domain.com/staging.php/other_controller.
Change the value of $config['index_page'] in /staging/config/config.php to staging.php, e.g.
$config['index_page'] = 'staging.php';
Without the above change redirect() and many other "helper" functions will not work correctly.
(Optional?) You can probably edit the main .htaccess and add rewrite rules for staging.php the same way it is done for index.php. But I will leave that as an exercise for you. (Or for another SO question.)
A final word. It might be necessary to make other configuration changes to accommodate the revised file structure. I tested the answer here but did not go beyond the most basic task of browsing to controllers. Most likely config values for sessions, cookies, and many others will need to be adjusted for the staged version to work correctly. Check out the documentation on Handling Multiple Environments for guidance on making this less painful.

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/

How to make higher than "public_html" folder work for cpanel installation?

I currently have a basic cpanel hosting account. It has the setup:
/home/mysite/ --> my home directory
/home/mysite/www --> symlink to the next folder
/home/mysite/public_html --> where all my files live
Problem is, with the latest laravel installation I did, I have a website with structure:
/app/ --> appy stuff
/public/ ---> what needs to be exposed
My problem is, I dont want to chuck everything into /home/mysite as there are other folders in there too. Is there a way that I can make the html directory something like:
/home/mysite/public_html/public
This way if people open www.mysite.com, it hits the /public folder. And not the /public_html folder. Is there a way to do this on a basic cpanel hosting solution?
I know this isnt the exact answer you want - but this is how I do it.
You rename public to public_html and there is no need to change any server settings.
Then in your bootstrap/paths.php file change
'public' => __DIR__.'/../public',
to
'public' => __DIR__.'/../public_html',
Then just put the other folders where they need to be. Trying to muck around with cPanel settings, and leaving your Laravel app inside the public_html folder might lead to an issue down the track.

Resources