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
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!
I have a legacy project on a server where the actual framework is in a folder above the public_html folder. So the directory structure is like this:
domainname.com
app/
framework/
public_html/
index.php
Now I want to place this on our own server. This is an application created by the hosting company. The root folder is default. So now my directory structure is like this:
default/
app/
framework/
public_html/
index.php
Now he's pointing to the default folder instead of the public_html folder. That's the only difference with the old version. So I've added a .htaccess file to the root folder default. The content of this file is:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /public_html/$1 [QSA,L]
</IfModule>
In my public_html folder I have a .htaccess file like this:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule . index.php [QSA,NS]
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule . /index.php [L,QSA,NS]
So when I go to my website it works. But when I click on a link I get: http://domainname.com/public_html/theclickedpage/.
But I don't want the public_html in my url. How can I fix this?
You may want to search for a way to make a virtual host. I know some of my webhost let me configure the vhost through my admin page. It will let your server consider the url without including the specified folder, but back in the stage it performs needed redirection.
You applied correct rule ,
but the problem is link for the solution of this issue you have to
remove public_html from
your all links and it will automatically redirect to your required
path without public_html.
I was going through all possible sample on internet to solve this. Still it is an headache.
I just want to avoid the 'public' in www.mylaravelsite.com/public/
and make it like www.mylaravelsite.com for the root directory.
Now I do not want to avoid the security concern,So I learned .htaccess would be the best way.
Any solution friends ?
& advance thanks for interacting !
Let's assume you have this folder structure in your server
.cpanel/
public_html/
public_ftp/
..
And the laravel folder structure is
app/
bootstrap/
public/
vendor/
composer.json
artisan
..
You can create a folder name mylaravelsite on your server inline with public_html and public_ftp folder, and copy to it the whole laravel application except the public folder because you will paste all of it contents on the public_html, so you have now:
.cpanel/
public_html/
public_html/packages
public_html/vendor
public_html/index.php
public_html/.htaccess
...
public_ftp/
mylaravelsite/
mylaravelsite/app
mylaravelsite/bootstrap
...
On your public_html/index.php change the following line:
require __DIR__.'/../bootstrap/autoload.php';
$app = require_once __DIR__.'/../bootstrap/start.php';
to
require __DIR__.'/../mylaravelsite/bootstrap/autoload.php';
$app = require_once __DIR__.'/../mylaravelsite/bootstrap/start.php';
and also don't forget to change /mylaravelsite/bootstrap/paths.php public path, you might use it.
'public' => __DIR__.'/../public',
to
'public' => __DIR__.'/../../public_html',
Your site should be running.
Create .htaccess in root folder and write these line
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
For production server it is recommended that you point your domain directly to the public folder
It sounds like the information you are missing is:
Documentroot
This is also called the "web root". Apache specifically calls it the DocumentRoot - Click that link for the official explanation.
Basically what it does is set which directory the server pulls files from. In Laravel, the document root should be the public folder.
Setting the DocumentRoot to public means that going to http://mylaravelsite.com in the browser will infact be "pointing" to the public folder as you want.
In the .htaccess file (actually, more likely in the virtual host configuration), you can set the DocumentRoot for your site:
DocumentRoot /path/to/laravel-app/public
How you set this up depends on your hosting. Each hosting has different ways to setup a website and so we cannot answer that specifically for you - you should consult your web hostings tech support. The key point is that the public directory should be the web root, while everything else should be "behind the web root".
Fair warning tho: some hosting providers do not give you enough access to accomplish that.
Just add .htaccess file on Laravel root if it's not present and add following lines in it, that's it,
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
create .htacess file in the root directory of your laravel project and put this code in.
<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
point your web directory to public/. If you are using apache this will work:
<VirtualHost *:80>
DocumentRoot /var/www.mylaravelsite.com/public/
ServerName www.mylaravelsite.com
</VirtualHost>
Here is the solution, But Must not do it in real projects, it is just for starter to test Laravel and i have tested it with laravel 5.0 and it is ,cut index.php and .htaccess files from public folder and paste it in the root directory and change these two lines as
require __DIR__.'/bootstrap/autoload.php';
$app = require_once __DIR__.'/bootstrap/app.php';
It is Highly Recommended not to use the above method without testing environment, and should use virtual host instead.
All you just need to change .env file in your laravel project files. if your laravel project inside in
Root/domain/public_html/laravel/
Go to .env file and edit
you need to edit app url path which should be
APP_URL=http://www.domainname.com/laravel/public/
Thats it your project will run now. if your laravel files is in public_html, then your app url must be like this
APP_URL=http://www.domainname.com
Thanks
For Lumen
Let's think you have a folder path like: /var/www/project/microservice(lumen src)
/var/www => document root for localhost/
Target you want:
localhost/project/microservice[/foo...] => localhost/project/microservice/public/foo...
I do this by .htaccess (placed at /project folder) like below:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^.*/public/.*
RewriteCond %{REQUEST_URI} ^(/[^/]+/[^/]+/).*
RewriteRule ^[^/]+(.*)$ %1public$1 [L,R=301,P]
On Server(Apache)
Create .htaccess in the root folder and write below line
RewriteEngine on
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]
Lean more on Gist
On Local Development
You can create a virtual host by adding the below line to your apache vhost config file.
Generally, you can find this file in C:\xampp\apache\conf\extra\httpd-vhosts.conf file
<VirtualHost *:80>
DocumentRoot "C:/xampp/htdocs/mysite/public/"
ServerName www.mysite.local
</VirtualHost>
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.