CORS issue with webroute in Laravel 7 - laravel

I try my laravel project in apache server
I clone my project
sudo -u www-data git clone ******* laravel
Lunch composer
sudo -u www-data composer install --no-dev --optimize-autoloader
Lunch npm and webpack
sudo -u www-data npm install
sudo -u www-data npm run production
Create .env
sudo -u www-data cp .env.example .env
Generate key
sudo -u www-data php artisan key:generate
sudo -u www-data php artisan config:cache
After clone my project and installed all packages, i configure .htaccess
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
RewriteBase /laravel/
# 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]
</IfModule>
and 000-default.conf
<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerName www.example.com
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html/laravel/public
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
</VirtualHost>
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
Alias /laravel /var/www/html/laravel/public/
<Directory "/var/www/html/laravel/public">
AllowOverride All
allow from all
Options +Indexes
</Directory>
But i have a 500 error when i try to login.
Does anyone have an explanation to this ?
[UPDATE 1]
I forgot to do this :
sudo -u www-data php artisan passport:install
But now all my web routes have this error
[UPDATE 2]
All my web routes have 127.0.0.1:8000. But i'm in production, i shouldn't have this, no ?
[UPDATE 3]
I has installed fruitcake/laravel-cors
sudo composer remove barryvdh/laravel-cors fruitcake/laravel-cors
sudo -u www-data composer require fruitcake/laravel-cors
I allow CORS for all your routes in app/Http/Kernel.php
protected $middleware = [
\Fruitcake\Cors\HandleCors::class,
// ...
]
I reload my Laravel configurations and allow my changes to reflect
sudo -u www-data php artisan config:cache
For mapApiRoutes it's work, but not for mapWebRoutes. How can i fix for mapWebRoutes ?

This:
allow from all
... is Apache 2.2 syntax. In Apache 2.4, use:
Require all granted
Info in the docs.

No need of both Allow and Require i guess, but i am using similar config in all projects and it's working
<Directory /var/www/html/laravel/public>
AllowOverride All
Order Allow,Deny
Allow from all
Require all granted

i was so stupid ...
In my component, l had this
created() {
axios.get('http://127.0.0.1:8000/tasksList')
.then(response => this.tasks = response.data)
.catch(error => console.log(error));
},
BUT, in my .env, i have
APP_URL=http://*****.*****.***:8080
I corrected my component
created() {
axios.get('/tasksList')
.then(response => this.tasks = response.data)
.catch(error => console.log(error));
},
NOW it's work.

Related

Accidental chmod 777 on laravel files made the custom app throw forbidden error in shopify store admin

The title says everything. It's a laravel setup running on apache. I have full root terminal access. Managed to wreck the permissions and now the app just shows the 'Forbidden' message for the /authenticate/token route.
Tried all different kinds of permission arrangements for directories and files. Confirmed ownership by apache and tried ownership by sudo user on selective files too. Disabled selinux as well
Don't remember how but something gave me a hint that the problem might have something to do with the session files in /storage/frameworks/sessions.
Weird bit, sometimes the app magically loads, but most times it doesn't.
Help?
EDIT: Made a mistake while checking details. The route for which the Forbidden message comes is "https://[domain]/?host=[...]&shop=[...]&token=[...]". The route I mentioned above is actually the referring URL.
EDIT:
.htaccess from /public folder
<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]
</IfModule>
Error Screen in Shopify Admin (Dev Store)
Laravel Error Log: Clean
Apache Error Log: "[evasive20:error] client denied by server configuration: /var/www/[laravel-folder]/public/, referrer [same path as the previous edit]"
EDIT: Apache server/vhost config:-
<VirtualHost *:80> //don't worry about this, after routing through ngrok's tunnel it comes out as https
ServerAdmin [email]
ServerName [url]
DocumentRoot /var/www/contact-us/public
<Directory /var/www/contact-us/public>
Options Indexes FollowSymlinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
firstly you try this:
php artisan storage:link
And make sure that you have all of the folders in your
storage/framework:
sessions
views
cache

404 error when calling api in laravel 6.4 with apache2 virtualhost

I have Laravel 6.4 installed on ubuntu 18.04 with php 7.2 and apache2. I have built a case study to develop an api and when I execute the php artisan serve command and use postman to do the query everything works fine. But if I create a virtualhost, I can see the website without problems, however, when executing a query through the postman I get the 404 error.
The .htaccess file is the one that brings by default laravel:
<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>
and my virtualhost is the following:
<VirtualHost test-api.local:80>
ServerName test-api.local
DocumentRoot /var/www/html/restlav/public
DirectoryIndex index.php
ErrorLog ${APACHE_LOG_DIR}/test-api.local-error.log
CustomLog ${APACHE_LOG_DIR}/test-api.local-access.log combined
<Directory "/var/www/html/restlav">
Options +FollowSymLinks
RewriteEngine On
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
I have already tried to make the changes that are suggested in the official documentation and nothing. The rewrite module is enabled. If anyone could help me I appreciate it in advance.
In the <Directory> section you need to set AllowOverride to All:
<VirtualHost test-api.local:80>
ServerName test-api.local
DocumentRoot /var/www/html/restlav/public
DirectoryIndex index.php
ErrorLog ${APACHE_LOG_DIR}/test-api.local-error.log
CustomLog ${APACHE_LOG_DIR}/test-api.local-access.log combined
<Directory "/var/www/html/restlav">
Options +FollowSymLinks
RewriteEngine On
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
Also make sure to enable the rewrite module in the apache server:
$ sudo a2enmod rewrite
And don't forget to finally restart the apache server to make the changes come into effect:
$ sudo systemctl restart apache2

laravel 5.6 and apache gives 404 for urls

I have apache2 server on Ubuntu 16.04LTS and I have Laravel 5.6 project called book_donation in /var/www/html now when I visit localhost the Laravel welcome screens appear no problem but whenever I click on any url it gives:
Not Found
The requested URL /book_donation/public/login was not found on this
server. Apache/2.4.18 (Ubuntu) Server at 127.0.0.1 Port 80
and here is .htaccess:
<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>
and here is my /etc/apache2/site-available/000-default.conf:
<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerName www.example.com
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html
<Directory "/var/www/html">
AllowOverride all
Require all granted
</Directory>
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
</VirtualHost>
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
My php version is 7.1 and apache version is 2.4.18, how to solve this?
As stated in the docs, you should configure your web server's document / web root to be the public directory.
Create a new file /etc/apache2/site-available/book_donation.test.conf:
<VirtualHost *:80>
ServerAdmin webmaster#localhost
ServerName book_donation.test
ServerAlias www.book_donation.test
DocumentRoot /var/www/html/book_donation/public
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Edit your /etc/hosts file and add the following:
127.0.1.1 book_donation.test
Then run:
sudo a2ensite book_donation.test.conf
sudo service apache2 restart
Then you should be able to visit book_donation.test and see your site.
I was facing same problem any new route was not working and gave 404 error. Also check php artisan route:list where new route details showing properly but still it was not working. I also tried following command to make it work
php artisan view:clear
php artisan config:clear
php artisan cache:clear
Here are my project details
My DocumentRoot is /var/www/html/project/public. when add new route in web.php, it was nor working and gave 404 error but root route was working fine. Means any new route was working. so I did following changes and it start working.
changed in /etc/apache2/sites-available/project.conf
<Directory /var/www/html/project/public>
**Allowoverride All**
and enable url rewrite by running sudo a2enmod rewrite
restart apache server by running sudo systemctl restart apache2

Nothing accessible in my Laravel project except the home page

been searching for the past 3 days about configuring laravel 5.4 on my linux mint 18.2 system running apache 2.4 . when apache server starts and i manually type in localhost in the browser,it does show up the default home page of laravel , but when i try to access it through live preview in brackets (live preview base url : http://localhost as my laravel project pro lies in /var/www/html/)but it throws internal server error corresponding to the errors in my apache configuration.So here i put forward my .htaccess file , 000-default.conf file in sites-enabled directory of apache2 and apache2.conf files :
One important thing : i got my project folder pro under /var/www/html/
1. 000-default.conf(sites-enabled)
<VirtualHost *:80>
ServerName localhost
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html/pro/public
<Directory /var/www/html/pro/public>
Options Indexes +FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
2. apache2.conf
Mutex file:${APACHE_LOCK_DIR} default
PidFile ${APACHE_PID_FILE}
Timeout 300
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5
User ${APACHE_RUN_USER}
Group ${APACHE_RUN_GROUP}
HostnameLookups Off
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
IncludeOptional mods-enabled/*.load
IncludeOptional mods-enabled/*.conf
Include ports.conf
<Directory />
Options FollowSymLinks
AllowOverride None
Require all denied
</Directory>
<Directory /usr/share>
AllowOverride None
Require all granted
</Directory>
<Directory /var/www/html/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
AccessFileName .htaccess
<FilesMatch "^\.ht">
Require all denied
</FilesMatch>
IncludeOptional conf-enabled/*.conf
IncludeOptional sites-enabled/*.conf
Include /etc/phpmyadmin/apache.conf
3 .htaccess (/var/www/html/pro/public/.htaccess)
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
RewriteBase /var/www/html/pro/public/
# 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>
thank you before hand!
Your all routes would be available after index.php, i mean if your route's url is abc, then it will be open as http://localhost/index.php/abc. Now you don't need to change anything in .htacesss file.
Go to your project root and give the read write execute permissions, so that laravel will able to cache the file inside bootstrap/cache directory and able to create your logs file inside storage/logs directory.
sudo chmod -R 777 bootstrap/cache
sudo chmod -R 777 storage/logs
If your project didn't run after that also, then sometimes you need to give this permission also.
sudo chmod -R 777 vendor/ storage/
Run these three commands after that
sudo a2ensite 000-default.conf
sudo a2enmod rewrite
sudo service apache2 restart
your route should be served after localhost.
Hope this will help :)
Try giving read/write permissions to your storage and bootstrap folder in laravel. Also setup virtual hosts if you can. Refer here.

How do I get rid of the need for the "/app.php/" in a new Symfony2 app's production URL?

I've done a 1st install of Symfony2,
php app/console --version
Symfony version 2.0.11 - app/dev/debug
I installed it under an existing DocumentRoot by configuring my apache2 server vhost
<VirtualHost 127.0.0.1:80>
...
DocumentRoot /srv/www/localhost_www
...
<Directory "/srv/www/localhost_www" >
AllowOverride none
Options -ExecCGI +FollowSymLinks +Includes +Indexes +MultiViews
Order Allow,Deny
Allow from All
</Directory>
...
Alias /TESTAPP "/srv/www/TESTAPP/web/"
<Directory "/srv/www/TESTAPP/web/">
Options All
AllowOverride All
Allow from All
</Directory>
Alias /TESTAPP.symfony "/srv/www/TESTAPP/"
<Directory "/srv/www/TESTAPP/">
Options All
AllowOverride All
Allow from All
</Directory>
...
After an OK install, and successfully following "The Book" to build the demo "Hello" app, if I nav in a browser to any/all of,
dev:
http://localhost/TESTAPP.symfony/web/app_dev.php/hello/ME
http://localhost/TESTAPP/app_dev.php/hello/ME
prod:
http://localhost/TESTAPP.symfony/web/app.php/hello/ME
http://localhost/TESTAPP/app.php/hello/ME
I see the expected,
"Hello ME!"
I want to rewrite only the production URLs, hiding "app.php" so that the following set of URLs work in the nav bar of a browser for my Symfony2 app:
dev:
http://localhost/TESTAPP.symfony/web/app_dev.php/hello/ME
http://localhost/TESTAPP/app_dev.php/hello/ME
prod:
!! http://localhost/TESTAPP.symfony/web/hello/ME
!! http://localhost/TESTAPP/hello/ME
Iiuc, the default web/.htaccess
cat web/.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app.php [QSA,L]
</IfModule>
needs to change.
I've dug around in the docs and on the web, tried vdiffferent recommendations for modding the RewriteRule, adding 'RedirectMatch permanent', etc., and I'm not managing to get a simple
http://localhost/TESTAPP/hello/ME
to ever work. I always seem to need to add the "/app.php/".
What are the specific changes I'd need to make in order to get this to work?
Thanks.
#chasen
is mod_rewrite enabled for apache?
yes
if you have your .htaccess file in your web directory
i do
and you host file pointing to the web folder the
it is, as above,
Alias /TESTAPP "/srv/www/TESTAPP/web/"
<Directory "/srv/www/TESTAPP/web/">
app.php should not be needed.
that's my problem. without it, it's not working for me.
Specifically,
https://localhost/TESTAPP/app.php/hello/ME
returns:
"Hello Me!"
but
https://localhost/TESTAPP/hello/ME
returns
Object not found!
The requested URL was not found on this server.
If you entered the URL manually please check your
spelling and try again.
If you think this is a server error, please contact
the webmaster.
Error 404
i know that its a silly question but is mod_rewrite enabled for apache?
if you have your .htaccess file in your web directory and you host file pointing to the web folder the app.php should not be needed.
if you are running ubuntu or a similar distro you can do:
sudo a2enmod rewrite
sudo service apache2 reload
sudo service apache2 restart
That should enable the mod, and reload apache.
EDIT:
That response you are getting back is it in a symfony page? if it is its likely that your prod cache needs to be cleared, from the root directory for the application use the symfony console to clear the cache:
php app/console cache:clear --env="prod"
EDIT:
If neither of those have worked you may try adding a RewriteBase to the htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /TESTAPP
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app.php [QSA,L]
</IfModule>
In my case I need to modify the line containing AllowOverride None to read AllowOverride All. This tells Apache that it's okay to allow .htaccess files to over-ride previous directives. You must reload Apache before this change will have an effect (https://help.ubuntu.com/community/EnablingUseOfApacheHtaccessFiles):
Do this: - go to "/etc/apache2" modify file "apache2.conf" section:
<Directory /var/www/>
Options FollowSymLinks
AllowOverride All
Require all granted
restart server: /etc/apache2# service apache2 restart
it should work. If it does not work, still you have done 1 step right to insure .htaccess is allowed to override. There are many other reasons can be fixed with the above instruction.
Adding the RewriteBase rule with the name of the folder in which the project is held to the .htaccess file does it's job! Thanks!
RewriteBase /project_name

Resources