{"id":2988,"date":"2024-02-15T21:12:51","date_gmt":"2024-02-03T20:54:33","guid":{"rendered":""},"modified":"2025-02-02T00:46:19","modified_gmt":"2025-02-01T23:46:19","slug":"how-to-install-flask-with-nginx-and-gunicorn-on-ubuntu-22-04","status":"publish","type":"post","link":"https:\/\/netcloud24.com\/knowledgebase\/how-to-install-flask-with-nginx-and-gunicorn-on-ubuntu-22-04\/","title":{"rendered":"Linux VPS &#038; VPS Windows Setup Guide | NetCloud24 Flask with Nginx and Gunicorn on Ubuntu 22.04"},"content":{"rendered":"<p>\u00a0<\/p>\n<\/p>\n<header>\n<h1>\u00a0<\/h1>\n<\/header>\n<article>\n<p>Flask is a lightweight and flexible Python web framework, and it\u2019s often used with Gunicorn (a Python WSGI HTTP server) and Nginx (a high-performance web server) for production deployments. This guide will walk you through installing Flask with Nginx and Gunicorn on Ubuntu 22.04. Whether you\u2019re hosting your Flask app locally or on a , this setup provides excellent scalability and performance.<\/p>\n<section>\n<h2>Step 1: Update Your System<\/h2>\n<p>Before starting, ensure your Ubuntu system is up to date. Run the following commands to update your package list and upgrade any existing packages:<\/p>\n<pre><code>sudo apt update &amp;&amp; sudo apt upgrade -y<\/code><\/pre>\n<p>Regular updates ensure that your server is secure and running smoothly, which is especially important for applications running on platforms like <a href=\"https:\/\/ie.netcloud24.com\">VPS Windows VPS Servers<\/a>.<\/p>\n<\/section>\n<section>\n<h2>Step 2: Install Python, Flask, and Virtual Environment<\/h2>\n<p>Install Python and the necessary tools for creating a virtual environment:<\/p>\n<pre><code>sudo apt install python3 python3-pip python3-venv -y<\/code><\/pre>\n<p>Next, create a project directory for your Flask app and navigate into it:<\/p>\n<pre><code>mkdir ~\/myflaskapp\r\ncd ~\/myflaskapp<\/code><\/pre>\n<p>Create a Python virtual environment to keep your project dependencies isolated:<\/p>\n<pre><code>python3 -m venv venv<\/code><\/pre>\n<p>Activate the virtual environment:<\/p>\n<pre><code>source venv\/bin\/activate<\/code><\/pre>\n<p>Install Flask within the virtual environment:<\/p>\n<pre><code>pip install Flask<\/code><\/pre>\n<\/section>\n<section>\n<h2>Step 3: Create a Simple Flask Application<\/h2>\n<p>Create a simple Flask app by creating a new Python file:<\/p>\n<pre><code>nano ~\/myflaskapp\/app.py<\/code><\/pre>\n<p>Add the following content to create a basic Flask app:<\/p>\n<pre><code>from flask import Flask\r\napp = Flask(__name__)\r\n\r\n@app.route('\/')\r\ndef hello():\r\n    return \"Hello, World!\"\r\n\r\nif __name__ == '__main__':\r\n    app.run(host='0.0.0.0')<\/code><\/pre>\n<p>Save and close the file.<\/p>\n<\/section>\n<section>\n<h2>Step 4: Install and Configure Gunicorn<\/h2>\n<p>Install Gunicorn inside your virtual environment:<\/p>\n<pre><code>pip install gunicorn<\/code><\/pre>\n<p>Now, run Gunicorn to serve your Flask application:<\/p>\n<pre><code>gunicorn --bind 0.0.0.0:8000 app:app<\/code><\/pre>\n<p>This will bind Gunicorn to port 8000 and run your Flask app. At this point, you can access your Flask app by navigating to <code>http:\/\/your_server_ip:8000<\/code> in your browser.<\/p>\n<\/section>\n<section>\n<h2>Step 5: Set Up Gunicorn as a Systemd Service<\/h2>\n<p>To run Gunicorn as a background service, create a systemd service file:<\/p>\n<pre><code>sudo nano \/etc\/systemd\/system\/myflaskapp.service<\/code><\/pre>\n<p>Add the following content, replacing <code>\/home\/your_username<\/code> with the correct path to your Flask app:<\/p>\n<pre><code>[Unit]\r\nDescription=Gunicorn instance to serve Flask app\r\nAfter=network.target\r\n\r\n[Service]\r\nUser=your_username\r\nGroup=www-data\r\nWorkingDirectory=\/home\/your_username\/myflaskapp\r\nEnvironment=\"PATH=\/home\/your_username\/myflaskapp\/venv\/bin\"\r\nExecStart=\/home\/your_username\/myflaskapp\/venv\/bin\/gunicorn --workers 3 --bind unix:myflaskapp.sock -m 007 app:app\r\n\r\n[Install]\r\nWantedBy=multi-user.target<\/code><\/pre>\n<p>Save and close the file, then reload systemd to apply the new service:<\/p>\n<pre><code>sudo systemctl daemon-reload<\/code><\/pre>\n<p>Start the Gunicorn service and enable it to start at boot:<\/p>\n<pre><code>sudo systemctl start myflaskapp\r\nsudo systemctl enable myflaskapp<\/code><\/pre>\n<\/section>\n<section>\n<h2>Step 6: Install and Configure Nginx<\/h2>\n<p>Install Nginx with the following command:<\/p>\n<pre><code>sudo apt install nginx -y<\/code><\/pre>\n<p>Create an Nginx server block for your Flask app:<\/p>\n<pre><code>sudo nano \/etc\/nginx\/sites-available\/myflaskapp<\/code><\/pre>\n<p>Add the following configuration:<\/p>\n<pre><code>server {\r\n    listen 80;\r\n    server_name your_domain_or_ip;\r\n\r\n    location \/ {\r\n        proxy_pass http:\/\/unix:\/home\/your_username\/myflaskapp\/myflaskapp.sock;\r\n        proxy_set_header Host $host;\r\n        proxy_set_header X-Real-IP $remote_addr;\r\n        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;\r\n        proxy_set_header X-Forwarded-Proto $scheme;\r\n    }\r\n\r\n    location \/static\/ {\r\n        alias \/home\/your_username\/myflaskapp\/static\/;\r\n    }\r\n}<\/code><\/pre>\n<p>Enable the Nginx configuration and restart the service:<\/p>\n<pre><code>sudo ln -s \/etc\/nginx\/sites-available\/myflaskapp \/etc\/nginx\/sites-enabled\/\r\nsudo systemctl restart nginx<\/code><\/pre>\n<\/section>\n<section>\n<h2>Step 7: Obtain an SSL Certificate with Let&#8217;s Encrypt (Optional)<\/h2>\n<p>To secure your Flask app with HTTPS, you can use Let&#8217;s Encrypt to obtain a free SSL certificate. First, install Certbot:<\/p>\n<pre><code>sudo apt install certbot python3-certbot-nginx -y<\/code><\/pre>\n<p>Then, run Certbot to obtain and configure SSL:<\/p>\n<pre><code>sudo certbot --nginx -d your_domain<\/code><\/pre>\n<p>Follow the prompts to set up SSL. Certbot will automatically update your Nginx configuration.<\/p>\n<\/section>\n<footer>\n<p>You have successfully installed Flask with Nginx and Gunicorn on Ubuntu 22.04. This setup provides a reliable and scalable production environment for your Flask application. For high-performance hosting solutions, consider using . They offer a range of hosting options, including <strong>windows virtual private servers<\/strong>, <strong>vps windows hosting<\/strong>, and <strong>windows virtual dedicated server hosting<\/strong>. Whether you need <strong>uk vps windows<\/strong> or <strong>windows vps italy<\/strong>, their hosting solutions provide the performance and scalability needed for deploying your Flask app.<\/p>\n<\/footer>\n<\/article>\n<div class=\"post-author-box\" style=\"border-top:1px solid #ddd;margin-top:20px;padding-top:15px;\">\n<p><strong>Author:<\/strong> \u0141ukasz Bodziony<\/p>\n<p><strong>Website:<\/strong> <a href=\"https:\/\/ca.netcloud24.com\" target=\"_blank\" rel=\"dofollow\">Windows VPS<\/a><\/p>\n<p><em>\u0141ukasz Bodziony is the CEO and founder of <a href=\"https:\/\/netcloud24.com\" target=\"_blank\" rel=\"dofollow\">NETCLOUD24<\/a>, a global VPS hosting brand proudly originating from Poland. With extensive experience in cloud computing, virtualization, and server management, he delivers high-performance <strong>Windows VPS<\/strong> and <strong>Remote Desktop Services (RDS)<\/strong> solutions to clients across Europe, North America, and beyond.<\/em><\/p>\n<p><em>His expertise covers a wide range of technologies, including <strong>Microsoft Azure<\/strong>, <strong>Proxmox VE<\/strong>, <strong>Amazon Web Services (AWS)<\/strong>, and numerous other virtualization and cloud platforms.<\/em><\/p>\n<p><em>Beyond running his hosting business, \u0141ukasz also provides <strong>professional paid server configuration and optimization services<\/strong> for companies and individuals. Outside of work, he is dedicated to caring for his children and building a secure future for them.<\/em><\/p>\n<p><em>If you are interested in working with him or need expert assistance with your hosting, cloud environment, or server setup, feel free to reach out via <a href=\"https:\/\/ca.netcloud24.com\" target=\"_blank\" rel=\"dofollow\">Windows VPS<\/a>.<\/em><\/p>\n<\/p><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u00a0 \u00a0 Flask is a lightweight and flexible Python web framework, and it\u2019s often used with Gunicorn (a Python WSGI HTTP server) and Nginx (a high-performance web server)\u2026<\/p>\n","protected":false},"author":1,"featured_media":3421,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_seopress_robots_primary_cat":"","_seopress_titles_title":"","_seopress_titles_desc":"","_seopress_robots_index":"","footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[],"tags":[14,12,11,23,20,21,22,17,7,8,6,10,18,19,15,24,16,5,13,9],"class_list":["post-2988","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","tag-cheapvps","tag-cloudvps","tag-hostingvps","tag-rds","tag-rdscal","tag-remotedesktop","tag-remotedesktopvps","tag-servervps","tag-ukvps","tag-virtualserver","tag-vpshosting","tag-vpsserver","tag-vpssolutions","tag-vpswindows","tag-vpswithwindows","tag-windowsrds","tag-windowsserver","tag-windowsvps","tag-windowsvpshosting","tag-windowsvpsuk"],"jetpack_publicize_connections":[],"_links":{"self":[{"href":"https:\/\/netcloud24.com\/knowledgebase\/wp-json\/wp\/v2\/posts\/2988","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/netcloud24.com\/knowledgebase\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/netcloud24.com\/knowledgebase\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/netcloud24.com\/knowledgebase\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/netcloud24.com\/knowledgebase\/wp-json\/wp\/v2\/comments?post=2988"}],"version-history":[{"count":0,"href":"https:\/\/netcloud24.com\/knowledgebase\/wp-json\/wp\/v2\/posts\/2988\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/netcloud24.com\/knowledgebase\/wp-json\/wp\/v2\/media\/3421"}],"wp:attachment":[{"href":"https:\/\/netcloud24.com\/knowledgebase\/wp-json\/wp\/v2\/media?parent=2988"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/netcloud24.com\/knowledgebase\/wp-json\/wp\/v2\/categories?post=2988"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/netcloud24.com\/knowledgebase\/wp-json\/wp\/v2\/tags?post=2988"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}