{"id":336,"date":"2023-01-19T11:15:59","date_gmt":"2023-01-19T11:15:59","guid":{"rendered":"https:\/\/hostup.se\/en\/blog\/?p=336"},"modified":"2023-02-15T13:52:58","modified_gmt":"2023-02-15T13:52:58","slug":"setup-python-flask-application","status":"publish","type":"post","link":"https:\/\/hostup.se\/en\/blog\/setup-python-flask-application\/","title":{"rendered":"How to setup Python flask application on shared hosting"},"content":{"rendered":"\n<p>In this tutorial I will show you how to setup a Python Flask application that starts by passenger<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 1: Create a place to house your files &amp; set correct document root<\/h2>\n\n\n\n<p>If you want you can use your main domain. However in this case I will use a subdomain where I will run my application.<\/p>\n\n\n\n<p>First Navigate to <strong>Web &gt; Subdomains<\/strong> and enter the subdomain of your preference.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"170\" src=\"https:\/\/hostup.se\/en\/blog\/wp-content\/uploads\/2023\/01\/flask-1024x170.png\" alt=\"\" class=\"wp-image-337\" srcset=\"https:\/\/hostup.se\/en\/blog\/wp-content\/uploads\/2023\/01\/flask-1024x170.png 1024w, https:\/\/hostup.se\/en\/blog\/wp-content\/uploads\/2023\/01\/flask-300x50.png 300w, https:\/\/hostup.se\/en\/blog\/wp-content\/uploads\/2023\/01\/flask-768x128.png 768w, https:\/\/hostup.se\/en\/blog\/wp-content\/uploads\/2023\/01\/flask-1536x256.png 1536w, https:\/\/hostup.se\/en\/blog\/wp-content\/uploads\/2023\/01\/flask-2048x341.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>In this case my subdomain is python and my maindomain is flask.3li.se<\/figcaption><\/figure>\n\n\n\n<p>For the next step we&#8217;ll want to head on over to the terminal. You may access it via <strong>Dev &gt; Terminal<\/strong> or via SSH by using the login details sent to you in the &#8220;Get started with your web host&#8221; email.<\/p>\n\n\n\n<p>Once there we&#8217;ll first create the correct document root.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mkdir \/var\/www\/<strong>python.flask.3li.se<\/strong>\/public<\/pre>\n\n\n\n<p>Make sure to replace python.flask.3li.se with the subdomain that you created<\/p>\n\n\n\n<p>Next we&#8217;ll go back to the control panel and update the document root to reflect the change. Navigate to <strong>Web &gt; Subdomains<\/strong> again and in the top left corner click <strong>Change Mode: List Subdomains<\/strong><\/p>\n\n\n\n<p>From there change the document root to the \/public directory you just created like so<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"113\" src=\"https:\/\/hostup.se\/en\/blog\/wp-content\/uploads\/2023\/01\/public-1024x113.png\" alt=\"\" class=\"wp-image-338\" srcset=\"https:\/\/hostup.se\/en\/blog\/wp-content\/uploads\/2023\/01\/public-1024x113.png 1024w, https:\/\/hostup.se\/en\/blog\/wp-content\/uploads\/2023\/01\/public-300x33.png 300w, https:\/\/hostup.se\/en\/blog\/wp-content\/uploads\/2023\/01\/public-768x85.png 768w, https:\/\/hostup.se\/en\/blog\/wp-content\/uploads\/2023\/01\/public-1536x170.png 1536w, https:\/\/hostup.se\/en\/blog\/wp-content\/uploads\/2023\/01\/public-2048x227.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Step 2: install a python version of your choice<\/h2>\n\n\n\n<p>From now on we&#8217;ll only work in the terminal.<\/p>\n\n\n\n<p>First find out what python version you&#8217;d like to use. In my case I chose <strong>3.10<\/strong> but you can choose any version you like.<\/p>\n\n\n\n<p>Run the following command to install the python version<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">pyenv install <strong>3.10<\/strong><\/pre>\n\n\n\n<p>It can take a few minutes for it to compile and install python.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 3: create all the necessary files.<\/h2>\n\n\n\n<p>First navigate one level below the document root of your site.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">cd \/var\/www\/<strong>python.flask.3li.se<\/strong><\/pre>\n\n\n\n<p>Next specify the python version you&#8217;d like to use<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">pyenv local 3.10<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Step 4: Install flask and create the passenger file<\/h2>\n\n\n\n<p>To install flask run the following command<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">pip3 install flask<\/pre>\n\n\n\n<p>Create a Passenger startup file to run Flask as a Python WSGI application: <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">touch passenger_wsgi.py<\/pre>\n\n\n\n<p>Edit the file with the editor of your choice and make sure it has the following content:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from flask import Flask\n<span id=\"rb1ln2\" class=\"line\" data-line=\"2\"><\/span>application = Flask(__name__)\n<span id=\"rb1ln3\" class=\"line\" data-line=\"3\"><\/span> \n<span id=\"rb1ln4\" class=\"line\" data-line=\"4\"><\/span>@application.route(\"\/\")\n<span id=\"rb1ln5\" class=\"line\" data-line=\"5\"><\/span>def hello():\n<span id=\"rb1ln6\" class=\"line\" data-line=\"6\"><\/span> return \"Hello World!\"\n<span id=\"rb1ln7\" class=\"line\" data-line=\"7\"><\/span>\n<span id=\"rb1ln8\" class=\"line\" data-line=\"8\"><\/span>if __name__ == \"__main__\":\n<span id=\"rb1ln9\" class=\"line\" data-line=\"9\"><\/span> application.run()<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Step 5: Create the .htaccess file to start passenger<\/h2>\n\n\n\n<p>The last thing to do is to create the .htaccess file that will start passenger.<\/p>\n\n\n\n<p>First navigate to your document root and create a file called .htaccess<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">cd \/var\/www\/<strong>python.flask.3li.se<\/strong>\/public &amp;&amp; touch .htaccess<\/pre>\n\n\n\n<p>Next edit the file with the editor of your choice and make sure it has the following content:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>PassengerAppRoot \/var\/www\/<strong>python.flask.3li.se<\/strong>\nPassengerPython \/usr\/local\/share\/python\/pyenv\/shims\/python\nPassengerEnabled on<\/code><\/pre>\n\n\n\n<p>Remember to replace python.flask.3li.se with your subdomain.<\/p>\n\n\n\n<p>The last thing to do is delete the template index.html file that was generated.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">rm \/var\/www\/<strong>python.flask.3li.se<\/strong>\/public\/index.html<\/pre>\n\n\n\n<p>That&#8217;s it. We&#8217;re done. You can now visit your site and you should see Hello World!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial I will show you how to setup a Python Flask application that starts by passenger Step 1: Create a place to house your files &amp; set correct document root If you want you can use your main domain. However in this case I will use a subdomain where I will run my [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-336","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/hostup.se\/en\/blog\/wp-json\/wp\/v2\/posts\/336","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hostup.se\/en\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hostup.se\/en\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hostup.se\/en\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/hostup.se\/en\/blog\/wp-json\/wp\/v2\/comments?post=336"}],"version-history":[{"count":8,"href":"https:\/\/hostup.se\/en\/blog\/wp-json\/wp\/v2\/posts\/336\/revisions"}],"predecessor-version":[{"id":361,"href":"https:\/\/hostup.se\/en\/blog\/wp-json\/wp\/v2\/posts\/336\/revisions\/361"}],"wp:attachment":[{"href":"https:\/\/hostup.se\/en\/blog\/wp-json\/wp\/v2\/media?parent=336"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hostup.se\/en\/blog\/wp-json\/wp\/v2\/categories?post=336"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hostup.se\/en\/blog\/wp-json\/wp\/v2\/tags?post=336"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}