Deployment Of Laravel 5.6 project to shared hosting - php

I have a major problem during the deployment of my laravel 5.6 project on shared hosting, It seems my blades are not going to work. I have copied all of my project files to a new folder in my root directory namely "muxproject" and then I coppied all of the files in public folder of laravel project to public_html, and i have changed the index.php file as follows
<?php
define('LARAVEL_START', microtime(true));
require __DIR__.'/../muxproject/vendor/autoload.php'; //changed
$app = require_once __DIR__.'/../muxproject/bootstrap/app.php'; //changed
$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);
$response = $kernel->handle(
$request = Illuminate\Http\Request::capture()
);
$response->send();
$kernel->terminate($request, $response);
When I navigate to the link http://example.com/ A login page according to my logic in web.php appear and ask for the login after login successfully the user is redirected to the link http://example.com/home no issue still here but when I navigate to http://example.com/register nothing load except my header, footer, and sidebar my registration page does not appear. the same is the case with http://example.com/branch and http://example.com/userbranch
following is the code for my web.php file
<?php
Route::middleware(['auth'])->group(function (){
Route::get('/home', 'HomeController#index')->name('home');
Route::get('/branch','Branch\BranchController#index')->name('index');
Route::get('/userbranch','Branch\UserBranchController#index')->name('index');
Route::get('/course', 'Course\CourseMasterController#index')->name('index');
Route::get('/coursedetail','Course\CourseDetailController#index')->name('idex');
Route::get('/studentenquiry','Enquiry\StudentEnquiryController#index')->name('index');
});
Route::get('/', function () {
if(\Illuminate\Support\Facades\Auth::check())
return view('home');
return view('welcome');
});
Auth::routes();
Route::get('delete/{id}','Auth\RegisterController#destroy');
Route::patch('update','Auth\RegisterController#update')->name('update');
Route::patch('/update/branch','Branch\BranchController#update')->name('update');
Route::post('/storebranch','Branch\BranchController#store')->name('store');
Route::post('/storebranchuser', 'Branch\UserBranchController#store')->name('store');
Route::get('/delete/branch/{id}','Branch\BranchController#destroy');
Route::get('/userbranch/delete/{id}', 'Branch\UserBranchController#destroy');
Route::patch('/update/userbranch','Branch\UserBranchController#update')->name('update');
Route::post('/storecourse','Course\CourseMasterController#store')->name('store');
Route::get('delete/course/{id}','Course\CourseMasterController#destroy');
Route::patch('update/course', 'Course\CourseMasterController#update')->name('update');
Following is the code for RegisterUsersController to show the registration form
public function showRegistrationForm()
{
$userData = \App\User::all();
return view('auth.register',compact('userData'));
}
#section('content')
//
#endsection
the content between here in my blades register.blade.php, branch.blade.php and
userbranch.blade.php is not working and I am having just empty page except header, footer and sidebar just on shared hosting Each and everything works fine on local
Below is the code for .htaccess file
<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]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
Please help me in this regard, I have gone through a lot of tutorials and I just figured out that for some reason the code in between #section('content') is not loading;

Why you have copied files and pasted in shared hosting and changed default files of laravel?
I think you should read this answer and understand and then deploy your project
You can do it in 2 ways
First way (recommended but time taking to learn):
1-You should learn how to use git (version control system). when you learn about it then you will be able to save your laravel project on a git server. while learning git you will read about clone it will let you download your project from git to any server, any computer. It can be difficult to understand at first but believe me it is very useful and you should use it. visit github.com , gitlab.com or bitbucket.com.
2- when you have learnt how to use git then save your code to git. Then use git bash (for windows) or use terminal for mac/linux these are command line tools. Using it you can connect to your server where you want host your site. login using ssh to shared hosting server and go to public_html and clone your laravel prpject there. After cloning,go to project direcrtory create .env file and change it according to your settings, then run composer install (it will install all packages that your project requires). Then set root directory of your laravel project to your domain using cpanel and hit domain. You should be able to see your laravel working
Second way:(NOT recommended but quick)
Copy project folder except vendor folder to shared hosting server
open your pc terminal, connect to server, go to laravel project directory, install laravel.
set domain to your laravel project root directory and hit domain

Related

Laravel - route 404 not found - how fix it?

I am creating a mobile application(flutter) that works with api on Laravel. The api should work but there are problems. I'm trying to figure it out, although I don't have previous experience with Laravel (I have experience with PHP). so on server Cpanel. when i try to call the api i get an error
404 Not Found
body:
The requested URL was not found on this server.
Additionally, a 404 Not Found
error was encountered while trying to use an ErrorDocument to handle the request
This is a fairly common problem, the points I checked are the following points.
the api is in the public_html folder, some folders inside:
app
routes
bootstrap
checked file public_html/.htaccess
# php -- BEGIN cPanel-generated handler, do not edit
# Set the “ea-php74” package as the default “PHP” programming language.
<IfModule mime_module>
AddHandler application/x-httpd-ea-php74 .php .php7 .phtml
</IfModule>
# php -- END cPanel-generated handler, do not edit
I changed it to:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
executed the following commands
php artisan route:clear
php artisan route:cache
after that i execute the command
php artisan route:list
result:
I try the following options for the url:
https://appdomain.com/api/v1/login
https://appdomain.com/api/v1/Check-login
https://appdomain.com/api/v1/user-login
https://appdomain.com/api/login
https://appdomain.com/api/Check-login
https://appdomain.com/api/user-login
https://appdomain.com/login
https://appdomain.com/Check-login
https://appdomain.com/user-login
but each of these urls return a 404 error.
Laravel - version 5.5.50
part of api.php
Route::group(['prefix' => 'v1'], function () {
Route::post('/user-login',['as' => 'login','uses' =>
'api\v1\UserController#user_login']);
});
7. when I added file public_htm/index.php - any request to api returns that file
Which url is correct?
What else can I check?
How can this be fixed?
Any advice - I will be very grateful.
Method 1:
Change Document Root to public_html/public and upload the source code to public_html.
Method 2:
Upload the source code except public folder to the parent folder of public_html folder. And upload the files in public folder to public_html folder.
Add the following code to public_html/index.php:
$app->bind('path.public', function() {
return __DIR__;
});
Place .htaccess file into root directory of your project . public_html/.htaccess
The first thing you should do is to verify if your request actually hits your Laravel Application. There are number of ways you can do this, like adding additional testing routes in web.php or api.php
Addtionally, you may install clockwork and capture all the request, so you can also easily see if your request actually hits the laravel app.
If it hits your Laravel application, turn on the debug mode in your env file and put the application in development mode, then try checking the laravel logs for any error.
If your request is not hitting the laravel application, then its something related to your server configuration or laravel installation configuration.
Normally, the root folder for laravel application is in laravel-root-directory/public/ which should be the root folder when configuring the domain, but I've seen people where they move the root folder to actual laravel app root and not inside public folder

How to deploy a Laravel-Vue project on shared server (GoDaddy)

This is the first time I am developing a Laravel Vue app. When I use php artisan serve, everything works fine. But when I load it with http://localhost/myProject/public, assets are not loaded. My images are in img directory inside public folder. I was using blade templating to manage this issue when I use Laravel alone. But now I can't use blade since it is a Vue component. How can I run the project without php artisan serve? My Ultimate aim is to deploy the project in godaddy shared server. Please help me.
Debug your application using 'php artisan serve' while you develop from your local machine.
When you publish it into godaddy,
compress the project folder (say 'my_project') and upload the zip file ('my_project.zip') to 'public_html' directory in godaddy.
unzip the folder.
Now the directory structure will be 'public_html/my_project...'
you will have a 'public' directory inside 'my_project'.
Now create a subdomain which points to the 'public' directory.
That's it. Now load the subdomain.
The assets and api's will work perfectly.
just rungit clone [your_project_address] in the www folder and then run 'php artisan migrate', of course, you should have configured well.
#ThasheelApps this is your solution to access api's.
Create a .htaccess file in root folder and paste the following code there:
<IfModule mod_rewrite.c>
Options +FollowSymLinks -Indexes
RewriteEngine On
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
This issue occurs many times due index.php in url. if you try your api endpoint including index.php, it will work perfectly. to prevent this issue we need to follow above soution htaccess
Follow these steps:
zip your project after npm run dev and php artisan serve without
any error.
Go to your shared hosting location and outside fo public_html
make a folder name say myproject. Inside this folder extract all
your project files.
cut your public folder from project folder on root, go to your
subdomain folder under public_html and paste.
go to index.php inside public folder and
require DIR.'/../vendor/autoload.php'; and
$app = require_once DIR.'/../bootstrap/app.php'; pointing to your project location on root.
That is all. Enjoy, if you have properly created and attached your DB to your project.

Deployed new Laravel project to live server - inner page links do not work

I have recently deployed a Laravel project to my live web server via FTP (Filezilla). Inside my young1.org web root folder I have the subdomain folder bookings, which displays web content at http://bookings.young1.org. Inside that folder I have the folder, 'laravel' that contains my entire laravel application, and inside that folder there is a 'public' directory.
I have imported my local database to one of the database accounts on the live web server via phpmyadmin, and I have switched the 'DB' credentials to point to the new database inside the env file in the laravel project root (changing the following variables: DB_DATABASE, DB_USERNAME, DB_PASSWORD).
When I navigate to http://bookings.young1.org/laravel/public, the home page of my application appears, fine and dandy. However, when I click on any of the internal links (e.g. the login and register) buttons, I just get a series of blank pages, and none of the internal pages appear.
Would anyone be able to take a guess at what the problem might be?
I have tried altering the .htaccess file to look like the below, and changing my 'PATHS' variable inside public/index.php.
Thanks,
Robert
London, UK
// public/.htaccess
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
RewriteBase /
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
// public/index.php
require DIR.'/../laravel/bootstrap/autoload.php';
$app = require_once DIR.'/../laravel/bootstrap/app.php';
do you have SSH on the server? if yes did u install laravel as you're supposed to, also could you include the .env file contents, you can mask out the DB username and pass and the key you had to generate
never mind my mistake, i clicked on a link and got a SQL connection error (refused)
i still need to know if you installed laravel via SSH or that you just made the public folder the root, because if that's the case laravel cant help you with that (you need a VPS and not a webhost that only supports FTP as far as i know)
and to be sure
BE CAREFULL!!!
you have SQL connection errors that show credentials
Notice that your URLs work fine if you use index.php in them, i.e.:
http://bookings.young1.org/laravel/public/index.php/register
To allow URLs without index.php the mod_rewrite module on your Apache web server must be enabled.
First try to add this line in your .htaccess file of Laravel above RewriteEngine On:
Options +FollowSymLinks
This directive is needed to enable mod_rewrite in .htaccess context.
If it doesn't work after this, then you can check if module is enabled on your web server, the easiest maybe is to paste this in the beginning of index.php in public folder:
phpinfo();
Then open any page and search for mod_rewrite on the page, and see if you can find it under Loaded Modules. If not, you have to enable it.
To do that, if you can access through SSH you can do:
sudo a2enmod rewrite
sudo service apache2 restart
For more help on enabling mod_rewrite on Apache web server, check this answer.

Have Apache handle requests if file exists, otherwise route it through Symfony front controller

I have a very legacy php application, that I'm trying to update to Symfony.
The application currently has a structure similar to this:
/www <-current web root
php_page1.php
php_page2.php
php_pagen.php
/dashboard
dashboard_page1.php
dashboard_page2.php
dashboard_pagen.php
The pages have helper files full of functions and the like, and there's also a few folders full of assets, but that's not really important. So to access a given page you'd point your browser to domain.com/php_page1.php or domain.com/dashboard/dashboard_page1.php... there's no front controller. All pages are accessed directly.
All in all, there's too much to refactor at once, so I want to do it in peices, one page at a time.
What I want to do is install symfony on top of what I have now, so the symfony structure will co-exist with what's currently here:
/www <-legacy
php_page1.php <-legacy
php_page2.php <-legacy
php_pagen.php <-legacy
/app <-symfony
/dashboard <-legacy
dashboard_page1.php <-legacy
dashboard_page2.php <-legacy
dashboard_pagen.php <-legacy
/src <-symfony
/web <-symfony (new web root)
.htaccess <-symfony (points to app.php)
app.php <-symfony (front controller)
I've successfully installed Symfony and migrated a sample legacy page over (Please note the web root moved). This works great.
The trouble is, ultimately, I need Apache to serve the legacy pages if they exist, and go through front controller (app.php) if they do not.
Here's what I've tried.
I've tried various .htaccess rules to try and make Apache serve files in /www and /www/dashboard. These fail, with Symfony telling me there's no route to match to.
I've also tried Symfony's $this->redirect('..\www\php_page1.php');. This fails with Symfony telling me "Looks like you tried to load a template outside of configured directories."
Finally I tried to add /dashboard as a route:
/**
* #Route("/cvdashboard/{file}")
*/
public function cvdashboardAction($file) {
return $this->render("#dashboard/$file");
}
And added a twig path:
twig:
...
paths:
'%%kernel.root_dir%/../dashboard':dashboard
This yealds three exceptions:
FileLoaderLoadException in FileLoader.php line 118:
InvalidArgumentException in YamlFileLoader.php line 371:
ParseException in Inline.php line 108:
Question
So my question is:
How can I configure Symfony / Apache such that if a url that currently exists is called (i.e. domain.com/php_page1.php) it will serve that page either through Symfony or Apache directly and if it does not exist, have Symfony's front controller handle the request?
EDIT
Here's the .htaccess I currently have:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . app_dev.php [L]
This works for Symfony and routes anything I have a defined route for. The problem is that it's in /www/web (new web root), which is where Symfony wants the web root to be. It won't access anything in /www or /www/dashboard, which is outside the web root.
I'm trying to use .htaccess to either cause Apache to grab files from one level down from the web root, or cause Symfony to serve those pages as the generic php scripts they are.
You can use mod_rewrite for that:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d # not an existing dir
RewriteCond %{REQUEST_FILENAME} !-f # not an existing file
# redirect to the symfony app.php file
RewriteRule ^(.*)$ www/app.php [L]

How to fix '/' in caffinated moudule in laravel

I make a fresh setup of laravel on localhost (/var/www/html/) with command composer create-project laravel/laravel moduleTesting --prefer-dist.
Then I move all the files inside folder to (/var/www/moduleTesting/setup), then move all the files from public folder(/var/www/moduleTesting/setup/public) to moduleTesting folder(/var/www/html/moduleTesting).
I changed bootstrap file path in index.php file placed in moduleexample.dev folder(/var/www/html/moduleTesting/).
require DIR.'/setup/bootstrap/autoload.php';
$app = require_once DIR.'/../../laravel_setup/bootstrap/app.php';
I also set the permission of folder /var/www/moduleTesting/setup/bootstrap/cache and /var/www/moduleTesting/setup/storage
Then I run command composer dump-autoload in terminal at (/var/www/moduleTesting/).
Then I try to run URL in the browser, and I see the welcome page of laravel app.
Then I install module package caffeinated/modules
Begin by installing the package through Composer.
composer require caffeinated/modules
Once this operation is complete, simply add both the service provider and facade classes to project /var/www/html/moduleTesting/setup/config/app.php file:
Service Provider
Caffeinated\Modules\ModulesServiceProvider::class,
Facade
'Module' => Caffeinated\Modules\Facades\Module::class,
After successful installation, I create a new module by command
make:module Admin and follow easy steps steps and it created successfully and run by hitting URL http://localhost/moduleTesting/admin'.
Issue
Now the problem is when I run URL.'http://localhost/moduleTesting/admin'
it runs successfully but when i run 'http://localhost/moduleTesting/admin'(add '/' only at the end of the same url) it does now work and redirect me to url 'http://localhost/admin'
Does anybody know, please help me how to solve this issue, On the same node if upload the setup on the server in a inner folder and run the same url it also redirect me.
This is probably because .htaccess redirects trailing slashes to an URL without a traling slash.
In this section in your .htaccess file, Apache redirects everything with a trailing slash - if not a folder - to its origin. This causes trouble, because the public of the project is located at a directory.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
Replace the second rule with:
RewriteRule ^(.*)/$ /moduleTesting/$1 [L,R=301]
Note: it is better not using a directory as your public destination because these kinds of problems occur. You should be better of creating a fake local domain, like: module-test.dev or something.

Categories