Error 500 internal server error Nginx

When developing web sites and web applications, you may encounter a 500 internal server error. At first it can be scary and confusing, because usually the web server gives a more specific error, which indicate the exact cause of the problem, for example, timed out, invalid request or file not found, and then just says that it encountered an internal error.

But not everything is so terrible and in most cases the problem is solved very quickly. In this article we will discuss how to fix Internal server error on Nginx.

How to fix 500 internal server error Nginx

Literally means Internal server error internal server error. And it can cause a few problems. Here are the main ones:

  • Error in PHP script is one of the most common causes;
  • Timed PHP script execution or memory limit;
  • Wrong permissions on the files website;
  • Wrong configuration of Nginx.

Now consider each of these reasons in more detail and examine the solutions.

1. Error in PHP script

We are accustomed to the fact that if a PHP script has an error, then immediately see them in the browser. However, on production servers displaying error messages in PHP is disabled to prevent the dissemination of information about server configuration to outsiders. Nginx cannot show the real cause of the error, because he did not know that an error has occurred, and therefore gives a generic message 500 internal server error.

To fix this error, you must first understand where exactly the problem is. You can enable error reporting in the php configuration file by changing the string value display_errors from off to on. Consider the example of Ubuntu and PHP 7.2:

vi /etc/php/7.2/php.ini 

display_errors = On

Restart php-fpm:

sudo systemctl restart php-fpm

Then refresh the page and you will see the error message, because of which the problem occurred. Further, it is possible to fix and disable the display of errors, then everything will work. You can still see error messages in the PHP error log with Nginx. It is usually located in /var/log/nginx/error.log, but for a virtual domain can be configured separately. For example, watch the last 100 lines in the log:

tail-n 100 -f /var/log/nginx/error.log

Now similarly, correct the error and the page will load normally, without the error 500.

2. Exceeded run time or memory limit

This is a continuation of the previous paragraph, so also belongs to the PHP error, but since the problem occurs quite often, I decided to submit it in a separate paragraph. In the php file.ini limits the script execution time and the amount of RAM that it can consume. If the script consumes more, the PHP interpreter is killing it and returns an error message.

Also, this error can occur if the server has run out of free RAM.

If error display is disabled, we get error 500. Please note that if the waiting time was restricted in the configuration file of Nginx, you will get the 504 error instead of HTTP 500 ERROR, so the problem is in php.ini.

To solve the problem increase the values of memory_limit and max_execution_time settings in php.ini:

sudo vi /etc/php/7.2/php.ini 

max_execution_time 300
memory_limit 512M

Also the problem may be caused by the excess of other limits established for the php script. See the php errors as described in the first paragraph. After making changes to the file then restart php-fpm:

sudo systemctl restart php-fpm

3. Wrong permissions on files

This error can occur if permissions for files that are accessed by Nginx installed on the right. Services Nginx and php-fpm should be running on behalf of the same user, and all files of the sites must belong to the same user. View on behalf of any user running Nginx with the command:

nginx -T | grep user

To find out what user is running php-fpm view the contents of the configuration file used by the pool, for example www.conf:

sudo vi /etc/php-fpm.d/www.conf

In my case it is the user nginx. Now we have to make sure that the website files that you are trying to access belong to this user. To do this, use the command namei:

namei -l /var/www/site

Website files should be owned by the user on whose behalf the services are running, and the path to the directory with the files should be read access for all users. If the files belong to the wrong user, you can very simple fix:

sudo chown nginx:nginx -R /var/www/site

With this command we change the owner and group of all files in a folder on nginx:nginx. Add read permissions for all users for a directory using the command chmod. For example:

sudo chmod o+r /var/www/

Then everything should work. Also, a permission problem could cause SELinux. Configure it correctly or disable:

setenforce 0


In this article we discussed what to do if your site was found error 500 internal server error nginx. As you can see, the problem is completely solved and in most cases will help you with the actions described in the article. And if does not help, write your decision in the comments!


(Visited 159 times, 1 visits today)