How to solve Error 404 when deploying laravel project to Cpanel? - php

I'm trying to deploy my laravel (Laravel Framework 7.28.3) to Cpanel, but got a 404 error.
I uploaded my project into /public_html, modified the index.php file to point to the correct files (as below).
I think there must be some mistake in the index.php file but couldn't figure it out.
This is my first time asking my question (after searching for it several times), so hope that I will get the answer!
require __DIR__.'/../vendor/autoload.php';
$app = require_once __DIR__.'/../bootstrap/app.php';
this is my file structure in File Manager:

Deploy laravel application in Cpanel
Setup 1 : - upload file to Cpanel the root directory – not the public_html.
Setup 2 : - Open the that folder and MOVE the CONTENTS of the public folder to your cpanel’s public_html .
Setup 3 : - Navigate to the public_html folder and locate the index.php file. Right click on it and select Code Editor from the menu.
and change this line
require __DIR__.'/../folderName/vendor/autoload.php';
$app = require_once __DIR__.'/../folderName/bootstrap/app.php';
NOTE : - folderName here is in root where you laravel application stay
that's it now all your request will come inside public_html folder index.php and this file will include require __DIR__.'/../folderName/vendor/autoload.php; and run laravel application
Folder structure will look like
/laravel
/public_html/index.php
indside index.php
require __DIR__.'/../laravel/vendor/autoload.php';;
$app = require_once __DIR__.'/../laravel/bootstrap/app.php'; // here laravel is folder name

You need to make sure your application is in a folder outside of your public_html.
Then you need to make a symbolic link to everything in your public directory inside your application. This symbolic link should be placed in your public_html.
This way your business logic is not available from the outside, only from your own application.
Actually it is advisable to clone your application using git and then install it following the steps in de docs. (https://laravel.com/docs/7.x/installation)

In your public/.htaccess file replaces the code given below..make sure that you uploaded your project in the root public_html folder.
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ ^$1 [N]
RewriteCond %{REQUEST_URI} (\.\w+$) [NC]
RewriteRule ^(.*)$ public/$1
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ server.php

Related

How to deploy laravel project on the godaddy hosting server (Plesk Onyx CPanel)

I currently new for deploying a project files to the godaddy hosting sites, so before i post this, I read some related question created already here in stackoverflow, I just confuse because there cpanel has public_html folder and my panel has no public html. so right now I experience
HTTP Error 404. The requested resource is not found.
The things only I do is to move the laravel files in the httpdocs.
My CPanel : Plesk Onyx 17.8.11
For the folder structure:
So Inside of httpdocs are all of my laravel files
Website shows like this..
Hope someone help me for this problem.
I think the reason your folder structure is like this is that your GoDaddy Hosting is a Windows Shared Hosting.
What to basically need to do is create a folder for your app on the home directory and then on the httpdocs, put the public folder from your app.
GoDaddy Plesk Folder List:
cgi-bin
error_docs
httpdocs
logs
<your laravel app folder minus the "Public" folder>
Inside the httpdocs folder:
App_Data
.user.ini
Default.aspx
web.config
<+ contents of your Public folder>
Once this is complete, you will need to edit a few files:
From Public Folder: index.php
Find:
require __DIR__.'/../vendor/autoload.php';
Change to:
require __DIR__.'/../<laravel app folder>/vendor/autoload.php';
Next Find:
$app = require_once __DIR__.'/../bootstrap/app.php';
Change to:
$app = require_once __DIR__.'/../<laravel app folder>/bootstrap/app.php';
After this, make sure the .env file is properly configured and all the tables from your local database is mirrored in you server database.
Also, if you have open_dir problems, you can disable it on the PHP Settings from Plesk but I am not sure what are the issues this will cause in the long run.
Well, there are two ways...
1st in cpanel search for "Installatron Applications Installer" or somekind of app installer :)
In installatron app installer screen on right top find "Applications Browser" from there search for Laravel and install it by choosing your domain(If you have more than one domain)
After installation copy and replace your local files with the remote files.
2nd second way, .zip your local laravel folder. And upload your files to your server, and then extract the files.
NOW FROM HERE IT'S THE SAME FOR BOTH WAYS->
"public" directory in laravel project will be in the root of your remote server(root folder for your domain, If you have addon domains for ex. public_html/example.com then "public" folder should be in that example.com)
make another folder and put all other laravel files and folders in that folder.
open and edit /public/index.php
change these two lines->
require DIR.'/../vendor/autoload.php';
app = require_once DIR.'/../bootstrap/app.php';
to reflect the file structure in your server.
For ex. if "public" folder is in the same directory with "vendor" and "bootstrap" remove /..
Edit your .env file pay attention to database credentials.
PS: do not change database host address it should remain same.
AND FINALLY the .htaccess files
you need to have two .htaccess files
one in "public" directory content like this->
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Send Requests To Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
Second .htaccess file will be in the root folder of the domain with content->
RewriteEngine on
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]
ONE PROBLEM YOU MIGHT COME ACCROSS if php version problem stating that composer dependencies version is php7.xx bla bla
If this happens go to cpanel again and search for "Select PHP Version"
From there change your current PHP version to suit with the PHP version you get in the error. And you need to click "Set as current"!
That's it :)

htaccess in laravel application

I've setup a laravel application on a shared server, the direct url works fine but when I access the other pages I receive the error message: The requested URL /contact was not found on this server.
but if I then type "http://unit13productions.co.uk/index.php/contact" this shows the correct page.
I'm not sure if this is the htaccess file or the index.php file?
My file directories on the server are as follow:
unit13 folder:
public_html folder:
my file directories inside my index.php is:
autoloader:
require __DIR__.'/../unit13/vendor/autoload.php';
turn the lights on:
$app = require_once __DIR__.'/../unit13/bootstrap/app.php';
contents of my htaccess file:
AddType x-httpd-php71 .php
This sounds like it could be an issue with your .htaccess file more than anything else. Try renaming your .htaccess file to .htaccess1 just so it isn't used and then create a new one and add the following to it
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
The details on how to configure your application for either Apache or Nginx is listed on the laravel documentation which I've listed below.
https://laravel.com/docs/5.7/installation#pretty-urls
Hope this helps!

Deploying Yii2 into shared hosting, Directly to basic template folder

I have read the doc from Yii2 official website to deploy yii2 into share host from this : http://www.yiiframework.com/doc-2.0/guide-tutorial-shared-hosting.html, and about discussion on this : stackoverflow.
So, I decide to use sftp to upload the yii2 folder.
And this is the list of directory in my share host.
access-logs
etc
logs
public_ftp
public_html
ssl
www (this is a link folder to public_html)
You know, because the share host can hold on domains untill 5 domains,
is it possible to upload yii2basic folder into public_html folder ?
So the result like this :
access-logs
etc
logs
public_ftp
public_html
-basic
- bunch of yii folders here
ssl
www (this is a link folder to public_html)
Because now, if I want to access my web, I have to write like this : mydomain.com/basic/public_html/index.php
I need like this :
mydomain.com/index.php
Please guide me.
Yes you can upload it inside the public_html folder but the only problem that I have been facing nowadays with yii2 basic app is the pretty URLs, you can clone or upload entire contents inside the public_html folder, what I did is as follows only the difference is I have a web folder instead of www
directory structure
public_html
assets
commands
config
controller
mail
migrations
models
runtime
tests
vendor
views
web
public_html/.htaccess
<IfModule mod_rewrite.c>
Options +SymLinksIfOwnerMatch
RewriteEngine On
</IfModule>
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_URI} ^/.*
RewriteRule ^(.*)$ web/$1 [L]
RewriteCond %{REQUEST_URI} !^/web/
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ web/index.php
</IfModule>
public_html/web/.htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
this works but in the URL it shows /web/index.php?r=site/index and as soon as I turn the prettyUrl on in the config file urlManager it would only show default index page and every link I try to open ends up on the home page view although the URL in the address bar is correct and urlManager parses the rules correctly, this is as far as I have gone.
If you don't mind bunch of Yii 2 folders in your root folder just place all basic template folders (except web) and files in the root folder. Then place the content of web folder in the public_html folder.
It should be something like:
access-logs
assets
commands
config
controllers
etc
logs
mail
models
public_ftp
public_html
// here should be the content of web folder of Yii 2 basic app
runtime
ssl
test
views
widgets

Remove public from URL Laravel 5.3

I'm using Laravel 5.3 is there a easy way to remove public from URL? is it necessary to add a htacces. when I change the htacces it is getting a n error like below,
copy both htaccess and index.php file from public folder to root directory and then change root index.php code as below
require __DIR__.'/bootstrap/autoload.php';
$app = require_once __DIR__.'/bootstrap/app.php';
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.
https://laravel.com/docs/5.3/installation#configuration
Do not edit .htaccess or Laravel related files, just point web server to a public directory instead of Laravel project directory:
DocumentRoot "/path_to_laravel_project/public"
<Directory "/path_to_laravel_project/public">
Then restart Apache.
Add a .htaccess file in root which contains -
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
Yes it is very simple no need to add extra htaccess,
Create a new folder in your laravel project folder.
Name it whatever you like. Example :- source
Move all the files from root into 'source' folder except 'public' folder.
4 . Move all the files from public folder to root.
5 . Change the autoload.php path
6 . Thats all. remove public folder if necessary.
In the root directory create a .htaccess file and put the following in
it
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
Even above solution(s) work(s) for you,but I would recommend not to do this.
Here is the reason why.
-You should be pointing your Apache host root to the $LARAVEL_PATH/public directory instead of $LARAVEL_PATH.
-you have to configure apache for user not access .env, git files , config folder's file
public directory is there for reason to make project a bit more secure. Solve the actual problem and don't try to break this simple but nice security addition.
Copy all files and folder from public folder to root folder and edit your .htaccess file as
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
And dont forget to change the paths in index.php file.
For details refere following links
Installing Laravel in a subfolder
https://ellislab.com/forums/archive/viewthread/151342/#734511

Apache rewrite for Laravel /public

I am sorry about this, but my htdocs root is wrong and I can't change that. So I have to make it work in the /public folder.
I use the normal Laravel .htaccess file with the following rewrite:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
If I open http://kemtime2_neu.pr.domain.de/public I get redirected to http://kemtime2_neu.pr.domain.de/public/http://kemtime2_neu.pr.domain.de/public/login
How can I fix this?
I would love to make it work from http://kemtime2_neu.pr.domain.de/ but getting it to work with http://kemtime2_neu.pr.domain.de/public/ would be fine.
I use the 3 solution of this post and works fine:
http://web.archive.org/web/20130320184846/http://forums.laravel.io/viewtopic.php?id=1258
Solution 1 - Alternate installation path with symlink.
This is the preferred solution and in general an all-around good idea. It's possible to install your application to a folder unrelated to public_html/ and then symlink the public folder to the public_html/ path.
For example:
Install your application to /home/applications/mysite.com
Imagine that your DocumentRoot points to /var/www/vhosts/mysite.com/httpdocs
Remove the httpdocs folder from the mysite.com vhosts folder then connect the two with a symlink: ln -s /home/applications/mysite.com/public /var/www/vhosts/mysite.com/httpdocs
Solution 2 - .htaccess with mod_rewrite
This solution enables you to drop Laravel into your public folder then use a .htaccess file to redirect requests to the public folder. This solution places your application and core system code into a publicly accessible folder. This is not something that we encourage you to do with any PHP framework.
Step 1. Place Laravel in your document root folder.
Step 2. Place the following .htaccess file in your document root folder.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
Step 3. Make sure that you manually set your 'url' configuration in application/config/application.php otherwise Laravel will generate incorrect URLs. Make sure that each of your environments have the correct application.url configuration. For more information on environment-specific configurations see: http://laravel.com/docs/install#environments
Solution 3 - merge the public folder into the installation root
This solution places your application and core system code into a publicly accessible folder. This is not something that we encourage you to do with any PHP framework.
Copy the contents of the public/ folder into your Laravel installation folder then change this line in your index.php file from:
require '../paths.php';
to
require 'paths.php';
Keep in mind that any bundles, libraries, or other types of third-party code may not be designed to be publicly accessible.
Note: It's also important to note that your bundles/ and public/bundles/ directories will now conflict. When using this approach you may want to not use artisan's bundle:publish task without knowing exactly what your bundles want to publish.
I solved it partly. If I have a .htaccess in the root instead of /public with
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /public/index.php/$1 [L]
I can open http://kemtime2_neu.pr.domain.de/login but the images and css is still wrong. I need to check first if the files exist in /public. I think this is a new question.

Categories