0

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!

4
  • What does "didn't work" mean? What is the error you get? Commented Oct 30, 2015 at 12:51
  • You are running the application is WSGI!... It is just simply the Flask application that it needs called application. Also what is Api() where is that class defined? Commented Oct 30, 2015 at 13:14
  • @dirn the error I get is 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 use python api.py Commented Oct 30, 2015 at 16:48
  • You either need to check the uWSGI log or run your application in debug mode. Commented Oct 30, 2015 at 16:49

1 Answer 1

1

Have you tried using:

from bitbucketrepo.api import application

If you're using python2, make sure you have a __init__.py file in the bitbucketrepo folder.

Sign up to request clarification or add additional context in comments.

5 Comments

Very stupid that I didn't think about that. But unfortunately, it's giving the same error message on screen: Internal Server Error. I have checked the error log of nginx but it isn't showing anything. I can also successfully run python api.py in the bitbucketrepo, so the code inside works. I didn't have an init file. Just made one, so thanks for that. All the above testen was done after i created it.
Forget what I just said. I had a simple typo. Your solution worked and my API in the bitbucketrepo is finally showing on the homepage. Thank you very much!
The only thing to mention is that init.py didn't work (got the same Internal Server Error), but __init__.py did work. What is the difference between these two? One is for python2, the other for 3? Apologies for the comment spam, but I can't edit them.
@KevinKromjong I think it's a typo. He just forget to escape the _ in markdown. And I can't edit it because it's less than 6 chars.
Correct, forgot to escape __. @KevinKromjong, the correct package identifier for Python 2 is __init__.py For Python 3, there's no need for an __init__.py file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.