Python

How to Post your Flask App in Ubuntu

How to Post your Flask App in Ubuntu

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 another Cloud)

These are the steps:-

DigitalOcean Referral Badge
  • Sign up and Enter your Card Details
  • 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 the page.
  • choose any image you want here I am gonna choose Ubunutu 18.04
  • choose the plan that suits your needs I am gonna go with the 10$ plan
  • Your Droplet is now created
  • now i am gonna skip the ssh steps please mention this for that step https://www.digitalocean.com/docs/droplets/how-to/connect-with-ssh/

Creating a basic flask app

  • Run this code pip install gunicorn flask to install the necessary dependencies

  • Now we can test the app by running gunicorn --bind 0.0.0.0:8000 wsgi

The Gurnicorn Configuration

  • SSH onto your server
  • Run the following command sudo nano /etc/systemd/system/myproject.service to create a new system service for your project
  • Paste the following into the file
[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
  • Save and exit out of the editor
  • Now Run sudo systemctl start myproject to start your project and sudo systemctl enable myproject to restart the project always
  • To check the status of your project run => sudo systemctl enable myproject

Configuring Nginx Proxy

  • Run sudo apt update to update the packages in your server and sudo apt-get install nginx -y to install Nginx server
  • Now create an Nginx configuration file for your project
  • Paste the following and feel free the customize the file according to your needs
server {
    listen 80;
    server_name your_domain www.your_domain;

    location / {
        include proxy_params;
        proxy_pass http://unix:/home/sammy/myproject/myproject.sock;
    }
}
  • Now link your project file to the sites-enabled folder by running sudo ln -s /etc/nginx/sites-available/myproject /etc/nginx/sites-enabled
  • Run sudo nginx -t to test your Nginx configuration
  • Time to restart Nginx configuration to apply our changes => sudo systemctl restart nginx
  • If you have UFW firewall in ubuntu then run this command to allow Nginx => sudo ufw allow 'Nginx Full'

Time to secure your app with an SSL

  • Run sudo add-apt-repository ppa:certbot/certbot && sudo apt install python-certbot-nginx -y to install certbot
  • In order to obtain a free SSL certificate run sudo certbot --nginx -d your_domain -d www.your_domain --agree-tos -m your_name@yourdomain.com
  • Run sudo ufw delete allow 'Nginx HTTP' to disable HTTP in the firewall
  • Now visit https://server_ip or https://www.domain.com

Share this post

About the author

Leave a Reply