<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Django Archives - ServerStadium</title>
	<atom:link href="https://serverstadium.com/knowledge-base/tag/django/feed/" rel="self" type="application/rss+xml" />
	<link>https://serverstadium.com/knowledge-base/tag/django/</link>
	<description>Public Cloud Solution by ServerStadium</description>
	<lastBuildDate>Thu, 04 Jan 2024 01:36:42 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.1.4</generator>

<image>
	<url>https://serverstadium.com/wp-content/uploads/2023/04/cropped-favicon-1-32x32.png</url>
	<title>Django Archives - ServerStadium</title>
	<link>https://serverstadium.com/knowledge-base/tag/django/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Making Your Django Project Production-Ready on Ubuntu 22.04: Gunicorn and Nginx Setup</title>
		<link>https://serverstadium.com/knowledge-base/making-your-django-project-production-ready-on-ubuntu-22-04-gunicorn-and-nginx-setup/</link>
		
		<dc:creator><![CDATA[Brian K]]></dc:creator>
		<pubDate>Thu, 04 Jan 2024 01:35:52 +0000</pubDate>
				<guid isPermaLink="false">https://serverstadium.com/?post_type=epkb_post_type_1&#038;p=11751</guid>

					<description><![CDATA[<p>This continuation of our guide will show you how to make your Django project production-ready on a ServerStadium VM using Gunicorn and Nginx. Step 8: Install and Configure Gunicorn Install Gunicorn with pip: Test Gunicorn&#8217;s ability to serve the project: Now, visit http://your_server_ip:8000 to confirm it’s serving your Django site correctly. Step 9: Create a&#8230;</p>
<p>The post <a href="https://serverstadium.com/knowledge-base/making-your-django-project-production-ready-on-ubuntu-22-04-gunicorn-and-nginx-setup/">Making Your Django Project Production-Ready on Ubuntu 22.04: Gunicorn and Nginx Setup</a> appeared first on <a href="https://serverstadium.com">ServerStadium</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>This continuation of our guide will show you how to make your Django project production-ready on a ServerStadium VM using Gunicorn and Nginx.</p>
<h3>Step 8: Install and Configure Gunicorn</h3>
<p>Install Gunicorn with pip:</p>
<div class="shortcode-code">
<p>pip3 install gunicorn</p></div>
<p>Test Gunicorn&#8217;s ability to serve the project:</p>
<div class="shortcode-code">
<p>gunicorn &#8211;workers 3 myproject.wsgi:application &#8211;&lt;span class=&quot;hljs-built_in&quot;&gt;bind&lt;/span&gt; 0.0.0.0:8000</p></div>
<p>Now, visit <code>http://your_server_ip:8000</code> to confirm it’s serving your Django site correctly.</p>
<h3>Step 9: Create a Gunicorn Systemd Service File</h3>
<p>Create a systemd service file for Gunicorn to manage your process:</p>
<div class="bg-black rounded-md">
<div class="p-4 overflow-y-auto"><div class="shortcode-code">
<p>sudo nano /etc/systemd/system/gunicorn.service</p></div></p></div>
<div></div>
</div>
<p>Add the following content, adjusting paths and usernames as necessary:</p>
<div class="bg-black rounded-md">
<div class="flex items-center relative text-gray-200 bg-gray-800 dark:bg-token-surface-primary px-4 py-2 text-xs font-sans justify-between rounded-t-md"></div>
<div class="p-4 overflow-y-auto">
<div class="shortcode-code">
<p>&#091;Unit&#093;<br />
Description=gunicorn daemon<br />
Requires=gunicorn.socket<br />
After=network.target</p>
<p>&#091;Service&#093;<br />
User=yourusername<br />
Group=www-data<br />
WorkingDirectory=/home/yourusername/mydjangoapp<br />
ExecStart=/home/yourusername/mydjangoapp/env/bin/gunicorn \<br />
&#8211;access-logfile &#8211; \<br />
&#8211;workers 3 \<br />
&#8211;bind unix:/run/gunicorn.sock \<br />
myproject.wsgi:application</p>
<p>&#091;Install&#093;<br />
WantedBy=multi-user.target</p>
<p></p></div>
</div>
</div>
<p>Enable and start the Gunicorn service:</p>
<div class="bg-black rounded-md">
<div class="p-4 overflow-y-auto"><div class="shortcode-code">
<p>sudo systemctl start gunicorn&lt;/div&gt;<br />
&lt;div class=&quot;p-4 overflow-y-auto&quot;&gt;sudo systemctl &lt;span class=&quot;hljs-built_in&quot;&gt;enable&lt;/span&gt; gunicorn</p></div></p></div>
</div>
<h3>Step 10: Install and Configure Nginx</h3>
<p>Install Nginx:</p>
<div class="bg-black rounded-md">
<div class="p-4 overflow-y-auto"><div class="shortcode-code">
<p>sudo apt install nginx</p></div></p></div>
<div></div>
</div>
<p>Configure Nginx to proxy pass to Gunicorn. Create a new server block:</p>
<div class="bg-black rounded-md">
<div class="p-4 overflow-y-auto"><div class="shortcode-code">
<p>sudo nano /etc/nginx/sites-available/mydjangoapp</p></div></p></div>
</div>
<p>Add the following configuration:</p>
<div class="bg-black rounded-md">
<div class="flex items-center relative text-gray-200 bg-gray-800 dark:bg-token-surface-primary px-4 py-2 text-xs font-sans justify-between rounded-t-md">
<div class="shortcode-code">
<p>server {<br />
listen 80;<br />
server_name your_server_ip;</p>
<p>location = /favicon.ico { access_log off; log_not_found off; }<br />
location /static/ {<br />
root /home/yourusername/mydjangoapp;<br />
}</p>
<p>location / {<br />
include proxy_params;<br />
proxy_pass http://unix:/run/gunicorn.sock;<br />
}<br />
}</p>
<p></p></div>
</div>
</div>
<div class="bg-black rounded-md">
<div class="flex items-center relative text-gray-200 bg-gray-800 dark:bg-token-surface-primary px-4 py-2 text-xs font-sans justify-between rounded-t-md">Link this file to the <code>sites-enabled</code> directory:</div>
<div></div>
<div class="p-4 overflow-y-auto"><div class="shortcode-code">
<p>sudo &lt;span class=&quot;hljs-built_in&quot;&gt;ln&lt;/span&gt; -s /etc/nginx/sites-available/mydjangoapp /etc/nginx/sites-enabled</p></div></p></div>
<div></div>
</div>
<p>Test your Nginx configuration:</p>
<div class="bg-black rounded-md">
<div class="p-4 overflow-y-auto"><div class="shortcode-code">
<p>sudo nginx -t</p></div></p></div>
<div></div>
</div>
<p>Restart Nginx:</p>
<div class="bg-black rounded-md">
<div class="p-4 overflow-y-auto"><div class="shortcode-code">
<p>sudo systemctl restart nginx</p></div></p></div>
</div>
<h3>Step 11: Secure Your Server with a Firewall</h3>
<p>Enable the Nginx Full profile on <code>ufw</code>:</p>
<div class="bg-black rounded-md">
<div class="p-4 overflow-y-auto"><div class="shortcode-code">
<p>sudo ufw allow &lt;span class=&quot;hljs-string&quot;&gt;&#039;Nginx Full&#039;&lt;/span&gt;</p></div></p></div>
</div>
<h3>Conclusion</h3>
<p>Your Django application is now configured with Gunicorn and Nginx on your <a href="https://cp.serverstadium.com/" target="_new" rel="noopener">ServerStadium VM</a>, ready for production.</p>
<p>For additional support, check out our <a href="https://serverstadium.com/knowledge-base/" target="_new" rel="noopener">knowledge base</a> or contact our support team.</p>
<p>The post <a href="https://serverstadium.com/knowledge-base/making-your-django-project-production-ready-on-ubuntu-22-04-gunicorn-and-nginx-setup/">Making Your Django Project Production-Ready on Ubuntu 22.04: Gunicorn and Nginx Setup</a> appeared first on <a href="https://serverstadium.com">ServerStadium</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Setting Up Python and Django on Ubuntu 22.04: A Comprehensive Guide for ServerStadium VMs</title>
		<link>https://serverstadium.com/knowledge-base/setting-up-python-and-django-on-ubuntu-22-04-a-comprehensive-guide-for-serverstadium-vms/</link>
		
		<dc:creator><![CDATA[Brian K]]></dc:creator>
		<pubDate>Thu, 04 Jan 2024 01:29:21 +0000</pubDate>
				<guid isPermaLink="false">https://serverstadium.com/?post_type=epkb_post_type_1&#038;p=11748</guid>

					<description><![CDATA[<p>This guide provides detailed instructions on installing Python and setting up Django on Ubuntu 22.04. This setup is suitable for deploying Django applications on a ServerStadium VM instance. Prerequisites Before you start, ensure you have an account with ServerStadium and have created a VM instance with Ubuntu 22.04. If not, please register and set up&#8230;</p>
<p>The post <a href="https://serverstadium.com/knowledge-base/setting-up-python-and-django-on-ubuntu-22-04-a-comprehensive-guide-for-serverstadium-vms/">Setting Up Python and Django on Ubuntu 22.04: A Comprehensive Guide for ServerStadium VMs</a> appeared first on <a href="https://serverstadium.com">ServerStadium</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>This guide provides detailed instructions on installing Python and setting up Django on Ubuntu 22.04. This setup is suitable for deploying Django applications on a ServerStadium VM instance.</p>
<h3>Prerequisites</h3>
<p>Before you start, ensure you have an account with ServerStadium and have created a VM instance with Ubuntu 22.04. If not, please <a href="https://cp.serverstadium.com/regist-user" target="_new" rel="noopener">register</a> and set up your VM instance through the <a href="https://cp.serverstadium.com/" target="_new" rel="noopener">Cloud Panel</a>.</p>
<h3>Step 1: Update and Upgrade Ubuntu Packages</h3>
<p>First, connect to your ServerStadium VM via SSH. Once connected, update and upgrade your Ubuntu packages:</p>
<pre><div class="shortcode-code">

sudo apt update
sudo apt upgrade</div></pre>
<h3>Step 2: Install Python</h3>
<p>Ubuntu 22.04 typically comes with Python pre-installed. Verify the installation and check the version:</p>
<div class="shortcode-code">
<p>python3 &#8211;version</p></div>
<p>If Python is not installed or you need a different version, you can install it using:</p>
<div class="shortcode-code">
<p>sudo apt install python3</p></div>
<h3>Step 3: Install pip (Python Package Installer)</h3>
<p>Install pip, the Python package manager, which you will use to install Django:</p>
<div class="shortcode-code">
<p>sudo apt install python3-pip</p></div>
<h3>Step 4: Install Django</h3>
<p>With pip installed, you can now install Django:</p>
<div class="shortcode-code">
<p>pip3 install Django</p></div>
<p>Verify the installation by checking the Django version:</p>
<div class="shortcode-code">
<p>django-admin &#8211;version</p></div>
<h3>Step 5: Create Your Django Project</h3>
<p>Create a directory for your Django project and navigate into it:</p>
<div class="shortcode-code">
<p>mkdir mydjangoapp &amp;&amp; cd mydjangoapp</p></div>
<p>Start a new Django project:</p>
<div class="shortcode-code">
<p>django-admin startproject myproject .</p></div>
<p>This command creates a new Django project named <code>myproject</code> in your current directory.</p>
<h3>Step 6: Configure the Django Application</h3>
<p>Edit the settings of your Django project to configure allowed hosts, database settings, and more. Open the settings file:</p>
<div class="shortcode-code">
<p>nano myproject/settings.py</p></div>
<p>In the <code>ALLOWED_HOSTS</code> array, add your server&#8217;s IP address or domain name.</p>
<h3>Step 7: Start the Django Development Server</h3>
<p>Run the Django development server to see if everything is set up correctly:</p>
<div class="shortcode-code">
<p>python3 manage.py runserver 0.0.0.0:8000</p></div>
<p>Open your web browser and visit <code>http://your_server_ip:8000</code>. You should see the Django welcome page.</p>
<h3>Conclusion</h3>
<p>Congratulations! You have successfully installed Python and set up Django on Ubuntu 22.04 on your ServerStadium VM instance. This setup provides a solid foundation for developing and deploying robust Django applications.</p>
<p>For further assistance, please visit our <a href="https://serverstadium.com/knowledge-base/" target="_new" rel="noopener">knowledge base</a> or contact our support team.</p>
<p>The post <a href="https://serverstadium.com/knowledge-base/setting-up-python-and-django-on-ubuntu-22-04-a-comprehensive-guide-for-serverstadium-vms/">Setting Up Python and Django on Ubuntu 22.04: A Comprehensive Guide for ServerStadium VMs</a> appeared first on <a href="https://serverstadium.com">ServerStadium</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>How to Deploy Django App on ServerStadium VM Using Gunicorn, Supervisor, and Nginx</title>
		<link>https://serverstadium.com/knowledge-base/how-to-deploy-django-app-on-serverstadium-vm-using-gunicorn-supervisor-and-nginx/</link>
		
		<dc:creator><![CDATA[Brian K]]></dc:creator>
		<pubDate>Sat, 11 Jun 2022 07:11:17 +0000</pubDate>
				<guid isPermaLink="false">https://cloudraya.com/knowledge-base/?p=2084</guid>

					<description><![CDATA[<p>A basic guide on deploying the Django app using Gunicorn, Supervisor, and Nginx stack on your ServerStadium VM instance. In this article, I will use Ubuntu 22.04 LTS as the deployment server, Python 3.8, and Django 4.0. TLDR Create a standard user called apps Install supervisor and Nginx Install your App&#8217;s Python version. Install pip&#8230;</p>
<p>The post <a href="https://serverstadium.com/knowledge-base/how-to-deploy-django-app-on-serverstadium-vm-using-gunicorn-supervisor-and-nginx/">How to Deploy Django App on ServerStadium VM Using Gunicorn, Supervisor, and Nginx</a> appeared first on <a href="https://serverstadium.com">ServerStadium</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p class="wp-block-paragraph">A basic guide on deploying the Django app using Gunicorn, Supervisor, and Nginx stack on your ServerStadium VM instance.</p>



<p class="wp-block-paragraph">In this article, I will use Ubuntu 22.04 LTS as the deployment server, Python 3.8, and Django 4.0.</p>



<h2 class="has-vivid-cyan-blue-color has-text-color wp-block-heading">TLDR</h2>



<ol class="wp-block-list">
<li>Create a standard user called <strong>apps</strong></li>
<li>Install supervisor and Nginx</li>
</ol>



<pre class="wp-block-code"><code>sudo apt install supervisor nginx -y</code></pre>



<ol class="wp-block-list" start="3">
<li>Install your App&#8217;s Python version.</li>
<li>Install <code>pip</code></li>
<li>Install <code>pipenv</code> globally.</li>
</ol>



<pre class="wp-block-code"><code>sudo pip install pipenv</code></pre>



<ol class="wp-block-list" start="3">
<li>Add your server IP and/or FQDN address to ALLOWED_HOSTS in <code>settings.py</code> file</li>
</ol>



<pre class="wp-block-code"><code>ALLOWED_HOSTS = [ "xx.yy.dd.zz", "your.domain.tld" ]</code></pre>



<ol class="wp-block-list" start="4">
<li>Copy the Django app to <code>/home/apps/django-app</code></li>
<li>Go to the app folder and run:</li>
</ol>



<pre class="wp-block-code"><code>cd /home/apps/django-app
python3.8 -m pipenv install --deploy</code></pre>



<ol class="wp-block-list" start="6">
<li>Install Gunicorn using pipenv on the same directory</li>
</ol>



<pre class="wp-block-code"><code>python3.8 -m pipenv install gunicorn</code></pre>



<ol class="wp-block-list" start="7">
<li>Create supervisor config file under <code>/etc/supervisor/conf.d/django-app.conf</code></li>
</ol>



<pre class="wp-block-code"><code>[program:django-app]
user=apps
directory=/home/apps/django-app/djangoapp/
command=/usr/local/bin/pipenv run gunicorn --workers=3 djangoapp.wsgi

autostart=true
autorestart=true
stdout_logfile=/home/apps/logs/django-app.log
stderr_logfile=/home/apps/logs/django-app.err.log</code></pre>



<ol class="wp-block-list" start="8">
<li>Create the required files and folder:</li>
</ol>



<pre class="wp-block-code"><code>mkdir /home/apps/logs/
touch /home/apps/django-app{,.err}.log</code></pre>



<ol class="wp-block-list" start="9">
<li>Restart supervisor to recognize the app</li>
</ol>



<pre class="wp-block-code"><code>sudo systemctl restart supervisor</code></pre>



<ol class="wp-block-list" start="10">
<li>Create nginx server block file for the app under <code>/etc/nginx/sites-available/django-app.conf</code></li>
</ol>



<pre class="wp-block-code"><code>server {
    listen 80;
    
    location / {
        include proxy_params;
        proxy_pass http://localhost:8000;
    }
}</code></pre>



<ol class="wp-block-list" start="11">
<li>Activate the config and reload nginx</li>
</ol>



<pre class="wp-block-code"><code>sudo ln -s <span style="background-color: inherit; font-family: monospace; font-size: inherit;">/etc/nginx/sites-available/django-app.conf</span> /etc/nginx/sites-enabled
sudo unlink /etc/nginx/sites-enabled/default
sudo nginx -t
sudo nginx -s reload</code></pre>



<ol class="wp-block-list" start="12">
<li>Access the app from your browser: <code>http://your-ip</code> or <code>http://your-domain.tld</code> and DONE!</li>
</ol>



<h2 class="has-vivid-cyan-blue-color has-text-color wp-block-heading">Step-by-step Guide</h2>



<h3 class="has-vivid-cyan-blue-color has-text-color wp-block-heading">Django App Peparation</h3>



<p class="wp-block-paragraph">I will assume that your Django App is ready to be deployed. This means it has Pipfile or requirements.txt. For this tutorial, I&#8217;m going to use <code>pipenv</code> as the virtual environment tool and here&#8217;s the directory structure of the app:</p>



<pre class="wp-block-code"><code>├── djangoapp
│   ├── db.sqlite3
│   ├── djangoapp
│   │   ├── asgi.py
│   │   ├── __init__.py
│   │   ├── settings.py
│   │   ├── urls.py
│   │   └── wsgi.py
│   └── manage.py
├── Pipfile
└── Pipfile.lock</code></pre>



<p class="wp-block-paragraph">Make sure the app runs successfully in our local environment by running the below command on the app directory:</p>



<pre class="wp-block-code"><code>pipenv run djangoapp/manage.py runserver</code></pre>


<div class="wp-block-image">
<figure class="aligncenter size-full is-resized"><img fetchpriority="high" decoding="async" class="wp-image-2147" src="https://serverstadium.com/wp-content/uploads/2022/06/carbon-1.png" alt="" width="768" height="380" srcset="https://serverstadium.com/wp-content/uploads/2022/06/carbon-1.png 1024w, https://serverstadium.com/wp-content/uploads/2022/06/carbon-1-300x149.png 300w, https://serverstadium.com/wp-content/uploads/2022/06/carbon-1-768x380.png 768w" sizes="(max-width: 768px) 100vw, 768px" /></figure>
</div>


<p class="wp-block-paragraph">Then access your browser with <code>http://127.0.0.1:8000</code></p>


<div class="wp-block-image">
<figure class="aligncenter size-full is-resized"><img decoding="async" class="wp-image-2148" src="https://serverstadium.com/wp-content/uploads/2022/06/image-1.png" alt="" width="886" height="645" /></figure>
</div>


<p class="wp-block-paragraph">Add your server IP and/or domain to ALLOWED_HOST entry in the <code>settings.py</code> file, then save it.</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img decoding="async" width="289" height="103" class="wp-image-2158" src="https://serverstadium.com/wp-content/uploads/2022/06/image-6.png" alt="" />
<figcaption>Allowed hosts configuration</figcaption>
</figure>
</div>


<p class="wp-block-paragraph">Good, now we are ready to do the deployment.</p>



<h3 class="has-vivid-cyan-blue-color has-text-color wp-block-heading">Server Preparation</h3>



<p class="wp-block-paragraph">We will need a specific user that we will use to serve the app, and it should be a non administrative user. To simplify, we will create <strong>apps</strong> user</p>



<pre class="wp-block-code"><code>sudo useradd --create-home --shell /bin/bash apps</code></pre>



<p class="wp-block-paragraph">We will also want to install supervisor and nginx from now</p>



<pre class="wp-block-code"><code>sudo apt update &amp;&amp; sudo apt install supervisor nginx</code></pre>



<p class="wp-block-paragraph">As mentioned earlier, we will be using Python 3.8, which isn&#8217;t available by default, so please take a look at this article on <a href="#" target="_blank" rel="noreferrer noopener">how to install Python 3.8 on Ubuntu 22.04 Jammy</a>. Don&#8217;t forget to install pip as well.</p>



<p class="wp-block-paragraph">Then install <code>pipenv</code> globally:</p>



<pre class="wp-block-code"><code>sudo pip install pipenv</code></pre>


<div class="wp-block-image">
<figure class="aligncenter size-full is-resized"><img loading="lazy" decoding="async" class="wp-image-2151" src="https://serverstadium.com/wp-content/uploads/2022/06/image-2.png" alt="" width="776" height="435" srcset="https://serverstadium.com/wp-content/uploads/2022/06/image-2.png 1034w, https://serverstadium.com/wp-content/uploads/2022/06/image-2-300x168.png 300w, https://serverstadium.com/wp-content/uploads/2022/06/image-2-1024x574.png 1024w, https://serverstadium.com/wp-content/uploads/2022/06/image-2-768x431.png 768w" sizes="(max-width: 776px) 100vw, 776px" />
<figcaption>Installing pipenv</figcaption>
</figure>
</div>


<h3 class="has-vivid-cyan-blue-color has-text-color wp-block-heading">Copy App Files</h3>



<p class="wp-block-paragraph">Copy or clone (using git if it&#8217;s supported) the app files to <code>/home/apps/django-app</code>. You can use FTP, SFTP, SCP or any other means of transferring local files to the remote server. On the remote server, the apps directory should look like follows:</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="705" height="531" class="wp-image-2150" src="https://serverstadium.com/wp-content/uploads/2022/06/carbon-2.png" alt="" srcset="https://serverstadium.com/wp-content/uploads/2022/06/carbon-2.png 705w, https://serverstadium.com/wp-content/uploads/2022/06/carbon-2-300x226.png 300w" sizes="(max-width: 705px) 100vw, 705px" />
<figcaption>django-app folder structure</figcaption>
</figure>
</div>


<h3 class="has-vivid-cyan-blue-color has-text-color wp-block-heading">Install Required Dependencies</h3>



<p class="wp-block-paragraph">A good thing about <code>pipenv</code> is that it will automatically create a virtual environment upon installing any required dependencies listed in the Pipfile. So, on the django-app folder run:</p>



<pre class="wp-block-code"><code># make sure it's on /home/apps/django-app
python3.8 -m pipenv install --deploy</code></pre>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="1131" height="258" class="wp-image-2152" src="https://serverstadium.com/wp-content/uploads/2022/06/image-3.png" alt="" srcset="https://serverstadium.com/wp-content/uploads/2022/06/image-3.png 1131w, https://serverstadium.com/wp-content/uploads/2022/06/image-3-300x68.png 300w, https://serverstadium.com/wp-content/uploads/2022/06/image-3-1024x234.png 1024w, https://serverstadium.com/wp-content/uploads/2022/06/image-3-768x175.png 768w" sizes="(max-width: 1131px) 100vw, 1131px" /></figure>



<p class="wp-block-paragraph">Next, we will install gunicorn:</p>



<pre class="wp-block-code"><code>python3.8 -m pipenv install gunicorn</code></pre>



<p class="wp-block-paragraph">Then, we will try to serve the app using gunicorn to test, but make sure you run the gunicorn from the app folder where <code>manage.py</code> resides:</p>



<pre class="wp-block-code"><code>cd djangoapp/
python3.8 -m pipenv run gunicorn djangoapp.wsgi</code></pre>



<p class="wp-block-paragraph">It should return something like this:</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="729" height="77" class="wp-image-2155" src="https://serverstadium.com/wp-content/uploads/2022/06/image-4.png" alt="" srcset="https://serverstadium.com/wp-content/uploads/2022/06/image-4.png 729w, https://serverstadium.com/wp-content/uploads/2022/06/image-4-300x32.png 300w" sizes="(max-width: 729px) 100vw, 729px" />
<figcaption>Serving django via gunicorn</figcaption>
</figure>
</div>


<p class="wp-block-paragraph">Awesome. Now we can setup <code>supervisor</code> to handle gunicorn process(es).</p>



<h3 class="has-vivid-cyan-blue-color has-text-color wp-block-heading">Install and Configure Supervisor</h3>



<p class="wp-block-paragraph">For <strong>Debian-based</strong> distros, run:</p>



<pre class="wp-block-code"><code>sudo apt install supervisor</code></pre>



<p class="wp-block-paragraph">For <strong>RHEL-based</strong> distros, run:</p>



<pre class="wp-block-code"><code>sudo dnf install supervisor
# or
sudo yum install supervisor</code></pre>



<p class="wp-block-paragraph">Then, we will create a config file for the django app in <code>/etc/supervisor/conf.d/</code> and name it as <code>django-app.conf</code>. Write or just copy the following lines to the file:</p>



<pre class="wp-block-code"><code>[program:django-app]
user=apps
directory=/home/apps/django-app/djangoapp
command=/usr/local/bin/pipenv run gunicorn --workers=3 djangoapp.wsgi

autostart=true
autorestart=true
stdout_logfile=/home/apps/logs/django-app.log
stderr_logfile=/home/apps/logs/django-app.err.log</code></pre>



<p class="wp-block-paragraph">Don&#8217;t forget to create the <code>./logs/</code> folder to store the app&#8217;s logs:</p>



<pre class="wp-block-code"><code># as the apps user
mkdir /home/apps/logs</code></pre>



<p class="wp-block-paragraph">Then restart supervisor:</p>



<pre class="wp-block-code"><code>sudo systemctl restart supervisor</code></pre>



<p class="wp-block-paragraph">Make sure it&#8217;s running by checking supervisor status:</p>



<pre class="wp-block-code"><code>sudo systemctl status supervisor</code></pre>


<div class="wp-block-image">
<figure class="aligncenter size-full is-resized"><img loading="lazy" decoding="async" class="wp-image-2156" src="https://serverstadium.com/wp-content/uploads/2022/06/image-5.png" alt="" width="792" height="311" srcset="https://serverstadium.com/wp-content/uploads/2022/06/image-5.png 1056w, https://serverstadium.com/wp-content/uploads/2022/06/image-5-300x118.png 300w, https://serverstadium.com/wp-content/uploads/2022/06/image-5-1024x401.png 1024w, https://serverstadium.com/wp-content/uploads/2022/06/image-5-768x301.png 768w" sizes="(max-width: 792px) 100vw, 792px" />
<figcaption>Supervisor status</figcaption>
</figure>
</div>


<p class="wp-block-paragraph">Next, we will install and configure Nginx so it proxies the connection.</p>



<h3 class="has-vivid-cyan-blue-color has-text-color wp-block-heading">Install and Configure NGINX</h3>



<p class="wp-block-paragraph">Here, nginx will proxy HTTP connections to the gunicorn workers. This will also make SSL setup easier, but I won&#8217;t explain it here. Install nginx and make sure it&#8217;s started on boot:</p>



<pre class="wp-block-code"><code>sudo apt install nginx
sudo systemctl enable nginx --now</code></pre>



<p class="wp-block-paragraph">Then create a new config file in <code>/etc/nginx/sites-available</code> for Ubuntu-based distros, RHEL-based ones should be in <code>/etc/nginx/conf.d</code></p>



<pre class="wp-block-code"><code>server {
    listen 80;
    
    location / {
        include proxy_params;
        proxy_pass http://localhost:8000;
    }
}</code></pre>



<p class="wp-block-paragraph">Save it as <code>django-app.conf</code>. Then for Ubuntu-based distro only, create a symlink in <code>/etc/nginx/sites-enabled/</code> for the config file:</p>



<pre class="wp-block-code"><code>sudo ln -s /etc/nginx/sites-available/django-app.conf /etc/nginx/sites-enabled/django-app.conf</code></pre>



<p class="wp-block-paragraph">Remove the default site config</p>



<pre class="wp-block-code"><code>sudo unlink /etc/nginx/sites-enabled/default</code></pre>



<p class="wp-block-paragraph">For RHEL-based distros, one might need to remove the default config or rename the extension to <code>.bak</code> or something else. Then, test nginx and reload it</p>



<pre class="wp-block-code"><code>sudo nginx -t
sudo nginx -s reload</code></pre>



<h3 class="has-vivid-cyan-blue-color has-text-color wp-block-heading">Access Test</h3>



<p class="wp-block-paragraph">Finally, test it from your browser via <code>http://YOUR-IP</code> or <code>http://YOUR-DOMAIN.TLD</code></p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="1040" height="541" class="wp-image-2159" src="https://serverstadium.com/wp-content/uploads/2022/06/image-7.png" alt="" srcset="https://serverstadium.com/wp-content/uploads/2022/06/image-7.png 1040w, https://serverstadium.com/wp-content/uploads/2022/06/image-7-300x156.png 300w, https://serverstadium.com/wp-content/uploads/2022/06/image-7-1024x533.png 1024w, https://serverstadium.com/wp-content/uploads/2022/06/image-7-768x400.png 768w" sizes="(max-width: 1040px) 100vw, 1040px" /></figure>



<p class="wp-block-paragraph">Yay! You&#8217;re now able to deploy your Django app!</p>



<p class="wp-block-paragraph"><strong>IMPORTANT NOTE:</strong> This is a basic setup that is insecure. For a complete documentation on how to configure your Django so it fits production deployment, please refer to <a href="https://docs.djangoproject.com/en/4.0/howto/deployment/checklist/" target="_blank" rel="noreferrer noopener nofollow">this official documentation</a></p>



<h2 class="has-vivid-cyan-blue-color has-text-color wp-block-heading">Conclusion</h2>



<p class="wp-block-paragraph">We just learned how to deploy a django application using Pipenv as the virtual environment tool, Gunicorn as the Web Server Gateway, Supervisor as the process manager, and NGINX as the reverse proxy.</p>



<p class="wp-block-paragraph">Find more tutorial articles in ServerStadium&#8217;s <a href="https://serverstadium.com/knowledge-base/" target="_blank" rel="noreferrer noopener">Knowledge Base</a>,  <a href="https://cp.serverstadium.com/regist-user" target="_blank" rel="noreferrer noopener"> Register now</a> for free in ServerStadium&#8217;s panel to try our cloud VMs.</p>
<p>The post <a href="https://serverstadium.com/knowledge-base/how-to-deploy-django-app-on-serverstadium-vm-using-gunicorn-supervisor-and-nginx/">How to Deploy Django App on ServerStadium VM Using Gunicorn, Supervisor, and Nginx</a> appeared first on <a href="https://serverstadium.com">ServerStadium</a>.</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
