{"id":360,"date":"2023-02-15T13:51:19","date_gmt":"2023-02-15T13:51:19","guid":{"rendered":"https:\/\/hostup.se\/en\/blog\/?p=360"},"modified":"2023-02-15T14:15:47","modified_gmt":"2023-02-15T14:15:47","slug":"setup-python-django-application","status":"publish","type":"post","link":"https:\/\/hostup.se\/en\/blog\/setup-python-django-application\/","title":{"rendered":"How to setup Python Django application on shared hosting"},"content":{"rendered":"\n<p>In this tutorial I will show you how to setup a Python Django application that starts by passenger whenever someone visits the site.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 1: Setup document root<\/h2>\n\n\n\n<p>In this example we&#8217;ll use the main domain, site.tld but you can use a subdomain if you prefer that. The important part is that the structure is correct. Passenger will expect to find the document root in \/public and the site files in \/.<\/p>\n\n\n\n<p>For this 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\">cd \/var\/www &amp;&amp; mv html html-django &amp;&amp; mkdir html-django\/public &amp;&amp; ln -s html-django\/public html<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Step 2: Install a python version of your choice<\/h2>\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\/html-django<\/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 Django and create the passenger file<\/h2>\n\n\n\n<p>To install django run the following command<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">pip3 install django<\/pre>\n\n\n\n<p>Now when Django is installed lets create a test application to make sure everything works fine.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">django-admin startproject myproject .<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">python3 manage.py migrate<\/pre>\n\n\n\n<p>After this let&#8217;s create an admin account<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">python3 manage.py createsuperuser<\/pre>\n\n\n\n<p>Make sure to save the username and password.<\/p>\n\n\n\n<p>After this we need to edit the settings.py file to add the domain to the allow<\/p>\n\n\n\n<p>Open <strong>myproject\/settings.py<\/strong> with an editor of your choice and make sure that ALLOWED_HOSTS = [] contains something like this<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ALLOWED_HOSTS = &#91;'site.tld']<\/code><\/pre>\n\n\n\n<p>Great now we&#8217;re done! While you can start Django directly at a port that&#8217;s allocated to your account with python3 manage.py runserver 0.0.0.0:41411 command, it&#8217;s best to setup passenger. This is because if the server should restart or something else happens you don&#8217;t have to keep on starting the Django server all the time, passenger does that for you!<\/p>\n\n\n\n<p>Create a Passenger startup file to run Django 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>import myproject.wsgi\napplication = myproject.wsgi.application<\/code><\/pre>\n\n\n\n<p>If the project name is different from myproject make sure to replace it to match your project name.<\/p>\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\/html-django\/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\/html-django\nPassengerPython \/usr\/local\/share\/python\/pyenv\/shims\/python\nPassengerEnabled on<\/code><\/pre>\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\/html-django\/public\/index.html<\/pre>\n\n\n\n<p>That&#8217;s it. We&#8217;re done.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial I will show you how to setup a Python Django application that starts by passenger whenever someone visits the site. Step 1: Setup document root In this example we&#8217;ll use the main domain, site.tld but you can use a subdomain if you prefer that. The important part is that the structure is [&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-360","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\/360","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=360"}],"version-history":[{"count":8,"href":"https:\/\/hostup.se\/en\/blog\/wp-json\/wp\/v2\/posts\/360\/revisions"}],"predecessor-version":[{"id":371,"href":"https:\/\/hostup.se\/en\/blog\/wp-json\/wp\/v2\/posts\/360\/revisions\/371"}],"wp:attachment":[{"href":"https:\/\/hostup.se\/en\/blog\/wp-json\/wp\/v2\/media?parent=360"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hostup.se\/en\/blog\/wp-json\/wp\/v2\/categories?post=360"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hostup.se\/en\/blog\/wp-json\/wp\/v2\/tags?post=360"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}