php - Laravel with nginx returns a internal server error
Get the solution ↓↓↓On my localhost server, usingphp artisan serve
the server runs perfectly, but when i uploaded to my vps that's not work
2020/08/14 01:54:45 [error] 9333#0: *5 FastCGI sent in stderr: "PHP message: PHP Fatal error: Uncaught Error: Call to undefined method ComposerAutoloaderInit643cdf30505619f27cedca17fc9059c6::getLoader() in /home/sites/storm/api.stormc.net/vendor/autoload.php:7
Stack trace:
#0 /home/sites/storm/api.stormc.net/public/index.php(24): require()
#1 {main}
thrown in /home/sites/storm/api.stormc.net/vendor/autoload.php on line 7" while reading response header from upstream, client: 172.69.3.155, server: _, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/var/run/php-fpm/php-fpm.sock:", host: "api.stormc.net"
Nginx configuration:
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /home/sites/storm/api.stormc.net/public;
index index.php;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
}
Answer
Solution:
set up your Nginx, by following these steps:
we need to give the webserver user write access to the storage and cache folders, where Laravel stores application-generated files:
$ sudo chown -R www-data.www-data /home/sites/storm/api.stormc.net/storage $ sudo chown -R www-data.www-data /home/sites/storm/api.stormc.net/bootstrap/cache
we need to configure Nginx to serve the content. To do this, we’ll create a new virtual host configuration file at
/etc/nginx/sites-available
:$sudo nano /etc/nginx/sites-available/name-of-project
The following configuration file contains the recommended settings for Laravel applications on Nginx:
*/etc/nginx/sites-available/name-of-project*
server {
listen 80;
server_name server_domain_or_IP;
root /home/sites/storm/api.stormc.net/public;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
index index.html index.htm index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;//check with your installed version on server
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
To activate the new virtual host configuration file, create a symbolic link to name-of-project in
sites-enabled
:$sudo ln -s /etc/nginx/sites-available/name-of-project /etc/nginx/sites-enabled/
To confirm that the configuration doesn’t contain any syntax errors, you can use:
$sudo nginx -t
You should see output like this:
$ nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
$ nginx: configuration file /etc/nginx/nginx.conf test is successful
To apply the changes, reload Nginx with:
$sudo systemctl reload nginx
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: 403 this action is unauthorized.
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.