Skip to content

Instantly share code, notes, and snippets.

@JackFurby
Last active December 5, 2024 23:49
Show Gist options
  • Save JackFurby/02ee263354fec56b447cdd53249c048b to your computer and use it in GitHub Desktop.
Save JackFurby/02ee263354fec56b447cdd53249c048b to your computer and use it in GitHub Desktop.
Deploy a Flask app in Virtualmin

How to setup a Flask app in Virtualmin

before I start, for full disclosure this is not really a guide on how to set up a Flask app in Virtualmin. It will achive it but as you may know Virtual min does not support Python apps. For this reason it will be in two parts. Part 1 with be in Virtualmin setting up the space for the app on a server and part 2 will be setting up the app in Apachie.

Virtualmin

  1. Create virtual server
  2. Add Flask app files into virtual server public_html folder (e.g. /home/virtual_server/public_html)

Apache

loged in as the user made with the virtual server. You will need to add this user to the sudo group first. Failing this you can also use the root user.

  1. navigate to virtual server (e.g. /home/virtual_server/public_html)
  2. install pip on server - sudo apt-get install python3-pip
  3. install virtualenv - sudo pip3 install virtualenv
  4. create an enviroment for the app - sudo virtualenv env_name
  5. activate enviroment - source env_name/bin/activate
  6. install Flask and other requirements - pip3 install Flask
  7. verify app starts - python3 run.py
  8. stop virtualenv - deactivate
  9. enter conf file - sudo nano conf_file_location (e.g. /etc/apache2/sites-available/domain_name.conf)
  10. Add WSGI config into file -
<VirtualHost 000.000.000.00:80>
WSGIDaemonProcess user=user group=group threads=5 python-path=/home/star/public_html/star/lib/python3.5/site-pa
WSGIScriptAlias /appname /home/virtual_server/public_html/app.wsgi
...
<Directory /home/virtual_server/public_html>
WSGIApplicationGroup %{GLOBAL}
WSGIProcessGroup appname
...
</Directory>
...
</VirtualHost>
  • appname, group and user has to be different for every Flask app you run on the server
  1. enable site (probably already done) - sudo a2ensite domain_name
  2. make .wsgi file (replaces run.py)
#!/usr/bin/python
import sys
sys.path.insert(0, "/home/virtual_server/public_html")

from blueprints import app as application

if __name__ == '__main__':
	app.run()
  1. restart apache - service apache2 restart

It should now be running

I do not class myself as an expert in this so is you have any improvements please let me know and I will add them into this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment