How to Fix HTTP error code “500 internal server error”
Learn how to fix the HTTP error code “500 internal server error.” This error means there is a problem on the server side. It is an all-purpose error, which means a serious problem is preventing your website from functioning.
Refresh the Page
The first thing to do when you encounter this error is wait a moment and then refresh the page. Sometimes this error is caused when a service restarts, and you happen to catch it at exactly the wrong time. If the problem persists after you refresh the page, continue troubleshooting.
Check Your Server Logs
Your first stop should be your website's error logs. On a Linux server, the default location for the main website error log is /var/log/httpd/error_log.
If you have multiple websites hosted on your server, they will likely have their own error logs. The location of these log files will be specified in the site's Apache configurations. Typically these are located in the website's /logs/ directory.
If the error log is large, it can be difficult to find the correct line. If you know that a particular script is causing the error, you can search the file for this script name by using the command:
more /var/log/httpd/error_log | grep [file name]
This will return any lines which have the file name in them.
If you are not sure what is causing the error, you can follow the error log in one window and watch it being generated. First, in your SSH session, use the command:
tail -f /var/log/httpd/error_log
Without closing the SSH session, go to a web browser and refresh the page to generate the 500 error again. You should see the error appear in the log file.
You can also view the last 10 lines in the error log with the command:
tail -20 /var/log/httpd/error_log
Once you find the error, copying and pasting it into Google will often yield helpful results.
An HTTP 500 error can be caused by a permissions problem with your website’s files or folders. Check the permissions on all of your main folders and scripts. Most Perl and CGI files need to have their permissions set to 755.
To check the permissions of files on a Linux system, connect via SSH to your server and go to the directory where the files are kept. Use the ll command to list all files and file details.
The first block of letters lists the file's permissions. There are three permission levels:
- Read (r)
- Write (w)
- Execute (x)
The permissions are shown in groups of three:
- Group 1: Owner
- Group 2: Group
- Group 3: World (everyone)
In the above example, the first file (test.cgi) has read/write/execute permissions for the owner, but only read/write permissions for group and world (755).
The second file (test.py) has read/write/execute permissions for owner, group, and world (777).
To change the permissions for a file, you need to use the chmod command, along with the numerical value of the permissions level you want to set. The most common permissions levels are:
- 7: Read, write, and execute (rwx)
- 5: Read and execute (r-x)
- 0: None (---)
Most scripts will require read/write/execute permissions for the owner, and read/execute permissions for group and world. To set this on a file, use the command:
chmod 755 [filename]
If you are still receiving an error when you run your script, try setting it to global read/write/execute permissions for testing purposes. To set this on a file, use the command:
chmod 777 [filename]
This permissions level can be risky, because it allows anyone to rewrite your file. Once you have finished troubleshooting, be sure to set the file back to the correct permissions.
Examine Your Scripts
If your website relies on scripts (such as Perl or CGI files), there are a number of things that can go wrong. Always check the file permissions first. If those are correct, examine the scripts themselves.
- Are they in the right place, and named correctly? Verify that the scripts have not been moved, deleted, or renamed by accident.
- Are the file paths correct? Many scripts will require other files and programs in order to run. For example, if you have a Perl script, it will start with the path to Perl. You can use the which command to find the path to Perl (or any other programming language or command) with the command:
- Do the scripts have permission to run in the folder where they are located? You may want to find a simple test script to check this.
- Be sure your test script is in the same language as the one you are troubleshooting, and uses the same file extension. For example, if you are having trouble with a Python script named myfile.py, search for a Python test script, and give the file a .py file extension. Using a Perl test script, or a Python test script that ends in .cgi won't be effective as a test.
- Was the script recently edited in a word processing program like Microsoft Word or Wordpad? CGI programs and other scripts should only be edited with a text editor like Notepad. To fix this problem, find a fresh copy of the script, and use a text editor like Notepad (for Windows) or BBEdit (for Mac).
Check the Apache Files
An HTTP 500 error can be caused by a problem with your Apache configurations. If you have scripts or CGI programs which are generating a 500 error, check to make sure they have permission to run in the directory where they are located.
Apache will not allow programs to execute by default. Permission must be given in either an .htaccess file in the directory where the program runs, or in the website's main Apache configuration file.
Granting Permissions With an .htaccess File
You can grant executable permissions on a directory-by-directory basis using an .htaccess file.
Note: because the file name begins with a period, the file will be hidden from a basic ls search from the command line. To list the files in the directory including any which begin with a period, use the command:
Either edit the existing file, or create one in the directory, if none exists. You can also create this file in Notepad on your desktop computer, and upload it to your website with FTP.
The file will need to have this line:
You will also need a line that will tell Apache how to run scripts written in Python, PHP, Perl, etc. Customize this line to add the extension your files are using (.py, .php, .pl, etc.).
AddHandler cgi-script .cgi .py .php .pl
Is the .htaccess File Being Read?
If you have an .htaccess file which is set up correctly, but you are still getting an HTTP 500 error, the problem might be that the .htaccess file is being ignored.
In order for the .htaccess file to work, Apache will need to be configured to allow it to run.
Check the website's main Apache configuration file. For the main domain on a server, this will typically be:
- Ubuntu and Debian: /etc/apache2/apache2.conf
- CentOS 7: /etc/httpd/conf/httpd.conf
For other domains being hosted on the server, the location and name of the file will vary depending on how the domain was set up.
Edit this main configuration file and find the block which pertains to the directory where you are adding an .htaccess file. For example, the default document root for the main directory will probably be:
Inside this block, look for the AllowOverride configuration. Set this to:
After making changes to the main Apache configuration file, restart Apache for the changes to take effect:
- Ubuntu and Debian: use the command service apache2 restart.
- CentOS 7: use the command systemctl restart httpd.
If you have scripts which call external networks or resources, an HTTP 500 error can be caused when those connections time out, usually because the resource is unreachable. Check your external resources to be sure they are accessible.