<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Aman Trigunait</title>
    <description>The latest articles on DEV Community by Aman Trigunait (@amantrigunait).</description>
    <link>https://dev.to/amantrigunait</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1029342%2F17b7e572-39df-4ff7-98a8-926667b57852.jpeg</url>
      <title>DEV Community: Aman Trigunait</title>
      <link>https://dev.to/amantrigunait</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/amantrigunait"/>
    <language>en</language>
    <item>
      <title>Efficient Production Deployment of Django with Nginx and Gunicorn</title>
      <dc:creator>Aman Trigunait</dc:creator>
      <pubDate>Sat, 18 Feb 2023 17:11:22 +0000</pubDate>
      <link>https://dev.to/amantrigunait/efficient-production-deployment-of-django-with-nginx-and-gunicorn-1edc</link>
      <guid>https://dev.to/amantrigunait/efficient-production-deployment-of-django-with-nginx-and-gunicorn-1edc</guid>
      <description>&lt;p&gt;&lt;em&gt;This post intends to help you in quick setup along with configuration files and their contents. In subsequent post I shall include ssl setup as well.&lt;/em&gt; &lt;/p&gt;

&lt;p&gt;We shall follow step-by-step approach to to deploy a Django application over AWS using nginx and gunicorn.&lt;/p&gt;

&lt;p&gt;Software version details in this blog:&lt;br&gt;
Django : 3.2&lt;br&gt;
Python: 3.10&lt;/p&gt;



&lt;p&gt;&lt;strong&gt;STEP 1 : Create a Django project&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;For now we shall assume that you have a basic Django project setup that you run locally using the command below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;python manage.py runserver
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;STEP 2 : Connecting gunicorn to Django app&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Install gunicorn in your virtual environment.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip install django gunicorn
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Configuring gunicorn. The below command should give you an indication whether the gunicorn is running correctly.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;gunicorn --bind 0.0.0.0:8000 &amp;lt;your_project_name&amp;gt;.wsgi
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now deactivate your virtual env.&lt;/p&gt;

&lt;p&gt;Daemonizing gunicorn using systemd (highly recommended).&lt;/p&gt;

&lt;p&gt;a) Create gunicorn socket file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo vi /etc/systemd/system/gunicorn.socket
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# file content of gunicorn.socket
[Unit]
Description=gunicorn socket
[Socket]
ListenStream=/run/gunicorn.sock
[Install]
WantedBy=sockets.target
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;b) Create gunicorn service file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo vim /etc/systemd/system/gunicorn.service
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# file content of gunicorn.service
[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target

[Service]
# here you should put the user who has access to the django project path (In my case it was ubuntu)
User=ubuntu
Group=www-data
WorkingDirectory=&amp;lt;path to your django project root&amp;gt;
ExecStart=&amp;lt;path to your django virtual env&amp;gt;/bin/gunicorn \
          --access-logfile - \
          --workers 3 \
          --bind unix:/run/gunicorn.sock \
          core.wsgi:application
[Install]
WantedBy=multi-user.target
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;c) Start and enable socket&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo systemctl start gunicorn.socket
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo systemctl enable gunicorn.socket
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;d) [optional] In case you update the gunicorn service file later, run the below commands&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo systemctl daemon-reload
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo systemctl restart gunicorn.service
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;STEP 3: Configuring Nginx as a reverse proxy&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Create a configuration file for Nginx using the following command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo vim /etc/nginx/sites-available/&amp;lt;your_project_name&amp;gt;.conf
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;server {
    listen 80;
    server_name xyz.domain.in;
    charset utf-8;

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    location /static/ {
        alias &amp;lt;your_project_path&amp;gt;/staticfiles/;
    }
    location /media/ {
        alias &amp;lt;your_project_path&amp;gt;/media/;
    }
    location / {
        include proxy_params;
        proxy_pass http://unix:/run/gunicorn.sock;
    }
    location /favicon.ico {
        access_log off;
        log_not_found off;
    }
    location /robots.txt {
        access_log off;
        log_not_found off;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What we shall do below is create a symlink of the conf file in directory sites-enabled and start ngnix.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cd /etc/nginx/sites-enabled
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo ln -s /etc/nginx/sites-available/&amp;lt;your_project_name&amp;gt;.conf
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo systemctl restart nginx
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>learning</category>
      <category>career</category>
      <category>productivity</category>
      <category>discuss</category>
    </item>
  </channel>
</rss>
