I've built a Laravel site locally, and everything works wonderfully—but when I've tried deploying it to my shared host (Hostinger), I cannot get everything replicated or set up correctly. The obvious problem is that Hostinger puts the public-facing code in a public_html folder, which my Laravel setup does not have (it has public instead).
Also, I'm using GitHub for version control and (hopefully) deployment. I'm able to connect my GitHub repo directly in Hostinger, but the problem is, it pulls my code into public_html instead of the root.
I'm really sorry if this question/problem is dumb; I'm more of a web designer than developer these days, and I just need a damn portfolio site up and running. :)
Local directory structure:
root
--app
--bootstrap
--config
--database
--node_modules
--public
--resources
--routes
--storage
--tests
--vendor
I've read various articles and posts about either copying contents of public into public_html after uploading the code, and then changing __DIR__ suffixes in index.php; or ways to change public to public_html locally up front...but the former didn't work and the latter scares me since so much of my Laravel workflow using npm run and whatnot utilizes the public dir.
I also don't really want to have my remote and local out of sync. My desired workflow:
code locally <---
| |
npm run dev/watch --
|
npm run prod
|
push to GitHub
|
deploy to host
|
enjoy fresh website without any changes to remote files on host :)
You don't want to move the contents of public to the root and serve your app from the root because you will expose important config files such as .env.
Create another .htaccess in your root with the following contents:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
Which will rewrite all of your urls to the public directory. This should work in the local and production environment.
Have you tried to change public path in Laravel :
See answer here maybe this is a solution.
Related
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.
Currently my local code and production code follow the same directory structure. After, pushing my changes from local, i pull it on the server and it works fine.
Website path is something like http://abcde.com/code/laravel/public
code directory is under version-control
Now, i'm trying to change path to http://abcde.com
i followed this guide and thinking about moving source code out of server directory. So, the structure:
/var/www will contain content of laravel's public folder
/var/source will contain laravel
But then how'll i manage it with Git because the structure will be different?
Edit: I'm aware of two solutions:
Changing apache root
use .htaccess to remove public
For 1, there is a possibility that we'll add another directory admin in www, which will have its own source code. So, existing content of www will be moved to a sub-directory.
For 2, It's not recommended to keep source code inside web server root.
Please correct me if i'm wrong.
My thought is, you can make symlinks to preserve your directory structure, with this you help apache to keep it original configuration, and at the same time will no have any issues with version control.
Symlink is by using this command: ln -s /path/to/origin path/to/destination.
Bests ;)
There is a simpler procedure. Pull you complete project in var/www and add a .htaccess in var/www which contains -
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
Now you will be able to use http://abcde.com
I developed my laravel application on wamp. I am finally done and rent a cloud hosting server with CPanel interface. I uploaded all my files on public html and tried going to the site. It's supposed to go to login page but not working.
I used to deploy classic html file, this is the first time I'm deploying a PHP laravel site. I've successfully imported my MySQL database so it's ready. I'm just clueless of the configuration etc.
I'll walk you through the steps. Since you mentioned about public_html folder, I assume that this solution will work for you. Please follow the steps and change myapp to whatever your app name is.
Copy your project's public folder into public_html folder and rename it (Say myapp_public)
You will see a .htaccess file in public_html folder. Edit the contents of that file as follows:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^myapp_public
RewriteRule ^(.*)$ myapp_public/$1 [L]
Copy other folders in the root directory inside another directory say myapp
Now the final thing that you need to do is, to edit index.php file at the path public_html/myapp_public/index.php (if you followed the steps I wrote above). Change require statement as follows
Change require statement as follows:
require __DIR__.'/../../myapp/bootstrap/autoload.php';
Change $app variable as follows:
$app = require_once __DIR__.'/../../myapp/bootstrap/app.php';
And this should do it. Drawing out the simple directory structure for easiness.
~
|__ myapp
| |____ .env
| |____ app
| |____ artisan
| |____ .........so on
|
|__ public_html
|_____ .htaccess (this is the file to edit)
|_____ myapp_public
|________ .htaccess
|________ index.php (this is the file to edit)
|________ robots.txt ...... so on
I will explain in detail if you do not understand what is happening here.
EDIT
Make sure that you have installed composer via cURL as apt-get wont work on shared hosts. Once done, run composer install from your project's root directory i.e. myapp if you followed above steps.
Finally I got it, Vishal's answer really helped, but I needed to change my PHP version setting to 5.5 or higher, after looking at the error log and googling the error. Thanks again Vishal. I still have problems with database connection but I think I can handle it from here.
I logged into openshift application using filezilla.
Earlier for normal website i just drag and drop files at www directory its works pretty fine but in at this case i found some other folders and i am unable to get an idea where should i drop my php app in at this particular machine. I am unable to locate where the index file is located also. if I am using git for the deployment of application it works pretty fine.
Openshift makes it easy to upload program changes by checking out the container. Adding the code and the pushing it.
see How to git-checkout first application created on OpenShift? on how to do that.
I'm not sure about the structure when you use filezilla to connect to the Openshift container, but it should be similar to the checked out structure
index.php Template PHP index page
.openshift/ Location for OpenShift specific files
action_hooks/ See the Action Hooks documentation
markers/ See the Markers section below
pear.txt List of pears to install
Depending on what cartridge you are using:
php/ # for backward compatibility with OpenShift Origin v1/v2
public/ # Zend Framework v1/v2, Laravel, FuelPHP, Surebert etc.
public_html/ # Apache per-user web directories, Slim Framework etc.
web/ # Symfony etc.
www/ # Nette etc.
./ # Drupal, Wordpress, CakePHP, CodeIgniter, Joomla, Kohana, PIP etc.
See https://developers.openshift.com/en/php-repository-layout.html for more details.
try this:
goto app-root > repo
put your files in this folder. if you put a index.php, it will be accessible through the web.
but there's a more convenient method.
create a folder name php inside the repo folder and openshift will use it as a document root. which means all other files will be unaccessible from the public.
conclusion
put all other files in app-root>repo. put the index.php in the php folder and link other files accordingly.
optional:
use GIT. its easy and way more convenient than sftp. you need to learn only 4 commands.
git add
git commit
git push
git clone
that's it!
edit1
IF php/ dir exists THEN DocumentRoot=php/
ELSE IF public/ dir exists THEN DocumentRoot=public/
ELSE IF public_html/ dir exists THEN DocumentRoot=public_html/
ELSE IF web/ dir exists THEN DocumentRoot=web/
ELSE IF www/ dir exists THEN DocumentRoot=www/
ELSE DocumentRoot=/
above is the priority of document roots in openshift. use whatever you like.
Had the same problem. You need to turn off APC cache. This can be done via an .htaccess file:
php_flag apc.cache_by_default Off
I'm trying to deploy an PHP application which is written with Zend Framework to a shared cPanel server.
There are not many tutorials available on this area online, however, I followed several of them. It is successful to run the test page which proves the zend framework is installed correctly.
However, since cPanel server has a default root directory called public_html/, it is impossible to simply rename it to the Zend Server's default public/.
As a result, I had two options in mind: (Say the project name is AAA)
1) upload my projects under the /public_html/ directory, then the project will be like /public_html/AAA/public, and etc.
However, this one simply fails to work.
My thought would be something wrong here with the baseUrl setting, however, no matter I comment ( which is to remove the baseUrl) or set to the root page, ( in this case /public_html/AAA) both failed.
2) I tried to follow the way listed in this article: http://blog.motane.lu/2009/11/24/zend-framework-and-web-hosting-services/. Still failed.
Can anyone suggest how to do it?
Really appreciate your help!
Just symlink it:
ln -s public public_html
then this structure will work:
htdocs/
myvhost.com/
public/
application/
library/
public_html # this is actually a symlink pointing to public
Whatever you do, dont just throw everything in the publicly accessible area... its just bad form :-)
I don't think ZF cares what you name your "public" directory. It's just the convention that's typically used.
I can't think of any ZF component or common use case where anything explicitly points at "public/...".
A project structure like this should work:
myproject/
application/
library/
public_html/ # this used to be public until you renamed it.