[{"data":1,"prerenderedAt":1626},["ShallowReactive",2],{"\u002Ffix-issues\u002Fflask-500-internal-server-error-in-production":3},{"id":4,"title":5,"body":6,"description":1616,"extension":1617,"meta":1618,"navigation":107,"path":1622,"seo":1623,"stem":1624,"__hash__":1625},"content\u002Ffix-issues\u002Fflask-500-internal-server-error-in-production.md","Flask 500 Internal Server Error in Production",{"type":7,"value":8,"toc":1599},"minimark",[9,13,17,22,25,146,149,153,166,170,715,718,764,767,774,777,805,812,815,890,893,900,937,940,947,972,975,996,1003,1006,1025,1029,1108,1112,1115,1120,1167,1170,1187,1191,1228,1230,1244,1248,1299,1301,1312,1316,1363,1365,1376,1380,1405,1407,1415,1418,1435,1439,1513,1517,1544,1548,1556,1564,1572,1580,1588,1592,1595],[10,11,5],"h1",{"id":12},"flask-500-internal-server-error-in-production",[14,15,16],"p",{},"If you're seeing a 500 Internal Server Error from a Flask app in production, this guide shows you how to identify the failing layer and fix it step-by-step. The goal is to restore the app, verify the root cause, and prevent repeat failures.",[18,19,21],"h2",{"id":20},"quick-fix-quick-setup","Quick Fix \u002F Quick Setup",[14,23,24],{},"Run the core checks first:",[26,27,32],"pre",{"className":28,"code":29,"language":30,"meta":31,"style":31},"language-bash shiki shiki-themes github-light github-dark","sudo journalctl -u gunicorn -n 100 --no-pager\nsudo nginx -t\nsudo tail -n 100 \u002Fvar\u002Flog\u002Fnginx\u002Ferror.log\ncurl -I http:\u002F\u002F127.0.0.1:8000\n\n# If Gunicorn crashed, restart and watch logs\nsudo systemctl restart gunicorn\nsudo journalctl -u gunicorn -f --no-pager\n","bash","",[33,34,35,64,75,90,102,109,116,130],"code",{"__ignoreMap":31},[36,37,40,44,48,52,55,58,61],"span",{"class":38,"line":39},"line",1,[36,41,43],{"class":42},"sScJk","sudo",[36,45,47],{"class":46},"sZZnC"," journalctl",[36,49,51],{"class":50},"sj4cs"," -u",[36,53,54],{"class":46}," gunicorn",[36,56,57],{"class":50}," -n",[36,59,60],{"class":50}," 100",[36,62,63],{"class":50}," --no-pager\n",[36,65,67,69,72],{"class":38,"line":66},2,[36,68,43],{"class":42},[36,70,71],{"class":46}," nginx",[36,73,74],{"class":50}," -t\n",[36,76,78,80,83,85,87],{"class":38,"line":77},3,[36,79,43],{"class":42},[36,81,82],{"class":46}," tail",[36,84,57],{"class":50},[36,86,60],{"class":50},[36,88,89],{"class":46}," \u002Fvar\u002Flog\u002Fnginx\u002Ferror.log\n",[36,91,93,96,99],{"class":38,"line":92},4,[36,94,95],{"class":42},"curl",[36,97,98],{"class":50}," -I",[36,100,101],{"class":46}," http:\u002F\u002F127.0.0.1:8000\n",[36,103,105],{"class":38,"line":104},5,[36,106,108],{"emptyLinePlaceholder":107},true,"\n",[36,110,112],{"class":38,"line":111},6,[36,113,115],{"class":114},"sJ8bj","# If Gunicorn crashed, restart and watch logs\n",[36,117,119,121,124,127],{"class":38,"line":118},7,[36,120,43],{"class":42},[36,122,123],{"class":46}," systemctl",[36,125,126],{"class":46}," restart",[36,128,129],{"class":46}," gunicorn\n",[36,131,133,135,137,139,141,144],{"class":38,"line":132},8,[36,134,43],{"class":42},[36,136,47],{"class":46},[36,138,51],{"class":50},[36,140,54],{"class":46},[36,142,143],{"class":50}," -f",[36,145,63],{"class":50},[14,147,148],{},"Most production 500s come from an application exception, missing environment variables, bad database settings, migration issues, or a failing Gunicorn process. Check Gunicorn logs first, then validate Nginx, app config, and database connectivity.",[18,150,152],{"id":151},"whats-happening","What’s Happening",[154,155,156,160,163],"ul",{},[157,158,159],"li",{},"HTTP 500 means the request reached your application stack but failed during processing.",[157,161,162],{},"The failure may be in Flask application code, Gunicorn startup, environment loading, database access, template rendering, file permissions, or reverse proxy configuration.",[157,164,165],{},"The fastest path is to isolate which layer fails: Nginx, Gunicorn, Flask app, or a dependency such as PostgreSQL.",[18,167,169],{"id":168},"step-by-step-guide","Step-by-Step Guide",[171,172,173,212,255,288,326,364,537,559,644,710],"ol",{},[157,174,175,179,182,183,205,207,208,211],{},[176,177,178],"strong",{},"Confirm where the 500 is generated.",[180,181],"br",{},"Check the public endpoint and the backend directly:",[26,184,186],{"className":28,"code":185,"language":30,"meta":31,"style":31},"curl -I https:\u002F\u002Fyour-domain.com\ncurl -I http:\u002F\u002F127.0.0.1:8000\n",[33,187,188,197],{"__ignoreMap":31},[36,189,190,192,194],{"class":38,"line":39},[36,191,95],{"class":42},[36,193,98],{"class":50},[36,195,196],{"class":46}," https:\u002F\u002Fyour-domain.com\n",[36,198,199,201,203],{"class":38,"line":66},[36,200,95],{"class":42},[36,202,98],{"class":50},[36,204,101],{"class":46},[180,206],{},"If Gunicorn returns ",[33,209,210],{},"500"," directly, the issue is inside the app or Gunicorn process.",[157,213,214,217,235,237,238],{},[176,215,216],{},"Check Gunicorn service status.",[26,218,220],{"className":28,"code":219,"language":30,"meta":31,"style":31},"sudo systemctl status gunicorn --no-pager\n",[33,221,222],{"__ignoreMap":31},[36,223,224,226,228,231,233],{"class":38,"line":39},[36,225,43],{"class":42},[36,227,123],{"class":46},[36,229,230],{"class":46}," status",[36,232,54],{"class":46},[36,234,63],{"class":50},[180,236],{},"Look for:",[154,239,240,243,246,249,252],{},[157,241,242],{},"failed workers",[157,244,245],{},"import errors",[157,247,248],{},"missing modules",[157,250,251],{},"wrong working directory",[157,253,254],{},"permission problems",[157,256,257,260,282,284,285,287],{},[176,258,259],{},"Read recent Gunicorn logs.",[26,261,263],{"className":28,"code":262,"language":30,"meta":31,"style":31},"sudo journalctl -u gunicorn -n 200 --no-pager\n",[33,264,265],{"__ignoreMap":31},[36,266,267,269,271,273,275,277,280],{"class":38,"line":39},[36,268,43],{"class":42},[36,270,47],{"class":46},[36,272,51],{"class":50},[36,274,54],{"class":46},[36,276,57],{"class":50},[36,278,279],{"class":50}," 200",[36,281,63],{"class":50},[180,283],{},"Find the actual Python traceback. Fix the first real exception, not the final ",[33,286,210],{}," wrapper.",[157,289,290,293,306,308,309],{},[176,291,292],{},"Validate Nginx configuration.",[26,294,296],{"className":28,"code":295,"language":30,"meta":31,"style":31},"sudo nginx -t\n",[33,297,298],{"__ignoreMap":31},[36,299,300,302,304],{"class":38,"line":39},[36,301,43],{"class":42},[36,303,71],{"class":46},[36,305,74],{"class":50},[180,307],{},"If the config test fails, correct syntax or upstream settings, then reload:",[26,310,312],{"className":28,"code":311,"language":30,"meta":31,"style":31},"sudo systemctl reload nginx\n",[33,313,314],{"__ignoreMap":31},[36,315,316,318,320,323],{"class":38,"line":39},[36,317,43],{"class":42},[36,319,123],{"class":46},[36,321,322],{"class":46}," reload",[36,324,325],{"class":46}," nginx\n",[157,327,328,331,348,237,350],{},[176,329,330],{},"Check Nginx error logs.",[26,332,334],{"className":28,"code":333,"language":30,"meta":31,"style":31},"sudo tail -n 100 \u002Fvar\u002Flog\u002Fnginx\u002Ferror.log\n",[33,335,336],{"__ignoreMap":31},[36,337,338,340,342,344,346],{"class":38,"line":39},[36,339,43],{"class":42},[36,341,82],{"class":46},[36,343,57],{"class":50},[36,345,60],{"class":50},[36,347,89],{"class":46},[180,349],{},[154,351,352,355,358,361],{},[157,353,354],{},"upstream connection failures",[157,356,357],{},"socket path problems",[157,359,360],{},"timeouts",[157,362,363],{},"permission denials",[157,365,366,369,371,372,464,466,467,492,494,495,534,536],{},[176,367,368],{},"Verify Gunicorn bind target matches Nginx upstream.",[180,370],{},"Example Nginx upstream using TCP:",[26,373,377],{"className":374,"code":375,"language":376,"meta":31,"style":31},"language-nginx shiki shiki-themes github-light github-dark","upstream flask_app {\n    server 127.0.0.1:8000;\n}\n\nserver {\n    listen 80;\n    server_name your-domain.com;\n\n    location \u002F {\n        proxy_pass http:\u002F\u002Fflask_app;\n        proxy_set_header Host $host;\n        proxy_set_header X-Real-IP $remote_addr;\n        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;\n        proxy_set_header X-Forwarded-Proto $scheme;\n    }\n}\n","nginx",[33,378,379,384,389,394,398,403,408,413,417,423,429,435,441,447,453,459],{"__ignoreMap":31},[36,380,381],{"class":38,"line":39},[36,382,383],{},"upstream flask_app {\n",[36,385,386],{"class":38,"line":66},[36,387,388],{},"    server 127.0.0.1:8000;\n",[36,390,391],{"class":38,"line":77},[36,392,393],{},"}\n",[36,395,396],{"class":38,"line":92},[36,397,108],{"emptyLinePlaceholder":107},[36,399,400],{"class":38,"line":104},[36,401,402],{},"server {\n",[36,404,405],{"class":38,"line":111},[36,406,407],{},"    listen 80;\n",[36,409,410],{"class":38,"line":118},[36,411,412],{},"    server_name your-domain.com;\n",[36,414,415],{"class":38,"line":132},[36,416,108],{"emptyLinePlaceholder":107},[36,418,420],{"class":38,"line":419},9,[36,421,422],{},"    location \u002F {\n",[36,424,426],{"class":38,"line":425},10,[36,427,428],{},"        proxy_pass http:\u002F\u002Fflask_app;\n",[36,430,432],{"class":38,"line":431},11,[36,433,434],{},"        proxy_set_header Host $host;\n",[36,436,438],{"class":38,"line":437},12,[36,439,440],{},"        proxy_set_header X-Real-IP $remote_addr;\n",[36,442,444],{"class":38,"line":443},13,[36,445,446],{},"        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;\n",[36,448,450],{"class":38,"line":449},14,[36,451,452],{},"        proxy_set_header X-Forwarded-Proto $scheme;\n",[36,454,456],{"class":38,"line":455},15,[36,457,458],{},"    }\n",[36,460,462],{"class":38,"line":461},16,[36,463,393],{},[180,465],{},"Matching Gunicorn bind:",[26,468,470],{"className":28,"code":469,"language":30,"meta":31,"style":31},"gunicorn -w 3 -b 127.0.0.1:8000 wsgi:app\n",[33,471,472],{"__ignoreMap":31},[36,473,474,477,480,483,486,489],{"class":38,"line":39},[36,475,476],{"class":42},"gunicorn",[36,478,479],{"class":50}," -w",[36,481,482],{"class":50}," 3",[36,484,485],{"class":50}," -b",[36,487,488],{"class":46}," 127.0.0.1:8000",[36,490,491],{"class":46}," wsgi:app\n",[180,493],{},"Example using a Unix socket:",[26,496,498],{"className":374,"code":497,"language":376,"meta":31,"style":31},"location \u002F {\n    proxy_pass http:\u002F\u002Funix:\u002Frun\u002Fgunicorn.sock;\n    proxy_set_header Host $host;\n    proxy_set_header X-Real-IP $remote_addr;\n    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;\n    proxy_set_header X-Forwarded-Proto $scheme;\n}\n",[33,499,500,505,510,515,520,525,530],{"__ignoreMap":31},[36,501,502],{"class":38,"line":39},[36,503,504],{},"location \u002F {\n",[36,506,507],{"class":38,"line":66},[36,508,509],{},"    proxy_pass http:\u002F\u002Funix:\u002Frun\u002Fgunicorn.sock;\n",[36,511,512],{"class":38,"line":77},[36,513,514],{},"    proxy_set_header Host $host;\n",[36,516,517],{"class":38,"line":92},[36,518,519],{},"    proxy_set_header X-Real-IP $remote_addr;\n",[36,521,522],{"class":38,"line":104},[36,523,524],{},"    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;\n",[36,526,527],{"class":38,"line":111},[36,528,529],{},"    proxy_set_header X-Forwarded-Proto $scheme;\n",[36,531,532],{"class":38,"line":118},[36,533,393],{},[180,535],{},"Confirm both sides use the same port or socket path.",[157,538,539,542,556,558],{},[176,540,541],{},"Test the Flask app process directly.",[26,543,545],{"className":28,"code":544,"language":30,"meta":31,"style":31},"curl -v http:\u002F\u002F127.0.0.1:8000\n",[33,546,547],{"__ignoreMap":31},[36,548,549,551,554],{"class":38,"line":39},[36,550,95],{"class":42},[36,552,553],{"class":50}," -v",[36,555,101],{"class":46},[180,557],{},"If direct access fails, the problem is not Nginx.",[157,560,561,564,566,567,583,585,586,633,635,636,639,640,643],{},[176,562,563],{},"Check environment variables used in production.",[180,565],{},"Inspect the systemd service:",[26,568,570],{"className":28,"code":569,"language":30,"meta":31,"style":31},"sudo systemctl cat gunicorn\n",[33,571,572],{"__ignoreMap":31},[36,573,574,576,578,581],{"class":38,"line":39},[36,575,43],{"class":42},[36,577,123],{"class":46},[36,579,580],{"class":46}," cat",[36,582,129],{"class":46},[180,584],{},"Example service configuration:",[26,587,591],{"className":588,"code":589,"language":590,"meta":31,"style":31},"language-ini shiki shiki-themes github-light github-dark","[Service]\nUser=www-data\nGroup=www-data\nWorkingDirectory=\u002Fpath\u002Fto\u002Fapp\nEnvironment=\"FLASK_ENV=production\"\nEnvironment=\"SECRET_KEY=change-me\"\nEnvironment=\"DATABASE_URL=postgresql:\u002F\u002Fuser:pass@db-host\u002Fdbname\"\nExecStart=\u002Fpath\u002Fto\u002Fapp\u002Fvenv\u002Fbin\u002Fgunicorn -w 3 -b 127.0.0.1:8000 wsgi:app\n","ini",[33,592,593,598,603,608,613,618,623,628],{"__ignoreMap":31},[36,594,595],{"class":38,"line":39},[36,596,597],{},"[Service]\n",[36,599,600],{"class":38,"line":66},[36,601,602],{},"User=www-data\n",[36,604,605],{"class":38,"line":77},[36,606,607],{},"Group=www-data\n",[36,609,610],{"class":38,"line":92},[36,611,612],{},"WorkingDirectory=\u002Fpath\u002Fto\u002Fapp\n",[36,614,615],{"class":38,"line":104},[36,616,617],{},"Environment=\"FLASK_ENV=production\"\n",[36,619,620],{"class":38,"line":111},[36,621,622],{},"Environment=\"SECRET_KEY=change-me\"\n",[36,624,625],{"class":38,"line":118},[36,626,627],{},"Environment=\"DATABASE_URL=postgresql:\u002F\u002Fuser:pass@db-host\u002Fdbname\"\n",[36,629,630],{"class":38,"line":132},[36,631,632],{},"ExecStart=\u002Fpath\u002Fto\u002Fapp\u002Fvenv\u002Fbin\u002Fgunicorn -w 3 -b 127.0.0.1:8000 wsgi:app\n",[180,634],{},"Missing ",[33,637,638],{},"SECRET_KEY",", ",[33,641,642],{},"DATABASE_URL",", or other required settings often trigger startup or request-time exceptions.",[157,645,646,649,651,652,672,674,675,707,709],{},[176,647,648],{},"Verify the application entry point.",[180,650],{},"Confirm Gunicorn references the correct module and app object:",[26,653,655],{"className":28,"code":654,"language":30,"meta":31,"style":31},"\u002Fpath\u002Fto\u002Fapp\u002Fvenv\u002Fbin\u002Fgunicorn -w 3 -b 127.0.0.1:8000 wsgi:app\n",[33,656,657],{"__ignoreMap":31},[36,658,659,662,664,666,668,670],{"class":38,"line":39},[36,660,661],{"class":42},"\u002Fpath\u002Fto\u002Fapp\u002Fvenv\u002Fbin\u002Fgunicorn",[36,663,479],{"class":50},[36,665,482],{"class":50},[36,667,485],{"class":50},[36,669,488],{"class":46},[36,671,491],{"class":46},[180,673],{},"Validate import manually:",[26,676,678],{"className":28,"code":677,"language":30,"meta":31,"style":31},"cd \u002Fpath\u002Fto\u002Fapp\nsource venv\u002Fbin\u002Factivate\npython -c \"from wsgi import app; print(app)\"\n",[33,679,680,688,696],{"__ignoreMap":31},[36,681,682,685],{"class":38,"line":39},[36,683,684],{"class":50},"cd",[36,686,687],{"class":46}," \u002Fpath\u002Fto\u002Fapp\n",[36,689,690,693],{"class":38,"line":66},[36,691,692],{"class":50},"source",[36,694,695],{"class":46}," venv\u002Fbin\u002Factivate\n",[36,697,698,701,704],{"class":38,"line":77},[36,699,700],{"class":42},"python",[36,702,703],{"class":50}," -c",[36,705,706],{"class":46}," \"from wsgi import app; print(app)\"\n",[180,708],{},"If this fails, fix the module path, app object name, or working directory.",[157,711,712],{},[176,713,714],{},"Check database connectivity.",[14,716,717],{},"Test from the production environment:",[26,719,721],{"className":28,"code":720,"language":30,"meta":31,"style":31},"cd \u002Fpath\u002Fto\u002Fapp\nsource venv\u002Fbin\u002Factivate\npython - \u003C\u003C'PY'\nfrom wsgi import app\nprint(\"app import ok\")\nPY\n",[33,722,723,729,735,749,754,759],{"__ignoreMap":31},[36,724,725,727],{"class":38,"line":39},[36,726,684],{"class":50},[36,728,687],{"class":46},[36,730,731,733],{"class":38,"line":66},[36,732,692],{"class":50},[36,734,695],{"class":46},[36,736,737,739,742,746],{"class":38,"line":77},[36,738,700],{"class":42},[36,740,741],{"class":46}," -",[36,743,745],{"class":744},"szBVR"," \u003C\u003C",[36,747,748],{"class":46},"'PY'\n",[36,750,751],{"class":38,"line":92},[36,752,753],{"class":46},"from wsgi import app\n",[36,755,756],{"class":38,"line":104},[36,757,758],{"class":46},"print(\"app import ok\")\n",[36,760,761],{"class":38,"line":111},[36,762,763],{"class":46},"PY\n",[14,765,766],{},"If your app depends on PostgreSQL, also verify host, port, username, password, SSL mode, and firewall access. If the traceback shows connection errors, correct the database settings before retrying.",[171,768,769],{"start":431},[157,770,771],{},[176,772,773],{},"Apply pending migrations.",[14,775,776],{},"If logs show missing tables or columns:",[26,778,780],{"className":28,"code":779,"language":30,"meta":31,"style":31},"cd \u002Fpath\u002Fto\u002Fapp\nsource venv\u002Fbin\u002Factivate\nflask db upgrade\n",[33,781,782,788,794],{"__ignoreMap":31},[36,783,784,786],{"class":38,"line":39},[36,785,684],{"class":50},[36,787,687],{"class":46},[36,789,790,792],{"class":38,"line":66},[36,791,692],{"class":50},[36,793,695],{"class":46},[36,795,796,799,802],{"class":38,"line":77},[36,797,798],{"class":42},"flask",[36,800,801],{"class":46}," db",[36,803,804],{"class":46}," upgrade\n",[171,806,807],{"start":437},[157,808,809],{},[176,810,811],{},"Check file and directory permissions.",[14,813,814],{},"Ensure the Gunicorn user can read the app code, templates, static files, and config files:",[26,816,818],{"className":28,"code":817,"language":30,"meta":31,"style":31},"sudo chown -R www-data:www-data \u002Fpath\u002Fto\u002Fapp\nsudo find \u002Fpath\u002Fto\u002Fapp -type d -exec chmod 755 {} \\;\nsudo find \u002Fpath\u002Fto\u002Fapp -type f -exec chmod 644 {} \\;\n",[33,819,820,835,866],{"__ignoreMap":31},[36,821,822,824,827,830,833],{"class":38,"line":39},[36,823,43],{"class":42},[36,825,826],{"class":46}," chown",[36,828,829],{"class":50}," -R",[36,831,832],{"class":46}," www-data:www-data",[36,834,687],{"class":46},[36,836,837,839,842,845,848,851,854,857,860,863],{"class":38,"line":66},[36,838,43],{"class":42},[36,840,841],{"class":46}," find",[36,843,844],{"class":46}," \u002Fpath\u002Fto\u002Fapp",[36,846,847],{"class":50}," -type",[36,849,850],{"class":46}," d",[36,852,853],{"class":50}," -exec",[36,855,856],{"class":46}," chmod",[36,858,859],{"class":50}," 755",[36,861,862],{"class":46}," {}",[36,864,865],{"class":50}," \\;\n",[36,867,868,870,872,874,876,879,881,883,886,888],{"class":38,"line":77},[36,869,43],{"class":42},[36,871,841],{"class":46},[36,873,844],{"class":46},[36,875,847],{"class":50},[36,877,878],{"class":46}," f",[36,880,853],{"class":50},[36,882,856],{"class":46},[36,884,885],{"class":50}," 644",[36,887,862],{"class":46},[36,889,865],{"class":50},[14,891,892],{},"If the app writes uploads, logs, or temporary files, those directories must also be writable by the service user.",[171,894,895],{"start":443},[157,896,897],{},[176,898,899],{},"Review installed dependencies inside the active environment.",[26,901,903],{"className":28,"code":902,"language":30,"meta":31,"style":31},"cd \u002Fpath\u002Fto\u002Fapp\nsource venv\u002Fbin\u002Factivate\npip freeze | grep -E 'Flask|gunicorn|psycopg|mysql|dotenv'\n",[33,904,905,911,917],{"__ignoreMap":31},[36,906,907,909],{"class":38,"line":39},[36,908,684],{"class":50},[36,910,687],{"class":46},[36,912,913,915],{"class":38,"line":66},[36,914,692],{"class":50},[36,916,695],{"class":46},[36,918,919,922,925,928,931,934],{"class":38,"line":77},[36,920,921],{"class":42},"pip",[36,923,924],{"class":46}," freeze",[36,926,927],{"class":744}," |",[36,929,930],{"class":42}," grep",[36,932,933],{"class":50}," -E",[36,935,936],{"class":46}," 'Flask|gunicorn|psycopg|mysql|dotenv'\n",[14,938,939],{},"Missing packages are common after deploys, especially when the wrong virtualenv is active.",[171,941,942],{"start":449},[157,943,944],{},[176,945,946],{},"Restart services after fixes.",[26,948,950],{"className":28,"code":949,"language":30,"meta":31,"style":31},"sudo systemctl restart gunicorn\nsudo systemctl reload nginx\n",[33,951,952,962],{"__ignoreMap":31},[36,953,954,956,958,960],{"class":38,"line":39},[36,955,43],{"class":42},[36,957,123],{"class":46},[36,959,126],{"class":46},[36,961,129],{"class":46},[36,963,964,966,968,970],{"class":38,"line":66},[36,965,43],{"class":42},[36,967,123],{"class":46},[36,969,322],{"class":46},[36,971,325],{"class":46},[14,973,974],{},"Then test again:",[26,976,978],{"className":28,"code":977,"language":30,"meta":31,"style":31},"curl -I https:\u002F\u002Fyour-domain.com\ncurl -v http:\u002F\u002F127.0.0.1:8000\n",[33,979,980,988],{"__ignoreMap":31},[36,981,982,984,986],{"class":38,"line":39},[36,983,95],{"class":42},[36,985,98],{"class":50},[36,987,196],{"class":46},[36,989,990,992,994],{"class":38,"line":66},[36,991,95],{"class":42},[36,993,553],{"class":50},[36,995,101],{"class":46},[171,997,998],{"start":455},[157,999,1000],{},[176,1001,1002],{},"Improve logging if the traceback is incomplete.",[14,1004,1005],{},"Start with service logs:",[26,1007,1009],{"className":28,"code":1008,"language":30,"meta":31,"style":31},"sudo journalctl -u gunicorn -f --no-pager\n",[33,1010,1011],{"__ignoreMap":31},[36,1012,1013,1015,1017,1019,1021,1023],{"class":38,"line":39},[36,1014,43],{"class":42},[36,1016,47],{"class":46},[36,1018,51],{"class":50},[36,1020,54],{"class":46},[36,1022,143],{"class":50},[36,1024,63],{"class":50},[18,1026,1028],{"id":1027},"common-causes","Common Causes",[154,1030,1031,1037,1046,1063,1069,1075,1081,1087,1093,1102],{},[157,1032,1033,1036],{},[176,1034,1035],{},"Application exception during request handling"," → Unhandled Python error in a route, template, or service layer → Read the Gunicorn traceback and fix the first raised exception.",[157,1038,1039,1042,1043,1045],{},[176,1040,1041],{},"Missing environment variables"," → ",[33,1044,638],{},", database URL, API keys, or config flags are not loaded in systemd or Docker → Add the variables in the real production runtime and restart Gunicorn.",[157,1047,1048,1051,1052,639,1055,1058,1059,1062],{},[176,1049,1050],{},"Wrong Gunicorn app module or working directory"," → Gunicorn starts with the wrong import path and workers fail → Correct ",[33,1053,1054],{},"ExecStart",[33,1056,1057],{},"WorkingDirectory",", and ",[33,1060,1061],{},"module:app",".",[157,1064,1065,1068],{},[176,1066,1067],{},"Database connection failure"," → Wrong credentials, host, SSL mode, or firewall rules cause runtime failures → Test connectivity and correct database settings.",[157,1070,1071,1074],{},[176,1072,1073],{},"Pending migrations"," → App code expects schema changes that are not applied → Run migration commands before serving traffic.",[157,1076,1077,1080],{},[176,1078,1079],{},"Template or file permission problem"," → Gunicorn cannot read templates, uploads, or config files → Fix ownership and permissions for the service user.",[157,1082,1083,1086],{},[176,1084,1085],{},"Dependency mismatch after deploy"," → New code requires packages not installed in the active virtualenv or image → Reinstall dependencies and redeploy.",[157,1088,1089,1092],{},[176,1090,1091],{},"Socket or upstream mismatch"," → Nginx points to the wrong socket or port → Align Nginx upstream and Gunicorn bind settings.",[157,1094,1095,1042,1098,1101],{},[176,1096,1097],{},"Bad production config",[33,1099,1100],{},"DEBUG=False"," exposes real config gaps such as invalid hosts, secret key issues, or disabled services → Review production settings explicitly.",[157,1103,1104,1107],{},[176,1105,1106],{},"External service failure"," → SMTP, cache, object storage, or third-party API errors raise exceptions in the request flow → Add timeouts, retries, and defensive error handling.",[18,1109,1111],{"id":1110},"debugging-section","Debugging Section",[14,1113,1114],{},"Check the failing layer in this order:",[1116,1117,1119],"h3",{"id":1118},"_1-gunicorn-service-and-logs","1. Gunicorn service and logs",[26,1121,1123],{"className":28,"code":1122,"language":30,"meta":31,"style":31},"sudo systemctl status gunicorn --no-pager\nsudo journalctl -u gunicorn -n 200 --no-pager\nsudo journalctl -u gunicorn -f --no-pager\n",[33,1124,1125,1137,1153],{"__ignoreMap":31},[36,1126,1127,1129,1131,1133,1135],{"class":38,"line":39},[36,1128,43],{"class":42},[36,1130,123],{"class":46},[36,1132,230],{"class":46},[36,1134,54],{"class":46},[36,1136,63],{"class":50},[36,1138,1139,1141,1143,1145,1147,1149,1151],{"class":38,"line":66},[36,1140,43],{"class":42},[36,1142,47],{"class":46},[36,1144,51],{"class":50},[36,1146,54],{"class":46},[36,1148,57],{"class":50},[36,1150,279],{"class":50},[36,1152,63],{"class":50},[36,1154,1155,1157,1159,1161,1163,1165],{"class":38,"line":77},[36,1156,43],{"class":42},[36,1158,47],{"class":46},[36,1160,51],{"class":50},[36,1162,54],{"class":46},[36,1164,143],{"class":50},[36,1166,63],{"class":50},[14,1168,1169],{},"What to look for:",[154,1171,1172,1175,1178,1181,1184],{},[157,1173,1174],{},"Python tracebacks",[157,1176,1177],{},"module import errors",[157,1179,1180],{},"worker boot failures",[157,1182,1183],{},"permission denied messages",[157,1185,1186],{},"missing environment values",[1116,1188,1190],{"id":1189},"_2-nginx-validation-and-error-log","2. Nginx validation and error log",[26,1192,1194],{"className":28,"code":1193,"language":30,"meta":31,"style":31},"sudo nginx -t\nsudo systemctl status nginx --no-pager\nsudo tail -n 100 \u002Fvar\u002Flog\u002Fnginx\u002Ferror.log\n",[33,1195,1196,1204,1216],{"__ignoreMap":31},[36,1197,1198,1200,1202],{"class":38,"line":39},[36,1199,43],{"class":42},[36,1201,71],{"class":46},[36,1203,74],{"class":50},[36,1205,1206,1208,1210,1212,1214],{"class":38,"line":66},[36,1207,43],{"class":42},[36,1209,123],{"class":46},[36,1211,230],{"class":46},[36,1213,71],{"class":46},[36,1215,63],{"class":50},[36,1217,1218,1220,1222,1224,1226],{"class":38,"line":77},[36,1219,43],{"class":42},[36,1221,82],{"class":46},[36,1223,57],{"class":50},[36,1225,60],{"class":50},[36,1227,89],{"class":46},[14,1229,1169],{},[154,1231,1232,1235,1238,1241],{},[157,1233,1234],{},"invalid config syntax",[157,1236,1237],{},"upstream connection errors",[157,1239,1240],{},"socket path mismatches",[157,1242,1243],{},"timeout events",[1116,1245,1247],{"id":1246},"_3-backend-listener-state","3. Backend listener state",[26,1249,1251],{"className":28,"code":1250,"language":30,"meta":31,"style":31},"ps aux | grep gunicorn\nsudo ss -ltnp | grep 8000\nsudo ss -lx | grep gunicorn\n",[33,1252,1253,1267,1284],{"__ignoreMap":31},[36,1254,1255,1258,1261,1263,1265],{"class":38,"line":39},[36,1256,1257],{"class":42},"ps",[36,1259,1260],{"class":46}," aux",[36,1262,927],{"class":744},[36,1264,930],{"class":42},[36,1266,129],{"class":46},[36,1268,1269,1271,1274,1277,1279,1281],{"class":38,"line":66},[36,1270,43],{"class":42},[36,1272,1273],{"class":46}," ss",[36,1275,1276],{"class":50}," -ltnp",[36,1278,927],{"class":744},[36,1280,930],{"class":42},[36,1282,1283],{"class":50}," 8000\n",[36,1285,1286,1288,1290,1293,1295,1297],{"class":38,"line":77},[36,1287,43],{"class":42},[36,1289,1273],{"class":46},[36,1291,1292],{"class":50}," -lx",[36,1294,927],{"class":744},[36,1296,930],{"class":42},[36,1298,129],{"class":46},[14,1300,1169],{},[154,1302,1303,1306,1309],{},[157,1304,1305],{},"Gunicorn master and worker processes",[157,1307,1308],{},"expected TCP port listening",[157,1310,1311],{},"expected Unix socket present",[1116,1313,1315],{"id":1314},"_4-direct-app-import-and-dependency-validation","4. Direct app import and dependency validation",[26,1317,1319],{"className":28,"code":1318,"language":30,"meta":31,"style":31},"cd \u002Fpath\u002Fto\u002Fapp && source venv\u002Fbin\u002Factivate && python -c \"from wsgi import app; print(app)\"\ncd \u002Fpath\u002Fto\u002Fapp && source venv\u002Fbin\u002Factivate && pip freeze\n",[33,1320,1321,1344],{"__ignoreMap":31},[36,1322,1323,1325,1327,1331,1333,1336,1338,1340,1342],{"class":38,"line":39},[36,1324,684],{"class":50},[36,1326,844],{"class":46},[36,1328,1330],{"class":1329},"sVt8B"," && ",[36,1332,692],{"class":50},[36,1334,1335],{"class":46}," venv\u002Fbin\u002Factivate",[36,1337,1330],{"class":1329},[36,1339,700],{"class":42},[36,1341,703],{"class":50},[36,1343,706],{"class":46},[36,1345,1346,1348,1350,1352,1354,1356,1358,1360],{"class":38,"line":66},[36,1347,684],{"class":50},[36,1349,844],{"class":46},[36,1351,1330],{"class":1329},[36,1353,692],{"class":50},[36,1355,1335],{"class":46},[36,1357,1330],{"class":1329},[36,1359,921],{"class":42},[36,1361,1362],{"class":46}," freeze\n",[14,1364,1169],{},[154,1366,1367,1370,1373],{},[157,1368,1369],{},"import success",[157,1371,1372],{},"correct Python environment",[157,1374,1375],{},"required packages installed",[1116,1377,1379],{"id":1378},"_5-schema-and-deploy-state","5. Schema and deploy state",[26,1381,1383],{"className":28,"code":1382,"language":30,"meta":31,"style":31},"cd \u002Fpath\u002Fto\u002Fapp && source venv\u002Fbin\u002Factivate && flask db upgrade\n",[33,1384,1385],{"__ignoreMap":31},[36,1386,1387,1389,1391,1393,1395,1397,1399,1401,1403],{"class":38,"line":39},[36,1388,684],{"class":50},[36,1390,844],{"class":46},[36,1392,1330],{"class":1329},[36,1394,692],{"class":50},[36,1396,1335],{"class":46},[36,1398,1330],{"class":1329},[36,1400,798],{"class":42},[36,1402,801],{"class":46},[36,1404,804],{"class":46},[14,1406,1169],{},[154,1408,1409,1412],{},[157,1410,1411],{},"unapplied migrations",[157,1413,1414],{},"schema drift after deploy",[14,1416,1417],{},"If the issue began after deployment, compare the current release with the last known-good release:",[154,1419,1420,1423,1426,1429,1432],{},[157,1421,1422],{},"environment file changes",[157,1424,1425],{},"dependency changes",[157,1427,1428],{},"migration state",[157,1430,1431],{},"Gunicorn service edits",[157,1433,1434],{},"Nginx upstream edits",[18,1436,1438],{"id":1437},"checklist","Checklist",[154,1440,1443,1452,1462,1468,1474,1480,1486,1492,1498,1507],{"className":1441},[1442],"contains-task-list",[157,1444,1447,1451],{"className":1445},[1446],"task-list-item",[1448,1449],"input",{"disabled":107,"type":1450},"checkbox"," gunicorn service is active and not crashing",[157,1453,1455,1457,1458,1461],{"className":1454},[1446],[1448,1456],{"disabled":107,"type":1450}," ",[33,1459,1460],{},"nginx -t"," passes without errors",[157,1463,1465,1467],{"className":1464},[1446],[1448,1466],{"disabled":107,"type":1450}," Nginx upstream matches Gunicorn bind target",[157,1469,1471,1473],{"className":1470},[1446],[1448,1472],{"disabled":107,"type":1450}," Flask app imports successfully in the production environment",[157,1475,1477,1479],{"className":1476},[1446],[1448,1478],{"disabled":107,"type":1450}," required environment variables are loaded",[157,1481,1483,1485],{"className":1482},[1446],[1448,1484],{"disabled":107,"type":1450}," database connection succeeds from the app host",[157,1487,1489,1491],{"className":1488},[1446],[1448,1490],{"disabled":107,"type":1450}," migrations are applied",[157,1493,1495,1497],{"className":1494},[1446],[1448,1496],{"disabled":107,"type":1450}," file permissions allow Gunicorn to read app files",[157,1499,1501,1503,1504,1506],{"className":1500},[1446],[1448,1502],{"disabled":107,"type":1450}," direct ",[33,1505,95],{}," to Gunicorn works or returns the expected app response",[157,1508,1510,1512],{"className":1509},[1446],[1448,1511],{"disabled":107,"type":1450}," public request no longer returns HTTP 500",[18,1514,1516],{"id":1515},"related-guides","Related Guides",[154,1518,1519,1526,1532,1538],{},[157,1520,1521],{},[1522,1523,1525],"a",{"href":1524},"\u002Fdeploy\u002Fdeploy-flask-with-nginx-plus-gunicorn-step-by-step-guide","Deploy Flask with Nginx + Gunicorn (Step-by-Step Guide)",[157,1527,1528],{},[1522,1529,1531],{"href":1530},"\u002Ffix-issues\u002Fflask-gunicorn-service-failed-to-start","Flask Gunicorn Service Failed to Start",[157,1533,1534],{},[1522,1535,1537],{"href":1536},"\u002Ffix-issues\u002Fflask-environment-variables-not-loading-in-production","Flask Environment Variables Not Loading in Production",[157,1539,1540],{},[1522,1541,1543],{"href":1542},"\u002Fchecklist\u002Fflask-production-checklist-everything-you-must-do","Flask Production Checklist (Everything You Must Do)",[18,1545,1547],{"id":1546},"faq","FAQ",[14,1549,1550,1553,1555],{},[176,1551,1552],{},"Q: What is the first log to check for a Flask 500 in production?",[180,1554],{},"\nA: Check Gunicorn logs first. They usually contain the Python traceback that caused the 500.",[14,1557,1558,1561,1563],{},[176,1559,1560],{},"Q: How do I tell if the problem is Nginx or Flask?",[180,1562],{},"\nA: Request Gunicorn directly on its port or socket target. If it still returns 500, the issue is in Gunicorn or the Flask app.",[14,1565,1566,1569,1571],{},[176,1567,1568],{},"Q: Can missing environment variables cause intermittent 500 errors?",[180,1570],{},"\nA: Yes. Some routes may fail only when they access a missing secret, API key, or database setting.",[14,1573,1574,1577,1579],{},[176,1575,1576],{},"Q: Why did the app work before deploy and fail after deploy?",[180,1578],{},"\nA: Common causes are missing dependencies, wrong app entry point, missing migrations, changed config, or stale environment values.",[14,1581,1582,1585,1587],{},[176,1583,1584],{},"Q: Should I restart both Gunicorn and Nginx after fixing the issue?",[180,1586],{},"\nA: Yes. Restart Gunicorn after app or environment fixes, and reload Nginx after proxy config changes.",[18,1589,1591],{"id":1590},"final-takeaway","Final Takeaway",[14,1593,1594],{},"A production Flask 500 is usually an application exception, not a generic server problem. Read Gunicorn logs first, validate Nginx second, then verify environment variables, database connectivity, migrations, and file permissions until the direct app request succeeds.",[1596,1597,1598],"style",{},"html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .sScJk, html code.shiki .sScJk{--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .sZZnC, html code.shiki .sZZnC{--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .sj4cs, html code.shiki .sj4cs{--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sJ8bj, html code.shiki .sJ8bj{--shiki-default:#6A737D;--shiki-dark:#6A737D}html pre.shiki code .szBVR, html code.shiki .szBVR{--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sVt8B, html code.shiki .sVt8B{--shiki-default:#24292E;--shiki-dark:#E1E4E8}",{"title":31,"searchDepth":66,"depth":66,"links":1600},[1601,1602,1603,1604,1605,1612,1613,1614,1615],{"id":20,"depth":66,"text":21},{"id":151,"depth":66,"text":152},{"id":168,"depth":66,"text":169},{"id":1027,"depth":66,"text":1028},{"id":1110,"depth":66,"text":1111,"children":1606},[1607,1608,1609,1610,1611],{"id":1118,"depth":77,"text":1119},{"id":1189,"depth":77,"text":1190},{"id":1246,"depth":77,"text":1247},{"id":1314,"depth":77,"text":1315},{"id":1378,"depth":77,"text":1379},{"id":1437,"depth":66,"text":1438},{"id":1515,"depth":66,"text":1516},{"id":1546,"depth":66,"text":1547},{"id":1590,"depth":66,"text":1591},"Complete guide on flask 500 internal server error in production for Flask production environments.","md",{"ogTitle":5,"ogDescription":1616,"twitterCard":1619,"robots":1620,"canonical":1621},"summary_large_image","index, follow","https:\u002F\u002Fflask-deployment.com\u002Ffix-issues\u002Fflask-500-internal-server-error-in-production","\u002Ffix-issues\u002Fflask-500-internal-server-error-in-production",{"title":5,"description":1616},"fix-issues\u002Fflask-500-internal-server-error-in-production","34HRZu2icp2K7ghF705T-7oAr1I-slfWQSqR3KFpA0g",1776805766549]