For a school project, I need to run a Python API on a server. I created a droplet on Digital Ocean and installed Ubuntu. After that, I followed this tutorial to create an environment with nginx, Flask and WSGI.
Everything works perfect, the "Hello There!" message is showing and I can change the text of it and see the change on screen. So no problems there.
The file structure at this moment is:
ProjectFolder
|
|── api.py (The file with "Hello There!")
|── api.ini
|── api.sock
|── wsgi.py
|── projectenv
The problem is that our API code is stored in a BitBucket repository. I have cloned the repository so that the file structure becomes:
ProjectFolder
|
|── api.py (The file with "Hello There!")
|── api.ini
|── api.sock
|── wsgi.py
|── projectenv/
|── bitbucketrepo/
|── api.py
Of course, because of the configurations in the other files, the server still loads the api.py with the "Hello There!" text. I have changed the wsgi.py and the file in the /etc/nginx/sites-enabled/ and the /etc/nginx/sites-available/ directory, so that everything directs to the api.py in the bitbucketrepo folder, but that didn't work. I also tried to move all the files in the root folder to the bitbucketrepo folder and changed the above mentioned files accordingly, but that also didn't work.
I also don't understand why the api.py with the "Hello There!" text automatically runs when I go to my server in a browser, but when I literally copy and paste my API code in that file, it doesn't work.
The code in the files are as follows:
ProjectFolder/apy.py:
from flask import Flask
application = Flask(__name__)
@application.route("/")
def hello():
return "<h1 style='color:blue'>Hello There!</h1>"
if __name__ == "__main__":
application.run(host='0.0.0.0')
Projectfolder/api.ini:
[uwsgi]
module = wsgi
master = true
processes = 5
socket = api.sock
chmod-socket = 660
vacuum = true
die-on-term = true
ProjectFolder/wsgi.py:
from api import application
if __name__ == "__main__":
application.run()
ProjectFolder/bitbucketrepo/api.py:
#imports
application = Flask(__name__)
api = Api(application)
#API code
if __name__ == '__main__':
application.run(debug=True, host='0.0.0.0')
So my problem here is that the API in the bitbucket folder only runs when I run the command python api.py. When I do that, the API is accessible on port 5000. So someone can only access my API when I run that terminal command.
What I want is that the API in the bitbucket folder always runs, like the api.py in the ProjectFolder.
I have the feeling that I'm very close to achieving this, but just missing a certain step in the process. I have no experience with servers and servermanagement, so this is quite new to me.
Does anyone know which steps to take to achiev this? My apologies if there is a similar question on StackOverflow. It was not my intention to post a duplicate question.
Thanks in advance!
application. Also what isApi()where is that class defined?Internal Server Error. Nothing more, nothing less. I also checked the error log of nginx, but it is empty. And the Python file runs perfectly when I usepython api.py