How to create a Server for a Flask application on DigitalOcean Droplet

  1. git clone your repo from github
   git clone [webapp github url]
  1. set a virtualenv for this app:
   virtualenv -p python3 myenv
  1. create a file in your flask app root folder with the following lines
from app import app

if __name__ == "__main__":
  1. remove debug mode from
if __name__ == "__main__":
  1. run python to check the configuration
  1. Dump your db from your previous server ( PostgreSQL )
pg_dump -d database_name -U username -h hostname  > filename.dump
  1. deactivate the virtualenv
  1. create a service with the file : /etc/systemd/system/projectname.service

Description=Gunicorn instance to serve myproject

ExecStart=/home/maru/myproject/myenv/bin/gunicorn --workers 4 --bind unix:myproject.sock -m 007 wsgi:app

  1. Start myproject
sudo systemctl start myproject
  1. Enable myproject
sudo systemctl enable myproject

You will see something like this:

Created symlink /etc/systemd/system/ → /etc/systemd/system/myproject.service.
  1. check status:
sudo systemctl status myproject
  1. Create the file /etc/nginx/sites-available/myproject
server {
    listen 80;
     location / {
        include proxy_params;
        proxy_pass http://unix:/home/username/myproject/myproject.sock;
  1. Create a link like this:
sudo ln -s /etc/nginx/sites-available/myproject /etc/nginx/sites-enabled
  1. check nginx with:
sudo nginx -t

If everything is OK the output looks like this:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
  1. restart nginx with:
sudo systemctl restart nginx
  1. Create a new subdomain in digitalocean droplet

  2. Create directory:

sudo mkdir -p /var/www/
  1. Set permissons for that:
sudo chown -R $USER:$USER /var/www/
  1. permission for the whole directory:
sudo chmod -R 755 /var/www
  1. Create the DB on your new local server

  2. If DB exists => dump and restore.

    Else => in the terminal run python and after import your app and run code:

$ python

>>>from app import db

© Copyright 2019