Synology webstation simple "hello world" quick start with python, flask and uwsgi
Asked Answered
E

5

5

I noticed that Synology's native webstation which uses uwsgi framework, has recently added support for python script. I was wondering if someone can help me figure out a simple hello world example. I am unclear about what to put in the uwsgi file. I followed the python-flask quickstart example on uwsgi documentation page: uwsgi python-flask quickstart

On webstaion>service portal : I setup a virtual host with nginx listening on port 8080: webstation virtual host profile

In this profile I then setup the appropriate folder containing the python script, callable entry function and uwsgi file: enter image description here

The "main.py" python script residing in this folder is the example in the quickstart page:

from flask import Flask

app = Flask(__name__)

@app.route('/')
def index():
    return "<span style='color:red'>I am app 1</span>"

I took the command-line parameters in that example and made the uwsgi.ini file and placed it in the same folder:

[uwsgi]
socket = 127.0.0.1:8080
wsgi-file = main.py 
callable = app 
processes = 4
threads = 2 
stats = 127.0.0.1:9191

In webstation's script>python page : I setup a "python-flask profile" and added the required flask module: enter image description here

enter image description here

I added this "python-flask profile" in the virtual host's python profile to make sure all modules are accessible to the python script.

However, when I browse to port 8080 I get error code 500 on my browser;

Internal Server Error

enter image description here

I would greatly appreciate if someone could try this out on webstation to figure out the correct setup. It seems that webstation makes deploying python based web-apps quite easy so solving this issue would greatly benefit newbies like me who are looking for a quick and easy deployment method on their Synology NAS.

Thanks in advance!

Exarate answered 13/6, 2022 at 1:59 Comment(1)
I was also on this chase.... and find community.synology.com/enu/forum/1/post/153851 where this question is also posted.... did you find a way ?Janina
P
5

It seems like you should pick your *.py file in this field

Field

Piave answered 19/7, 2022 at 12:40 Comment(1)
This solution worked for me : If your flask app is called "testapp.py" then enter "testapp.py" in the "WSGI file" field.Holyhead
G
5

I do not have the full answer, but I was able to make it work. Here is my setup.

At the Web Station application: enter image description here enter image description here enter image description here enter image description here

At my router: enter image description here

What I can see on my web browser. Tested with and without VPN (to simulate being outside my local network). Works with HTTP and HTTPS. enter image description here

My Python code at main.py (same as OP) is:

from flask import Flask

app = Flask(__name__)

@app.route('/')
def index():
    return "<span style='color:green'>I am app 1</span>"

if __name__ == '__main__':
    app.run()

A few limitations:

  • I am not able to make it work with another port. For example, 8080
  • The "Hostname" must be set
  • Only work with ports 80/443
  • Clear your browser cache
  • I am not able to find the server log
  • If the code crashes, it needs to Disable and Enable the Virtual Host
Gilroy answered 18/8, 2022 at 18:31 Comment(6)
I was looking for flask and synology and this totaly helped me out whit the installation process..... i got 1 question... do you know how to set the flask in debug mode ? $ export FLASK_ENV=developmentJanina
@hexedecimal, I haven't tried. I would try to login to the NAS using SSH. Then, using an administrator account, try to set the environment variable. Maybe you need to sudo the command.Gilroy
@hexedecimal, alternatively, you could try: #5971812. Or my current personal preference, use a Docker with Python installed.Gilroy
do you mean the python-dotenv answer in the link? via ssh i don't know how to set this export at all... or how to connect to the loaded python to set the development... I'm a little bit noobish around this and thinking about this wrong... was glad whit this step by step screenshots :).... a docker option --> i dont know how to connect this via the webserver... again was glad i got this working :DJanina
@hexedecimal, did you try: import os os.environ["FLASK_ENV"] = "development"? Maybe it is the easiest way to make it work. Other option, usgin if __name__ == '__main__': app.run(debug=True) (from https://mcmap.net/q/383907/-set-flask-environment-to-development-mode-as-default).Gilroy
You can also set configuration parameters, using a .cfg file. Check: flask.palletsprojects.com/en/1.1.x/configGilroy
C
3

Server logs

I found that (running DSM 7.1 and Python 3.9) Python errors on my machine appear in

python3.9-uwsgi.log

in

/volume1/@appdata/Python3.9/logs

Chromosome answered 11/5, 2023 at 11:35 Comment(0)
L
0

For flask, I had to start on 0.0.0.0 to ensure the app was accessible from the LAN/WAN. If started on 127.0.0.1 or localhost, only the computer itself could access the app. It may be a first hint, for you? I manage to made my app run and accessible from the web but I still have to start it manually (ssh > command line) so it closed every time I closed the synology user session. On this part you seems far ahead of me.

Lubalubba answered 9/7, 2022 at 7:56 Comment(2)
Could you provide more feedback on what do you mean about port addresses 0.0.0.0 and 127.0.0.1? Where those addresses should be set?Gilroy
@Gilroy here are to command line that explain the 0.0.0.0 starting point : uwsgi --http 0.0.0.0:xxxx --master --wsgi-file run.py --callable app --threads 2 flask run -h 0.0.0.0 -p portNb When I started with those commands, the server was accessible on my local IP and on localhost. It was not otherwise. Only local IP or only localhost/127.0.0.1 .Lubalubba
D
0

enter image description here

as already pointed out from @Jeremie you have to enter here (or navigate with "Browse...") to your "main.py" ! Ensure, that you have seted up the Root-Path corectly and the "main.py" is exactly at that location!

@ juanbretti: I get it also run without "Hostname"-Config so with only use a HTTP-Port and the same Hostname/IP as SynologyNAS

BR

Department answered 7/10, 2022 at 15:50 Comment(2)
update: and delete any "app.run()" statement in main.py will help Momently I not figured out how to run in debug mode....Department
I haven't tried without "hostname" defined. Thank you @Department for letting me know.Gilroy

© 2022 - 2024 — McMap. All rights reserved.