How to Post your Flask App in Ubuntu
Published: 2020-06-14 16:12:27

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](https://www.digitalocean.com/docs/images/create.89738c267dbfe7dc338abe8f0f5d2d0976a9a3f8b39eb65ca983cc4bc95ff728.png)
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](https://www.digitalocean.com/docs/images/droplets/create/choose-image.6a83962611e90c5b3066c908cad05bac2a2a2d0a65a4accd4403a31a021b3519.png)
4. choose any image you want here i am gonna choose Ubunutu 18.04
![Digital-ocean-plans](https://www.digitalocean.com/docs/images/droplets/create/choose-image.6a83962611e90c5b3066c908cad05bac2a2a2d0a65a4accd4403a31a021b3519.png)
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() 
```
3. 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 
```
4. Them do a crtl+c to save it and crtl+x to exit

5. Now Run this commands 
```
sudo systemctl start myproject
sudo systemctl enable myproject
```
6. Now you can check the status by running this command
```
```
# Configuring Nginx to proxy the requsets

1. Run this command 
```
sudo apt update 
sudo apt-get install nginx
```
2.after instaliing nginx go to your project.conf by using this command
```
sudo nano /etc/nginx/sites-available/myproject
```

3. 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;
    }
}

```

4. then run this code ```sudo ln -s /etc/nginx/sites-available/myproject /etc/nginx/sites-enabled```

5. Run this code to Test your Configuration ```sudo nginx -t```

6. Then run this to restart nginx ```sudo systemctl restart nginx```

7. If you have enabled ufw then run this or skip this 
```
sudo ufw allow 'Nginx Full'  
```

8.  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
```
2. Run this command to get an Certificate ```sudo certbot --nginx -d your_domain -d www.your_domain --agree-tos -m your_name@yourdomain.com```

3. 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):
```
4. select your choice and hit ```enter```
5. 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
```
7. now run this command to diable http ```sudo ufw delete allow 'Nginx HTTP'```
8. now got to ```https://your ip or Your domain


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