Laravel deployment on ubuntu server - php

I want to deploy my laravel project on an Ubuntu server but it only shows the welcome page. I tried to login and it keeps telling me that the URL is not found on the server. I already modified my conf file in apache:
Alias /hris/imiforms /var/www/apps/hris/imiforms/public
<Directory /var/www/apps/hris/imiforms>
Option Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
and I my .htaccess file is shown below.
It has rewrite Engine all and I already executed apache restart command. Can you help me and telling me what is wrong?
<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
RewriteCond %{REQUEST_FILENAME} !—d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ $1 [L,R=30l]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>

I see in your apache configuration an alias, but you don't use it.
You need to set your full directory like that, or use the alias defined before instead of full path:
<Directory /var/www/apps/hris/imiforms/public>
...
</Directory>
The /public part is very important because it is the Laravel entry point.

Related

How to issue and customize .htaccess for Laravel?

I have just launched a Laravel 5.3 project on a live server and all is going well except one.
But my issue is, My websites are running on all 3 different URL.
My server is Ubuntu 16.4.
website.com
website.com/public/index.php
website.com/ any word /index.php
My .htaccess is: (This file is in the root folder)
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1%{REQUEST_URI} [R=301,QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
RewriteRule ^(.*)$ public/index.php$1 [L]
</IfModule>
But I just want to see my website with website.com URL only.
Seems like your Laravel app is accesible via an Apache HTTP alias. Follow these steps
Try to navigate to your expected route prepend it with /index.php/, in your case: website.com/index.php/users. If it works (no 404) then you problem is with the Rewrite Module configuration of Apache HTTP, you should follow the next steps.
Edit the file public/.htaccess.
Under the line RewriteEngine On add RewriteBase / if files are on root directory if in folder like laravel then add RewriteBase /laravel/
Try to navigate to an existing route.
i.e
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1%{REQUEST_URI} [R=301,QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
RewriteRule ^(.*)$ public/index.php$1 [L]
</IfModule>
Remove /public from your Laravel
create a virtual host
go to /etc/apache2/sites-available create .conf file or update 000-default.conf
<VirtualHost *:80>
ServerAdmin admin#example.com
ServerName website.com
ServerAlias www.website.com
DocumentRoot /var/www/html/index.html
<Directory /var/www/html/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
RewriteEngine on
RewriteCond %{SERVER_NAME} =website.com [OR]
RewriteCond %{SERVER_NAME} =www.website.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent]
</VirtualHost>
To only match the homepage, i.e. http://domainxyz.com/ you need to match the empty path (as mod_Rewrite removes the leading /.
RewriteCond %{HTTP_HOST} ^www.domainxyz.com [NC]
RewriteRule ^$ /view.php?page=process/ [NC,L]
Try using this in your .htaccess file:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^website.com/([a-zA-Z-]*)/index.php [NC,OR]
RewriteCond %{HTTP_HOST} ^www.website.com/([a-zA-Z-]*)/index.php [NC]
RewriteRule ^(.*)$ http://www.website.com [L,R=302,NC]
Make sure you clear your cache before testing this. You will notice I've used R=302, this is a temporary redirect. If the redirect works and you're happy with it, then switch that to R=301 which is a permanent redirect.
This code worked for me. It will remove extention from your website e.g .php and all. Try it once.
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [NC,L]
1. I understand why you are trying to change the .htaccess but this can be solved alot simpler. Revert the .htaccess file back to the Laravel standard
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
2. Move all of your folders and files apart from public and make sure they are behind your HTML folder.
3. Move all of your public folders and files out of the public folder and into your html folder
4. Tell laravel to look in your html folder instead of public by altering the server.php file
<?php
/**
* Laravel - A PHP Framework For Web Artisans
*
* #package Laravel
* #author Taylor Otwell <taylorotwell#gmail.com>
*/
$uri = urldecode(
parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)
);
// This file allows us to emulate Apache's "mod_rewrite" functionality from the
// built-in PHP web server. This provides a convenient way to test a Laravel
// application without having installed a "real" web server software here.
if ($uri !== '/' && file_exists(__DIR__.'/html'.$uri)) {
return false;
}
require_once __DIR__.'/html/index.php';
I find this solution a lot simpler than messing around with you htaccess. You also ensure all of the Laravel core files cannot be accessed from a url.
Other issue you are having (Internal pages)
This is probably due to your apache not allowing override with Laravels htaccess
Go to the command line of your server and access the apache2.conf file
cd /etc/apache2
Open the apache2.conf file with nano
sudo nano apache2.conf
Now locate the following
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
Change this to
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
Writeout the changes and exit nano.
Restart apache
sudo service apache2 restart
It should now be working :)

Laravel and Apache2 : unable to correctly use redirects

I have an issue with Laravel. On an intranet Apache 2.4.10 Webserver runing with Debian 8.5, named fmbsrv130, I would like to install a Laravel Project named sentinelle.
So Laravel installation is OK, I can access to it with http://fmbsrv130/sentinelle/ (I have the welcome page of Laravel). But if I try to go to http://fmbsrv130/sentinelle/1 (that does not exists), I have 404 from Apache2 that says "The requested URL /home/webadmin/sentinelle/public/index.php was not found on this server." If I try to access http://fmbsrv130/sentinelle/index.php/1 I have the 404 from Laravel : "Sorry, the page you are looking for could not be found.
1/1 NotFoundHttpException in RouteCollection.php line 161: ..."
rewrite_mod is correctly enabled in Apache.
Here is config file of Apache (000-default.conf in sites-available) :
Alias "/sentinelle" "/home/webadmin/sentinelle/public/"
<Directory "/home/webadmin/sentinelle/public/">
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
For testing, if I try to modify .htaccess file from public folder of Laravel with
RewriteEngine On
RewriteRule ^.*$ htaccess_tester.php
(htaccess_tester.php is a file that is used to test content of .htaccessfile (see it on GitHub) and is currently into public folder), I have Apache2 404 error message The requested URL /home/webadmin/sentinelle/public/htaccess_tester.php was not found on this server.
Is it an issue from .htaccess file OR an issue from Apache2 config OR an issue from Laravel config OR a file access right ?
is your mod_rewrite enabled on your apache2 please confirm it should be enabled to .htaccess work and in your .htaccess file add default laravel .htaccess content
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>
also if that doesn't work try to remove your index.php out of public directory and place it in root directory at /sentinell and modify .htaccess to RewriteRule ^ public/index.php [L] that should do the trick
OK so finally I found a solution by doing some modifications : first one is .htaccess file into public folder:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
RewriteBase /sentinelle/
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>
(Adding RewriteBase /sentinelle/)
And the second one was into default.conf Apache file :
Alias /sentinelle /home/webadmin/sentinelle/public/
<Directory "/home/webadmin/sentinelle/public">
AllowOverride All
Order allow,deny
allow from all
Require all granted
</Directory>
It solved the issue described into this topic, I'll open another one because now I have issue with last "/" in address :)

Apache Authoization Header Not found Even After .htaccess modification

I have a laravel application having .htaccess file as below
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
RewriteRule ^ - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>
I am expecting a "Authorization" key in my request headers but its not getting through in my dev server which is running Ubuntu 14.0.4.
Same OS is in my local machine and in local machine it is working perfect.
The only difference between these two environment is in my local machine i have made a VirtualHost where as i cannot make VirtualHost on dev server so i am accessing it via IP Address / projectroot
So Finally i found the reason. It was actually my Apache default configuration file was not allowing my project .htaccess file to overwrite its configurations. That is why the line in .htaccess
RewriteRule ^ - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
has no effect. To allow so I put that this directive in my apache configuration file in my case its /etc/apache2/sites-available/000-default.conf
<Directory /var/www/html/>
Options Indexes FollowSymLinks MultiViews
# AllowOverride None (Old value)
AllowOverride All #(New Value)
Order allow,deny
allow from all
# Uncomment this directive is you want to see apache2's
# default start page (in /apache2-default) when you go to /
#RedirectMatch ^/$ /apache2-default/
</Directory>

which one is better to run laravel?

I have installed laravel in my cpanel and localhost, but i found two way to execute/run laravel.
access the
/public/
use
php artisan serve
if i run in my notebook/localhost i can use php artisan serve and it done well like routing and database, but in my cpanel i cant use the second way, so i must use first way, but it not done well like routing and etc.
So which one is better and how to make the first way run well?
Normally, you would use
php artisan serve
on your development environment only.
On production servers, you will add a <VirtualHost> or an Alias drective in your Apache configuration.
<VirtualHost *:80>
ServerAdmin admin#localhost
DocumentRoot /path/to/your/laravel/public
ServerName your.domain.com
<Directory /path/to/your/laravel/public>
AllowOverride None
Order allow,deny
Allow from All
Options FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</Directory>
</VirtualHost>
With this type of configuration, you can access your Laravel site as:
http://your.domain.com/
When using an Alias, your Apache configuration will be something like this:
Alias /any/path/you/want /path/to/your/laravel/public
<Directory "/path/to/your/laravel/public">
Order allow,deny
Allow from All
Options +FollowSymLinks
RewriteEngine On
RewriteBase /any/path/you/want
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</Directory>
With this configuration, you will access your laravel as: http://your.domain.com/any/path/you/want
On some web hosting providers, you don't have access to the Apache configuration,
in which case, you will have to put your configuration in an .htaccess file inside your public directory.
Laravel has a default .htaccess file inside the public directory that contains this:
<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>
You will have to check if your hosting allows .htaccess

The url requested was not found laravel 4

I'm trying to create an app with laravel 4 but i'm facing a url issue. i have wamp installed on my machine. i set up a new virtual host in my httpd-vhost.conf with this code
<VirtualHost mobile.dev>
DocumentRoot "C:\wamp\www\mobile.dev\public"
ServerName mobile.dev
<Directory "C:\wamp\www\mobile.dev">
Options FollowSymLinks Indexes
AllowOverride All
Order deny,allow
Allow from 127.0.0.1
Deny from all
Require all granted
</Directory>
mobile.dev is a folder and also the domain name in my localhost.
here is my Route.php file
Route::get('/','HomeController#showWelcome');
Route::post('login','LoginController#userLogin');
Route::get('login','LoginController#getUsers');
when i ask for mobile.dev/login it gives me the requested url was not found.
can you help me please solve this issue. but when i ask for mobile.dev/ its working.
Here is my .htaccess file :
<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]
Here is more details. when i remove my htaccess content everything works fine with the default url http://mobile.dev/index.php/login
So the problem is in my htaccess and the rewriting in my virtual host
It might be not Laravel problem, but seems your wamp server haven't enabled mod_rewrite in httpd.conf file yet. Locate the line -
#LoadModule rewrite_module modules/mod_rewrite.so
and remove the comment symbol '#'. Then save the file and restart Apache.
Solution:
1- when you have wamp installed in your machine please go to C:\wamp\bin\apache\Apache*.*.*\conf\httpd.conf and make sure that rewrite_mod is enabled by deleting the hash tag. (instead of using the interface provided by wamp)
2-there are two ways to enable rewriting for laravel in .htaccess file :
<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>
or using this :
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
3- if you're setting a virtual host be sure to add this option in the <virtualhost> tag
<Directory "C:\wamp\www\mobile.dev\public">
Options FollowSymLinks Indexes Multiviews
AllowOverride All
</Directory>
Thanks a lot for your time.
Have you tried
Route::post('login','LoginController#userLogin');
without the () after userLogin?
Your <Directory "C:\wamp\www\mobile.dev"> in config should be <Directory "C:\wamp\www\mobile.dev\public"> to specify the public directory to the root of the htaccess rewrite rules.
I originally thought you were missing the </IfModule> at the end of your .htaccess file but when I posted my .htaccess file it hid the </IfModule> as well so that is a stack overflow issue. Otherwise your htaccess is correct.

Categories