I'm trying to deploy a simple python flask web application in OpenShift,
But after deploying the application I'm getting "500 Internal Server error" when i try to access the url and the following error is thrown in the server log,
rhc tail -a app -l mail -p password
Related
I have a Flask app deployed with Gunicorn & Nginx and I am trying to debug a strange issue I am having with sending emails from the application (via Flask-Mail library). Sending emails works from my local development environment and also works on my server (on Digital Ocean) if I start my application via the flask run command. This is important as most of the answers I have found mention that their hosting provider blocks outgoing mail over the default SMTP port.
Once I try to send any email from the application hosted in Gunicorn I get the following error in my logs -
File "/usr/lib/python3.10/smtplib.py", line 365, in send
raise SMTPServerDisconnected('please run connect() first')
smtplib.SMTPServerDisconnected: please run connect() first
My application is launched in Gunicorn via the below command -
gunicorn --workers 3 \
--timeout 120 \
--bind 0.0.0.0:9000 \
--log-file=dt.log \
--bind unix:dt.sock -m 007 \
--log-level=info wsgi:app
I have created a very simple route to send an email and I see the same behavior as my existing code. This is the route I created.
#bp.route("/debugmail")
def debugmail():
msg = Message(
"An Email Subject!",
sender=("Email Sender Name", "emailfromsample#gmail.com"),
recipients=["ping#pong.com"],
)
msg.body = "This is an email from the App (tons of uses for this)."
# Also Tried Adding the Connect Command Here
# mail.connect()
mail.send(msg)
return {"message": "Email sent!"}, 200
The configuration file I use for this app is below -
MAIL_SERVER=smtp.gmail.com
MAIL_PORT=587
MAIL_USE_TLS=True
MAIL_USE_SSL=False
MAIL_USERNAME=username#gmail.com
MAIL_PASSWORD=app-specific-password
Any help or thoughts or tests for me to run would be greatly appreciated!
It turns out that by using a .env file that Flask was able to read automatically, was not passed into the context of the Gunicorn runner.
As per the answer linked belo, I have created this file which guarantees that both my .flaskenv and .env load no matter the context.
Running flask app with gunicorn and environment variables
# gunicorn.conf.py
import os
from dotenv import load_dotenv
for env_file in ('.env', '.flaskenv'):
env = os.path.join(os.getcwd(), env_file)
if os.path.exists(env):
load_dotenv(env)
I want to deploy my flask-restx application on a shared hosting. Since I am beginner in deployment, I followed a video tutorial from youtube.
I did step by step by following this tutorial.
For those who do not want to go through the tutorial, I am writing the steps:
I created an application from the Python cPanel
Initial set up in Cpanel
Then I opened terminal and changed my venv and installed flask by "pip install flask"
Project Structure
filas_folder/
├──public
├──tmp
│ └──restart.txt
├──app.py
└──passenger_wsgi.py
app.py looks like
from flask import Flask
app = Flask(__name__)
#app.route("/")
def main_():
return "flask is running"
#app.route("/user")
def main_2():
return "user is running"
if __name__ == "__main__": app.run()
Restart app from cpanel
passenger.py looks like
import imp
import os
import sys
sys.path.insert(0, os.path.dirname(__file__))
wsgi = imp.load_source('wsgi', 'app.py')
application = wsgi.app
when I open www.example.com
flask is running
But when I open www.example.com/user
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator at webmaster#example.com to inform them of the time this error occurred, and the actions you performed just before this error.
More information about this error may be available in the server error log.
Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request.
My system has cloudlinux and uses apache server. This is not the first deployment. Many wordpress and static websites are running on the server.
I opened apache logs at /usr/local/apache/logs/error_log
I get the error "Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace., referer" http://example.com/user"
Add the following to the top of your .htaccess file:
RewriteEngine on
RewriteRule ^http://%{HTTP_HOST}%{REQUEST_URI} [END,NE]
Got this info from: https://stackoverflow.com/a/63971427/10122266
I am getting an error when trying to secure a Flask website with Certbot. I do not receive the following error on a normal site but when running Certbot on a Flask sudo certbot --apache
site I get:
Failed authorization procedure. example.com (http-01): urn:ietf:params:acme:error:unauthorized :: The client lacks sufficient authorization :: Invalid response from http://example.com/.well-known/acme-challenge/j7Q5mIX-Cmk9wevuSQxxfBV5KBBgyQuoG9HXUEbU2Gw:
I am running Apache on Ubuntu 18.04 and Python2.7
I setup a vagrant VM with django installed, and setup the port forwarding like this:
config.vm.network "forwarded_port", guest: 8000, host: 8001
So in the guest machine I run the django server like this:
python manage.py 0.0.0.0:8000
And the server starts up and tells me its running on http://127.0.0.1:8000
When I open up firefox on the host machine and point it to http://127.0.0.1:8001, it automatically redirects to https://127.0.0.1:8001 and I get this error:
An error occurred during a connection to 127.0.0.1:8001. SSL received
a record that exceeded the maximum permissible length. Error code:
SSL_ERROR_RX_RECORD_TOO_LONG
Nowhere in the settings.py file does it force https. I don't know whats causing it to redirect to https, or how I can go about figuring that out. Is this a problem with virtualbox, vagrant or django?
These error messages appear in the terminal where I run the server:
[06/Jan/2017 05:17:22] code 400, message Bad request syntax ('\x16\x03\x01\x00¥\x01\x00\x00¡\x03\x03d')
You're accessing the development server over HTTPS, but it only supports HTTP.
[06/Jan/2017 05:17:31] code 400, message Bad HTTP/0.9 request type ("\x16\x03\x01\x00¥\x01\x00\x00¡\x03\x03É\x8aVY#¦Û2\x
9c'\x1a5n¬òðÿ𪪮pÛ%å\x15#8jÕQé\x00\x00")
You're accessing the development server over HTTPS, but it only supports HTTP.
So the question is why is my browser trying to access the server over HTTPS?
You need to add this in your settings.py file :
SECURE_SSL_REDIRECT = False
For further reading, Read from here.
anyone using django_auth_ldap against an active directory server
I am trying to set up auth through django_auth_ldap and am having an issue.
if i run my auth from the django interactive shell the auth works fine.
example from the shell
>>> from django.contrib.auth import authenticate
>>> authenticate(username='#############',password='*************')
search_s('ou=People, o=hp.com', 2, '(uid=%(user)s)') returned 1 objects: uid=###########,ou=people,o=hp.com
Populating Django ###########
Django user ########## does not have a profile to populate
<User:########## >
but the same code from within a view in the app fails with
Caught LDAPError while authenticating ##########: SERVER_DOWN({'desc': "Can't contact LDAP server"},)
I figured it out. I decided I would set up remote debugging so that I could step through the process and see where it was failing in that process I found that the httpd process was being prevented (by selinux) from making a network connection back to my eclipse IDE fixing this fixed the app. I think selinux was preventing the app from connecting to the ldap server. When I got my debug environment all worked out and stepped through it all worked fine !
the command to allow httpd to make a network connection
as root
setsebool -P httpd_can_network_connect 1