<?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>Supervisor Archives - ServerStadium</title>
	<atom:link href="https://serverstadium.com/knowledge-base/tag/supervisor/feed/" rel="self" type="application/rss+xml" />
	<link>https://serverstadium.com/knowledge-base/tag/supervisor/</link>
	<description>Public Cloud Solution by ServerStadium</description>
	<lastBuildDate>Mon, 24 Apr 2023 18:46:08 +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>Supervisor Archives - ServerStadium</title>
	<link>https://serverstadium.com/knowledge-base/tag/supervisor/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<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>
