Issue
I am using this tutorial
Background: I am using flask
post API to activate selenium to extract information and send them to website. This work perfectly when I don't apply nginx
configuration and just run the below code on server (as in local machine using port 5000).
Problem: when I try to deploy the flask application by mean of nginx
and uwsgi
and following the indication of this tutorial I get an internal server error whenever I do the POST request http://my_domain/find_data
.
Below I'll show part of my code.
from app import app
if __name__ == "__main__":
app.run()
The app.py
code (it manages the POST request http://my_domain/find_data
) is:
from flask import Flask, send_from_directory, stream_with_context, request, Response
from flask_restful import Api, Resource, reqparse
from flask_cors import CORS #comment this on deployment
from data_finder import DataFinder
from time import sleep
from contextlib import closing
import requests
import json
from flask import jsonify
from flask import jsonify, make_response
app = Flask(__name__, static_url_path='', static_folder='frontend/build')
CORS(app) #comment this on deployment
api = Api(app)
@app.route("/")
def hello():
return send_from_directory(app.static_folder, 'index.html')
@app.route("/find_data", methods = ['POST'])
def FindData():
person = request.get_json()['person']
print(person)
results = {}
data_finder = DataFinder()
results['Spokeo'] = list(data_finder.get_spokeo_data(person))
results['White Pages'] = list(data_finder.get_whitepages_data(person).keys())
return make_response(jsonify(results), 200)
My project.ini
file is this:
[uwsgi]
module = wsgi:app
master = true
processes = 5
socket = myproject.sock
chmod-socket = 660
vacuum = true
die-on-term = true
Lastly my nginx
configuration file is
server {
listen 80;
server_name my_domain www.my_domain;
location / {
include uwsgi_params;
uwsgi_pass unix:/home/help/data-finder/privaseame_web_app/myproject.sock;
uwsgi_read_timeout 900;
proxy_read_timeout 1800;
proxy_connect_timeout 1800;
proxy_send_timeout 1800;
send_timeout 1800;
}
client_header_timeout 900s;
client_body_timeout 900s;
keepalive_timeout 900s;
send_timeout 900s;
}
I am using digital ocean linux droplet Ubuntu 18-0.4 LTS Running python3.10.6 I used below stack overflow links but information on them is not sufficient
If I miss any information please let me know I will provide.
Thanks for your insight
Solution
I have followed the same tutorial.
From your answer are not clear names of the different files that you have created. I suggest you to follow exactly the tutorial in particular the names schema that it suggests. I try to give you some advices.
- The name of your file:
from app import app
if __name__ == "__main__":
app.run()
must be uwsgi.py
because in the file project.ini
you use the option:
[uwsgi]
module = wsgi:app
- The name of your file:
from flask import Flask, send_from_directory, stream_with_context, request, Response
from flask_restful import Api, Resource, reqparse
from flask_cors import CORS #comment this on deployment
from data_finder import DataFinder
from time import sleep
from contextlib import closing
import requests
import json
from flask import jsonify
from flask import jsonify, make_response
app = Flask(__name__, static_url_path='', static_folder='frontend/build')
CORS(app) #comment this on deployment
api = Api(app)
@app.route("/")
def hello():
return send_from_directory(app.static_folder, 'index.html')
@app.route("/find_data", methods = ['POST'])
def FindData():
person = request.get_json()['person']
print(person)
results = {}
data_finder = DataFinder()
results['Spokeo'] = list(data_finder.get_spokeo_data(person))
results['White Pages'] = list(data_finder.get_whitepages_data(person).keys())
return make_response(jsonify(results), 200)
must be app.py
because in the file uwsgi.py
you use the instruction:
from app import app
- Make sure you have created the Unix socket file
myproject.sock
in the same folder that containsapp.py
anduwsgi.py
. In fact in the fileproject.ini
you used the option:
socket = myproject.sock
To create this socket and give it the right permission you can use the following Python script:
import socket as s
import pwd
import grp
import os
path = './myproject.sock'
# create the `Unix Domain Socket`
sock = s.socket(s.AF_UNIX)
sock.bind(path)
# set user and group for the Unix Domain Socket created (www is the same user of NGINX)
uid = pwd.getpwnam("www").pw_uid
gid = grp.getgrnam("www").gr_gid
os.chown(path, uid, gid)
os.chmod(path, 0o660)
Save previous program in the folder where you have to create the Unix Domain Socket (see the instruction path = './myproject.sock'
).
May be it exist other way to create a Unix Socket Domain file, but I used the previous Python program in my flask/uwsgi application.
I hope that these advises are useful for you.
Answered By - frankfalse Answer Checked By - Willingham (WPSolving Volunteer)