Start python flask webserver automatically after booting the system and keep it on till the end

18,941

Solution 1

I'd like to suggest using supervisor, the documentation is here


for a very simple demo purpose, after you installed it and finish the set up, touch a new a file like this:

[program:flask_app]                                                                  
command = python main.py                                      
directory = /dir/to/your/app                            
autostart = true                                                                
autorestart = true

then

$ sudo supervisorctl update

Now, you should be good to go. The flask app will start every time after you boot you machine.(note: distribution package has already integrated into the service management infrastructure, if you're using others, see here)

to check whether you app is running:

$ sudo supervisorctl status

For production, you can use nginx+uwsgi+supervisor. The flask deployment documentation is here

Solution 2

One well documented solution is to use Gunicorn and Nginx server:

  1. Install Components and setup a Python virtualenv with dependencies
  2. Create the wsgi.py file :
from myproject import application

    if __name__ == "__main__":

         application.run()

That will be handled by Gunicorn :

gunicorn --bind 0.0.0.0:8000 wsgi
  1. Configure Gunicorn with setting up a systemd config file: /etc/systemd/system/myproject.service :
 [Unit]
    Description=Gunicorn instance to serve myproject
    After=network.target

 [Service]
    User=sammy
    Group=www-data
    WorkingDirectory=/home/sammy/myproject
    Environment="PATH=/home/sammy/myproject/myprojectenv/bin"
    ExecStart=/home/sammy/myproject/myprojectenv/bin/gunicorn 
    --workers 3 --bind unix:myproject.sock -m 007 wsgi:app

[Install]
    WantedBy=multi-user.target
  1. Start the Gunicorn service at boot :
    sudo systemctl start myproject
    sudo systemctl enable myproject
Share:
18,941
Sina Sh
Author by

Sina Sh

Updated on June 08, 2022

Comments

  • Sina Sh
    Sina Sh almost 2 years

    I'm using flask as a webserver for my UI (it's a simple web interface which controls the recording using gstreamer on ubuntu from a webcam and a framegrabber simultaneously / kinda simple player)

    Every time I need to run the command "python main.py" to run the server from command prompt manually.

    I've tried the init.d solution or even writing a simple shell script and launching it every time after rebooting the system on start up but it fails to keep the server up and running till the end (just invokes the server and terminates it I guess)

    is there any solution that could help me to start the webserver every time after booting the system on startup and keep it on and running?

    I'd like to configure my system to boot directly into the browser so don't wanna have any need for more actions by the user.

    Any Kind of suggestion/help is appreciated.

  • Dan Safee
    Dan Safee over 8 years
    I also have to recommend supervisor as a great solution for managing any long running process. I've used it many times in production and have been very happy.
  • Sina Sh
    Sina Sh over 8 years
    Well, I did this solution and based on status of it I can say that it's successful on keeping the server running! but I faced another problem, it seems that supervisor runs the server as root (I want it to invoke it as the current user) and because of that the recording process fails. I already put the config file into /etc/supervisor/conf.d (I had to do it via sudo) does this make the problem? and one more thing, one of the options on UI is to show a preview of the Webcam with gstreamer and this one also failes with this error: could not initialise X output
  • lord63. j
    lord63. j over 8 years
    @SinaSh 1. no problem, I also put it there. I run the supervisor with root permission and it's fine. Why do you want to invoke it with current user? 2. sorry, I have no code experience about webcam before. You may google it or ask another question with more detailed info.
  • Sina Sh
    Sina Sh over 8 years
    Well it's mostly because the final product would be unaccessible for new user at the end since the user need to convert and transfer the final products to somewhere else and because of this I had to asign him the root privileges (which I don't) Anyway thanks a lot for your help and appreciate if you have something in your mind as a hint about running it as normal user as well :)
  • ddtraveller
    ddtraveller over 7 years
    This config file example may help get this running. You need a supervisord section for this to really work. gist.github.com/didip/802561
  • Tomas P
    Tomas P over 5 years
    It doesn't support python 3. :(
  • NMO
    NMO almost 4 years
    How must the .conf file look like?
  • Gerard Rozsavolgyi
    Gerard Rozsavolgyi almost 4 years
    I've updated my answer with systemd conf, the link gives you more details
  • NMO
    NMO almost 4 years
    I like your solution more than the accepted answer because it is a general way of making services.