DEV Community

Raghav Mrituanjaya
Raghav Mrituanjaya

Posted on • Updated on • Originally published at blog.gogamic.com

How to Post your Flask App in Ubuntu

Hi guys so today in this post we will be Posting a Flask app to a ubuntu server

Creating a server in Digital Ocean

So we are gonna utilize the 100$ free credit which is given for new users in Digital Ocean (note: This step is optional if you have a server in other Cloud)

These are the Steps:-

  1. Click on this link https://m.do.co/c/63a3ac211390 to go to Digital Ocean signup page
  2. Sign up and Enter your Card details Digital-ocean-droplet
  3. In the create menu, click Droplets to open the Droplet create page. If you don't have any Droplets, the Resources tab displays a large, blue Get Started with a Droplet button, which takes you to the same Droplet create page. Digital-ocean-os
  4. choose any image you want here i am gonna choose Ubunutu 18.04 Digital-ocean-plans
  5. choose the plan that susits your needs i am goona go with the 10$ plan

  6. Your Droplet is now created

  7. now i am gonna skip the ssh steps please metion this for that step https://www.digitalocean.com/docs/droplets/how-to/connect-with-ssh/

Creating a basic flask app

  1. run this code

    pip install gunicorn flask

  2. This is a minimal flask app and i am gonna name is wsgi.py you can name it anything

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
  return 'Hello, World!'

if __name__ == '__main__': 
  app.run() 
Enter fullscreen mode Exit fullscreen mode
  1. now we can test it by running gunicorn --bind 0.0.0.0:8000 wsgi

The Gurnicorn Configuration

  1. ssh into your sever

  2. run the following command

    sudo nano /etc/systemd/system/myproject.service

  3. There popups a editor and write the following commands

[Unit]
Description=Gunicorn instance to serve myproject
After=network.target

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

[Install]
WantedBy=multi-user.target 
Enter fullscreen mode Exit fullscreen mode
  1. Them do a crtl+c to save it and crtl+x to exit

  2. Now Run this commands

sudo systemctl start myproject
sudo systemctl enable myproject
Enter fullscreen mode Exit fullscreen mode
  1. Now you can check the status by running this command
Enter fullscreen mode Exit fullscreen mode

Configuring Nginx to proxy the requsets

  1. Run this command
sudo apt update 
sudo apt-get install nginx
Enter fullscreen mode Exit fullscreen mode

2.after instaliing nginx go to your project.conf by using this command

sudo nano /etc/nginx/sites-available/myproject
Enter fullscreen mode Exit fullscreen mode
  1. then type this code
server {
    listen 80;
    server_name your_domain www.your_domain;

    location / {
        include proxy_params;
        proxy_pass http://unix:/home/sammy/myproject/myproject.sock;
    }
}

Enter fullscreen mode Exit fullscreen mode
  1. then run this code

    sudo ln -s /etc/nginx/sites-available/myproject /etc/nginx/sites-enabled

  2. Run this code to Test your Configuration

    sudo nginx -t

  3. Then run this to restart nginx

    sudo systemctl restart nginx

  4. If you have enabled ufw then run this or skip this

sudo ufw allow 'Nginx Full'  
Enter fullscreen mode Exit fullscreen mode
  1. visit http://yourip

securing your app with an ssl

  1. Run these commands in order to download certbot
sudo add-apt-repository ppa:certbot/certbot
sudo apt install python-certbot-nginx
Enter fullscreen mode Exit fullscreen mode
  1. Run this command to get an Certificate

    sudo certbot --nginx -d your_domain -d www.your_domain --agree-tos -m your_name@yourdomain.com

  2. This will be the output

Output
Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.
-------------------------------------------------------------------------------
1: No redirect - Make no further changes to the webserver configuration.
2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for
new sites, or if you're confident your site works on HTTPS. You can undo this
change by editing your web server's configuration.
-------------------------------------------------------------------------------
Select the appropriate number [1-2] then [enter] (press 'c' to cancel):
Enter fullscreen mode Exit fullscreen mode
  1. select your choice and hit enter
  1. You will get a out put like this
IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/your_domain/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/your_domain/privkey.pem
   Your cert will expire on 2018-07-23. To obtain a new or tweaked
   version of this certificate in the future, simply run certbot again
   with the "certonly" option. To non-interactively renew *all* of
   your certificates, run "certbot renew"
 - Your account credentials have been saved in your Certbot
   configuration directory at /etc/letsencrypt. You should make a
   secure backup of this folder now. This configuration directory will
   also contain certificates and private keys obtained by Certbot so
   making regular backups of this folder is ideal.
 - If you like Certbot, please consider supporting our work by:

   Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
   Donating to EFF:                    https://eff.org/donate-le
Enter fullscreen mode Exit fullscreen mode
  1. now run this command to diable http

    sudo ufw delete allow 'Nginx HTTP'

  2. now got to

    
    https://your ip or Your domain
    

Support me on Pateron => https://www.patreon.com/gogamic

Top comments (1)

Collapse
 
peter279k profile image
peter279k

I also suggest that it should set the network-online.target for Wants and After value setting on myproject.service.

The network-online.target is a target that actively waits until the nework is "up" and you can look at more details on Concepts in systemd section via this reference link :).

And it would be better to change into:

After=network-online.target
Wants=network-online.target