[php] Getting a 500 Internal Server Error on Laravel 5+ Ubuntu 14.04

I have installed Laravel many times on Windows OS but it never occurred this type of problem. This 500 internal server usually occurs when your "mod_rewrite" module is not turned on.

However, on Ubuntu 14.04 this problem is giving me a headache. I have installed rewrite_mod but it's also not working. I've given access to all my folders and files inside i.e.

/var/www/html/laravel_project

Still it doesn't work. Changed .htaccess as well from original to this.

    +FollowSymLinks
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

I have all the necessary extensions needed for Laravel 5+ as well. Is there something left that I didn't do?

This question is related to php apache .htaccess ubuntu mod-rewrite

The answer is


I just ran the following command:

php artisan passport:install

I was using passport to run my application completely based on API's and Vue.js. Laravel worked fine but everytime i tried to login via my API i would get the error. After running the command and updating the client_id and client_secret on my Laravel files then pushed the new updates to the live server, the problem was solved. In my user model i have a script as follows:

public function generateToken($request)
{
    $http = new \GuzzleHttp\Client();
    $response = $http->post(URL::to('/').'/oauth/token', [
        'form_params' => [
            'grant_type' => 'password',
            'client_id' => '6',
            'client_secret' => 'x3yhgWVqF8sSaMev4JI3yvsVxfbgkfRJmqzlpiMQ',
            'username' => $this->email,
            'password' => $request->input('password'),
            'scope' => '',
        ],
    ]);
    //  Lets get an array instead of a stdObject so that we can return without errors
    $response = json_decode($response->getBody(), true);

    return oq_api_notify([
                'auth' => $response,                                        //  API ACCESS TOKEN
                'user' => $this->load(['settings'])->toArray(),
            ], 201);
}

I just updated the client_id and client_secret only then saved. Since the passport command gives you two client keys:

1) Personal access client (client_id & client_secret)

2) Password grant client (client_id & client_secret)

I used the Password grant client. Hopes this helps someone out there :)


Run these two commands on root of laravel

find * -type d -print0 | xargs -0 chmod 0755 # for directories

find . -type f -print0 | xargs -0 chmod 0644 # for files


Because of no right to write the log's directory.
chmod 755 storage -R


I faced this issue and fixed by upgrade my php version in apache up to 5.6.x


A frequent issue when using git:

Laravel's .gitignore ignores the .env file which when missing generates this error

Solved this by manually adding an .env file on the server or uploading it through FTP


if its on a live server try this

<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
    Options -MultiViews -Indexes
</IfModule>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /


# 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]

then also make sure that the php version in your composer.json is the same as that of your server.

Check your php version in your terminal using php -v


May another solution to this problem:

Install the required packages by running the composer command from the the root of the project:

sudo composer install

UPDATE:

  • You should not run this command on a production server, but some issues with composer can be resolved with this on local envs.

EDIT:


Try to check if you have .env file.

Mostly this thing can cause something like that. Try to create a file then copy everything from .env.example, paste it to your created file and name it .env. or jsut simply rename the .env.example file to .env and run php artisan key:generate


Make sure, you've run composer update on your server instance.


Run these two commands in the directory where Laravel is installed:

sudo chmod 755 -R DIRECTORY_NAME
chmod -R o+w DIRECTORY_NAME/storage

Then clear the cache and dump the auto load:

php artisan cache:clear
composer dump-autoload

First allow all permissions for your project folder (let say it's called laravel), for the storage subfolder and its logs subsubfolder and for the vendor subfolder (laravel/storage, laravel/storage/logs and laravel/vendor).

Then check if your have .env file - if not you can run:

$ mv .env.example .env

to rename your build-in .env.example to the needed .env.

Otherwise turn on the debug mode - open .env and set

APP_DEBUG=true

and open laravel/config/app.php and change

'debug' => env('APP_DEBUG', false), 

to

'debug' => env('APP_DEBUG', true),

so you can find out what is the reason for your error.


Make sure storage folder with write previlege (chmod o+w), work for me like a charm.


I fixed this problem by this commands:

mv .env.example .env
php artisan cache:clear
composer dump-autoload
php artisan key:generate

then

php artisan serve

I have faced a similar error. I checked the log in /var/log/apache2/error.log and found an UnexpectedValueException

I changed the owner to my apache user of the storage folder under the project dir.

sudo chown -R www-data:www-data ./storage

In my case apache2 process owner is www-data, so change this to yours, this can be found in apache2 config file. Hope this is useful to you.


I had PHP 7.0 and PHP 7.1 installed and I was using PHP 7.1 on command line and PHP 7.0 was enabled on Apache, that messy can bring problems when you are using Laravel Framework and localhost Apache2 with laravel.

Check your composer file first to see your PHP version.

"php": "^7.1.3",
"laravel/framework": "5.6.*",

Check your currently php version on command line

php -v

Check your currently php version enabled on Apache, I did using browser.

http://localhost

If it's not the same disable the current version and enable the newest one.

sudo a2dismod php7.2

sudo a2enmod php7.1

sudo service apache2 restart

After that change folder permissions

sudo chmod 755 -R blog

for storage folder

chmod -R o+w blog/storage

I read all the comments and suggestions. 500 - HTTP ERROR CODE represents internal server error.

Reasons for this error:

  • These mainly cause due to permission issues
  • Environment variables not found or .env file not found on your root directory
  • PHP extensions problem
  • Database problem

Fix:

  • Set the correct permissions:
  • Run these commands (Ubuntu/Debian)
find /path/to/your/root/dir/ -type f -exec chmod 644 {} \;
find /path/to/your/root/dir/ -type d -exec chmod 755 {} \;

chown -R www-data:www-data /path/to/your/root/dir/

chgrp -R www-data storage bootstrap/cache
chmod -R ug+rwx storage bootstrap/cache
  • If .env file doesn't exist, create one by touch .env and paste your environment variables and then run
   php artisan key:generate
   php artisan cache:clear
   php artisan config:clear
   composer dump-autoload
  • Check your php.ini file and uncomment the extensions you need (In some case you have to install the extension by running this command apt-get install php7.2-[extension-name]
  • Check your database credentials and values in .env file. And grant permissions to the database user for that database.

These are some common problem you likely going to face when deploying your laravel app and once you start getting all these commands, I suggest you to make a script which will save your time.


For those of you who like me still got errors after trying all the other answers :

Check the version of php apache uses, latest laravel only works with php7.1. So you have to :

sudo a2dismod php[yourversion]
sudo a2enmod php7.1
sudo systemctl restart apache2

hope this helps


Sometimes there is problem with php version. We need to change php version from server. Just write down below string in .htaccess file:

AddHandler application/x-httpd-php5 .php

According to the logs :

[06-Feb-2016 22:38:48 Europe/Berlin] PHP Warning:  require(/Users/tomsihap/Documents/Development/mamp/partie_1_exo/bootstrap/../vendor/autoload.php): failed to open stream: No such file or directory in /Users/tomsihap/Documents/Development/mamp/partie_1_exo/bootstrap/autoload.php on line 17
[06-Feb-2016 22:38:48 Europe/Berlin] PHP Fatal error:  require(): Failed opening required '/Users/tomsihap/Documents/Development/mamp/partie_1_exo/bootstrap/../vendor/autoload.php' (include_path='.:/Applications/MAMP/bin/php/php7.0.0/lib/php') in /Users/tomsihap/Documents/Development/mamp/partie_1_exo/bootstrap/autoload.php on line 17
[06-Feb-2016 22:43:37 Europe/Berlin] PHP Warning:  require(/Users/tomsihap/Documents/Development/mamp/partie_1_exo/bootstrap/../vendor/autoload.php): failed to open stream: No such file or directory in /Users/tomsihap/Documents/Development/mamp/partie_1_exo/bootstrap/autoload.php on line 17
[06-Feb-2016 22:43:37 Europe/Berlin] PHP Fatal error:  require(): Failed opening required '/Users/tomsihap/Documents/Development/mamp/partie_1_exo/bootstrap/../vendor/autoload.php' (include_path='.:/Applications/MAMP/bin/php/php7.0.0/lib/php') in /Users/tomsihap/Documents/Development/mamp/partie_1_exo/bootstrap/autoload.php on line 17

There are some failures opening files into the /vendor folder.

By installing and updating via composer, I was able to finally solve the issue.

sudo composer install
sudo composer update

Make sure a .env file, containing an APP_KEY, exists in root.

What should be in the .env hasn't been explicitly stated in other solutions, and thought I'd boil it down to the sentence above.

This fixed my 500 error on a fresh install of Laravel.

Steps:

  1. Create a .env file in root (e.g. touch .env)
  2. Make sure it contains at least one line: APP_KEY=
  3. Generate an app key in terminal: php artisan key:generate

Notes:

  • My particular installation didn't include any .env whatsoever (example or otherwise)

  • Simply having a blank .env does not work.

  • A .env containing parameters, but no APP_KEY parameter, does not work.

Bug?: When generating an app key in terminal, it may report as successful, however no key will actually get placed in the .env if the file has no pre-existing APP_KEY= line.

As a reference, here's the official .env with useful baseline parameters. Copy-paste what you need:

https://github.com/laravel/laravel/blob/master/.env.example


Only change the web folder permission through this command:

sudo chmod 755 -R your_folder


After installing run below command

sudo chmod 755 -R laravel
chmod -R o+w laravel/storage

here "laravel" is the name of directory where laravel installed


If you use vagrant, try this:

First remove config.php in current/vendor.

Run these command:

php artisan config:clear
php artisan clear-compiled
php artisan optimize

NOT RUN php artisan config:cache.

Hope this help.


Create the .env file and also run :

php artisan key:generate

This worked for me after pulling a git project.

After creating .env file and generating the key, run the code below:

php artisan cache:clear 
php artisan config:clear

I have faced this problem many times. Try one of these steps it helped me a lot. Maybe it will also help you.

  1. First of all check your file permissions.
  2. To fix file permissions sudo chmod 755 -R your_project
  3. Then chmod -R o+w your_project/storage to write file to storage folder.
  4. php artisan cache:clear
    composer dump-autoload
  5. php artisan key:generate
  6. Then check server requirements as per the laravel requirement.
  7. Many times you got this error because of the php version. Try changing your php version in cpanel.
  8. Then configure your .htaccess file properly

I fixed with this Command:

   rm -rf app/storage/logs/laravel.logs
   chmod -R 777 app/storage,
   php artisan cache:clear,
   php artisan dump-autoload OR composer dump-autoload

Than restart a server eather XAMPP ore another one and that should be working.


I have a similar issue with a share Host. I was having 500 error. I just fixed by checking the Laravel version and PHP version. The error was because Laravel 5.6 doesn't run on PHP 7.0.x Once I know this I just reconfigure the project to Laravel 5.5 that is compatible with PHP 7.0.x now everything is right. Another reason I have issues sometimes is the FTP I get corrupted Files and have to upload the project more than once. Hope this help in the future I don't found so many information in this topic.


First if there is not .env file in your laravel repository. Copy and paste .env.example file and rename it to .env. then open .env file and in database part give your database description. Run: php artisan key:generate php artisan cache:clear php artisan config:cache php artisan config:clear composer dump-autoload php artisan serve


Examples related to php

I am receiving warning in Facebook Application using PHP SDK Pass PDO prepared statement to variables Parse error: syntax error, unexpected [ Preg_match backtrack error Removing "http://" from a string How do I hide the PHP explode delimiter from submitted form results? Problems with installation of Google App Engine SDK for php in OS X Laravel 4 with Sentry 2 add user to a group on Registration php & mysql query not echoing in html with tags? How do I show a message in the foreach loop?

Examples related to apache

Enable PHP Apache2 Switch php versions on commandline ubuntu 16.04 Laravel: PDOException: could not find driver How to deploy a React App on Apache web server Apache POI error loading XSSFWorkbook class How to enable directory listing in apache web server Job for httpd.service failed because the control process exited with error code. See "systemctl status httpd.service" and "journalctl -xe" for details How to enable php7 module in apache? java.lang.RuntimeException: Unable to instantiate org.apache.hadoop.hive.ql.metadata.SessionHiveMetaStoreClient The program can't start because api-ms-win-crt-runtime-l1-1-0.dll is missing while starting Apache server on my computer

Examples related to .htaccess

Use .htaccess to redirect HTTP to HTTPs Getting a 500 Internal Server Error on Laravel 5+ Ubuntu 14.04 Server unable to read htaccess file, denying access to be safe Laravel 5 – Remove Public from URL Laravel 5 not finding css files How can I fix the 'Missing Cross-Origin Resource Sharing (CORS) Response Header' webfont issue? How Can I Remove “public/index.php” in the URL Generated Laravel? Apache 2.4 - Request exceeded the limit of 10 internal redirects due to probable configuration error Forbidden You don't have permission to access / on this server Htaccess: add/remove trailing slash from URL

Examples related to ubuntu

grep's at sign caught as whitespace "E: Unable to locate package python-pip" on Ubuntu 18.04 How to Install pip for python 3.7 on Ubuntu 18? "Repository does not have a release file" error ping: google.com: Temporary failure in name resolution How to install JDK 11 under Ubuntu? How to upgrade Python version to 3.7? Issue in installing php7.2-mcrypt Install Qt on Ubuntu Failed to start mongod.service: Unit mongod.service not found

Examples related to mod-rewrite

Getting a 500 Internal Server Error on Laravel 5+ Ubuntu 14.04 Forbidden You don't have permission to access / on this server Htaccess: add/remove trailing slash from URL Permission denied: /var/www/abc/.htaccess pcfg_openfile: unable to check htaccess file, ensure it is readable? 404 Not Found The requested URL was not found on this server Redirect all to index.php using htaccess .htaccess not working on localhost with XAMPP URL rewriting with PHP No input file specified htaccess redirect to https://www