Home
Posts
Categories
Django
Nginx
Gunicorn
Postgres
Ubuntu
Deployment
Django Production Server Deployement
IIDM
Digital Marketing
Concept Explained
Insights
Marketing Strategy
Artificial Intelligent
Discussion
Branding
Paid Marketing
SEO
Acounts
Login
Logout
Register
Certifications
Upcoming Courses
View Site
Contact US
Login
UPDATE
Title*
Summery*
<p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5; color: rgb(77, 91, 124); font-family: Inter, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><a href="https://www.djangoproject.com/" style="box-sizing: border-box; margin: 0px; background: linear-gradient(rgb(0, 105, 255), rgb(0, 105, 255)) center bottom / 1px 1px repeat-x; color: rgb(0, 105, 255); text-decoration: none; border-bottom: none; transition: background 0.3s ease-in-out 0s, color 0.3s ease-in-out 0s; overflow-wrap: break-word;">Django</a><span> </span>is a powerful web framework that can help get your Python application or website off the ground. Django includes a simplified development server for testing your code locally. If you prefer anything even slightly production related, a more secure and powerful web server is recommended.</p><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5; color: rgb(77, 91, 124); font-family: Inter, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">In this tutorial, you will install and configure some components on Ubuntu 18.04 to support and serve Django applications, set up a PostgreSQL database instead of using the default SQLite database, and then configure the Gunicorn application server to interface with your applications. Lastly, you’ll set up Nginx to reverse proxy to Gunicorn, giving you access to its security and performance features to serve your applications.</p><p></p>
Discussion*
<div class="Markdown_markdown___3RCz" style="box-sizing: border-box; margin: 0px; color: rgb(77, 91, 124); font-size: medium; position: relative; z-index: 0; font-family: Inter, sans-serif; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><h3 id="introduction" style="box-sizing: border-box; margin: 0px 0px 1em; font-family: Epilogue, sans-serif;"><a href="https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-18-04#introduction" style="box-sizing: border-box; margin: 0px; background: linear-gradient(rgb(0, 105, 255), rgb(0, 105, 255)) center bottom / 0px 0px repeat-x; color: inherit; text-decoration: none; border-bottom: none; transition: background 0.3s ease-in-out 0s, color 0.3s ease-in-out 0s;">Introduction</a><a class="hash-anchor" href="https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-18-04#introduction" aria-hidden="true" style="box-sizing: border-box; margin: 0px 0px 0px 0.5em; background: linear-gradient(rgb(0, 105, 255), rgb(0, 105, 255)) center bottom / 0px 0px repeat-x; color: rgb(138, 150, 181); text-decoration: none; border-bottom: none; transition: opacity 0.2s ease-in-out 0s, visibility 0s linear 0.2s; font-size: 0.85em; opacity: 0; visibility: hidden;"></a></h3><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;"><a href="https://www.djangoproject.com/" style="box-sizing: border-box; margin: 0px; background: linear-gradient(rgb(0, 105, 255), rgb(0, 105, 255)) center bottom / 1px 1px repeat-x; color: rgb(0, 105, 255); text-decoration: none; border-bottom: none; transition: background 0.3s ease-in-out 0s, color 0.3s ease-in-out 0s; overflow-wrap: break-word;">Django</a><span> </span>is a powerful web framework that can help get your Python application or website off the ground. Django includes a simplified development server for testing your code locally. If you prefer anything even slightly production related, a more secure and powerful web server is recommended.</p><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">In this tutorial, you will install and configure some components on Ubuntu 18.04 to support and serve Django applications, set up a PostgreSQL database instead of using the default SQLite database, and then configure the Gunicorn application server to interface with your applications. Lastly, you’ll set up Nginx to reverse proxy to Gunicorn, giving you access to its security and performance features to serve your applications.</p><h2 id="prerequisites" style="box-sizing: border-box; margin: 1em 0px; font-family: Epilogue, sans-serif;"><a href="https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-18-04#prerequisites" style="box-sizing: border-box; margin: 0px; background: linear-gradient(rgb(0, 105, 255), rgb(0, 105, 255)) center bottom / 0px 0px repeat-x; color: inherit; text-decoration: none; border-bottom: none; transition: background 0.3s ease-in-out 0s, color 0.3s ease-in-out 0s;">Prerequisites</a><a class="hash-anchor" href="https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-18-04#prerequisites" aria-hidden="true" style="box-sizing: border-box; margin: 0px 0px 0px 0.5em; background: linear-gradient(rgb(0, 105, 255), rgb(0, 105, 255)) center bottom / 0px 0px repeat-x; color: rgb(138, 150, 181); text-decoration: none; border-bottom: none; transition: opacity 0.2s ease-in-out 0s, visibility 0s linear 0.2s; font-size: 0.85em; opacity: 0; visibility: hidden;"></a></h2><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">To complete this tutorial you will need an Ubuntu 18.04 server set up, a non-root user with<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">sudo</code><span> </span>privileges configured, and a firewall enabled. Follow our<span> </span><a href="https://www.digitalocean.com/community/tutorials/initial-server-setup-with-ubuntu-18-04" style="box-sizing: border-box; margin: 0px; background: linear-gradient(rgb(0, 105, 255), rgb(0, 105, 255)) center bottom / 1px 1px repeat-x; color: rgb(0, 105, 255); text-decoration: none; border-bottom: none; transition: background 0.3s ease-in-out 0s, color 0.3s ease-in-out 0s; overflow-wrap: break-word;">Ubuntu 18.04 initial server setup guide</a><span> </span>to get started.</p><h2 id="step-1-installing-packages-from-the-ubuntu-repositories" style="box-sizing: border-box; margin: 1em 0px; font-family: Epilogue, sans-serif;"><a href="https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-18-04#step-1-installing-packages-from-the-ubuntu-repositories" style="box-sizing: border-box; margin: 0px; background: linear-gradient(rgb(0, 105, 255), rgb(0, 105, 255)) center bottom / 0px 0px repeat-x; color: inherit; text-decoration: none; border-bottom: none; transition: background 0.3s ease-in-out 0s, color 0.3s ease-in-out 0s;">Step 1 — Installing Packages from the Ubuntu Repositories</a><a class="hash-anchor" href="https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-18-04#step-1-installing-packages-from-the-ubuntu-repositories" aria-hidden="true" style="box-sizing: border-box; margin: 0px 0px 0px 0.5em; background: linear-gradient(rgb(0, 105, 255), rgb(0, 105, 255)) center bottom / 0px 0px repeat-x; color: rgb(138, 150, 181); text-decoration: none; border-bottom: none; transition: opacity 0.2s ease-in-out 0s, visibility 0s linear 0.2s; font-size: 0.85em; opacity: 0; visibility: hidden;"></a></h2><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">Start by downloading and installing all of the items you need from the Ubuntu repositories. Use the Python package manager<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">pip</code><span> </span>to install additional components later on.</p><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">First, update the local<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">apt</code><span> </span>package index and then download and install the packages:</p><div class="code-toolbar" style="box-sizing: border-box; margin: 1em 0px; position: relative;"><pre class="prefixed command language-bash" style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: rgb(17, 25, 46); border-radius: 16px; color: rgb(247, 248, 251); line-height: 22px; padding: 24px; box-shadow: rgba(17, 25, 46, 0.1) 0px 0px 0px 2px inset; display: block; overflow: auto; white-space: normal; overflow-wrap: normal;"><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: transparent; border-radius: 0px; color: inherit; line-height: 22px; padding: 0px; white-space: normal; text-shadow: none;"><ol style="box-sizing: border-box; margin: 0px; list-style: none; padding: 0px;"><li data-prefix="$" style="box-sizing: border-box; margin: 0px; padding: 0px; white-space: pre;"><span class="token function" style="box-sizing: border-box; margin: 0px; text-shadow: none; background: none; border-radius: 0px; display: inline; font-weight: 400; padding: 0px; color: rgb(255, 175, 140);">sudo</span> <span class="token function" style="box-sizing: border-box; margin: 0px; text-shadow: none; background: none; border-radius: 0px; display: inline; font-weight: 400; padding: 0px; color: rgb(255, 175, 140);">apt</span> update </li></ol></code></pre><div class="toolbar" style="box-sizing: border-box; margin: 0px; position: absolute; right: 24px; top: 24px;"><div class="toolbar-item" style="box-sizing: border-box; margin: 0px; display: inline-block;"><button type="button" style="box-sizing: border-box; margin: 0px; font-family: inherit; font-size: 0.9em; line-height: normal; overflow: visible; text-transform: none; appearance: button; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-optical-sizing: inherit; font-kerning: inherit; font-feature-settings: inherit; font-variation-settings: inherit; background: rgb(105, 111, 176); border: 0px; cursor: pointer; user-select: none; border-radius: 10px; color: rgb(255, 255, 255); padding: 6px 8px; transition: color 0.25s ease 0s, background 0.25s ease 0s;">Copy</button></div></div></div><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">Next, install your packages. This depends on which version of Python your project will use.</p><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">If you’re using Django with<span> </span><strong style="box-sizing: border-box; margin: 0px; font-weight: bolder;">Python 3</strong>, run the following:</p><div class="code-toolbar" style="box-sizing: border-box; margin: 1em 0px; position: relative;"><pre class="prefixed command language-bash" style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: rgb(17, 25, 46); border-radius: 16px; color: rgb(247, 248, 251); line-height: 22px; padding: 24px; box-shadow: rgba(17, 25, 46, 0.1) 0px 0px 0px 2px inset; display: block; overflow: auto; white-space: normal; overflow-wrap: normal;"><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: transparent; border-radius: 0px; color: inherit; line-height: 22px; padding: 0px; white-space: normal; text-shadow: none;"><ol style="box-sizing: border-box; margin: 0px; list-style: none; padding: 0px;"><li data-prefix="$" style="box-sizing: border-box; margin: 0px; padding: 0px; white-space: pre;"><span class="token function" style="box-sizing: border-box; margin: 0px; text-shadow: none; background: none; border-radius: 0px; display: inline; font-weight: 400; padding: 0px; color: rgb(255, 175, 140);">sudo</span> <span class="token function" style="box-sizing: border-box; margin: 0px; text-shadow: none; background: none; border-radius: 0px; display: inline; font-weight: 400; padding: 0px; color: rgb(255, 175, 140);">apt</span> <span class="token function" style="box-sizing: border-box; margin: 0px; text-shadow: none; background: none; border-radius: 0px; display: inline; font-weight: 400; padding: 0px; color: rgb(255, 175, 140);">install</span> python3-pip python3-dev libpq-dev postgresql postgresql-contrib nginx <span class="token function" style="box-sizing: border-box; margin: 0px; text-shadow: none; background: none; border-radius: 0px; display: inline; font-weight: 400; padding: 0px; color: rgb(255, 175, 140);">curl</span> </li></ol></code></pre><div class="toolbar" style="box-sizing: border-box; margin: 0px; position: absolute; right: 24px; top: 24px;"><div class="toolbar-item" style="box-sizing: border-box; margin: 0px; display: inline-block;"><button type="button" style="box-sizing: border-box; margin: 0px; font-family: inherit; font-size: 0.9em; line-height: normal; overflow: visible; text-transform: none; appearance: button; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-optical-sizing: inherit; font-kerning: inherit; font-feature-settings: inherit; font-variation-settings: inherit; background: rgb(105, 111, 176); border: 0px; cursor: pointer; user-select: none; border-radius: 10px; color: rgb(255, 255, 255); padding: 6px 8px; transition: color 0.25s ease 0s, background 0.25s ease 0s;">Copy</button></div></div></div><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">Django 1.11 is the last release of Django that will support<span> </span><strong style="box-sizing: border-box; margin: 0px; font-weight: bolder;">Python 2</strong>. If you are starting new projects, it is strongly recommended that you choose Python 3. If you still need to use Python 2, run the following:</p><div class="code-toolbar" style="box-sizing: border-box; margin: 1em 0px; position: relative;"><pre class="prefixed command language-bash" style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: rgb(17, 25, 46); border-radius: 16px; color: rgb(247, 248, 251); line-height: 22px; padding: 24px; box-shadow: rgba(17, 25, 46, 0.1) 0px 0px 0px 2px inset; display: block; overflow: auto; white-space: normal; overflow-wrap: normal;"><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: transparent; border-radius: 0px; color: inherit; line-height: 22px; padding: 0px; white-space: normal; text-shadow: none;"><ol style="box-sizing: border-box; margin: 0px; list-style: none; padding: 0px;"><li data-prefix="$" style="box-sizing: border-box; margin: 0px; padding: 0px; white-space: pre;"><span class="token function" style="box-sizing: border-box; margin: 0px; text-shadow: none; background: none; border-radius: 0px; display: inline; font-weight: 400; padding: 0px; color: rgb(255, 175, 140);">sudo</span> <span class="token function" style="box-sizing: border-box; margin: 0px; text-shadow: none; background: none; border-radius: 0px; display: inline; font-weight: 400; padding: 0px; color: rgb(255, 175, 140);">apt</span> <span class="token function" style="box-sizing: border-box; margin: 0px; text-shadow: none; background: none; border-radius: 0px; display: inline; font-weight: 400; padding: 0px; color: rgb(255, 175, 140);">install</span> python-pip python-dev libpq-dev postgresql postgresql-contrib nginx <span class="token function" style="box-sizing: border-box; margin: 0px; text-shadow: none; background: none; border-radius: 0px; display: inline; font-weight: 400; padding: 0px; color: rgb(255, 175, 140);">curl</span> </li></ol></code></pre><div class="toolbar" style="box-sizing: border-box; margin: 0px; position: absolute; right: 24px; top: 24px;"><div class="toolbar-item" style="box-sizing: border-box; margin: 0px; display: inline-block;"><button type="button" style="box-sizing: border-box; margin: 0px; font-family: inherit; font-size: 0.9em; line-height: normal; overflow: visible; text-transform: none; appearance: button; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-optical-sizing: inherit; font-kerning: inherit; font-feature-settings: inherit; font-variation-settings: inherit; background: rgb(105, 111, 176); border: 0px; cursor: pointer; user-select: none; border-radius: 10px; color: rgb(255, 255, 255); padding: 6px 8px; transition: color 0.25s ease 0s, background 0.25s ease 0s;">Copy</button></div></div></div><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">This will install<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">pip</code>, the Python development files needed to build Gunicorn later, the Postgres database system and the libraries needed to interact with it, and the Nginx web server.</p><h2 id="step-2-creating-the-postgresql-database-and-user" style="box-sizing: border-box; margin: 1em 0px; font-family: Epilogue, sans-serif;"><a href="https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-18-04#step-2-creating-the-postgresql-database-and-user" style="box-sizing: border-box; margin: 0px; background: linear-gradient(rgb(0, 105, 255), rgb(0, 105, 255)) center bottom / 0px 0px repeat-x; color: inherit; text-decoration: none; border-bottom: none; transition: background 0.3s ease-in-out 0s, color 0.3s ease-in-out 0s;">Step 2 — Creating the PostgreSQL Database and User</a><a class="hash-anchor" href="https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-18-04#step-2-creating-the-postgresql-database-and-user" aria-hidden="true" style="box-sizing: border-box; margin: 0px 0px 0px 0.5em; background: linear-gradient(rgb(0, 105, 255), rgb(0, 105, 255)) center bottom / 0px 0px repeat-x; color: rgb(138, 150, 181); text-decoration: none; border-bottom: none; transition: opacity 0.2s ease-in-out 0s, visibility 0s linear 0.2s; font-size: 0.85em; opacity: 0; visibility: hidden;"></a></h2><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">In this step, you’ll create a database and database user for your Django application with PostgreSQL, also known as “Postgres.”</p><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">By default, Postgres uses an authentication scheme called<span> </span><em style="box-sizing: border-box; margin: 0px;">peer authentication</em><span> </span>for local connections. This means that if the user’s operating system username matches a valid Postgres username, that user can login with no further authentication.</p><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">During the Postgres installation, an operating system user named<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">postgres</code><span> </span>was created to correspond to the<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">postgres</code><span> </span>PostgreSQL administrative user. You need to use this user to perform administrative tasks. Log in to an interactive Postgres session by using<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">sudo</code><span> </span>and pass in the username with the<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">-u</code><span> </span>option:</p><div class="code-toolbar" style="box-sizing: border-box; margin: 1em 0px; position: relative;"><pre class="prefixed command language-bash" style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: rgb(17, 25, 46); border-radius: 16px; color: rgb(247, 248, 251); line-height: 22px; padding: 24px; box-shadow: rgba(17, 25, 46, 0.1) 0px 0px 0px 2px inset; display: block; overflow: auto; white-space: normal; overflow-wrap: normal;"><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: transparent; border-radius: 0px; color: inherit; line-height: 22px; padding: 0px; white-space: normal; text-shadow: none;"><ol style="box-sizing: border-box; margin: 0px; list-style: none; padding: 0px;"><li data-prefix="$" style="box-sizing: border-box; margin: 0px; padding: 0px; white-space: pre;"><span class="token function" style="box-sizing: border-box; margin: 0px; text-shadow: none; background: none; border-radius: 0px; display: inline; font-weight: 400; padding: 0px; color: rgb(255, 175, 140);">sudo</span> <span class="token parameter variable" style="box-sizing: border-box; margin: 0px; text-shadow: none; background: none; border-radius: 0px; display: inline; font-weight: 400; padding: 0px; color: rgb(177, 228, 144);">-u</span> postgres psql </li></ol></code></pre><div class="toolbar" style="box-sizing: border-box; margin: 0px; position: absolute; right: 24px; top: 24px;"><div class="toolbar-item" style="box-sizing: border-box; margin: 0px; display: inline-block;"><button type="button" style="box-sizing: border-box; margin: 0px; font-family: inherit; font-size: 0.9em; line-height: normal; overflow: visible; text-transform: none; appearance: button; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-optical-sizing: inherit; font-kerning: inherit; font-feature-settings: inherit; font-variation-settings: inherit; background: rgb(105, 111, 176); border: 0px; cursor: pointer; user-select: none; border-radius: 10px; color: rgb(255, 255, 255); padding: 6px 8px; transition: color 0.25s ease 0s, background 0.25s ease 0s;">Copy</button></div></div></div><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">You’ll receive a PostgreSQL prompt where you can set up your requirements. First, create a database for your project:</p><div class="code-toolbar" style="box-sizing: border-box; margin: 1em 0px; position: relative;"><pre class="prefixed custom_prefix language-bash" style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: rgb(17, 25, 46); border-radius: 16px; color: rgb(247, 248, 251); line-height: 22px; padding: 24px; box-shadow: rgba(17, 25, 46, 0.1) 0px 0px 0px 2px inset; display: block; overflow: auto; white-space: normal; overflow-wrap: normal;"><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: transparent; border-radius: 0px; color: inherit; line-height: 22px; padding: 0px; white-space: normal; text-shadow: none;"><ol style="box-sizing: border-box; margin: 0px; list-style: none; padding: 0px;"><li data-prefix="postgres=#" style="box-sizing: border-box; margin: 0px; padding: 0px; white-space: pre;">CREATE DATABASE <mark style="box-sizing: border-box; margin: 0px; background: rgb(41, 51, 77); border-radius: 2px; color: inherit; display: inline; line-height: calc(1.4em + 1px); padding: 2px 6px;">myproject</mark><span class="token punctuation" style="box-sizing: border-box; margin: 0px; text-shadow: none; background: none; border-radius: 0px; display: inline; font-weight: 400; padding: 0px; color: rgb(247, 248, 251);">;</span> </li></ol></code></pre><div class="toolbar" style="box-sizing: border-box; margin: 0px; position: absolute; right: 24px; top: 24px;"><div class="toolbar-item" style="box-sizing: border-box; margin: 0px; display: inline-block;"><button type="button" style="box-sizing: border-box; margin: 0px; font-family: inherit; font-size: 0.9em; line-height: normal; overflow: visible; text-transform: none; appearance: button; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-optical-sizing: inherit; font-kerning: inherit; font-feature-settings: inherit; font-variation-settings: inherit; background: rgb(105, 111, 176); border: 0px; cursor: pointer; user-select: none; border-radius: 10px; color: rgb(255, 255, 255); padding: 6px 8px; transition: color 0.25s ease 0s, background 0.25s ease 0s;">Copy</button></div></div></div><div class="callout note" style="box-sizing: border-box; margin: 0px 0px 1.5em; background-color: rgb(213, 240, 195); border-radius: 16px; color: rgb(0, 12, 42); display: block; font-size: 15px; line-height: 1.5em; padding: 1em 1.25em; position: relative; z-index: 0;"><p style="box-sizing: border-box; margin: 0px; line-height: 1.5;"><strong style="box-sizing: border-box; margin: 0px; font-weight: bolder;">Note:</strong><span> </span>Every Postgres statement must end with a semicolon. Make sure that your command ends with one if you are experiencing issues.</p></div><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">Next, create a database user for your project and select a secure password:</p><div class="code-toolbar" style="box-sizing: border-box; margin: 1em 0px; position: relative;"><pre class="prefixed custom_prefix language-bash" style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: rgb(17, 25, 46); border-radius: 16px; color: rgb(247, 248, 251); line-height: 22px; padding: 24px; box-shadow: rgba(17, 25, 46, 0.1) 0px 0px 0px 2px inset; display: block; overflow: auto; white-space: normal; overflow-wrap: normal;"><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: transparent; border-radius: 0px; color: inherit; line-height: 22px; padding: 0px; white-space: normal; text-shadow: none;"><ol style="box-sizing: border-box; margin: 0px; list-style: none; padding: 0px;"><li data-prefix="postgres=#" style="box-sizing: border-box; margin: 0px; padding: 0px; white-space: pre;">CREATE <span class="token environment constant" style="box-sizing: border-box; margin: 0px; text-shadow: none; background: none; border-radius: 0px; display: inline; font-weight: 400; padding: 0px; color: rgb(159, 221, 255);">USER</span> <mark style="box-sizing: border-box; margin: 0px; background: rgb(41, 51, 77); border-radius: 2px; color: inherit; display: inline; line-height: calc(1.4em + 1px); padding: 2px 6px;">myprojectuser</mark> WITH PASSWORD <span class="token string" style="box-sizing: border-box; margin: 0px; text-shadow: none; background: none; border-radius: 0px; display: inline; font-weight: 400; padding: 0px; color: rgb(177, 228, 144);">'<mark style="box-sizing: border-box; margin: 0px; background: rgb(41, 51, 77); border-radius: 2px; color: inherit; display: inline; line-height: calc(1.4em + 1px); padding: 2px 6px;">password</mark>'</span><span class="token punctuation" style="box-sizing: border-box; margin: 0px; text-shadow: none; background: none; border-radius: 0px; display: inline; font-weight: 400; padding: 0px; color: rgb(247, 248, 251);">;</span> </li></ol></code></pre><div class="toolbar" style="box-sizing: border-box; margin: 0px; position: absolute; right: 24px; top: 24px;"><div class="toolbar-item" style="box-sizing: border-box; margin: 0px; display: inline-block;"><button type="button" style="box-sizing: border-box; margin: 0px; font-family: inherit; font-size: 0.9em; line-height: normal; overflow: visible; text-transform: none; appearance: button; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-optical-sizing: inherit; font-kerning: inherit; font-feature-settings: inherit; font-variation-settings: inherit; background: rgb(105, 111, 176); border: 0px; cursor: pointer; user-select: none; border-radius: 10px; color: rgb(255, 255, 255); padding: 6px 8px; transition: color 0.25s ease 0s, background 0.25s ease 0s;">Copy</button></div></div></div><pre style="box-sizing: border-box; margin: 1em 0px; font-family: monospace, monospace; font-size: 14px; background: rgb(17, 25, 46); border-radius: 16px; color: rgb(247, 248, 251); line-height: 22px; padding: 24px; box-shadow: rgba(17, 25, 46, 0.1) 0px 0px 0px 2px inset; display: block; overflow: auto; white-space: normal; overflow-wrap: normal;"><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: 0px center; border-radius: 0px; color: inherit; line-height: 22px; padding: 0px; white-space: pre;"><div class="secondary-code-label" title="Output" style="box-sizing: border-box; margin: 0px 0px 4px; color: rgb(159, 221, 255);">Output</div>CREATE ROLE </code></pre><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">After, modify a few of the connection parameters for the user you created. This will speed up database operations so that the correct values do not have to be queried and set each time a connection is established.</p><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">Set the default encoding to<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">UTF-8</code>, which Django expects:</p><div class="code-toolbar" style="box-sizing: border-box; margin: 1em 0px; position: relative;"><pre class="prefixed custom_prefix language-bash" style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: rgb(17, 25, 46); border-radius: 16px; color: rgb(247, 248, 251); line-height: 22px; padding: 24px; box-shadow: rgba(17, 25, 46, 0.1) 0px 0px 0px 2px inset; display: block; overflow: auto; white-space: normal; overflow-wrap: normal;"><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: transparent; border-radius: 0px; color: inherit; line-height: 22px; padding: 0px; white-space: normal; text-shadow: none;"><ol style="box-sizing: border-box; margin: 0px; list-style: none; padding: 0px;"><li data-prefix="postgres=#" style="box-sizing: border-box; margin: 0px; padding: 0px; white-space: pre;">ALTER ROLE <mark style="box-sizing: border-box; margin: 0px; background: rgb(41, 51, 77); border-radius: 2px; color: inherit; display: inline; line-height: calc(1.4em + 1px); padding: 2px 6px;">myprojectuser</mark> SET client_encoding TO <span class="token string" style="box-sizing: border-box; margin: 0px; text-shadow: none; background: none; border-radius: 0px; display: inline; font-weight: 400; padding: 0px; color: rgb(177, 228, 144);">'utf8'</span><span class="token punctuation" style="box-sizing: border-box; margin: 0px; text-shadow: none; background: none; border-radius: 0px; display: inline; font-weight: 400; padding: 0px; color: rgb(247, 248, 251);">;</span> </li></ol></code></pre><div class="toolbar" style="box-sizing: border-box; margin: 0px; position: absolute; right: 24px; top: 24px;"><div class="toolbar-item" style="box-sizing: border-box; margin: 0px; display: inline-block;"><button type="button" style="box-sizing: border-box; margin: 0px; font-family: inherit; font-size: 0.9em; line-height: normal; overflow: visible; text-transform: none; appearance: button; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-optical-sizing: inherit; font-kerning: inherit; font-feature-settings: inherit; font-variation-settings: inherit; background: rgb(105, 111, 176); border: 0px; cursor: pointer; user-select: none; border-radius: 10px; color: rgb(255, 255, 255); padding: 6px 8px; transition: color 0.25s ease 0s, background 0.25s ease 0s;">Copy</button></div></div></div><pre style="box-sizing: border-box; margin: 1em 0px; font-family: monospace, monospace; font-size: 14px; background: rgb(17, 25, 46); border-radius: 16px; color: rgb(247, 248, 251); line-height: 22px; padding: 24px; box-shadow: rgba(17, 25, 46, 0.1) 0px 0px 0px 2px inset; display: block; overflow: auto; white-space: normal; overflow-wrap: normal;"><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: 0px center; border-radius: 0px; color: inherit; line-height: 22px; padding: 0px; white-space: pre;"><div class="secondary-code-label" title="Output" style="box-sizing: border-box; margin: 0px 0px 4px; color: rgb(159, 221, 255);">Output</div>ALTER ROLE </code></pre><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">Then set the default transaction isolation scheme to<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">read committed</code><span> </span>to block reads from uncommitted transactions:</p><div class="code-toolbar" style="box-sizing: border-box; margin: 1em 0px; position: relative;"><pre class="prefixed custom_prefix language-bash" style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: rgb(17, 25, 46); border-radius: 16px; color: rgb(247, 248, 251); line-height: 22px; padding: 24px; box-shadow: rgba(17, 25, 46, 0.1) 0px 0px 0px 2px inset; display: block; overflow: auto; white-space: normal; overflow-wrap: normal;"><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: transparent; border-radius: 0px; color: inherit; line-height: 22px; padding: 0px; white-space: normal; text-shadow: none;"><ol style="box-sizing: border-box; margin: 0px; list-style: none; padding: 0px;"><li data-prefix="postgres=#" style="box-sizing: border-box; margin: 0px; padding: 0px; white-space: pre;">ALTER ROLE <mark style="box-sizing: border-box; margin: 0px; background: rgb(41, 51, 77); border-radius: 2px; color: inherit; display: inline; line-height: calc(1.4em + 1px); padding: 2px 6px;">myprojectuser</mark> SET default_transaction_isolation TO <span class="token string" style="box-sizing: border-box; margin: 0px; text-shadow: none; background: none; border-radius: 0px; display: inline; font-weight: 400; padding: 0px; color: rgb(177, 228, 144);">'read committed'</span><span class="token punctuation" style="box-sizing: border-box; margin: 0px; text-shadow: none; background: none; border-radius: 0px; display: inline; font-weight: 400; padding: 0px; color: rgb(247, 248, 251);">;</span> </li></ol></code></pre><div class="toolbar" style="box-sizing: border-box; margin: 0px; position: absolute; right: 24px; top: 24px;"><div class="toolbar-item" style="box-sizing: border-box; margin: 0px; display: inline-block;"><button type="button" style="box-sizing: border-box; margin: 0px; font-family: inherit; font-size: 0.9em; line-height: normal; overflow: visible; text-transform: none; appearance: button; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-optical-sizing: inherit; font-kerning: inherit; font-feature-settings: inherit; font-variation-settings: inherit; background: rgb(105, 111, 176); border: 0px; cursor: pointer; user-select: none; border-radius: 10px; color: rgb(255, 255, 255); padding: 6px 8px; transition: color 0.25s ease 0s, background 0.25s ease 0s;">Copy</button></div></div></div><pre style="box-sizing: border-box; margin: 1em 0px; font-family: monospace, monospace; font-size: 14px; background: rgb(17, 25, 46); border-radius: 16px; color: rgb(247, 248, 251); line-height: 22px; padding: 24px; box-shadow: rgba(17, 25, 46, 0.1) 0px 0px 0px 2px inset; display: block; overflow: auto; white-space: normal; overflow-wrap: normal;"><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: 0px center; border-radius: 0px; color: inherit; line-height: 22px; padding: 0px; white-space: pre;"><div class="secondary-code-label" title="Output" style="box-sizing: border-box; margin: 0px 0px 4px; color: rgb(159, 221, 255);">Output</div>ALTER ROLE </code></pre><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">Since your Django projects will be set to use<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">UTC</code><span> </span>by default, set the relevant timezone:</p><div class="code-toolbar" style="box-sizing: border-box; margin: 1em 0px; position: relative;"><pre class="prefixed custom_prefix language-bash" style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: rgb(17, 25, 46); border-radius: 16px; color: rgb(247, 248, 251); line-height: 22px; padding: 24px; box-shadow: rgba(17, 25, 46, 0.1) 0px 0px 0px 2px inset; display: block; overflow: auto; white-space: normal; overflow-wrap: normal;"><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: transparent; border-radius: 0px; color: inherit; line-height: 22px; padding: 0px; white-space: normal; text-shadow: none;"><ol style="box-sizing: border-box; margin: 0px; list-style: none; padding: 0px;"><li data-prefix="postgres=#" style="box-sizing: border-box; margin: 0px; padding: 0px; white-space: pre;">ALTER ROLE <mark style="box-sizing: border-box; margin: 0px; background: rgb(41, 51, 77); border-radius: 2px; color: inherit; display: inline; line-height: calc(1.4em + 1px); padding: 2px 6px;">myprojectuser</mark> SET timezone TO <span class="token string" style="box-sizing: border-box; margin: 0px; text-shadow: none; background: none; border-radius: 0px; display: inline; font-weight: 400; padding: 0px; color: rgb(177, 228, 144);">'UTC'</span><span class="token punctuation" style="box-sizing: border-box; margin: 0px; text-shadow: none; background: none; border-radius: 0px; display: inline; font-weight: 400; padding: 0px; color: rgb(247, 248, 251);">;</span> </li></ol></code></pre><div class="toolbar" style="box-sizing: border-box; margin: 0px; position: absolute; right: 24px; top: 24px;"><div class="toolbar-item" style="box-sizing: border-box; margin: 0px; display: inline-block;"><button type="button" style="box-sizing: border-box; margin: 0px; font-family: inherit; font-size: 0.9em; line-height: normal; overflow: visible; text-transform: none; appearance: button; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-optical-sizing: inherit; font-kerning: inherit; font-feature-settings: inherit; font-variation-settings: inherit; background: rgb(105, 111, 176); border: 0px; cursor: pointer; user-select: none; border-radius: 10px; color: rgb(255, 255, 255); padding: 6px 8px; transition: color 0.25s ease 0s, background 0.25s ease 0s;">Copy</button></div></div></div><pre style="box-sizing: border-box; margin: 1em 0px; font-family: monospace, monospace; font-size: 14px; background: rgb(17, 25, 46); border-radius: 16px; color: rgb(247, 248, 251); line-height: 22px; padding: 24px; box-shadow: rgba(17, 25, 46, 0.1) 0px 0px 0px 2px inset; display: block; overflow: auto; white-space: normal; overflow-wrap: normal;"><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: 0px center; border-radius: 0px; color: inherit; line-height: 22px; padding: 0px; white-space: pre;"><div class="secondary-code-label" title="Output" style="box-sizing: border-box; margin: 0px 0px 4px; color: rgb(159, 221, 255);">Output</div>ALTER ROLE </code></pre><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">These are all recommendations from<span> </span><a href="https://docs.djangoproject.com/en/4.0/ref/databases/#optimizing-postgresql-s-configuration" style="box-sizing: border-box; margin: 0px; background: linear-gradient(rgb(0, 105, 255), rgb(0, 105, 255)) center bottom / 1px 1px repeat-x; color: rgb(0, 105, 255); text-decoration: none; border-bottom: none; transition: background 0.3s ease-in-out 0s, color 0.3s ease-in-out 0s; overflow-wrap: break-word;">the Django project</a>.</p><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">Now give your new user access to administer your new database:</p><div class="code-toolbar" style="box-sizing: border-box; margin: 1em 0px; position: relative;"><pre class="prefixed custom_prefix language-bash" style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: rgb(17, 25, 46); border-radius: 16px; color: rgb(247, 248, 251); line-height: 22px; padding: 24px; box-shadow: rgba(17, 25, 46, 0.1) 0px 0px 0px 2px inset; display: block; overflow: auto; white-space: normal; overflow-wrap: normal;"><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: transparent; border-radius: 0px; color: inherit; line-height: 22px; padding: 0px; white-space: normal; text-shadow: none;"><ol style="box-sizing: border-box; margin: 0px; list-style: none; padding: 0px;"><li data-prefix="postgres=#" style="box-sizing: border-box; margin: 0px; padding: 0px; white-space: pre;">GRANT ALL PRIVILEGES ON DATABASE <mark style="box-sizing: border-box; margin: 0px; background: rgb(41, 51, 77); border-radius: 2px; color: inherit; display: inline; line-height: calc(1.4em + 1px); padding: 2px 6px;">myproject</mark> TO <mark style="box-sizing: border-box; margin: 0px; background: rgb(41, 51, 77); border-radius: 2px; color: inherit; display: inline; line-height: calc(1.4em + 1px); padding: 2px 6px;">myprojectuser</mark><span class="token punctuation" style="box-sizing: border-box; margin: 0px; text-shadow: none; background: none; border-radius: 0px; display: inline; font-weight: 400; padding: 0px; color: rgb(247, 248, 251);">;</span> </li></ol></code></pre><div class="toolbar" style="box-sizing: border-box; margin: 0px; position: absolute; right: 24px; top: 24px;"><div class="toolbar-item" style="box-sizing: border-box; margin: 0px; display: inline-block;"><button type="button" style="box-sizing: border-box; margin: 0px; font-family: inherit; font-size: 0.9em; line-height: normal; overflow: visible; text-transform: none; appearance: button; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-optical-sizing: inherit; font-kerning: inherit; font-feature-settings: inherit; font-variation-settings: inherit; background: rgb(105, 111, 176); border: 0px; cursor: pointer; user-select: none; border-radius: 10px; color: rgb(255, 255, 255); padding: 6px 8px; transition: color 0.25s ease 0s, background 0.25s ease 0s;">Copy</button></div></div></div><pre style="box-sizing: border-box; margin: 1em 0px; font-family: monospace, monospace; font-size: 14px; background: rgb(17, 25, 46); border-radius: 16px; color: rgb(247, 248, 251); line-height: 22px; padding: 24px; box-shadow: rgba(17, 25, 46, 0.1) 0px 0px 0px 2px inset; display: block; overflow: auto; white-space: normal; overflow-wrap: normal;"><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: 0px center; border-radius: 0px; color: inherit; line-height: 22px; padding: 0px; white-space: pre;"><div class="secondary-code-label" title="Output" style="box-sizing: border-box; margin: 0px 0px 4px; color: rgb(159, 221, 255);">Output</div>GRANT </code></pre><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">When you are finished, exit out of the PostgreSQL prompt by running the following:</p><div class="code-toolbar" style="box-sizing: border-box; margin: 1em 0px; position: relative;"><pre class="prefixed custom_prefix language-bash" style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: rgb(17, 25, 46); border-radius: 16px; color: rgb(247, 248, 251); line-height: 22px; padding: 24px; box-shadow: rgba(17, 25, 46, 0.1) 0px 0px 0px 2px inset; display: block; overflow: auto; white-space: normal; overflow-wrap: normal;"><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: transparent; border-radius: 0px; color: inherit; line-height: 22px; padding: 0px; white-space: normal; text-shadow: none;"><ol style="box-sizing: border-box; margin: 0px; list-style: none; padding: 0px;"><li data-prefix="postgres=#" style="box-sizing: border-box; margin: 0px; padding: 0px; white-space: pre;"><span class="token punctuation" style="box-sizing: border-box; margin: 0px; text-shadow: none; background: none; border-radius: 0px; display: inline; font-weight: 400; padding: 0px; color: rgb(247, 248, 251);">\</span>q </li></ol></code></pre><div class="toolbar" style="box-sizing: border-box; margin: 0px; position: absolute; right: 24px; top: 24px;"><div class="toolbar-item" style="box-sizing: border-box; margin: 0px; display: inline-block;"><button type="button" style="box-sizing: border-box; margin: 0px; font-family: inherit; font-size: 0.9em; line-height: normal; overflow: visible; text-transform: none; appearance: button; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-optical-sizing: inherit; font-kerning: inherit; font-feature-settings: inherit; font-variation-settings: inherit; background: rgb(105, 111, 176); border: 0px; cursor: pointer; user-select: none; border-radius: 10px; color: rgb(255, 255, 255); padding: 6px 8px; transition: color 0.25s ease 0s, background 0.25s ease 0s;">Copy</button></div></div></div><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">Postgres is now successfully set up so that Django can connect to and manage its database information.</p><h2 id="step-3-creating-a-python-virtual-environment-for-your-project" style="box-sizing: border-box; margin: 1em 0px; font-family: Epilogue, sans-serif;"><a href="https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-18-04#step-3-creating-a-python-virtual-environment-for-your-project" style="box-sizing: border-box; margin: 0px; background: linear-gradient(rgb(0, 105, 255), rgb(0, 105, 255)) center bottom / 0px 0px repeat-x; color: inherit; text-decoration: none; border-bottom: none; transition: background 0.3s ease-in-out 0s, color 0.3s ease-in-out 0s;">Step 3 — Creating a Python Virtual Environment for your Project</a><a class="hash-anchor" href="https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-18-04#step-3-creating-a-python-virtual-environment-for-your-project" aria-hidden="true" style="box-sizing: border-box; margin: 0px 0px 0px 0.5em; background: linear-gradient(rgb(0, 105, 255), rgb(0, 105, 255)) center bottom / 0px 0px repeat-x; color: rgb(138, 150, 181); text-decoration: none; border-bottom: none; transition: opacity 0.2s ease-in-out 0s, visibility 0s linear 0.2s; font-size: 0.85em; opacity: 0; visibility: hidden;"></a></h2><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">Now that you have your database, you can begin getting the rest of your project requirements ready. This entails installing your Python requirements within a virtual environment for more efficient management. Installing Django into a virtual environment specific to your project will allow your projects and their requirements to be handled separately.</p><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">To do this, you’ll need access to the<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">virtualenv</code><span> </span>command. Start by installing this with<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">pip</code>.</p><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">If you’re using<span> </span><strong style="box-sizing: border-box; margin: 0px; font-weight: bolder;">Python 3</strong>, upgrade<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">pip</code>:</p><div class="code-toolbar" style="box-sizing: border-box; margin: 1em 0px; position: relative;"><pre class="prefixed command language-bash" style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: rgb(17, 25, 46); border-radius: 16px; color: rgb(247, 248, 251); line-height: 22px; padding: 24px; box-shadow: rgba(17, 25, 46, 0.1) 0px 0px 0px 2px inset; display: block; overflow: auto; white-space: normal; overflow-wrap: normal;"><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: transparent; border-radius: 0px; color: inherit; line-height: 22px; padding: 0px; white-space: normal; text-shadow: none;"><ol style="box-sizing: border-box; margin: 0px; list-style: none; padding: 0px;"><li data-prefix="$" style="box-sizing: border-box; margin: 0px; padding: 0px; white-space: pre;"><span class="token function" style="box-sizing: border-box; margin: 0px; text-shadow: none; background: none; border-radius: 0px; display: inline; font-weight: 400; padding: 0px; color: rgb(255, 175, 140);">sudo</span> <span class="token parameter variable" style="box-sizing: border-box; margin: 0px; text-shadow: none; background: none; border-radius: 0px; display: inline; font-weight: 400; padding: 0px; color: rgb(177, 228, 144);">-H</span> pip3 <span class="token function" style="box-sizing: border-box; margin: 0px; text-shadow: none; background: none; border-radius: 0px; display: inline; font-weight: 400; padding: 0px; color: rgb(255, 175, 140);">install</span> <span class="token parameter variable" style="box-sizing: border-box; margin: 0px; text-shadow: none; background: none; border-radius: 0px; display: inline; font-weight: 400; padding: 0px; color: rgb(177, 228, 144);">--upgrade</span> pip </li></ol></code></pre><div class="toolbar" style="box-sizing: border-box; margin: 0px; position: absolute; right: 24px; top: 24px;"><div class="toolbar-item" style="box-sizing: border-box; margin: 0px; display: inline-block;"><button type="button" style="box-sizing: border-box; margin: 0px; font-family: inherit; font-size: 0.9em; line-height: normal; overflow: visible; text-transform: none; appearance: button; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-optical-sizing: inherit; font-kerning: inherit; font-feature-settings: inherit; font-variation-settings: inherit; background: rgb(105, 111, 176); border: 0px; cursor: pointer; user-select: none; border-radius: 10px; color: rgb(255, 255, 255); padding: 6px 8px; transition: color 0.25s ease 0s, background 0.25s ease 0s;">Copy</button></div></div></div><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">Then install the package:</p><div class="code-toolbar" style="box-sizing: border-box; margin: 1em 0px; position: relative;"><pre class="prefixed command language-bash" style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: rgb(17, 25, 46); border-radius: 16px; color: rgb(247, 248, 251); line-height: 22px; padding: 24px; box-shadow: rgba(17, 25, 46, 0.1) 0px 0px 0px 2px inset; display: block; overflow: auto; white-space: normal; overflow-wrap: normal;"><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: transparent; border-radius: 0px; color: inherit; line-height: 22px; padding: 0px; white-space: normal; text-shadow: none;"><ol style="box-sizing: border-box; margin: 0px; list-style: none; padding: 0px;"><li data-prefix="$" style="box-sizing: border-box; margin: 0px; padding: 0px; white-space: pre;"><span class="token function" style="box-sizing: border-box; margin: 0px; text-shadow: none; background: none; border-radius: 0px; display: inline; font-weight: 400; padding: 0px; color: rgb(255, 175, 140);">sudo</span> <span class="token parameter variable" style="box-sizing: border-box; margin: 0px; text-shadow: none; background: none; border-radius: 0px; display: inline; font-weight: 400; padding: 0px; color: rgb(177, 228, 144);">-H</span> pip3 <span class="token function" style="box-sizing: border-box; margin: 0px; text-shadow: none; background: none; border-radius: 0px; display: inline; font-weight: 400; padding: 0px; color: rgb(255, 175, 140);">install</span> virtualenv </li></ol></code></pre><div class="toolbar" style="box-sizing: border-box; margin: 0px; position: absolute; right: 24px; top: 24px;"><div class="toolbar-item" style="box-sizing: border-box; margin: 0px; display: inline-block;"><button type="button" style="box-sizing: border-box; margin: 0px; font-family: inherit; font-size: 0.9em; line-height: normal; overflow: visible; text-transform: none; appearance: button; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-optical-sizing: inherit; font-kerning: inherit; font-feature-settings: inherit; font-variation-settings: inherit; background: rgb(105, 111, 176); border: 0px; cursor: pointer; user-select: none; border-radius: 10px; color: rgb(255, 255, 255); padding: 6px 8px; transition: color 0.25s ease 0s, background 0.25s ease 0s;">Copy</button></div></div></div><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">If you’re using<span> </span><strong style="box-sizing: border-box; margin: 0px; font-weight: bolder;">Python 2</strong>, upgrade<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">pip</code>:</p><div class="code-toolbar" style="box-sizing: border-box; margin: 1em 0px; position: relative;"><pre class="prefixed command language-bash" style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: rgb(17, 25, 46); border-radius: 16px; color: rgb(247, 248, 251); line-height: 22px; padding: 24px; box-shadow: rgba(17, 25, 46, 0.1) 0px 0px 0px 2px inset; display: block; overflow: auto; white-space: normal; overflow-wrap: normal;"><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: transparent; border-radius: 0px; color: inherit; line-height: 22px; padding: 0px; white-space: normal; text-shadow: none;"><ol style="box-sizing: border-box; margin: 0px; list-style: none; padding: 0px;"><li data-prefix="$" style="box-sizing: border-box; margin: 0px; padding: 0px; white-space: pre;"><span class="token function" style="box-sizing: border-box; margin: 0px; text-shadow: none; background: none; border-radius: 0px; display: inline; font-weight: 400; padding: 0px; color: rgb(255, 175, 140);">sudo</span> <span class="token parameter variable" style="box-sizing: border-box; margin: 0px; text-shadow: none; background: none; border-radius: 0px; display: inline; font-weight: 400; padding: 0px; color: rgb(177, 228, 144);">-H</span> pip <span class="token function" style="box-sizing: border-box; margin: 0px; text-shadow: none; background: none; border-radius: 0px; display: inline; font-weight: 400; padding: 0px; color: rgb(255, 175, 140);">install</span> <span class="token parameter variable" style="box-sizing: border-box; margin: 0px; text-shadow: none; background: none; border-radius: 0px; display: inline; font-weight: 400; padding: 0px; color: rgb(177, 228, 144);">--upgrade</span> pip </li></ol></code></pre><div class="toolbar" style="box-sizing: border-box; margin: 0px; position: absolute; right: 24px; top: 24px;"><div class="toolbar-item" style="box-sizing: border-box; margin: 0px; display: inline-block;"><button type="button" style="box-sizing: border-box; margin: 0px; font-family: inherit; font-size: 0.9em; line-height: normal; overflow: visible; text-transform: none; appearance: button; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-optical-sizing: inherit; font-kerning: inherit; font-feature-settings: inherit; font-variation-settings: inherit; background: rgb(105, 111, 176); border: 0px; cursor: pointer; user-select: none; border-radius: 10px; color: rgb(255, 255, 255); padding: 6px 8px; transition: color 0.25s ease 0s, background 0.25s ease 0s;">Copy</button></div></div></div><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">Then install the package:</p><div class="code-toolbar" style="box-sizing: border-box; margin: 1em 0px; position: relative;"><pre class="prefixed command language-bash" style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: rgb(17, 25, 46); border-radius: 16px; color: rgb(247, 248, 251); line-height: 22px; padding: 24px; box-shadow: rgba(17, 25, 46, 0.1) 0px 0px 0px 2px inset; display: block; overflow: auto; white-space: normal; overflow-wrap: normal;"><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: transparent; border-radius: 0px; color: inherit; line-height: 22px; padding: 0px; white-space: normal; text-shadow: none;"><ol style="box-sizing: border-box; margin: 0px; list-style: none; padding: 0px;"><li data-prefix="$" style="box-sizing: border-box; margin: 0px; padding: 0px; white-space: pre;"><span class="token function" style="box-sizing: border-box; margin: 0px; text-shadow: none; background: none; border-radius: 0px; display: inline; font-weight: 400; padding: 0px; color: rgb(255, 175, 140);">sudo</span> <span class="token parameter variable" style="box-sizing: border-box; margin: 0px; text-shadow: none; background: none; border-radius: 0px; display: inline; font-weight: 400; padding: 0px; color: rgb(177, 228, 144);">-H</span> pip <span class="token function" style="box-sizing: border-box; margin: 0px; text-shadow: none; background: none; border-radius: 0px; display: inline; font-weight: 400; padding: 0px; color: rgb(255, 175, 140);">install</span> virtualenv </li></ol></code></pre><div class="toolbar" style="box-sizing: border-box; margin: 0px; position: absolute; right: 24px; top: 24px;"><div class="toolbar-item" style="box-sizing: border-box; margin: 0px; display: inline-block;"><button type="button" style="box-sizing: border-box; margin: 0px; font-family: inherit; font-size: 0.9em; line-height: normal; overflow: visible; text-transform: none; appearance: button; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-optical-sizing: inherit; font-kerning: inherit; font-feature-settings: inherit; font-variation-settings: inherit; background: rgb(105, 111, 176); border: 0px; cursor: pointer; user-select: none; border-radius: 10px; color: rgb(255, 255, 255); padding: 6px 8px; transition: color 0.25s ease 0s, background 0.25s ease 0s;">Copy</button></div></div></div><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">With<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">virtualenv</code><span> </span>installed, you can begin forming your project. First create the directory where you can keep your project files. Here we’ll name it<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">myprojectdir</code>, feel free to name it whatever you prefer:</p><div class="code-toolbar" style="box-sizing: border-box; margin: 1em 0px; position: relative;"><pre class="prefixed command language-bash" style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: rgb(17, 25, 46); border-radius: 16px; color: rgb(247, 248, 251); line-height: 22px; padding: 24px; box-shadow: rgba(17, 25, 46, 0.1) 0px 0px 0px 2px inset; display: block; overflow: auto; white-space: normal; overflow-wrap: normal;"><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: transparent; border-radius: 0px; color: inherit; line-height: 22px; padding: 0px; white-space: normal; text-shadow: none;"><ol style="box-sizing: border-box; margin: 0px; list-style: none; padding: 0px;"><li data-prefix="$" style="box-sizing: border-box; margin: 0px; padding: 0px; white-space: pre;"><span class="token function" style="box-sizing: border-box; margin: 0px; text-shadow: none; background: none; border-radius: 0px; display: inline; font-weight: 400; padding: 0px; color: rgb(255, 175, 140);">mkdir</span> ~/<mark style="box-sizing: border-box; margin: 0px; background: rgb(41, 51, 77); border-radius: 2px; color: inherit; display: inline; line-height: calc(1.4em + 1px); padding: 2px 6px;">myprojectdir</mark> </li></ol></code></pre><div class="toolbar" style="box-sizing: border-box; margin: 0px; position: absolute; right: 24px; top: 24px;"><div class="toolbar-item" style="box-sizing: border-box; margin: 0px; display: inline-block;"><button type="button" style="box-sizing: border-box; margin: 0px; font-family: inherit; font-size: 0.9em; line-height: normal; overflow: visible; text-transform: none; appearance: button; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-optical-sizing: inherit; font-kerning: inherit; font-feature-settings: inherit; font-variation-settings: inherit; background: rgb(105, 111, 176); border: 0px; cursor: pointer; user-select: none; border-radius: 10px; color: rgb(255, 255, 255); padding: 6px 8px; transition: color 0.25s ease 0s, background 0.25s ease 0s;">Copy</button></div></div></div><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">Next move into that directory:</p><div class="code-toolbar" style="box-sizing: border-box; margin: 1em 0px; position: relative;"><pre class="prefixed command language-bash" style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: rgb(17, 25, 46); border-radius: 16px; color: rgb(247, 248, 251); line-height: 22px; padding: 24px; box-shadow: rgba(17, 25, 46, 0.1) 0px 0px 0px 2px inset; display: block; overflow: auto; white-space: normal; overflow-wrap: normal;"><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: transparent; border-radius: 0px; color: inherit; line-height: 22px; padding: 0px; white-space: normal; text-shadow: none;"><ol style="box-sizing: border-box; margin: 0px; list-style: none; padding: 0px;"><li data-prefix="$" style="box-sizing: border-box; margin: 0px; padding: 0px; white-space: pre;"><span class="token builtin class-name" style="box-sizing: border-box; margin: 0px; text-shadow: none; background: none; border-radius: 0px; display: inline; font-weight: 400; padding: 0px; color: rgb(255, 175, 140);">cd</span> ~/<mark style="box-sizing: border-box; margin: 0px; background: rgb(41, 51, 77); border-radius: 2px; color: inherit; display: inline; line-height: calc(1.4em + 1px); padding: 2px 6px;">myprojectdir</mark> </li></ol></code></pre><div class="toolbar" style="box-sizing: border-box; margin: 0px; position: absolute; right: 24px; top: 24px;"><div class="toolbar-item" style="box-sizing: border-box; margin: 0px; display: inline-block;"><button type="button" style="box-sizing: border-box; margin: 0px; font-family: inherit; font-size: 0.9em; line-height: normal; overflow: visible; text-transform: none; appearance: button; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-optical-sizing: inherit; font-kerning: inherit; font-feature-settings: inherit; font-variation-settings: inherit; background: rgb(105, 111, 176); border: 0px; cursor: pointer; user-select: none; border-radius: 10px; color: rgb(255, 255, 255); padding: 6px 8px; transition: color 0.25s ease 0s, background 0.25s ease 0s;">Copy</button></div></div></div><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">Within the project directory, create a Python virtual environment:</p><div class="code-toolbar" style="box-sizing: border-box; margin: 1em 0px; position: relative;"><pre class="prefixed command language-bash" style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: rgb(17, 25, 46); border-radius: 16px; color: rgb(247, 248, 251); line-height: 22px; padding: 24px; box-shadow: rgba(17, 25, 46, 0.1) 0px 0px 0px 2px inset; display: block; overflow: auto; white-space: normal; overflow-wrap: normal;"><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: transparent; border-radius: 0px; color: inherit; line-height: 22px; padding: 0px; white-space: normal; text-shadow: none;"><ol style="box-sizing: border-box; margin: 0px; list-style: none; padding: 0px;"><li data-prefix="$" style="box-sizing: border-box; margin: 0px; padding: 0px; white-space: pre;">virtualenv <mark style="box-sizing: border-box; margin: 0px; background: rgb(41, 51, 77); border-radius: 2px; color: inherit; display: inline; line-height: calc(1.4em + 1px); padding: 2px 6px;">myprojectenv</mark> </li></ol></code></pre><div class="toolbar" style="box-sizing: border-box; margin: 0px; position: absolute; right: 24px; top: 24px;"><div class="toolbar-item" style="box-sizing: border-box; margin: 0px; display: inline-block;"><button type="button" style="box-sizing: border-box; margin: 0px; font-family: inherit; font-size: 0.9em; line-height: normal; overflow: visible; text-transform: none; appearance: button; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-optical-sizing: inherit; font-kerning: inherit; font-feature-settings: inherit; font-variation-settings: inherit; background: rgb(105, 111, 176); border: 0px; cursor: pointer; user-select: none; border-radius: 10px; color: rgb(255, 255, 255); padding: 6px 8px; transition: color 0.25s ease 0s, background 0.25s ease 0s;">Copy</button></div></div></div><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">This will create a directory called<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;"><mark style="box-sizing: border-box; margin: 0px; background: rgba(255, 192, 1, 0.3); border-radius: 2px; color: inherit; display: inline; line-height: calc(1.4em + 1px); padding: 2px 6px;">myprojectenv</mark></code><span> </span>within your<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;"><mark style="box-sizing: border-box; margin: 0px; background: rgba(255, 192, 1, 0.3); border-radius: 2px; color: inherit; display: inline; line-height: calc(1.4em + 1px); padding: 2px 6px;">myprojectdir</mark></code><span> </span>directory. Inside, it will install a local version of Python and a local version of<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">pip</code>. You can use this to install and configure an isolated Python environment for your project.</p><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">But before installing your project’s Python requirements, you need to activate the virtual environment:</p><div class="code-toolbar" style="box-sizing: border-box; margin: 1em 0px; position: relative;"><pre class="prefixed command language-bash" style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: rgb(17, 25, 46); border-radius: 16px; color: rgb(247, 248, 251); line-height: 22px; padding: 24px; box-shadow: rgba(17, 25, 46, 0.1) 0px 0px 0px 2px inset; display: block; overflow: auto; white-space: normal; overflow-wrap: normal;"><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: transparent; border-radius: 0px; color: inherit; line-height: 22px; padding: 0px; white-space: normal; text-shadow: none;"><ol style="box-sizing: border-box; margin: 0px; list-style: none; padding: 0px;"><li data-prefix="$" style="box-sizing: border-box; margin: 0px; padding: 0px; white-space: pre;"><span class="token builtin class-name" style="box-sizing: border-box; margin: 0px; text-shadow: none; background: none; border-radius: 0px; display: inline; font-weight: 400; padding: 0px; color: rgb(255, 175, 140);">source</span> <mark style="box-sizing: border-box; margin: 0px; background: rgb(41, 51, 77); border-radius: 2px; color: inherit; display: inline; line-height: calc(1.4em + 1px); padding: 2px 6px;">myprojectenv</mark>/bin/activate </li></ol></code></pre><div class="toolbar" style="box-sizing: border-box; margin: 0px; position: absolute; right: 24px; top: 24px;"><div class="toolbar-item" style="box-sizing: border-box; margin: 0px; display: inline-block;"><button type="button" style="box-sizing: border-box; margin: 0px; font-family: inherit; font-size: 0.9em; line-height: normal; overflow: visible; text-transform: none; appearance: button; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-optical-sizing: inherit; font-kerning: inherit; font-feature-settings: inherit; font-variation-settings: inherit; background: rgb(105, 111, 176); border: 0px; cursor: pointer; user-select: none; border-radius: 10px; color: rgb(255, 255, 255); padding: 6px 8px; transition: color 0.25s ease 0s, background 0.25s ease 0s;">Copy</button></div></div></div><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">Your prompt should change to indicate that you are now operating within a Python virtual environment. It will look something like the following:<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">(<mark style="box-sizing: border-box; margin: 0px; background: rgba(255, 192, 1, 0.3); border-radius: 2px; color: inherit; display: inline; line-height: calc(1.4em + 1px); padding: 2px 6px;">myprojectenv</mark>)<mark style="box-sizing: border-box; margin: 0px; background: rgba(255, 192, 1, 0.3); border-radius: 2px; color: inherit; display: inline; line-height: calc(1.4em + 1px); padding: 2px 6px;">user</mark>@<mark style="box-sizing: border-box; margin: 0px; background: rgba(255, 192, 1, 0.3); border-radius: 2px; color: inherit; display: inline; line-height: calc(1.4em + 1px); padding: 2px 6px;">host</mark>:~/<mark style="box-sizing: border-box; margin: 0px; background: rgba(255, 192, 1, 0.3); border-radius: 2px; color: inherit; display: inline; line-height: calc(1.4em + 1px); padding: 2px 6px;">myprojectdir</mark>$</code>.</p><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">With your virtual environment active, install Django, Gunicorn, and the<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">psycopg2</code><span> </span>PostgreSQL adaptor with the local instance of<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">pip</code>:</p><div class="callout note" style="box-sizing: border-box; margin: 0px 0px 1.5em; background-color: rgb(213, 240, 195); border-radius: 16px; color: rgb(0, 12, 42); display: block; font-size: 15px; line-height: 1.5em; padding: 1em 1.25em; position: relative; z-index: 0;"><p style="box-sizing: border-box; margin: 0px; line-height: 1.5;"><strong style="box-sizing: border-box; margin: 0px; font-weight: bolder;">Note:</strong><span> </span>When the virtual environment is activated (when your prompt has<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">(myprojectenv)</code><span> </span>preceding it), use<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">pip</code><span> </span>instead of<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">pip3</code>, even if you are using Python 3. The virtual environment’s copy of the tool is always named<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">pip</code>, regardless of the Python version.</p></div><div class="code-toolbar" style="box-sizing: border-box; margin: 1em 0px; position: relative;"><pre class="prefixed custom_prefix language-bash" style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: rgb(17, 25, 46); border-radius: 16px; color: rgb(247, 248, 251); line-height: 22px; padding: 24px; box-shadow: rgba(17, 25, 46, 0.1) 0px 0px 0px 2px inset; display: block; overflow: auto; white-space: normal; overflow-wrap: normal;"><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: transparent; border-radius: 0px; color: inherit; line-height: 22px; padding: 0px; white-space: normal; text-shadow: none;"><ol style="box-sizing: border-box; margin: 0px; list-style: none; padding: 0px;"><li data-prefix="(myprojectenv) $" style="box-sizing: border-box; margin: 0px; padding: 0px; white-space: pre;">pip <span class="token function" style="box-sizing: border-box; margin: 0px; text-shadow: none; background: none; border-radius: 0px; display: inline; font-weight: 400; padding: 0px; color: rgb(255, 175, 140);">install</span> django gunicorn psycopg2-binary </li></ol></code></pre><div class="toolbar" style="box-sizing: border-box; margin: 0px; position: absolute; right: 24px; top: 24px;"><div class="toolbar-item" style="box-sizing: border-box; margin: 0px; display: inline-block;"><button type="button" style="box-sizing: border-box; margin: 0px; font-family: inherit; font-size: 0.9em; line-height: normal; overflow: visible; text-transform: none; appearance: button; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-optical-sizing: inherit; font-kerning: inherit; font-feature-settings: inherit; font-variation-settings: inherit; background: rgb(105, 111, 176); border: 0px; cursor: pointer; user-select: none; border-radius: 10px; color: rgb(255, 255, 255); padding: 6px 8px; transition: color 0.25s ease 0s, background 0.25s ease 0s;">Copy</button></div></div></div><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">Now you have all of the software needed to start a Django project.</p><h2 id="step-4-creating-and-configuring-a-new-django-project" style="box-sizing: border-box; margin: 1em 0px; font-family: Epilogue, sans-serif;"><a href="https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-18-04#step-4-creating-and-configuring-a-new-django-project" style="box-sizing: border-box; margin: 0px; background: linear-gradient(rgb(0, 105, 255), rgb(0, 105, 255)) center bottom / 0px 0px repeat-x; color: inherit; text-decoration: none; border-bottom: none; transition: background 0.3s ease-in-out 0s, color 0.3s ease-in-out 0s;">Step 4 — Creating and Configuring a New Django Project</a><a class="hash-anchor" href="https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-18-04#step-4-creating-and-configuring-a-new-django-project" aria-hidden="true" style="box-sizing: border-box; margin: 0px 0px 0px 0.5em; background: linear-gradient(rgb(0, 105, 255), rgb(0, 105, 255)) center bottom / 0px 0px repeat-x; color: rgb(138, 150, 181); text-decoration: none; border-bottom: none; transition: opacity 0.2s ease-in-out 0s, visibility 0s linear 0.2s; font-size: 0.85em; opacity: 0; visibility: hidden;"></a></h2><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">With your Python components installed, now you can create the actual Django project files. Since you already have a project directory, tell Django to install the files here. It will create a second level directory with the actual code, which is normal, and place a management script in this directory. This is important because you’re defining the directory explicitly instead of allowing Django to make decisions relative to your current directory:</p><div class="code-toolbar" style="box-sizing: border-box; margin: 1em 0px; position: relative;"><pre class="prefixed custom_prefix language-bash" style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: rgb(17, 25, 46); border-radius: 16px; color: rgb(247, 248, 251); line-height: 22px; padding: 24px; box-shadow: rgba(17, 25, 46, 0.1) 0px 0px 0px 2px inset; display: block; overflow: auto; white-space: normal; overflow-wrap: normal;"><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: transparent; border-radius: 0px; color: inherit; line-height: 22px; padding: 0px; white-space: normal; text-shadow: none;"><ol style="box-sizing: border-box; margin: 0px; list-style: none; padding: 0px;"><li data-prefix="(myprojectenv) $" style="box-sizing: border-box; margin: 0px; padding: 0px; white-space: pre;">django-admin.py startproject <mark style="box-sizing: border-box; margin: 0px; background: rgb(41, 51, 77); border-radius: 2px; color: inherit; display: inline; line-height: calc(1.4em + 1px); padding: 2px 6px;">myproject</mark> ~/<mark style="box-sizing: border-box; margin: 0px; background: rgb(41, 51, 77); border-radius: 2px; color: inherit; display: inline; line-height: calc(1.4em + 1px); padding: 2px 6px;">myprojectdir</mark> </li></ol></code></pre><div class="toolbar" style="box-sizing: border-box; margin: 0px; position: absolute; right: 24px; top: 24px;"><div class="toolbar-item" style="box-sizing: border-box; margin: 0px; display: inline-block;"><button type="button" style="box-sizing: border-box; margin: 0px; font-family: inherit; font-size: 0.9em; line-height: normal; overflow: visible; text-transform: none; appearance: button; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-optical-sizing: inherit; font-kerning: inherit; font-feature-settings: inherit; font-variation-settings: inherit; background: rgb(105, 111, 176); border: 0px; cursor: pointer; user-select: none; border-radius: 10px; color: rgb(255, 255, 255); padding: 6px 8px; transition: color 0.25s ease 0s, background 0.25s ease 0s;">Copy</button></div></div></div><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">At this point, your project directory (<code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">~/<mark style="box-sizing: border-box; margin: 0px; background: rgba(255, 192, 1, 0.3); border-radius: 2px; color: inherit; display: inline; line-height: calc(1.4em + 1px); padding: 2px 6px;">myprojectdir</mark></code><span> </span>in this case) will have the following content:</p><ul style="box-sizing: border-box; margin: 0px 0px 1.7em; list-style: disc; padding: 0px 0px 0px 2.5em;"><li style="box-sizing: border-box; margin: 0px;"><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">~/myprojectdir/manage.py</code>: A Django project management script.</li><li style="box-sizing: border-box; margin: 0px;"><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">~/myprojectdir/myproject/</code>: The Django project package. This will contain the<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">__init__.py</code>,<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">settings.py</code>,<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">urls.py</code>, and<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">wsgi.py</code><span> </span>files.</li><li style="box-sizing: border-box; margin: 0px;"><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">~/myprojectdir/myprojectenv/</code>: The virtual environment directory you created earlier.</li></ul><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">The next thing to do with your newly created project files is adjust the settings. Open the settings file in your preferred text editor. Here we’ll use<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">nano</code>:</p><div class="code-toolbar" style="box-sizing: border-box; margin: 1em 0px; position: relative;"><pre class="prefixed custom_prefix language-bash" style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: rgb(17, 25, 46); border-radius: 16px; color: rgb(247, 248, 251); line-height: 22px; padding: 24px; box-shadow: rgba(17, 25, 46, 0.1) 0px 0px 0px 2px inset; display: block; overflow: auto; white-space: normal; overflow-wrap: normal;"><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: transparent; border-radius: 0px; color: inherit; line-height: 22px; padding: 0px; white-space: normal; text-shadow: none;"><ol style="box-sizing: border-box; margin: 0px; list-style: none; padding: 0px;"><li data-prefix="(myprojectenv) $" style="box-sizing: border-box; margin: 0px; padding: 0px; white-space: pre;"><span class="token function" style="box-sizing: border-box; margin: 0px; text-shadow: none; background: none; border-radius: 0px; display: inline; font-weight: 400; padding: 0px; color: rgb(255, 175, 140);">nano</span> ~/<mark style="box-sizing: border-box; margin: 0px; background: rgb(41, 51, 77); border-radius: 2px; color: inherit; display: inline; line-height: calc(1.4em + 1px); padding: 2px 6px;">myprojectdir</mark>/<mark style="box-sizing: border-box; margin: 0px; background: rgb(41, 51, 77); border-radius: 2px; color: inherit; display: inline; line-height: calc(1.4em + 1px); padding: 2px 6px;">myproject</mark>/settings.py </li></ol></code></pre><div class="toolbar" style="box-sizing: border-box; margin: 0px; position: absolute; right: 24px; top: 24px;"><div class="toolbar-item" style="box-sizing: border-box; margin: 0px; display: inline-block;"><button type="button" style="box-sizing: border-box; margin: 0px; font-family: inherit; font-size: 0.9em; line-height: normal; overflow: visible; text-transform: none; appearance: button; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-optical-sizing: inherit; font-kerning: inherit; font-feature-settings: inherit; font-variation-settings: inherit; background: rgb(105, 111, 176); border: 0px; cursor: pointer; user-select: none; border-radius: 10px; color: rgb(255, 255, 255); padding: 6px 8px; transition: color 0.25s ease 0s, background 0.25s ease 0s;">Copy</button></div></div></div><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">Start by locating the<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">ALLOWED_HOSTS</code><span> </span>directive. This defines a list of the server’s addresses or domain names that may be used to connect to the Django instance. Any incoming requests with a<span> </span><strong style="box-sizing: border-box; margin: 0px; font-weight: bolder;">Host</strong><span> </span>header not on this list will raise an exception. Django requires that you set this to prevent a certain class of security vulnerability.</p><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">In the square brackets, list the IP addresses or domain names that are associated with your Django server. Each item should be listed in quotations with entries separated by a comma. If you prefer requests for an entire domain and any subdomains, prepend a period to the beginning of the entry. In the following snippet, there are a few commented out examples used to demonstrate:</p><div class="callout note" style="box-sizing: border-box; margin: 0px 0px 1.5em; background-color: rgb(213, 240, 195); border-radius: 16px; color: rgb(0, 12, 42); display: block; font-size: 15px; line-height: 1.5em; padding: 1em 1.25em; position: relative; z-index: 0;"><p style="box-sizing: border-box; margin: 0px; line-height: 1.5;"><strong style="box-sizing: border-box; margin: 0px; font-weight: bolder;">Note:</strong><span> </span>Be sure to include<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">localhost</code><span> </span>as one of the options since you will be proxying connections through a local Nginx instance.</p></div><div class="code-label" title="~/myprojectdir/myproject/settings.py" style="box-sizing: border-box; margin: 1em 0px 0px; background-color: rgb(214, 220, 234); border-radius: 16px 16px 0px 0px; color: rgb(36, 51, 90); display: block; font-size: 16px; padding: 12px; position: relative; text-align: center; z-index: 2;">~/myprojectdir/myproject/settings.py</div><pre style="box-sizing: border-box; margin: 0px 0px 1em; font-family: monospace, monospace; font-size: 14px; background: rgb(17, 25, 46); border-radius: 0px 0px 16px 16px; color: rgb(247, 248, 251); line-height: 22px; padding: 24px; box-shadow: rgba(17, 25, 46, 0.1) 0px 0px 0px 2px inset; display: block; overflow: auto; white-space: normal; overflow-wrap: normal;"><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: 0px center; border-radius: 0px; color: inherit; line-height: 22px; padding: 0px; white-space: pre;">. . . # The simplest case: just add the domain name(s) and IP addresses of your Django server # ALLOWED_HOSTS = [ 'example.com', '203.0.113.5'] # To respond to 'example.com' and any subdomains, start the domain with a dot # ALLOWED_HOSTS = ['.example.com', '203.0.113.5'] ALLOWED_HOSTS = ['<mark style="box-sizing: border-box; margin: 0px; background: rgb(41, 51, 77); border-radius: 2px; color: inherit; display: inline; line-height: calc(1.4em + 1px); padding: 2px 6px;">your_server_domain_or_IP</mark>', '<mark style="box-sizing: border-box; margin: 0px; background: rgb(41, 51, 77); border-radius: 2px; color: inherit; display: inline; line-height: calc(1.4em + 1px); padding: 2px 6px;">second_domain_or_IP</mark>', <mark style="box-sizing: border-box; margin: 0px; background: rgb(41, 51, 77); border-radius: 2px; color: inherit; display: inline; line-height: calc(1.4em + 1px); padding: 2px 6px;">. . .</mark>, 'localhost'] </code></pre><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">Next, find the section that configures database access. It will start with<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">DATABASES</code>. The configuration in the file is for a SQLite database. Since you already created a PostgreSQL database for your project, you’ll need to adjust these settings.</p><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">Update the settings with your PostgreSQL database information. Then tell Django to use the<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">psycopg2</code><span> </span>adaptor you installed with<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">pip</code>. You also need to provide the database name, the username of the database user you just created, the database user’s password, and specify that the database can be found on the local computer. You can leave the<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">PORT</code><span> </span>setting as an empty string:</p><div class="code-label" title="~/myprojectdir/myproject/settings.py" style="box-sizing: border-box; margin: 1em 0px 0px; background-color: rgb(214, 220, 234); border-radius: 16px 16px 0px 0px; color: rgb(36, 51, 90); display: block; font-size: 16px; padding: 12px; position: relative; text-align: center; z-index: 2;">~/myprojectdir/myproject/settings.py</div><pre style="box-sizing: border-box; margin: 0px 0px 1em; font-family: monospace, monospace; font-size: 14px; background: rgb(17, 25, 46); border-radius: 0px 0px 16px 16px; color: rgb(247, 248, 251); line-height: 22px; padding: 24px; box-shadow: rgba(17, 25, 46, 0.1) 0px 0px 0px 2px inset; display: block; overflow: auto; white-space: normal; overflow-wrap: normal;"><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: 0px center; border-radius: 0px; color: inherit; line-height: 22px; padding: 0px; white-space: pre;">. . . DATABASES = { 'default': { 'ENGINE': 'django.db.backends.<mark style="box-sizing: border-box; margin: 0px; background: rgb(41, 51, 77); border-radius: 2px; color: inherit; display: inline; line-height: calc(1.4em + 1px); padding: 2px 6px;">postgresql_psycopg2</mark>', 'NAME': '<mark style="box-sizing: border-box; margin: 0px; background: rgb(41, 51, 77); border-radius: 2px; color: inherit; display: inline; line-height: calc(1.4em + 1px); padding: 2px 6px;">myproject</mark>', 'USER': '<mark style="box-sizing: border-box; margin: 0px; background: rgb(41, 51, 77); border-radius: 2px; color: inherit; display: inline; line-height: calc(1.4em + 1px); padding: 2px 6px;">myprojectuser</mark>', 'PASSWORD': '<mark style="box-sizing: border-box; margin: 0px; background: rgb(41, 51, 77); border-radius: 2px; color: inherit; display: inline; line-height: calc(1.4em + 1px); padding: 2px 6px;">password</mark>', 'HOST': '<mark style="box-sizing: border-box; margin: 0px; background: rgb(41, 51, 77); border-radius: 2px; color: inherit; display: inline; line-height: calc(1.4em + 1px); padding: 2px 6px;">localhost</mark>', 'PORT': '', } } . . . </code></pre><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">Next, go toward the end of the file and add a setting indicating where the static files should be placed. This is necessary so that Nginx can handle requests for these items. The following line tells Django to place them in a directory called<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">static</code><span> </span>in the base project directory:</p><div class="code-label" title="~/myprojectdir/myproject/settings.py" style="box-sizing: border-box; margin: 1em 0px 0px; background-color: rgb(214, 220, 234); border-radius: 16px 16px 0px 0px; color: rgb(36, 51, 90); display: block; font-size: 16px; padding: 12px; position: relative; text-align: center; z-index: 2;">~/myprojectdir/myproject/settings.py</div><pre style="box-sizing: border-box; margin: 0px 0px 1em; font-family: monospace, monospace; font-size: 14px; background: rgb(17, 25, 46); border-radius: 0px 0px 16px 16px; color: rgb(247, 248, 251); line-height: 22px; padding: 24px; box-shadow: rgba(17, 25, 46, 0.1) 0px 0px 0px 2px inset; display: block; overflow: auto; white-space: normal; overflow-wrap: normal;"><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: 0px center; border-radius: 0px; color: inherit; line-height: 22px; padding: 0px; white-space: pre;">. . . STATIC_URL = '/static/' <mark style="box-sizing: border-box; margin: 0px; background: rgb(41, 51, 77); border-radius: 2px; color: inherit; display: inline; line-height: calc(1.4em + 1px); padding: 2px 6px;">import os</mark> <mark style="box-sizing: border-box; margin: 0px; background: rgb(41, 51, 77); border-radius: 2px; color: inherit; display: inline; line-height: calc(1.4em + 1px); padding: 2px 6px;">STATIC_ROOT = os.path.join(BASE_DIR, 'static/')</mark> </code></pre><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">Save and close the file when you are finished. If you’re using<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">nano</code>, you can do this by pressing<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">CTRL + X</code><span> </span>then<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">Y</code><span> </span>and<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">ENTER</code>.</p><h2 id="step-5-completing-initial-project-setup" style="box-sizing: border-box; margin: 1em 0px; font-family: Epilogue, sans-serif;"><a href="https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-18-04#step-5-completing-initial-project-setup" style="box-sizing: border-box; margin: 0px; background: linear-gradient(rgb(0, 105, 255), rgb(0, 105, 255)) center bottom / 0px 0px repeat-x; color: inherit; text-decoration: none; border-bottom: none; transition: background 0.3s ease-in-out 0s, color 0.3s ease-in-out 0s;">Step 5 — Completing Initial Project Setup</a><a class="hash-anchor" href="https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-18-04#step-5-completing-initial-project-setup" aria-hidden="true" style="box-sizing: border-box; margin: 0px 0px 0px 0.5em; background: linear-gradient(rgb(0, 105, 255), rgb(0, 105, 255)) center bottom / 0px 0px repeat-x; color: rgb(138, 150, 181); text-decoration: none; border-bottom: none; transition: opacity 0.2s ease-in-out 0s, visibility 0s linear 0.2s; font-size: 0.85em; opacity: 0; visibility: hidden;"></a></h2><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">The next step will be migrating the initial database schema to your PostgreSQL database using the following management script:</p><div class="code-toolbar" style="box-sizing: border-box; margin: 1em 0px; position: relative;"><pre class="prefixed custom_prefix language-bash" style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: rgb(17, 25, 46); border-radius: 16px; color: rgb(247, 248, 251); line-height: 22px; padding: 24px; box-shadow: rgba(17, 25, 46, 0.1) 0px 0px 0px 2px inset; display: block; overflow: auto; white-space: normal; overflow-wrap: normal;"><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: transparent; border-radius: 0px; color: inherit; line-height: 22px; padding: 0px; white-space: normal; text-shadow: none;"><ol style="box-sizing: border-box; margin: 0px; list-style: none; padding: 0px;"><li data-prefix="(myprojectenv) $" style="box-sizing: border-box; margin: 0px; padding: 0px; white-space: pre;">~/<mark style="box-sizing: border-box; margin: 0px; background: rgb(41, 51, 77); border-radius: 2px; color: inherit; display: inline; line-height: calc(1.4em + 1px); padding: 2px 6px;">myprojectdir</mark>/manage.py makemigrations </li><li data-prefix="(myprojectenv) $" style="box-sizing: border-box; margin: 0px; padding: 0px; white-space: pre;">~/<mark style="box-sizing: border-box; margin: 0px; background: rgb(41, 51, 77); border-radius: 2px; color: inherit; display: inline; line-height: calc(1.4em + 1px); padding: 2px 6px;">myprojectdir</mark>/manage.py migrate </li></ol></code></pre><div class="toolbar" style="box-sizing: border-box; margin: 0px; position: absolute; right: 24px; top: 24px;"><div class="toolbar-item" style="box-sizing: border-box; margin: 0px; display: inline-block;"><button type="button" style="box-sizing: border-box; margin: 0px; font-family: inherit; font-size: 0.9em; line-height: normal; overflow: visible; text-transform: none; appearance: button; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-optical-sizing: inherit; font-kerning: inherit; font-feature-settings: inherit; font-variation-settings: inherit; background: rgb(105, 111, 176); border: 0px; cursor: pointer; user-select: none; border-radius: 10px; color: rgb(255, 255, 255); padding: 6px 8px; transition: color 0.25s ease 0s, background 0.25s ease 0s;">Copy</button></div></div></div><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">Create an administrative user for the project:</p><div class="code-toolbar" style="box-sizing: border-box; margin: 1em 0px; position: relative;"><pre class="prefixed custom_prefix language-bash" style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: rgb(17, 25, 46); border-radius: 16px; color: rgb(247, 248, 251); line-height: 22px; padding: 24px; box-shadow: rgba(17, 25, 46, 0.1) 0px 0px 0px 2px inset; display: block; overflow: auto; white-space: normal; overflow-wrap: normal;"><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: transparent; border-radius: 0px; color: inherit; line-height: 22px; padding: 0px; white-space: normal; text-shadow: none;"><ol style="box-sizing: border-box; margin: 0px; list-style: none; padding: 0px;"><li data-prefix="(myprojectenv) $" style="box-sizing: border-box; margin: 0px; padding: 0px; white-space: pre;">~/<mark style="box-sizing: border-box; margin: 0px; background: rgb(41, 51, 77); border-radius: 2px; color: inherit; display: inline; line-height: calc(1.4em + 1px); padding: 2px 6px;">myprojectdir</mark>/manage.py createsuperuser </li></ol></code></pre><div class="toolbar" style="box-sizing: border-box; margin: 0px; position: absolute; right: 24px; top: 24px;"><div class="toolbar-item" style="box-sizing: border-box; margin: 0px; display: inline-block;"><button type="button" style="box-sizing: border-box; margin: 0px; font-family: inherit; font-size: 0.9em; line-height: normal; overflow: visible; text-transform: none; appearance: button; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-optical-sizing: inherit; font-kerning: inherit; font-feature-settings: inherit; font-variation-settings: inherit; background: rgb(105, 111, 176); border: 0px; cursor: pointer; user-select: none; border-radius: 10px; color: rgb(255, 255, 255); padding: 6px 8px; transition: color 0.25s ease 0s, background 0.25s ease 0s;">Copy</button></div></div></div><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">You will have to select a username, provide an email address, and choose then confirm a password.</p><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">Collect all of the static content into the directory location you configured by running the following:</p><div class="code-toolbar" style="box-sizing: border-box; margin: 1em 0px; position: relative;"><pre class="prefixed custom_prefix language-bash" style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: rgb(17, 25, 46); border-radius: 16px; color: rgb(247, 248, 251); line-height: 22px; padding: 24px; box-shadow: rgba(17, 25, 46, 0.1) 0px 0px 0px 2px inset; display: block; overflow: auto; white-space: normal; overflow-wrap: normal;"><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: transparent; border-radius: 0px; color: inherit; line-height: 22px; padding: 0px; white-space: normal; text-shadow: none;"><ol style="box-sizing: border-box; margin: 0px; list-style: none; padding: 0px;"><li data-prefix="(myprojectenv) $" style="box-sizing: border-box; margin: 0px; padding: 0px; white-space: pre;">~/<mark style="box-sizing: border-box; margin: 0px; background: rgb(41, 51, 77); border-radius: 2px; color: inherit; display: inline; line-height: calc(1.4em + 1px); padding: 2px 6px;">myprojectdir</mark>/manage.py collectstatic </li></ol></code></pre><div class="toolbar" style="box-sizing: border-box; margin: 0px; position: absolute; right: 24px; top: 24px;"><div class="toolbar-item" style="box-sizing: border-box; margin: 0px; display: inline-block;"><button type="button" style="box-sizing: border-box; margin: 0px; font-family: inherit; font-size: 0.9em; line-height: normal; overflow: visible; text-transform: none; appearance: button; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-optical-sizing: inherit; font-kerning: inherit; font-feature-settings: inherit; font-variation-settings: inherit; background: rgb(105, 111, 176); border: 0px; cursor: pointer; user-select: none; border-radius: 10px; color: rgb(255, 255, 255); padding: 6px 8px; transition: color 0.25s ease 0s, background 0.25s ease 0s;">Copy</button></div></div></div><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">The static files will then be placed in a directory called<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">static</code><span> </span>within your project directory.</p><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">If you followed the initial server setup guide, you should have a UFW firewall protecting your server. In order to test the development server, you’ll have to allow access to the port you’ll be using. In this case, create an exception for port<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">8000</code>:</p><div class="code-toolbar" style="box-sizing: border-box; margin: 1em 0px; position: relative;"><pre class="prefixed custom_prefix language-bash" style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: rgb(17, 25, 46); border-radius: 16px; color: rgb(247, 248, 251); line-height: 22px; padding: 24px; box-shadow: rgba(17, 25, 46, 0.1) 0px 0px 0px 2px inset; display: block; overflow: auto; white-space: normal; overflow-wrap: normal;"><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: transparent; border-radius: 0px; color: inherit; line-height: 22px; padding: 0px; white-space: normal; text-shadow: none;"><ol style="box-sizing: border-box; margin: 0px; list-style: none; padding: 0px;"><li data-prefix="(myprojectenv) $" style="box-sizing: border-box; margin: 0px; padding: 0px; white-space: pre;"><span class="token function" style="box-sizing: border-box; margin: 0px; text-shadow: none; background: none; border-radius: 0px; display: inline; font-weight: 400; padding: 0px; color: rgb(255, 175, 140);">sudo</span> ufw allow <span class="token number" style="box-sizing: border-box; margin: 0px; text-shadow: none; background: none; border-radius: 0px; display: inline; font-weight: 400; padding: 0px; color: rgb(11, 225, 255);">8000</span> </li></ol></code></pre><div class="toolbar" style="box-sizing: border-box; margin: 0px; position: absolute; right: 24px; top: 24px;"><div class="toolbar-item" style="box-sizing: border-box; margin: 0px; display: inline-block;"><button type="button" style="box-sizing: border-box; margin: 0px; font-family: inherit; font-size: 0.9em; line-height: normal; overflow: visible; text-transform: none; appearance: button; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-optical-sizing: inherit; font-kerning: inherit; font-feature-settings: inherit; font-variation-settings: inherit; background: rgb(105, 111, 176); border: 0px; cursor: pointer; user-select: none; border-radius: 10px; color: rgb(255, 255, 255); padding: 6px 8px; transition: color 0.25s ease 0s, background 0.25s ease 0s;">Copy</button></div></div></div><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">Finally, test your project by starting up the Django development server with the following command:</p><div class="code-toolbar" style="box-sizing: border-box; margin: 1em 0px; position: relative;"><pre class="prefixed custom_prefix language-bash" style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: rgb(17, 25, 46); border-radius: 16px; color: rgb(247, 248, 251); line-height: 22px; padding: 24px; box-shadow: rgba(17, 25, 46, 0.1) 0px 0px 0px 2px inset; display: block; overflow: auto; white-space: normal; overflow-wrap: normal;"><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: transparent; border-radius: 0px; color: inherit; line-height: 22px; padding: 0px; white-space: normal; text-shadow: none;"><ol style="box-sizing: border-box; margin: 0px; list-style: none; padding: 0px;"><li data-prefix="(myprojectenv) $" style="box-sizing: border-box; margin: 0px; padding: 0px; white-space: pre;">~/<mark style="box-sizing: border-box; margin: 0px; background: rgb(41, 51, 77); border-radius: 2px; color: inherit; display: inline; line-height: calc(1.4em + 1px); padding: 2px 6px;">myprojectdir</mark>/manage.py runserver <span class="token number" style="box-sizing: border-box; margin: 0px; text-shadow: none; background: none; border-radius: 0px; display: inline; font-weight: 400; padding: 0px; color: rgb(11, 225, 255);">0.0</span>.0.0:8000 </li></ol></code></pre><div class="toolbar" style="box-sizing: border-box; margin: 0px; position: absolute; right: 24px; top: 24px;"><div class="toolbar-item" style="box-sizing: border-box; margin: 0px; display: inline-block;"><button type="button" style="box-sizing: border-box; margin: 0px; font-family: inherit; font-size: 0.9em; line-height: normal; overflow: visible; text-transform: none; appearance: button; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-optical-sizing: inherit; font-kerning: inherit; font-feature-settings: inherit; font-variation-settings: inherit; background: rgb(105, 111, 176); border: 0px; cursor: pointer; user-select: none; border-radius: 10px; color: rgb(255, 255, 255); padding: 6px 8px; transition: color 0.25s ease 0s, background 0.25s ease 0s;">Copy</button></div></div></div><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">In your web browser, visit your server’s domain name or IP address followed by<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">:8000</code>:</p><pre style="box-sizing: border-box; margin: 1em 0px; font-family: monospace, monospace; font-size: 14px; background: rgb(17, 25, 46); border-radius: 16px; color: rgb(247, 248, 251); line-height: 22px; padding: 24px; box-shadow: rgba(17, 25, 46, 0.1) 0px 0px 0px 2px inset; display: block; overflow: auto; white-space: normal; overflow-wrap: normal;"><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: 0px center; border-radius: 0px; color: inherit; line-height: 22px; padding: 0px; white-space: pre;">http://<mark style="box-sizing: border-box; margin: 0px; background: rgb(41, 51, 77); border-radius: 2px; color: inherit; display: inline; line-height: calc(1.4em + 1px); padding: 2px 6px;">server_domain_or_IP</mark>:8000 </code></pre><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">You should receive the following default Django index page:</p><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;"><img src="https://assets.digitalocean.com/articles/django_gunicorn_nginx_1804/django_index.png" alt="Django index page" style="box-sizing: border-box; margin: 1rem auto; border: 2px solid rgb(227, 232, 244); display: block; max-width: 100%; border-radius: 16px;"></p><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">If you append<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">/admin</code><span> </span>to the end of the URL in the address bar, you will be prompted for the administrative username and password you created with the<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">createsuperuser</code><span> </span>command:</p><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;"><img src="https://assets.digitalocean.com/articles/django_gunicorn_nginx_1804/admin_login.png" alt="Django admin login" style="box-sizing: border-box; margin: 1rem auto; border: 2px solid rgb(227, 232, 244); display: block; max-width: 100%; border-radius: 16px;"></p><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">After authenticating, you can access the default Django administrative interface:</p><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;"><img src="https://assets.digitalocean.com/articles/django_gunicorn_nginx_1804/admin_interface.png" alt="Django admin interface" style="box-sizing: border-box; margin: 1rem auto; border: 2px solid rgb(227, 232, 244); display: block; max-width: 100%; border-radius: 16px;"></p><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">When you are finished exploring, press<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">CTRL + C</code><span> </span>in the terminal window to shut down the development server.</p><h2 id="step-6-testing-gunicorn-s-ability-to-serve-the-project" style="box-sizing: border-box; margin: 1em 0px; font-family: Epilogue, sans-serif;"><a href="https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-18-04#step-6-testing-gunicorn-s-ability-to-serve-the-project" style="box-sizing: border-box; margin: 0px; background: linear-gradient(rgb(0, 105, 255), rgb(0, 105, 255)) center bottom / 0px 0px repeat-x; color: inherit; text-decoration: none; border-bottom: none; transition: background 0.3s ease-in-out 0s, color 0.3s ease-in-out 0s;">Step 6 — Testing Gunicorn’s Ability to Serve the Project</a><a class="hash-anchor" href="https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-18-04#step-6-testing-gunicorn-s-ability-to-serve-the-project" aria-hidden="true" style="box-sizing: border-box; margin: 0px 0px 0px 0.5em; background: linear-gradient(rgb(0, 105, 255), rgb(0, 105, 255)) center bottom / 0px 0px repeat-x; color: rgb(138, 150, 181); text-decoration: none; border-bottom: none; transition: opacity 0.2s ease-in-out 0s, visibility 0s linear 0.2s; font-size: 0.85em; opacity: 0; visibility: hidden;"></a></h2><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">Before leaving your virtual environment, test Gunicorn to make sure that it can serve the application. You can do this by first moving into your project directory:</p><div class="code-toolbar" style="box-sizing: border-box; margin: 1em 0px; position: relative;"><pre class="prefixed custom_prefix language-bash" style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: rgb(17, 25, 46); border-radius: 16px; color: rgb(247, 248, 251); line-height: 22px; padding: 24px; box-shadow: rgba(17, 25, 46, 0.1) 0px 0px 0px 2px inset; display: block; overflow: auto; white-space: normal; overflow-wrap: normal;"><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: transparent; border-radius: 0px; color: inherit; line-height: 22px; padding: 0px; white-space: normal; text-shadow: none;"><ol style="box-sizing: border-box; margin: 0px; list-style: none; padding: 0px;"><li data-prefix="(myprojectenv) $" style="box-sizing: border-box; margin: 0px; padding: 0px; white-space: pre;"><span class="token builtin class-name" style="box-sizing: border-box; margin: 0px; text-shadow: none; background: none; border-radius: 0px; display: inline; font-weight: 400; padding: 0px; color: rgb(255, 175, 140);">cd</span> ~/<mark style="box-sizing: border-box; margin: 0px; background: rgb(41, 51, 77); border-radius: 2px; color: inherit; display: inline; line-height: calc(1.4em + 1px); padding: 2px 6px;">myprojectdir</mark> </li></ol></code></pre><div class="toolbar" style="box-sizing: border-box; margin: 0px; position: absolute; right: 24px; top: 24px;"><div class="toolbar-item" style="box-sizing: border-box; margin: 0px; display: inline-block;"><button type="button" style="box-sizing: border-box; margin: 0px; font-family: inherit; font-size: 0.9em; line-height: normal; overflow: visible; text-transform: none; appearance: button; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-optical-sizing: inherit; font-kerning: inherit; font-feature-settings: inherit; font-variation-settings: inherit; background: rgb(105, 111, 176); border: 0px; cursor: pointer; user-select: none; border-radius: 10px; color: rgb(255, 255, 255); padding: 6px 8px; transition: color 0.25s ease 0s, background 0.25s ease 0s;">Copy</button></div></div></div><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">Then run<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">gunicorn</code><span> </span>to load the project’s WSGI module:</p><div class="code-toolbar" style="box-sizing: border-box; margin: 1em 0px; position: relative;"><pre class="prefixed custom_prefix language-bash" style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: rgb(17, 25, 46); border-radius: 16px; color: rgb(247, 248, 251); line-height: 22px; padding: 24px; box-shadow: rgba(17, 25, 46, 0.1) 0px 0px 0px 2px inset; display: block; overflow: auto; white-space: normal; overflow-wrap: normal;"><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: transparent; border-radius: 0px; color: inherit; line-height: 22px; padding: 0px; white-space: normal; text-shadow: none;"><ol style="box-sizing: border-box; margin: 0px; list-style: none; padding: 0px;"><li data-prefix="(myprojectenv) $" style="box-sizing: border-box; margin: 0px; padding: 0px; white-space: pre;">gunicorn <span class="token parameter variable" style="box-sizing: border-box; margin: 0px; text-shadow: none; background: none; border-radius: 0px; display: inline; font-weight: 400; padding: 0px; color: rgb(177, 228, 144);">--bind</span> <span class="token number" style="box-sizing: border-box; margin: 0px; text-shadow: none; background: none; border-radius: 0px; display: inline; font-weight: 400; padding: 0px; color: rgb(11, 225, 255);">0.0</span>.0.0:8000 <mark style="box-sizing: border-box; margin: 0px; background: rgb(41, 51, 77); border-radius: 2px; color: inherit; display: inline; line-height: calc(1.4em + 1px); padding: 2px 6px;">myproject</mark>.wsgi </li></ol></code></pre><div class="toolbar" style="box-sizing: border-box; margin: 0px; position: absolute; right: 24px; top: 24px;"><div class="toolbar-item" style="box-sizing: border-box; margin: 0px; display: inline-block;"><button type="button" style="box-sizing: border-box; margin: 0px; font-family: inherit; font-size: 0.9em; line-height: normal; overflow: visible; text-transform: none; appearance: button; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-optical-sizing: inherit; font-kerning: inherit; font-feature-settings: inherit; font-variation-settings: inherit; background: rgb(105, 111, 176); border: 0px; cursor: pointer; user-select: none; border-radius: 10px; color: rgb(255, 255, 255); padding: 6px 8px; transition: color 0.25s ease 0s, background 0.25s ease 0s;">Copy</button></div></div></div><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">This will start Gunicorn on the same interface that the Django development server was running on. You can go back and test the application again.</p><div class="callout note" style="box-sizing: border-box; margin: 0px 0px 1.5em; background-color: rgb(213, 240, 195); border-radius: 16px; color: rgb(0, 12, 42); display: block; font-size: 15px; line-height: 1.5em; padding: 1em 1.25em; position: relative; z-index: 0;"><p style="box-sizing: border-box; margin: 0px; line-height: 1.5;"><strong style="box-sizing: border-box; margin: 0px; font-weight: bolder;">Note:</strong><span> </span>The administrative interface will not have any of the styling applied since Gunicorn does not know how to find the static CSS content responsible for this.</p></div><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">To recap, you passed Gunicorn a module by specifying the relative directory path to Django’s<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">wsgi.py</code><span> </span>file, which is the entry point to your application, using Python’s module syntax. Gunicorn serves as an interface to your application, translating client requests from HTTP to Python calls that your application can process. Inside of this file, a function called<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">application</code><span> </span>was defined, which is used to communicate with the application. If you’re interested, you can learn more about the<span> </span><a href="https://www.digitalocean.com/community/tutorials/how-to-set-up-uwsgi-and-nginx-to-serve-python-apps-on-ubuntu-14-04#definitions-and-concepts" style="box-sizing: border-box; margin: 0px; background: linear-gradient(rgb(0, 105, 255), rgb(0, 105, 255)) center bottom / 1px 1px repeat-x; color: rgb(0, 105, 255); text-decoration: none; border-bottom: none; transition: background 0.3s ease-in-out 0s, color 0.3s ease-in-out 0s; overflow-wrap: break-word;">WSGI specification</a>.</p><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">When you are finished testing, press<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">CTRL + C</code><span> </span>in the terminal window to stop Gunicorn.</p><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">Now you’re finished configuring your Django application and can deactivate your virtual environment:</p><div class="code-toolbar" style="box-sizing: border-box; margin: 1em 0px; position: relative;"><pre class="prefixed custom_prefix language-bash" style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: rgb(17, 25, 46); border-radius: 16px; color: rgb(247, 248, 251); line-height: 22px; padding: 24px; box-shadow: rgba(17, 25, 46, 0.1) 0px 0px 0px 2px inset; display: block; overflow: auto; white-space: normal; overflow-wrap: normal;"><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: transparent; border-radius: 0px; color: inherit; line-height: 22px; padding: 0px; white-space: normal; text-shadow: none;"><ol style="box-sizing: border-box; margin: 0px; list-style: none; padding: 0px;"><li data-prefix="(myprojectenv) $" style="box-sizing: border-box; margin: 0px; padding: 0px; white-space: pre;">deactivate </li></ol></code></pre><div class="toolbar" style="box-sizing: border-box; margin: 0px; position: absolute; right: 24px; top: 24px;"><div class="toolbar-item" style="box-sizing: border-box; margin: 0px; display: inline-block;"><button type="button" style="box-sizing: border-box; margin: 0px; font-family: inherit; font-size: 0.9em; line-height: normal; overflow: visible; text-transform: none; appearance: button; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-optical-sizing: inherit; font-kerning: inherit; font-feature-settings: inherit; font-variation-settings: inherit; background: rgb(105, 111, 176); border: 0px; cursor: pointer; user-select: none; border-radius: 10px; color: rgb(255, 255, 255); padding: 6px 8px; transition: color 0.25s ease 0s, background 0.25s ease 0s;">Copy</button></div></div></div><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">The virtual environment indicator in your prompt will be removed.</p><h2 id="step-7-creating-systemd-socket-and-service-files-for-gunicorn" style="box-sizing: border-box; margin: 1em 0px; font-family: Epilogue, sans-serif;"><a href="https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-18-04#step-7-creating-systemd-socket-and-service-files-for-gunicorn" style="box-sizing: border-box; margin: 0px; background: linear-gradient(rgb(0, 105, 255), rgb(0, 105, 255)) center bottom / 0px 0px repeat-x; color: inherit; text-decoration: none; border-bottom: none; transition: background 0.3s ease-in-out 0s, color 0.3s ease-in-out 0s;">Step 7 — Creating systemd Socket and Service Files for Gunicorn</a><a class="hash-anchor" href="https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-18-04#step-7-creating-systemd-socket-and-service-files-for-gunicorn" aria-hidden="true" style="box-sizing: border-box; margin: 0px 0px 0px 0.5em; background: linear-gradient(rgb(0, 105, 255), rgb(0, 105, 255)) center bottom / 0px 0px repeat-x; color: rgb(138, 150, 181); text-decoration: none; border-bottom: none; transition: opacity 0.2s ease-in-out 0s, visibility 0s linear 0.2s; font-size: 0.85em; opacity: 0; visibility: hidden;"></a></h2><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">Now that you’ve tested that Gunicorn can interact with your Django application, you should implement a more robust way of starting and stopping the application server. To accomplish this, you’ll create systemd service and socket files.</p><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">The Gunicorn socket will be created at boot and will listen for connections. When a connection occurs, systemd will automatically start the Gunicorn process to handle the connection.</p><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">Start by creating and opening a systemd socket file for Gunicorn with<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">sudo</code><span> </span>privileges in your preferred text editor:</p><div class="code-toolbar" style="box-sizing: border-box; margin: 1em 0px; position: relative;"><pre class="prefixed command language-bash" style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: rgb(17, 25, 46); border-radius: 16px; color: rgb(247, 248, 251); line-height: 22px; padding: 24px; box-shadow: rgba(17, 25, 46, 0.1) 0px 0px 0px 2px inset; display: block; overflow: auto; white-space: normal; overflow-wrap: normal;"><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: transparent; border-radius: 0px; color: inherit; line-height: 22px; padding: 0px; white-space: normal; text-shadow: none;"><ol style="box-sizing: border-box; margin: 0px; list-style: none; padding: 0px;"><li data-prefix="$" style="box-sizing: border-box; margin: 0px; padding: 0px; white-space: pre;"><span class="token function" style="box-sizing: border-box; margin: 0px; text-shadow: none; background: none; border-radius: 0px; display: inline; font-weight: 400; padding: 0px; color: rgb(255, 175, 140);">sudo</span> <span class="token function" style="box-sizing: border-box; margin: 0px; text-shadow: none; background: none; border-radius: 0px; display: inline; font-weight: 400; padding: 0px; color: rgb(255, 175, 140);">nano</span> /etc/systemd/system/gunicorn.socket </li></ol></code></pre><div class="toolbar" style="box-sizing: border-box; margin: 0px; position: absolute; right: 24px; top: 24px;"><div class="toolbar-item" style="box-sizing: border-box; margin: 0px; display: inline-block;"><button type="button" style="box-sizing: border-box; margin: 0px; font-family: inherit; font-size: 0.9em; line-height: normal; overflow: visible; text-transform: none; appearance: button; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-optical-sizing: inherit; font-kerning: inherit; font-feature-settings: inherit; font-variation-settings: inherit; background: rgb(105, 111, 176); border: 0px; cursor: pointer; user-select: none; border-radius: 10px; color: rgb(255, 255, 255); padding: 6px 8px; transition: color 0.25s ease 0s, background 0.25s ease 0s;">Copy</button></div></div></div><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">Inside, create a<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">[Unit]</code><span> </span>section to describe the socket, a<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">[Socket]</code><span> </span>section to define the socket location, and an<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">[Install]</code><span> </span>section to make sure the socket is created at the right time:</p><div class="code-label" title="/etc/systemd/system/gunicorn.socket" style="box-sizing: border-box; margin: 1em 0px 0px; background-color: rgb(214, 220, 234); border-radius: 16px 16px 0px 0px; color: rgb(36, 51, 90); display: block; font-size: 16px; padding: 12px; position: relative; text-align: center; z-index: 2;">/etc/systemd/system/gunicorn.socket</div><pre style="box-sizing: border-box; margin: 0px 0px 1em; font-family: monospace, monospace; font-size: 14px; background: rgb(17, 25, 46); border-radius: 0px 0px 16px 16px; color: rgb(247, 248, 251); line-height: 22px; padding: 24px; box-shadow: rgba(17, 25, 46, 0.1) 0px 0px 0px 2px inset; display: block; overflow: auto; white-space: normal; overflow-wrap: normal;"><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: 0px center; border-radius: 0px; color: inherit; line-height: 22px; padding: 0px; white-space: pre;">[Unit] Description=gunicorn socket [Socket] ListenStream=/run/gunicorn.sock [Install] WantedBy=sockets.target </code></pre><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">Save and close the file when you are finished.</p><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">Next, create and open a systemd service file for Gunicorn with<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">sudo</code><span> </span>privileges in your preferred text editor. The service filename should match the socket filename with the exception of the extension:</p><div class="code-toolbar" style="box-sizing: border-box; margin: 1em 0px; position: relative;"><pre class="prefixed command language-bash" style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: rgb(17, 25, 46); border-radius: 16px; color: rgb(247, 248, 251); line-height: 22px; padding: 24px; box-shadow: rgba(17, 25, 46, 0.1) 0px 0px 0px 2px inset; display: block; overflow: auto; white-space: normal; overflow-wrap: normal;"><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: transparent; border-radius: 0px; color: inherit; line-height: 22px; padding: 0px; white-space: normal; text-shadow: none;"><ol style="box-sizing: border-box; margin: 0px; list-style: none; padding: 0px;"><li data-prefix="$" style="box-sizing: border-box; margin: 0px; padding: 0px; white-space: pre;"><span class="token function" style="box-sizing: border-box; margin: 0px; text-shadow: none; background: none; border-radius: 0px; display: inline; font-weight: 400; padding: 0px; color: rgb(255, 175, 140);">sudo</span> <span class="token function" style="box-sizing: border-box; margin: 0px; text-shadow: none; background: none; border-radius: 0px; display: inline; font-weight: 400; padding: 0px; color: rgb(255, 175, 140);">nano</span> /etc/systemd/system/gunicorn.service </li></ol></code></pre><div class="toolbar" style="box-sizing: border-box; margin: 0px; position: absolute; right: 24px; top: 24px;"><div class="toolbar-item" style="box-sizing: border-box; margin: 0px; display: inline-block;"><button type="button" style="box-sizing: border-box; margin: 0px; font-family: inherit; font-size: 0.9em; line-height: normal; overflow: visible; text-transform: none; appearance: button; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-optical-sizing: inherit; font-kerning: inherit; font-feature-settings: inherit; font-variation-settings: inherit; background: rgb(105, 111, 176); border: 0px; cursor: pointer; user-select: none; border-radius: 10px; color: rgb(255, 255, 255); padding: 6px 8px; transition: color 0.25s ease 0s, background 0.25s ease 0s;">Copy</button></div></div></div><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">Start with the<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">[Unit]</code><span> </span>section, which is used to specify metadata and dependencies. Add a description of your service here and tell the init system to only start this service after the networking target has been reached. Because your service relies on the socket from the socket file, you need to include a<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">Requires</code><span> </span>directive to indicate that relationship:</p><div class="code-label" title="/etc/systemd/system/gunicorn.service" style="box-sizing: border-box; margin: 1em 0px 0px; background-color: rgb(214, 220, 234); border-radius: 16px 16px 0px 0px; color: rgb(36, 51, 90); display: block; font-size: 16px; padding: 12px; position: relative; text-align: center; z-index: 2;">/etc/systemd/system/gunicorn.service</div><pre style="box-sizing: border-box; margin: 0px 0px 1em; font-family: monospace, monospace; font-size: 14px; background: rgb(17, 25, 46); border-radius: 0px 0px 16px 16px; color: rgb(247, 248, 251); line-height: 22px; padding: 24px; box-shadow: rgba(17, 25, 46, 0.1) 0px 0px 0px 2px inset; display: block; overflow: auto; white-space: normal; overflow-wrap: normal;"><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: 0px center; border-radius: 0px; color: inherit; line-height: 22px; padding: 0px; white-space: pre;">[Unit] Description=gunicorn daemon Requires=gunicorn.socket After=network.target </code></pre><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">Next, add a<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">[Service]</code><span> </span>section and specify the user and group that you want the process to run under. Provide your regular user account ownership of the process since it owns all of the relevant files. Then give group ownership to the<span> </span><strong style="box-sizing: border-box; margin: 0px; font-weight: bolder;">www-data</strong><span> </span>group so that Nginx can communicate with Gunicorn.</p><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">Afterward, map out the working directory and specify the command to run to start the service. In this case, specify the full path to the Gunicorn executable, which is installed within your virtual environment. Then bind the process to the Unix socket you created within the<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">/run</code><span> </span>directory so that the process can communicate with Nginx. Log all data to standard output so that the<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">journald</code><span> </span>process can collect the Gunicorn logs. You can also specify any optional Gunicorn tweaks here. For our example, we specified 3 worker processes:</p><div class="code-label" title="/etc/systemd/system/gunicorn.service" style="box-sizing: border-box; margin: 1em 0px 0px; background-color: rgb(214, 220, 234); border-radius: 16px 16px 0px 0px; color: rgb(36, 51, 90); display: block; font-size: 16px; padding: 12px; position: relative; text-align: center; z-index: 2;">/etc/systemd/system/gunicorn.service</div><pre style="box-sizing: border-box; margin: 0px 0px 1em; font-family: monospace, monospace; font-size: 14px; background: rgb(17, 25, 46); border-radius: 0px 0px 16px 16px; color: rgb(247, 248, 251); line-height: 22px; padding: 24px; box-shadow: rgba(17, 25, 46, 0.1) 0px 0px 0px 2px inset; display: block; overflow: auto; white-space: normal; overflow-wrap: normal;"><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: 0px center; border-radius: 0px; color: inherit; line-height: 22px; padding: 0px; white-space: pre;">[Unit] Description=gunicorn daemon Requires=gunicorn.socket After=network.target [Service] User=<mark style="box-sizing: border-box; margin: 0px; background: rgb(41, 51, 77); border-radius: 2px; color: inherit; display: inline; line-height: calc(1.4em + 1px); padding: 2px 6px;">sammy</mark> Group=www-data WorkingDirectory=/home/<mark style="box-sizing: border-box; margin: 0px; background: rgb(41, 51, 77); border-radius: 2px; color: inherit; display: inline; line-height: calc(1.4em + 1px); padding: 2px 6px;">sammy</mark>/<mark style="box-sizing: border-box; margin: 0px; background: rgb(41, 51, 77); border-radius: 2px; color: inherit; display: inline; line-height: calc(1.4em + 1px); padding: 2px 6px;">myprojectdir</mark> ExecStart=/home/<mark style="box-sizing: border-box; margin: 0px; background: rgb(41, 51, 77); border-radius: 2px; color: inherit; display: inline; line-height: calc(1.4em + 1px); padding: 2px 6px;">sammy</mark>/<mark style="box-sizing: border-box; margin: 0px; background: rgb(41, 51, 77); border-radius: 2px; color: inherit; display: inline; line-height: calc(1.4em + 1px); padding: 2px 6px;">myprojectdir</mark>/<mark style="box-sizing: border-box; margin: 0px; background: rgb(41, 51, 77); border-radius: 2px; color: inherit; display: inline; line-height: calc(1.4em + 1px); padding: 2px 6px;">myprojectenv</mark>/bin/gunicorn \ --access-logfile - \ --workers 3 \ --bind unix:/run/gunicorn.sock \ <mark style="box-sizing: border-box; margin: 0px; background: rgb(41, 51, 77); border-radius: 2px; color: inherit; display: inline; line-height: calc(1.4em + 1px); padding: 2px 6px;">myproject</mark>.wsgi:application </code></pre><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">Finally, add an<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">[Install]</code><span> </span>section. This will tell systemd what to link this service to if you enable it to start at boot. You want this service to start when the regular multi-user system is up and running:</p><div class="code-label" title="/etc/systemd/system/gunicorn.service" style="box-sizing: border-box; margin: 1em 0px 0px; background-color: rgb(214, 220, 234); border-radius: 16px 16px 0px 0px; color: rgb(36, 51, 90); display: block; font-size: 16px; padding: 12px; position: relative; text-align: center; z-index: 2;">/etc/systemd/system/gunicorn.service</div><pre style="box-sizing: border-box; margin: 0px 0px 1em; font-family: monospace, monospace; font-size: 14px; background: rgb(17, 25, 46); border-radius: 0px 0px 16px 16px; color: rgb(247, 248, 251); line-height: 22px; padding: 24px; box-shadow: rgba(17, 25, 46, 0.1) 0px 0px 0px 2px inset; display: block; overflow: auto; white-space: normal; overflow-wrap: normal;"><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: 0px center; border-radius: 0px; color: inherit; line-height: 22px; padding: 0px; white-space: pre;">[Unit] Description=gunicorn daemon Requires=gunicorn.socket After=network.target [Service] User=<mark style="box-sizing: border-box; margin: 0px; background: rgb(41, 51, 77); border-radius: 2px; color: inherit; display: inline; line-height: calc(1.4em + 1px); padding: 2px 6px;">sammy</mark> Group=www-data WorkingDirectory=/home/<mark style="box-sizing: border-box; margin: 0px; background: rgb(41, 51, 77); border-radius: 2px; color: inherit; display: inline; line-height: calc(1.4em + 1px); padding: 2px 6px;">sammy</mark>/<mark style="box-sizing: border-box; margin: 0px; background: rgb(41, 51, 77); border-radius: 2px; color: inherit; display: inline; line-height: calc(1.4em + 1px); padding: 2px 6px;">myprojectdir</mark> ExecStart=/home/<mark style="box-sizing: border-box; margin: 0px; background: rgb(41, 51, 77); border-radius: 2px; color: inherit; display: inline; line-height: calc(1.4em + 1px); padding: 2px 6px;">sammy</mark>/<mark style="box-sizing: border-box; margin: 0px; background: rgb(41, 51, 77); border-radius: 2px; color: inherit; display: inline; line-height: calc(1.4em + 1px); padding: 2px 6px;">myprojectdir</mark>/<mark style="box-sizing: border-box; margin: 0px; background: rgb(41, 51, 77); border-radius: 2px; color: inherit; display: inline; line-height: calc(1.4em + 1px); padding: 2px 6px;">myprojectenv</mark>/bin/gunicorn \ --access-logfile - \ --workers 3 \ --bind unix:/run/gunicorn.sock \ <mark style="box-sizing: border-box; margin: 0px; background: rgb(41, 51, 77); border-radius: 2px; color: inherit; display: inline; line-height: calc(1.4em + 1px); padding: 2px 6px;">myproject</mark>.wsgi:application [Install] WantedBy=multi-user.target </code></pre><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">With that, your systemd service file is complete. Save and close the file now.</p><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">Next, start the Gunicorn socket. This will create the socket file at<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">/run/gunicorn.sock</code><span> </span>now and at boot:</p><div class="code-toolbar" style="box-sizing: border-box; margin: 1em 0px; position: relative;"><pre class="prefixed command language-bash" style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: rgb(17, 25, 46); border-radius: 16px; color: rgb(247, 248, 251); line-height: 22px; padding: 24px; box-shadow: rgba(17, 25, 46, 0.1) 0px 0px 0px 2px inset; display: block; overflow: auto; white-space: normal; overflow-wrap: normal;"><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: transparent; border-radius: 0px; color: inherit; line-height: 22px; padding: 0px; white-space: normal; text-shadow: none;"><ol style="box-sizing: border-box; margin: 0px; list-style: none; padding: 0px;"><li data-prefix="$" style="box-sizing: border-box; margin: 0px; padding: 0px; white-space: pre;"><span class="token function" style="box-sizing: border-box; margin: 0px; text-shadow: none; background: none; border-radius: 0px; display: inline; font-weight: 400; padding: 0px; color: rgb(255, 175, 140);">sudo</span> systemctl start gunicorn.socket </li></ol></code></pre><div class="toolbar" style="box-sizing: border-box; margin: 0px; position: absolute; right: 24px; top: 24px;"><div class="toolbar-item" style="box-sizing: border-box; margin: 0px; display: inline-block;"><button type="button" style="box-sizing: border-box; margin: 0px; font-family: inherit; font-size: 0.9em; line-height: normal; overflow: visible; text-transform: none; appearance: button; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-optical-sizing: inherit; font-kerning: inherit; font-feature-settings: inherit; font-variation-settings: inherit; background: rgb(105, 111, 176); border: 0px; cursor: pointer; user-select: none; border-radius: 10px; color: rgb(255, 255, 255); padding: 6px 8px; transition: color 0.25s ease 0s, background 0.25s ease 0s;">Copy</button></div></div></div><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">Then enable it so that when a connection is made to that socket, systemd will automatically start the<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">gunicorn.service</code><span> </span>to handle it:</p><div class="code-toolbar" style="box-sizing: border-box; margin: 1em 0px; position: relative;"><pre class="prefixed command language-bash" style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: rgb(17, 25, 46); border-radius: 16px; color: rgb(247, 248, 251); line-height: 22px; padding: 24px; box-shadow: rgba(17, 25, 46, 0.1) 0px 0px 0px 2px inset; display: block; overflow: auto; white-space: normal; overflow-wrap: normal;"><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: transparent; border-radius: 0px; color: inherit; line-height: 22px; padding: 0px; white-space: normal; text-shadow: none;"><ol style="box-sizing: border-box; margin: 0px; list-style: none; padding: 0px;"><li data-prefix="$" style="box-sizing: border-box; margin: 0px; padding: 0px; white-space: pre;"><span class="token function" style="box-sizing: border-box; margin: 0px; text-shadow: none; background: none; border-radius: 0px; display: inline; font-weight: 400; padding: 0px; color: rgb(255, 175, 140);">sudo</span> systemctl <span class="token builtin class-name" style="box-sizing: border-box; margin: 0px; text-shadow: none; background: none; border-radius: 0px; display: inline; font-weight: 400; padding: 0px; color: rgb(255, 175, 140);">enable</span> gunicorn.socket </li></ol></code></pre><div class="toolbar" style="box-sizing: border-box; margin: 0px; position: absolute; right: 24px; top: 24px;"><div class="toolbar-item" style="box-sizing: border-box; margin: 0px; display: inline-block;"><button type="button" style="box-sizing: border-box; margin: 0px; font-family: inherit; font-size: 0.9em; line-height: normal; overflow: visible; text-transform: none; appearance: button; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-optical-sizing: inherit; font-kerning: inherit; font-feature-settings: inherit; font-variation-settings: inherit; background: rgb(105, 111, 176); border: 0px; cursor: pointer; user-select: none; border-radius: 10px; color: rgb(255, 255, 255); padding: 6px 8px; transition: color 0.25s ease 0s, background 0.25s ease 0s;">Copy</button></div></div></div><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">You can confirm that the operation was successful by checking for the socket file.</p><h2 id="step-8-checking-for-the-gunicorn-socket-file" style="box-sizing: border-box; margin: 1em 0px; font-family: Epilogue, sans-serif;"><a href="https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-18-04#step-8-checking-for-the-gunicorn-socket-file" style="box-sizing: border-box; margin: 0px; background: linear-gradient(rgb(0, 105, 255), rgb(0, 105, 255)) center bottom / 0px 0px repeat-x; color: inherit; text-decoration: none; border-bottom: none; transition: background 0.3s ease-in-out 0s, color 0.3s ease-in-out 0s;">Step 8 — Checking for the Gunicorn Socket File</a><a class="hash-anchor" href="https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-18-04#step-8-checking-for-the-gunicorn-socket-file" aria-hidden="true" style="box-sizing: border-box; margin: 0px 0px 0px 0.5em; background: linear-gradient(rgb(0, 105, 255), rgb(0, 105, 255)) center bottom / 0px 0px repeat-x; color: rgb(138, 150, 181); text-decoration: none; border-bottom: none; transition: opacity 0.2s ease-in-out 0s, visibility 0s linear 0.2s; font-size: 0.85em; opacity: 0; visibility: hidden;"></a></h2><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">Check the status of the process to find out whether it was able to start:</p><div class="code-toolbar" style="box-sizing: border-box; margin: 1em 0px; position: relative;"><pre class="prefixed command language-bash" style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: rgb(17, 25, 46); border-radius: 16px; color: rgb(247, 248, 251); line-height: 22px; padding: 24px; box-shadow: rgba(17, 25, 46, 0.1) 0px 0px 0px 2px inset; display: block; overflow: auto; white-space: normal; overflow-wrap: normal;"><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: transparent; border-radius: 0px; color: inherit; line-height: 22px; padding: 0px; white-space: normal; text-shadow: none;"><ol style="box-sizing: border-box; margin: 0px; list-style: none; padding: 0px;"><li data-prefix="$" style="box-sizing: border-box; margin: 0px; padding: 0px; white-space: pre;"><span class="token function" style="box-sizing: border-box; margin: 0px; text-shadow: none; background: none; border-radius: 0px; display: inline; font-weight: 400; padding: 0px; color: rgb(255, 175, 140);">sudo</span> systemctl status gunicorn.socket </li></ol></code></pre><div class="toolbar" style="box-sizing: border-box; margin: 0px; position: absolute; right: 24px; top: 24px;"><div class="toolbar-item" style="box-sizing: border-box; margin: 0px; display: inline-block;"><button type="button" style="box-sizing: border-box; margin: 0px; font-family: inherit; font-size: 0.9em; line-height: normal; overflow: visible; text-transform: none; appearance: button; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-optical-sizing: inherit; font-kerning: inherit; font-feature-settings: inherit; font-variation-settings: inherit; background: rgb(105, 111, 176); border: 0px; cursor: pointer; user-select: none; border-radius: 10px; color: rgb(255, 255, 255); padding: 6px 8px; transition: color 0.25s ease 0s, background 0.25s ease 0s;">Copy</button></div></div></div><pre style="box-sizing: border-box; margin: 1em 0px; font-family: monospace, monospace; font-size: 14px; background: rgb(17, 25, 46); border-radius: 16px; color: rgb(247, 248, 251); line-height: 22px; padding: 24px; box-shadow: rgba(17, 25, 46, 0.1) 0px 0px 0px 2px inset; display: block; overflow: auto; white-space: normal; overflow-wrap: normal;"><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: 0px center; border-radius: 0px; color: inherit; line-height: 22px; padding: 0px; white-space: pre;"><div class="secondary-code-label" title="Output" style="box-sizing: border-box; margin: 0px 0px 4px; color: rgb(159, 221, 255);">Output</div>● gunicorn.socket - gunicorn socket Loaded: loaded (/etc/systemd/system/gunicorn.socket; enabled; vendor prese> Active: <mark style="box-sizing: border-box; margin: 0px; background: rgb(41, 51, 77); border-radius: 2px; color: inherit; display: inline; line-height: calc(1.4em + 1px); padding: 2px 6px;">active (listening)</mark> since Thu 2021-12-02 19:58:48 UTC; 14s ago Triggers: ● gunicorn.service Listen: /run/gunicorn.sock (Stream) Tasks: 0 (limit: 1136) Memory: 0B CGroup: /system.slice/gunicorn.socket Dec 02 19:58:48 gunicorn systemd[1]: Listening on gunicorn socket. </code></pre><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">Next, check for the existence of the<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">gunicorn.sock</code><span> </span>file within the<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">/run</code><span> </span>directory:</p><div class="code-toolbar" style="box-sizing: border-box; margin: 1em 0px; position: relative;"><pre class="prefixed command language-bash" style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: rgb(17, 25, 46); border-radius: 16px; color: rgb(247, 248, 251); line-height: 22px; padding: 24px; box-shadow: rgba(17, 25, 46, 0.1) 0px 0px 0px 2px inset; display: block; overflow: auto; white-space: normal; overflow-wrap: normal;"><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: transparent; border-radius: 0px; color: inherit; line-height: 22px; padding: 0px; white-space: normal; text-shadow: none;"><ol style="box-sizing: border-box; margin: 0px; list-style: none; padding: 0px;"><li data-prefix="$" style="box-sizing: border-box; margin: 0px; padding: 0px; white-space: pre;"><span class="token function" style="box-sizing: border-box; margin: 0px; text-shadow: none; background: none; border-radius: 0px; display: inline; font-weight: 400; padding: 0px; color: rgb(255, 175, 140);">file</span> /run/gunicorn.sock </li></ol></code></pre><div class="toolbar" style="box-sizing: border-box; margin: 0px; position: absolute; right: 24px; top: 24px;"><div class="toolbar-item" style="box-sizing: border-box; margin: 0px; display: inline-block;"><button type="button" style="box-sizing: border-box; margin: 0px; font-family: inherit; font-size: 0.9em; line-height: normal; overflow: visible; text-transform: none; appearance: button; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-optical-sizing: inherit; font-kerning: inherit; font-feature-settings: inherit; font-variation-settings: inherit; background: rgb(105, 111, 176); border: 0px; cursor: pointer; user-select: none; border-radius: 10px; color: rgb(255, 255, 255); padding: 6px 8px; transition: color 0.25s ease 0s, background 0.25s ease 0s;">Copy</button></div></div></div><pre style="box-sizing: border-box; margin: 1em 0px; font-family: monospace, monospace; font-size: 14px; background: rgb(17, 25, 46); border-radius: 16px; color: rgb(247, 248, 251); line-height: 22px; padding: 24px; box-shadow: rgba(17, 25, 46, 0.1) 0px 0px 0px 2px inset; display: block; overflow: auto; white-space: normal; overflow-wrap: normal;"><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: 0px center; border-radius: 0px; color: inherit; line-height: 22px; padding: 0px; white-space: pre;"><div class="secondary-code-label" title="Output" style="box-sizing: border-box; margin: 0px 0px 4px; color: rgb(159, 221, 255);">Output</div>/run/gunicorn.sock: socket </code></pre><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">If the<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">systemctl status</code><span> </span>command indicates that an error occurred or if you do not find the<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">gunicorn.sock</code><span> </span>file in the directory, it’s an indication that the Gunicorn socket was not created correctly. Check the Gunicorn socket’s logs by running the following:</p><div class="code-toolbar" style="box-sizing: border-box; margin: 1em 0px; position: relative;"><pre class="prefixed command language-bash" style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: rgb(17, 25, 46); border-radius: 16px; color: rgb(247, 248, 251); line-height: 22px; padding: 24px; box-shadow: rgba(17, 25, 46, 0.1) 0px 0px 0px 2px inset; display: block; overflow: auto; white-space: normal; overflow-wrap: normal;"><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: transparent; border-radius: 0px; color: inherit; line-height: 22px; padding: 0px; white-space: normal; text-shadow: none;"><ol style="box-sizing: border-box; margin: 0px; list-style: none; padding: 0px;"><li data-prefix="$" style="box-sizing: border-box; margin: 0px; padding: 0px; white-space: pre;"><span class="token function" style="box-sizing: border-box; margin: 0px; text-shadow: none; background: none; border-radius: 0px; display: inline; font-weight: 400; padding: 0px; color: rgb(255, 175, 140);">sudo</span> journalctl <span class="token parameter variable" style="box-sizing: border-box; margin: 0px; text-shadow: none; background: none; border-radius: 0px; display: inline; font-weight: 400; padding: 0px; color: rgb(177, 228, 144);">-u</span> gunicorn.socket </li></ol></code></pre><div class="toolbar" style="box-sizing: border-box; margin: 0px; position: absolute; right: 24px; top: 24px;"><div class="toolbar-item" style="box-sizing: border-box; margin: 0px; display: inline-block;"><button type="button" style="box-sizing: border-box; margin: 0px; font-family: inherit; font-size: 0.9em; line-height: normal; overflow: visible; text-transform: none; appearance: button; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-optical-sizing: inherit; font-kerning: inherit; font-feature-settings: inherit; font-variation-settings: inherit; background: rgb(105, 111, 176); border: 0px; cursor: pointer; user-select: none; border-radius: 10px; color: rgb(255, 255, 255); padding: 6px 8px; transition: color 0.25s ease 0s, background 0.25s ease 0s;">Copy</button></div></div></div><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">Check your<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">/etc/systemd/system/gunicorn.socket</code><span> </span>file to fix any problems before continuing.</p><h2 id="step-9-testing-socket-activation" style="box-sizing: border-box; margin: 1em 0px; font-family: Epilogue, sans-serif;"><a href="https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-18-04#step-9-testing-socket-activation" style="box-sizing: border-box; margin: 0px; background: linear-gradient(rgb(0, 105, 255), rgb(0, 105, 255)) center bottom / 0px 0px repeat-x; color: inherit; text-decoration: none; border-bottom: none; transition: background 0.3s ease-in-out 0s, color 0.3s ease-in-out 0s;">Step 9 — Testing Socket Activation</a><a class="hash-anchor" href="https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-18-04#step-9-testing-socket-activation" aria-hidden="true" style="box-sizing: border-box; margin: 0px 0px 0px 0.5em; background: linear-gradient(rgb(0, 105, 255), rgb(0, 105, 255)) center bottom / 0px 0px repeat-x; color: rgb(138, 150, 181); text-decoration: none; border-bottom: none; transition: opacity 0.2s ease-in-out 0s, visibility 0s linear 0.2s; font-size: 0.85em; opacity: 0; visibility: hidden;"></a></h2><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">If you only started the<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">gunicorn.socket</code><span> </span>unit, the<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">gunicorn.service</code><span> </span>will not be active yet since the socket has not received any connections. Check the status:</p><div class="code-toolbar" style="box-sizing: border-box; margin: 1em 0px; position: relative;"><pre class="prefixed command language-bash" style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: rgb(17, 25, 46); border-radius: 16px; color: rgb(247, 248, 251); line-height: 22px; padding: 24px; box-shadow: rgba(17, 25, 46, 0.1) 0px 0px 0px 2px inset; display: block; overflow: auto; white-space: normal; overflow-wrap: normal;"><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: transparent; border-radius: 0px; color: inherit; line-height: 22px; padding: 0px; white-space: normal; text-shadow: none;"><ol style="box-sizing: border-box; margin: 0px; list-style: none; padding: 0px;"><li data-prefix="$" style="box-sizing: border-box; margin: 0px; padding: 0px; white-space: pre;"><span class="token function" style="box-sizing: border-box; margin: 0px; text-shadow: none; background: none; border-radius: 0px; display: inline; font-weight: 400; padding: 0px; color: rgb(255, 175, 140);">sudo</span> systemctl status gunicorn </li></ol></code></pre><div class="toolbar" style="box-sizing: border-box; margin: 0px; position: absolute; right: 24px; top: 24px;"><div class="toolbar-item" style="box-sizing: border-box; margin: 0px; display: inline-block;"><button type="button" style="box-sizing: border-box; margin: 0px; font-family: inherit; font-size: 0.9em; line-height: normal; overflow: visible; text-transform: none; appearance: button; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-optical-sizing: inherit; font-kerning: inherit; font-feature-settings: inherit; font-variation-settings: inherit; background: rgb(105, 111, 176); border: 0px; cursor: pointer; user-select: none; border-radius: 10px; color: rgb(255, 255, 255); padding: 6px 8px; transition: color 0.25s ease 0s, background 0.25s ease 0s;">Copy</button></div></div></div><pre style="box-sizing: border-box; margin: 1em 0px; font-family: monospace, monospace; font-size: 14px; background: rgb(17, 25, 46); border-radius: 16px; color: rgb(247, 248, 251); line-height: 22px; padding: 24px; box-shadow: rgba(17, 25, 46, 0.1) 0px 0px 0px 2px inset; display: block; overflow: auto; white-space: normal; overflow-wrap: normal;"><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: 0px center; border-radius: 0px; color: inherit; line-height: 22px; padding: 0px; white-space: pre;"><div class="secondary-code-label" title="Output" style="box-sizing: border-box; margin: 0px 0px 4px; color: rgb(159, 221, 255);">Output</div>● gunicorn.service - gunicorn daemon Loaded: loaded (/etc/systemd/system/gunicorn.service; disabled; vendor preset: enabled) <mark style="box-sizing: border-box; margin: 0px; background: rgb(41, 51, 77); border-radius: 2px; color: inherit; display: inline; line-height: calc(1.4em + 1px); padding: 2px 6px;">Active: inactive (dead)</mark> </code></pre><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">To test the socket activation mechanism, send a connection to the socket through<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">curl</code>:</p><div class="code-toolbar" style="box-sizing: border-box; margin: 1em 0px; position: relative;"><pre class="prefixed command language-bash" style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: rgb(17, 25, 46); border-radius: 16px; color: rgb(247, 248, 251); line-height: 22px; padding: 24px; box-shadow: rgba(17, 25, 46, 0.1) 0px 0px 0px 2px inset; display: block; overflow: auto; white-space: normal; overflow-wrap: normal;"><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: transparent; border-radius: 0px; color: inherit; line-height: 22px; padding: 0px; white-space: normal; text-shadow: none;"><ol style="box-sizing: border-box; margin: 0px; list-style: none; padding: 0px;"><li data-prefix="$" style="box-sizing: border-box; margin: 0px; padding: 0px; white-space: pre;"><span class="token function" style="box-sizing: border-box; margin: 0px; text-shadow: none; background: none; border-radius: 0px; display: inline; font-weight: 400; padding: 0px; color: rgb(255, 175, 140);">curl</span> --unix-socket /run/gunicorn.sock localhost </li></ol></code></pre><div class="toolbar" style="box-sizing: border-box; margin: 0px; position: absolute; right: 24px; top: 24px;"><div class="toolbar-item" style="box-sizing: border-box; margin: 0px; display: inline-block;"><button type="button" style="box-sizing: border-box; margin: 0px; font-family: inherit; font-size: 0.9em; line-height: normal; overflow: visible; text-transform: none; appearance: button; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-optical-sizing: inherit; font-kerning: inherit; font-feature-settings: inherit; font-variation-settings: inherit; background: rgb(105, 111, 176); border: 0px; cursor: pointer; user-select: none; border-radius: 10px; color: rgb(255, 255, 255); padding: 6px 8px; transition: color 0.25s ease 0s, background 0.25s ease 0s;">Copy</button></div></div></div><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">You should receive the HTML output from your application in the terminal. This confirms that Gunicorn was started and able to serve your Django application. You can verify that the Gunicorn service is running by checking the status again:</p><div class="code-toolbar" style="box-sizing: border-box; margin: 1em 0px; position: relative;"><pre class="prefixed command language-bash" style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: rgb(17, 25, 46); border-radius: 16px; color: rgb(247, 248, 251); line-height: 22px; padding: 24px; box-shadow: rgba(17, 25, 46, 0.1) 0px 0px 0px 2px inset; display: block; overflow: auto; white-space: normal; overflow-wrap: normal;"><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: transparent; border-radius: 0px; color: inherit; line-height: 22px; padding: 0px; white-space: normal; text-shadow: none;"><ol style="box-sizing: border-box; margin: 0px; list-style: none; padding: 0px;"><li data-prefix="$" style="box-sizing: border-box; margin: 0px; padding: 0px; white-space: pre;"><span class="token function" style="box-sizing: border-box; margin: 0px; text-shadow: none; background: none; border-radius: 0px; display: inline; font-weight: 400; padding: 0px; color: rgb(255, 175, 140);">sudo</span> systemctl status gunicorn </li></ol></code></pre><div class="toolbar" style="box-sizing: border-box; margin: 0px; position: absolute; right: 24px; top: 24px;"><div class="toolbar-item" style="box-sizing: border-box; margin: 0px; display: inline-block;"><button type="button" style="box-sizing: border-box; margin: 0px; font-family: inherit; font-size: 0.9em; line-height: normal; overflow: visible; text-transform: none; appearance: button; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-optical-sizing: inherit; font-kerning: inherit; font-feature-settings: inherit; font-variation-settings: inherit; background: rgb(105, 111, 176); border: 0px; cursor: pointer; user-select: none; border-radius: 10px; color: rgb(255, 255, 255); padding: 6px 8px; transition: color 0.25s ease 0s, background 0.25s ease 0s;">Copy</button></div></div></div><pre style="box-sizing: border-box; margin: 1em 0px; font-family: monospace, monospace; font-size: 14px; background: rgb(17, 25, 46); border-radius: 16px; color: rgb(247, 248, 251); line-height: 22px; padding: 24px; box-shadow: rgba(17, 25, 46, 0.1) 0px 0px 0px 2px inset; display: block; overflow: auto; white-space: normal; overflow-wrap: normal;"><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: 0px center; border-radius: 0px; color: inherit; line-height: 22px; padding: 0px; white-space: pre;"><div class="secondary-code-label" title="Output" style="box-sizing: border-box; margin: 0px 0px 4px; color: rgb(159, 221, 255);">Output</div>● gunicorn.service - gunicorn daemon Loaded: loaded (/etc/systemd/system/gunicorn.service; disabled; vendor preset Active: <mark style="box-sizing: border-box; margin: 0px; background: rgb(41, 51, 77); border-radius: 2px; color: inherit; display: inline; line-height: calc(1.4em + 1px); padding: 2px 6px;">active (running)</mark> since Tue 2021-11-23 22:55:12 UTC; 11s ago Main PID: 11002 (gunicorn) Tasks: 4 (limit: 1151) CGroup: /system.slice/gunicorn.service ├─11002 /home/sammy/myprojectdir/myprojectenv/bin/python /home/sammy/ ├─11018 /home/sammy/myprojectdir/myprojectenv/bin/python /home/sammy/ ├─11020 /home/sammy/myprojectdir/myprojectenv/bin/python /home/sammy/ └─11022 /home/sammy/myprojectdir/myprojectenv/bin/python /home/sammy/ . . . </code></pre><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">If the output from<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">curl</code><span> </span>or the output of<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">systemctl status</code><span> </span>indicates that a problem occurred, check the logs for additional details:</p><div class="code-toolbar" style="box-sizing: border-box; margin: 1em 0px; position: relative;"><pre class="prefixed command language-bash" style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: rgb(17, 25, 46); border-radius: 16px; color: rgb(247, 248, 251); line-height: 22px; padding: 24px; box-shadow: rgba(17, 25, 46, 0.1) 0px 0px 0px 2px inset; display: block; overflow: auto; white-space: normal; overflow-wrap: normal;"><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: transparent; border-radius: 0px; color: inherit; line-height: 22px; padding: 0px; white-space: normal; text-shadow: none;"><ol style="box-sizing: border-box; margin: 0px; list-style: none; padding: 0px;"><li data-prefix="$" style="box-sizing: border-box; margin: 0px; padding: 0px; white-space: pre;"><span class="token function" style="box-sizing: border-box; margin: 0px; text-shadow: none; background: none; border-radius: 0px; display: inline; font-weight: 400; padding: 0px; color: rgb(255, 175, 140);">sudo</span> journalctl <span class="token parameter variable" style="box-sizing: border-box; margin: 0px; text-shadow: none; background: none; border-radius: 0px; display: inline; font-weight: 400; padding: 0px; color: rgb(177, 228, 144);">-u</span> gunicorn </li></ol></code></pre><div class="toolbar" style="box-sizing: border-box; margin: 0px; position: absolute; right: 24px; top: 24px;"><div class="toolbar-item" style="box-sizing: border-box; margin: 0px; display: inline-block;"><button type="button" style="box-sizing: border-box; margin: 0px; font-family: inherit; font-size: 0.9em; line-height: normal; overflow: visible; text-transform: none; appearance: button; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-optical-sizing: inherit; font-kerning: inherit; font-feature-settings: inherit; font-variation-settings: inherit; background: rgb(105, 111, 176); border: 0px; cursor: pointer; user-select: none; border-radius: 10px; color: rgb(255, 255, 255); padding: 6px 8px; transition: color 0.25s ease 0s, background 0.25s ease 0s;">Copy</button></div></div></div><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">Check your<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">/etc/systemd/system/gunicorn.service</code><span> </span>file for problems. If you make changes to the<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">/etc/systemd/system/gunicorn.service</code><span> </span>file, reload the daemon to reread the service definition:</p><div class="code-toolbar" style="box-sizing: border-box; margin: 1em 0px; position: relative;"><pre class="prefixed command language-bash" style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: rgb(17, 25, 46); border-radius: 16px; color: rgb(247, 248, 251); line-height: 22px; padding: 24px; box-shadow: rgba(17, 25, 46, 0.1) 0px 0px 0px 2px inset; display: block; overflow: auto; white-space: normal; overflow-wrap: normal;"><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: transparent; border-radius: 0px; color: inherit; line-height: 22px; padding: 0px; white-space: normal; text-shadow: none;"><ol style="box-sizing: border-box; margin: 0px; list-style: none; padding: 0px;"><li data-prefix="$" style="box-sizing: border-box; margin: 0px; padding: 0px; white-space: pre;"><span class="token function" style="box-sizing: border-box; margin: 0px; text-shadow: none; background: none; border-radius: 0px; display: inline; font-weight: 400; padding: 0px; color: rgb(255, 175, 140);">sudo</span> systemctl daemon-reload </li></ol></code></pre><div class="toolbar" style="box-sizing: border-box; margin: 0px; position: absolute; right: 24px; top: 24px;"><div class="toolbar-item" style="box-sizing: border-box; margin: 0px; display: inline-block;"><button type="button" style="box-sizing: border-box; margin: 0px; font-family: inherit; font-size: 0.9em; line-height: normal; overflow: visible; text-transform: none; appearance: button; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-optical-sizing: inherit; font-kerning: inherit; font-feature-settings: inherit; font-variation-settings: inherit; background: rgb(105, 111, 176); border: 0px; cursor: pointer; user-select: none; border-radius: 10px; color: rgb(255, 255, 255); padding: 6px 8px; transition: color 0.25s ease 0s, background 0.25s ease 0s;">Copy</button></div></div></div><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">Then restart the Gunicorn process:</p><div class="code-toolbar" style="box-sizing: border-box; margin: 1em 0px; position: relative;"><pre class="prefixed command language-bash" style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: rgb(17, 25, 46); border-radius: 16px; color: rgb(247, 248, 251); line-height: 22px; padding: 24px; box-shadow: rgba(17, 25, 46, 0.1) 0px 0px 0px 2px inset; display: block; overflow: auto; white-space: normal; overflow-wrap: normal;"><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: transparent; border-radius: 0px; color: inherit; line-height: 22px; padding: 0px; white-space: normal; text-shadow: none;"><ol style="box-sizing: border-box; margin: 0px; list-style: none; padding: 0px;"><li data-prefix="$" style="box-sizing: border-box; margin: 0px; padding: 0px; white-space: pre;"><span class="token function" style="box-sizing: border-box; margin: 0px; text-shadow: none; background: none; border-radius: 0px; display: inline; font-weight: 400; padding: 0px; color: rgb(255, 175, 140);">sudo</span> systemctl restart gunicorn </li></ol></code></pre><div class="toolbar" style="box-sizing: border-box; margin: 0px; position: absolute; right: 24px; top: 24px;"><div class="toolbar-item" style="box-sizing: border-box; margin: 0px; display: inline-block;"><button type="button" style="box-sizing: border-box; margin: 0px; font-family: inherit; font-size: 0.9em; line-height: normal; overflow: visible; text-transform: none; appearance: button; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-optical-sizing: inherit; font-kerning: inherit; font-feature-settings: inherit; font-variation-settings: inherit; background: rgb(105, 111, 176); border: 0px; cursor: pointer; user-select: none; border-radius: 10px; color: rgb(255, 255, 255); padding: 6px 8px; transition: color 0.25s ease 0s, background 0.25s ease 0s;">Copy</button></div></div></div><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">If any issues such as these occur, troubleshoot them before continuing.</p><h2 id="step-10-configure-nginx-to-proxy-pass-to-gunicorn" style="box-sizing: border-box; margin: 1em 0px; font-family: Epilogue, sans-serif;"><a href="https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-18-04#step-10-configure-nginx-to-proxy-pass-to-gunicorn" style="box-sizing: border-box; margin: 0px; background: linear-gradient(rgb(0, 105, 255), rgb(0, 105, 255)) center bottom / 0px 0px repeat-x; color: inherit; text-decoration: none; border-bottom: none; transition: background 0.3s ease-in-out 0s, color 0.3s ease-in-out 0s;">Step 10 — Configure Nginx to Proxy Pass to Gunicorn</a><a class="hash-anchor" href="https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-18-04#step-10-configure-nginx-to-proxy-pass-to-gunicorn" aria-hidden="true" style="box-sizing: border-box; margin: 0px 0px 0px 0.5em; background: linear-gradient(rgb(0, 105, 255), rgb(0, 105, 255)) center bottom / 0px 0px repeat-x; color: rgb(138, 150, 181); text-decoration: none; border-bottom: none; transition: opacity 0.2s ease-in-out 0s, visibility 0s linear 0.2s; font-size: 0.85em; opacity: 0; visibility: hidden;"></a></h2><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">Now that Gunicorn is set up, next you’ll configure Nginx to pass traffic to the process.</p><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">Start by creating and opening a new server block in Nginx’s<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">sites-available</code><span> </span>directory:</p><div class="code-toolbar" style="box-sizing: border-box; margin: 1em 0px; position: relative;"><pre class="prefixed command language-bash" style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: rgb(17, 25, 46); border-radius: 16px; color: rgb(247, 248, 251); line-height: 22px; padding: 24px; box-shadow: rgba(17, 25, 46, 0.1) 0px 0px 0px 2px inset; display: block; overflow: auto; white-space: normal; overflow-wrap: normal;"><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: transparent; border-radius: 0px; color: inherit; line-height: 22px; padding: 0px; white-space: normal; text-shadow: none;"><ol style="box-sizing: border-box; margin: 0px; list-style: none; padding: 0px;"><li data-prefix="$" style="box-sizing: border-box; margin: 0px; padding: 0px; white-space: pre;"><span class="token function" style="box-sizing: border-box; margin: 0px; text-shadow: none; background: none; border-radius: 0px; display: inline; font-weight: 400; padding: 0px; color: rgb(255, 175, 140);">sudo</span> <span class="token function" style="box-sizing: border-box; margin: 0px; text-shadow: none; background: none; border-radius: 0px; display: inline; font-weight: 400; padding: 0px; color: rgb(255, 175, 140);">nano</span> /etc/nginx/sites-available/<mark style="box-sizing: border-box; margin: 0px; background: rgb(41, 51, 77); border-radius: 2px; color: inherit; display: inline; line-height: calc(1.4em + 1px); padding: 2px 6px;">myproject</mark> </li></ol></code></pre><div class="toolbar" style="box-sizing: border-box; margin: 0px; position: absolute; right: 24px; top: 24px;"><div class="toolbar-item" style="box-sizing: border-box; margin: 0px; display: inline-block;"><button type="button" style="box-sizing: border-box; margin: 0px; font-family: inherit; font-size: 0.9em; line-height: normal; overflow: visible; text-transform: none; appearance: button; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-optical-sizing: inherit; font-kerning: inherit; font-feature-settings: inherit; font-variation-settings: inherit; background: rgb(105, 111, 176); border: 0px; cursor: pointer; user-select: none; border-radius: 10px; color: rgb(255, 255, 255); padding: 6px 8px; transition: color 0.25s ease 0s, background 0.25s ease 0s;">Copy</button></div></div></div><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">Inside, open up a new server block. Start by specifying that this block should listen on the normal port<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">80</code><span> </span>and that it should respond to your server’s domain name or IP address:</p><div class="code-label" title="/etc/nginx/sites-available/myproject" style="box-sizing: border-box; margin: 1em 0px 0px; background-color: rgb(214, 220, 234); border-radius: 16px 16px 0px 0px; color: rgb(36, 51, 90); display: block; font-size: 16px; padding: 12px; position: relative; text-align: center; z-index: 2;">/etc/nginx/sites-available/myproject</div><pre style="box-sizing: border-box; margin: 0px 0px 1em; font-family: monospace, monospace; font-size: 14px; background: rgb(17, 25, 46); border-radius: 0px 0px 16px 16px; color: rgb(247, 248, 251); line-height: 22px; padding: 24px; box-shadow: rgba(17, 25, 46, 0.1) 0px 0px 0px 2px inset; display: block; overflow: auto; white-space: normal; overflow-wrap: normal;"><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: 0px center; border-radius: 0px; color: inherit; line-height: 22px; padding: 0px; white-space: pre;">server { listen 80; server_name <mark style="box-sizing: border-box; margin: 0px; background: rgb(41, 51, 77); border-radius: 2px; color: inherit; display: inline; line-height: calc(1.4em + 1px); padding: 2px 6px;">server_domain_or_IP</mark>; } </code></pre><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">Next, instruct Nginx to ignore any problems with finding a favicon. Also, tell it where to find the static assets that you collected in your<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">~/<mark style="box-sizing: border-box; margin: 0px; background: rgba(255, 192, 1, 0.3); border-radius: 2px; color: inherit; display: inline; line-height: calc(1.4em + 1px); padding: 2px 6px;">myprojectdir</mark>/static</code><span> </span>directory. All of these files have a standard URI prefix of<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">/static</code>, so you can create a location block to match those requests:</p><div class="code-label" title="/etc/nginx/sites-available/myproject" style="box-sizing: border-box; margin: 1em 0px 0px; background-color: rgb(214, 220, 234); border-radius: 16px 16px 0px 0px; color: rgb(36, 51, 90); display: block; font-size: 16px; padding: 12px; position: relative; text-align: center; z-index: 2;">/etc/nginx/sites-available/myproject</div><pre style="box-sizing: border-box; margin: 0px 0px 1em; font-family: monospace, monospace; font-size: 14px; background: rgb(17, 25, 46); border-radius: 0px 0px 16px 16px; color: rgb(247, 248, 251); line-height: 22px; padding: 24px; box-shadow: rgba(17, 25, 46, 0.1) 0px 0px 0px 2px inset; display: block; overflow: auto; white-space: normal; overflow-wrap: normal;"><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: 0px center; border-radius: 0px; color: inherit; line-height: 22px; padding: 0px; white-space: pre;">server { listen 80; server_name <mark style="box-sizing: border-box; margin: 0px; background: rgb(41, 51, 77); border-radius: 2px; color: inherit; display: inline; line-height: calc(1.4em + 1px); padding: 2px 6px;">server_domain_or_IP</mark>; location = /favicon.ico { access_log off; log_not_found off; } location /static/ { root /home/<mark style="box-sizing: border-box; margin: 0px; background: rgb(41, 51, 77); border-radius: 2px; color: inherit; display: inline; line-height: calc(1.4em + 1px); padding: 2px 6px;">sammy</mark>/<mark style="box-sizing: border-box; margin: 0px; background: rgb(41, 51, 77); border-radius: 2px; color: inherit; display: inline; line-height: calc(1.4em + 1px); padding: 2px 6px;">myprojectdir</mark>; } } </code></pre><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">Finally, create a<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">location / {}</code><span> </span>block to match all other requests. Inside of this location, include the standard<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">proxy_params</code><span> </span>file from the Nginx installation and then pass the traffic directly to the Gunicorn socket:</p><div class="code-label" title="/etc/nginx/sites-available/myproject" style="box-sizing: border-box; margin: 1em 0px 0px; background-color: rgb(214, 220, 234); border-radius: 16px 16px 0px 0px; color: rgb(36, 51, 90); display: block; font-size: 16px; padding: 12px; position: relative; text-align: center; z-index: 2;">/etc/nginx/sites-available/myproject</div><pre style="box-sizing: border-box; margin: 0px 0px 1em; font-family: monospace, monospace; font-size: 14px; background: rgb(17, 25, 46); border-radius: 0px 0px 16px 16px; color: rgb(247, 248, 251); line-height: 22px; padding: 24px; box-shadow: rgba(17, 25, 46, 0.1) 0px 0px 0px 2px inset; display: block; overflow: auto; white-space: normal; overflow-wrap: normal;"><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: 0px center; border-radius: 0px; color: inherit; line-height: 22px; padding: 0px; white-space: pre;">server { listen 80; server_name <mark style="box-sizing: border-box; margin: 0px; background: rgb(41, 51, 77); border-radius: 2px; color: inherit; display: inline; line-height: calc(1.4em + 1px); padding: 2px 6px;">server_domain_or_IP</mark>; location = /favicon.ico { access_log off; log_not_found off; } location /static/ { root /home/<mark style="box-sizing: border-box; margin: 0px; background: rgb(41, 51, 77); border-radius: 2px; color: inherit; display: inline; line-height: calc(1.4em + 1px); padding: 2px 6px;">sammy</mark>/<mark style="box-sizing: border-box; margin: 0px; background: rgb(41, 51, 77); border-radius: 2px; color: inherit; display: inline; line-height: calc(1.4em + 1px); padding: 2px 6px;">myprojectdir</mark>; } location / { include proxy_params; proxy_pass http://unix:/run/gunicorn.sock; } } </code></pre><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">Save and close the file when you are finished. Now, you can enable the file by linking it to the<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">sites-enabled</code><span> </span>directory:</p><div class="code-toolbar" style="box-sizing: border-box; margin: 1em 0px; position: relative;"><pre class="prefixed command language-bash" style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: rgb(17, 25, 46); border-radius: 16px; color: rgb(247, 248, 251); line-height: 22px; padding: 24px; box-shadow: rgba(17, 25, 46, 0.1) 0px 0px 0px 2px inset; display: block; overflow: auto; white-space: normal; overflow-wrap: normal;"><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: transparent; border-radius: 0px; color: inherit; line-height: 22px; padding: 0px; white-space: normal; text-shadow: none;"><ol style="box-sizing: border-box; margin: 0px; list-style: none; padding: 0px;"><li data-prefix="$" style="box-sizing: border-box; margin: 0px; padding: 0px; white-space: pre;"><span class="token function" style="box-sizing: border-box; margin: 0px; text-shadow: none; background: none; border-radius: 0px; display: inline; font-weight: 400; padding: 0px; color: rgb(255, 175, 140);">sudo</span> <span class="token function" style="box-sizing: border-box; margin: 0px; text-shadow: none; background: none; border-radius: 0px; display: inline; font-weight: 400; padding: 0px; color: rgb(255, 175, 140);">ln</span> <span class="token parameter variable" style="box-sizing: border-box; margin: 0px; text-shadow: none; background: none; border-radius: 0px; display: inline; font-weight: 400; padding: 0px; color: rgb(177, 228, 144);">-s</span> /etc/nginx/sites-available/<mark style="box-sizing: border-box; margin: 0px; background: rgb(41, 51, 77); border-radius: 2px; color: inherit; display: inline; line-height: calc(1.4em + 1px); padding: 2px 6px;">myproject</mark> /etc/nginx/sites-enabled </li></ol></code></pre><div class="toolbar" style="box-sizing: border-box; margin: 0px; position: absolute; right: 24px; top: 24px;"><div class="toolbar-item" style="box-sizing: border-box; margin: 0px; display: inline-block;"><button type="button" style="box-sizing: border-box; margin: 0px; font-family: inherit; font-size: 0.9em; line-height: normal; overflow: visible; text-transform: none; appearance: button; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-optical-sizing: inherit; font-kerning: inherit; font-feature-settings: inherit; font-variation-settings: inherit; background: rgb(105, 111, 176); border: 0px; cursor: pointer; user-select: none; border-radius: 10px; color: rgb(255, 255, 255); padding: 6px 8px; transition: color 0.25s ease 0s, background 0.25s ease 0s;">Copy</button></div></div></div><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">Test your Nginx configuration for syntax errors:</p><div class="code-toolbar" style="box-sizing: border-box; margin: 1em 0px; position: relative;"><pre class="prefixed command language-bash" style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: rgb(17, 25, 46); border-radius: 16px; color: rgb(247, 248, 251); line-height: 22px; padding: 24px; box-shadow: rgba(17, 25, 46, 0.1) 0px 0px 0px 2px inset; display: block; overflow: auto; white-space: normal; overflow-wrap: normal;"><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: transparent; border-radius: 0px; color: inherit; line-height: 22px; padding: 0px; white-space: normal; text-shadow: none;"><ol style="box-sizing: border-box; margin: 0px; list-style: none; padding: 0px;"><li data-prefix="$" style="box-sizing: border-box; margin: 0px; padding: 0px; white-space: pre;"><span class="token function" style="box-sizing: border-box; margin: 0px; text-shadow: none; background: none; border-radius: 0px; display: inline; font-weight: 400; padding: 0px; color: rgb(255, 175, 140);">sudo</span> nginx <span class="token parameter variable" style="box-sizing: border-box; margin: 0px; text-shadow: none; background: none; border-radius: 0px; display: inline; font-weight: 400; padding: 0px; color: rgb(177, 228, 144);">-t</span> </li></ol></code></pre><div class="toolbar" style="box-sizing: border-box; margin: 0px; position: absolute; right: 24px; top: 24px;"><div class="toolbar-item" style="box-sizing: border-box; margin: 0px; display: inline-block;"><button type="button" style="box-sizing: border-box; margin: 0px; font-family: inherit; font-size: 0.9em; line-height: normal; overflow: visible; text-transform: none; appearance: button; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-optical-sizing: inherit; font-kerning: inherit; font-feature-settings: inherit; font-variation-settings: inherit; background: rgb(105, 111, 176); border: 0px; cursor: pointer; user-select: none; border-radius: 10px; color: rgb(255, 255, 255); padding: 6px 8px; transition: color 0.25s ease 0s, background 0.25s ease 0s;">Copy</button></div></div></div><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">If no errors are reported, go ahead and restart Nginx:</p><div class="code-toolbar" style="box-sizing: border-box; margin: 1em 0px; position: relative;"><pre class="prefixed command language-bash" style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: rgb(17, 25, 46); border-radius: 16px; color: rgb(247, 248, 251); line-height: 22px; padding: 24px; box-shadow: rgba(17, 25, 46, 0.1) 0px 0px 0px 2px inset; display: block; overflow: auto; white-space: normal; overflow-wrap: normal;"><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: transparent; border-radius: 0px; color: inherit; line-height: 22px; padding: 0px; white-space: normal; text-shadow: none;"><ol style="box-sizing: border-box; margin: 0px; list-style: none; padding: 0px;"><li data-prefix="$" style="box-sizing: border-box; margin: 0px; padding: 0px; white-space: pre;"><span class="token function" style="box-sizing: border-box; margin: 0px; text-shadow: none; background: none; border-radius: 0px; display: inline; font-weight: 400; padding: 0px; color: rgb(255, 175, 140);">sudo</span> systemctl restart nginx </li></ol></code></pre><div class="toolbar" style="box-sizing: border-box; margin: 0px; position: absolute; right: 24px; top: 24px;"><div class="toolbar-item" style="box-sizing: border-box; margin: 0px; display: inline-block;"><button type="button" style="box-sizing: border-box; margin: 0px; font-family: inherit; font-size: 0.9em; line-height: normal; overflow: visible; text-transform: none; appearance: button; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-optical-sizing: inherit; font-kerning: inherit; font-feature-settings: inherit; font-variation-settings: inherit; background: rgb(105, 111, 176); border: 0px; cursor: pointer; user-select: none; border-radius: 10px; color: rgb(255, 255, 255); padding: 6px 8px; transition: color 0.25s ease 0s, background 0.25s ease 0s;">Copy</button></div></div></div><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">Since you no longer need access to the development server, adjust your firewall settings by removing the rule to open port<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">8000</code>:</p><div class="code-toolbar" style="box-sizing: border-box; margin: 1em 0px; position: relative;"><pre class="prefixed command language-bash" style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: rgb(17, 25, 46); border-radius: 16px; color: rgb(247, 248, 251); line-height: 22px; padding: 24px; box-shadow: rgba(17, 25, 46, 0.1) 0px 0px 0px 2px inset; display: block; overflow: auto; white-space: normal; overflow-wrap: normal;"><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: transparent; border-radius: 0px; color: inherit; line-height: 22px; padding: 0px; white-space: normal; text-shadow: none;"><ol style="box-sizing: border-box; margin: 0px; list-style: none; padding: 0px;"><li data-prefix="$" style="box-sizing: border-box; margin: 0px; padding: 0px; white-space: pre;"><span class="token function" style="box-sizing: border-box; margin: 0px; text-shadow: none; background: none; border-radius: 0px; display: inline; font-weight: 400; padding: 0px; color: rgb(255, 175, 140);">sudo</span> ufw delete allow <span class="token number" style="box-sizing: border-box; margin: 0px; text-shadow: none; background: none; border-radius: 0px; display: inline; font-weight: 400; padding: 0px; color: rgb(11, 225, 255);">8000</span> </li></ol></code></pre><div class="toolbar" style="box-sizing: border-box; margin: 0px; position: absolute; right: 24px; top: 24px;"><div class="toolbar-item" style="box-sizing: border-box; margin: 0px; display: inline-block;"><button type="button" style="box-sizing: border-box; margin: 0px; font-family: inherit; font-size: 0.9em; line-height: normal; overflow: visible; text-transform: none; appearance: button; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-optical-sizing: inherit; font-kerning: inherit; font-feature-settings: inherit; font-variation-settings: inherit; background: rgb(105, 111, 176); border: 0px; cursor: pointer; user-select: none; border-radius: 10px; color: rgb(255, 255, 255); padding: 6px 8px; transition: color 0.25s ease 0s, background 0.25s ease 0s;">Copy</button></div></div></div><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">Then allow normal traffic on ports<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">80</code><span> </span>and<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">443</code><span> </span>— thereby allowing HTTP and HTTPS connections, respectively — with the following command:</p><div class="code-toolbar" style="box-sizing: border-box; margin: 1em 0px; position: relative;"><pre class="prefixed command language-bash" style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: rgb(17, 25, 46); border-radius: 16px; color: rgb(247, 248, 251); line-height: 22px; padding: 24px; box-shadow: rgba(17, 25, 46, 0.1) 0px 0px 0px 2px inset; display: block; overflow: auto; white-space: normal; overflow-wrap: normal;"><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: transparent; border-radius: 0px; color: inherit; line-height: 22px; padding: 0px; white-space: normal; text-shadow: none;"><ol style="box-sizing: border-box; margin: 0px; list-style: none; padding: 0px;"><li data-prefix="$" style="box-sizing: border-box; margin: 0px; padding: 0px; white-space: pre;"><span class="token function" style="box-sizing: border-box; margin: 0px; text-shadow: none; background: none; border-radius: 0px; display: inline; font-weight: 400; padding: 0px; color: rgb(255, 175, 140);">sudo</span> ufw allow <span class="token string" style="box-sizing: border-box; margin: 0px; text-shadow: none; background: none; border-radius: 0px; display: inline; font-weight: 400; padding: 0px; color: rgb(177, 228, 144);">'Nginx Full'</span> </li></ol></code></pre><div class="toolbar" style="box-sizing: border-box; margin: 0px; position: absolute; right: 24px; top: 24px;"><div class="toolbar-item" style="box-sizing: border-box; margin: 0px; display: inline-block;"><button type="button" style="box-sizing: border-box; margin: 0px; font-family: inherit; font-size: 0.9em; line-height: normal; overflow: visible; text-transform: none; appearance: button; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-optical-sizing: inherit; font-kerning: inherit; font-feature-settings: inherit; font-variation-settings: inherit; background: rgb(105, 111, 176); border: 0px; cursor: pointer; user-select: none; border-radius: 10px; color: rgb(255, 255, 255); padding: 6px 8px; transition: color 0.25s ease 0s, background 0.25s ease 0s;">Copy</button></div></div></div><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">You should now be able to go to your server’s domain or IP address to view your application.</p><div class="callout note" style="box-sizing: border-box; margin: 0px 0px 1.5em; background-color: rgb(213, 240, 195); border-radius: 16px; color: rgb(0, 12, 42); display: block; font-size: 15px; line-height: 1.5em; padding: 1em 1.25em; position: relative; z-index: 0;"><p style="box-sizing: border-box; margin: 0px 0px 1em; line-height: 1.5;"><strong style="box-sizing: border-box; margin: 0px; font-weight: bolder;">Note:</strong><span> </span>After configuring Nginx, the next step should be securing traffic to the server using SSL/TLS. This is important because without it, all information, including passwords, are sent over the network in plain text.</p><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">If you have a domain name, the quickest way to get an SSL certificate to secure your traffic is using Let’s Encrypt. You can set it up by following our guide on<span> </span><a href="https://www.digitalocean.com/community/tutorials/how-to-secure-nginx-with-let-s-encrypt-on-ubuntu-18-04" style="box-sizing: border-box; margin: 0px; background: linear-gradient(rgb(0, 105, 255), rgb(0, 105, 255)) center bottom / 1px 1px repeat-x; color: rgb(0, 105, 255); text-decoration: none; border-bottom: none; transition: background 0.3s ease-in-out 0s, color 0.3s ease-in-out 0s; overflow-wrap: break-word;">How to Secure Let’s Encrypt with Nginx on Ubuntu 18.04</a>. Follow the guide using the Nginx server block you created in this guide.</p><p style="box-sizing: border-box; margin: 1em 0px 0px; line-height: 1.5;">If you do not have a domain name, you can still secure your site for testing and learning with a<span> </span><a href="https://www.digitalocean.com/community/tutorials/how-to-create-a-self-signed-ssl-certificate-for-nginx-in-ubuntu-18-04" style="box-sizing: border-box; margin: 0px; background: linear-gradient(rgb(0, 105, 255), rgb(0, 105, 255)) center bottom / 1px 1px repeat-x; color: rgb(0, 105, 255); text-decoration: none; border-bottom: none; transition: background 0.3s ease-in-out 0s, color 0.3s ease-in-out 0s; overflow-wrap: break-word;">self-signed SSL certificate</a>. Again, follow the process using the Nginx server block you created in this tutorial.</p></div><h2 id="step-11-troubleshooting-nginx-and-gunicorn" style="box-sizing: border-box; margin: 1em 0px; font-family: Epilogue, sans-serif;"><a href="https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-18-04#step-11-troubleshooting-nginx-and-gunicorn" style="box-sizing: border-box; margin: 0px; background: linear-gradient(rgb(0, 105, 255), rgb(0, 105, 255)) center bottom / 0px 0px repeat-x; color: inherit; text-decoration: none; border-bottom: none; transition: background 0.3s ease-in-out 0s, color 0.3s ease-in-out 0s;">Step 11 —Troubleshooting Nginx and Gunicorn</a><a class="hash-anchor" href="https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-18-04#step-11-troubleshooting-nginx-and-gunicorn" aria-hidden="true" style="box-sizing: border-box; margin: 0px 0px 0px 0.5em; background: linear-gradient(rgb(0, 105, 255), rgb(0, 105, 255)) center bottom / 0px 0px repeat-x; color: rgb(138, 150, 181); text-decoration: none; border-bottom: none; transition: opacity 0.2s ease-in-out 0s, visibility 0s linear 0.2s; font-size: 0.85em; opacity: 0; visibility: hidden;"></a></h2><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">If you’re experiencing issues with your application, the following sections provide guidance on how to troubleshoot your installation.</p><h3 id="nginx-is-showing-the-default-page-instead-of-the-django-application" style="box-sizing: border-box; margin: 1em 0px; font-family: Epilogue, sans-serif;"><a href="https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-18-04#nginx-is-showing-the-default-page-instead-of-the-django-application" style="box-sizing: border-box; margin: 0px; background: linear-gradient(rgb(0, 105, 255), rgb(0, 105, 255)) center bottom / 0px 0px repeat-x; color: inherit; text-decoration: none; border-bottom: none; transition: background 0.3s ease-in-out 0s, color 0.3s ease-in-out 0s;">Nginx Is Showing the Default Page Instead of the Django Application</a><a class="hash-anchor" href="https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-18-04#nginx-is-showing-the-default-page-instead-of-the-django-application" aria-hidden="true" style="box-sizing: border-box; margin: 0px 0px 0px 0.5em; background: linear-gradient(rgb(0, 105, 255), rgb(0, 105, 255)) center bottom / 0px 0px repeat-x; color: rgb(138, 150, 181); text-decoration: none; border-bottom: none; transition: opacity 0.2s ease-in-out 0s, visibility 0s linear 0.2s; font-size: 0.85em; opacity: 0; visibility: hidden;"></a></h3><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">If Nginx displays the default page instead of proxying to your application, it usually means that you need to adjust the<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">server_name</code><span> </span>within the<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">/etc/nginx/sites-available/<mark style="box-sizing: border-box; margin: 0px; background: rgba(255, 192, 1, 0.3); border-radius: 2px; color: inherit; display: inline; line-height: calc(1.4em + 1px); padding: 2px 6px;">myproject</mark></code><span> </span>file to point to your server’s IP address or domain name.</p><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">Nginx uses the<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">server_name</code><span> </span>directive to determine which server block to use to respond to requests. If you are getting the default Nginx page, it is a sign that Nginx wasn’t able to match the request to a server block explicitly, so it’s falling back on the default block defined in<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">/etc/nginx/sites-available/default</code>.</p><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">The<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">server_name</code><span> </span>in your project’s server block must be more specific than the one in the default server block to be selected.</p><h3 id="nginx-is-displaying-a-502-bad-gateway-error-instead-of-the-django-application" style="box-sizing: border-box; margin: 1em 0px; font-family: Epilogue, sans-serif;"><a href="https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-18-04#nginx-is-displaying-a-502-bad-gateway-error-instead-of-the-django-application" style="box-sizing: border-box; margin: 0px; background: linear-gradient(rgb(0, 105, 255), rgb(0, 105, 255)) center bottom / 0px 0px repeat-x; color: inherit; text-decoration: none; border-bottom: none; transition: background 0.3s ease-in-out 0s, color 0.3s ease-in-out 0s;">Nginx Is Displaying a 502 Bad Gateway Error Instead of the Django Application</a><a class="hash-anchor" href="https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-18-04#nginx-is-displaying-a-502-bad-gateway-error-instead-of-the-django-application" aria-hidden="true" style="box-sizing: border-box; margin: 0px 0px 0px 0.5em; background: linear-gradient(rgb(0, 105, 255), rgb(0, 105, 255)) center bottom / 0px 0px repeat-x; color: rgb(138, 150, 181); text-decoration: none; border-bottom: none; transition: opacity 0.2s ease-in-out 0s, visibility 0s linear 0.2s; font-size: 0.85em; opacity: 0; visibility: hidden;"></a></h3><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">A 502 error indicates that Nginx is unable to successfully proxy the request. A wide range of configuration problems express themselves with a 502 error, so more information is required to troubleshoot properly.</p><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">The primary place to find more information is in Nginx’s error logs. Generally, this will tell you what conditions caused problems during the proxying event. Check the Nginx error logs by running the following:</p><div class="code-toolbar" style="box-sizing: border-box; margin: 1em 0px; position: relative;"><pre class="prefixed command language-bash" style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: rgb(17, 25, 46); border-radius: 16px; color: rgb(247, 248, 251); line-height: 22px; padding: 24px; box-shadow: rgba(17, 25, 46, 0.1) 0px 0px 0px 2px inset; display: block; overflow: auto; white-space: normal; overflow-wrap: normal;"><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: transparent; border-radius: 0px; color: inherit; line-height: 22px; padding: 0px; white-space: normal; text-shadow: none;"><ol style="box-sizing: border-box; margin: 0px; list-style: none; padding: 0px;"><li data-prefix="$" style="box-sizing: border-box; margin: 0px; padding: 0px; white-space: pre;"><span class="token function" style="box-sizing: border-box; margin: 0px; text-shadow: none; background: none; border-radius: 0px; display: inline; font-weight: 400; padding: 0px; color: rgb(255, 175, 140);">sudo</span> <span class="token function" style="box-sizing: border-box; margin: 0px; text-shadow: none; background: none; border-radius: 0px; display: inline; font-weight: 400; padding: 0px; color: rgb(255, 175, 140);">tail</span> <span class="token parameter variable" style="box-sizing: border-box; margin: 0px; text-shadow: none; background: none; border-radius: 0px; display: inline; font-weight: 400; padding: 0px; color: rgb(177, 228, 144);">-F</span> /var/log/nginx/error.log </li></ol></code></pre><div class="toolbar" style="box-sizing: border-box; margin: 0px; position: absolute; right: 24px; top: 24px;"><div class="toolbar-item" style="box-sizing: border-box; margin: 0px; display: inline-block;"><button type="button" style="box-sizing: border-box; margin: 0px; font-family: inherit; font-size: 0.9em; line-height: normal; overflow: visible; text-transform: none; appearance: button; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-optical-sizing: inherit; font-kerning: inherit; font-feature-settings: inherit; font-variation-settings: inherit; background: rgb(105, 111, 176); border: 0px; cursor: pointer; user-select: none; border-radius: 10px; color: rgb(255, 255, 255); padding: 6px 8px; transition: color 0.25s ease 0s, background 0.25s ease 0s;">Copy</button></div></div></div><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">Now, make another request in your browser to generate a fresh error (try refreshing the page). You should receive a fresh error message written to the log. If you read the message, it should help you narrow down the problem.</p><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">You might receive a message like the following:</p><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;"><strong style="box-sizing: border-box; margin: 0px; font-weight: bolder;">connect() to unix:/run/gunicorn.sock failed (2: No such file or directory)</strong></p><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">This indicates that Nginx was unable to find the<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">gunicorn.sock</code><span> </span>file at the given location. You should compare the<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">proxy_pass</code><span> </span>location defined within the<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">/etc/nginx/sites-available/myproject</code><span> </span>file to the actual location of the<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">gunicorn.sock</code><span> </span>file generated by the<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">gunicorn.socket</code><span> </span>systemd unit.</p><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">If you cannot find a<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">gunicorn.sock</code><span> </span>file within the<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">/run</code><span> </span>directory, it generally means that the systemd socket file was unable to create it. Return to the section on<span> </span><a href="https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-18-04#step-8-%E2%80%94-checking-for-the-gunicorn-socket-file" style="box-sizing: border-box; margin: 0px; background: linear-gradient(rgb(0, 105, 255), rgb(0, 105, 255)) center bottom / 1px 1px repeat-x; color: rgb(0, 105, 255); text-decoration: none; border-bottom: none; transition: background 0.3s ease-in-out 0s, color 0.3s ease-in-out 0s; overflow-wrap: break-word;">checking for the Gunicorn socket file</a><span> </span>to walk through the troubleshooting steps for Gunicorn.</p><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">Another message may read like the following:</p><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;"><strong style="box-sizing: border-box; margin: 0px; font-weight: bolder;">connect() to unix:/run/gunicorn.sock failed (13: Permission denied)</strong></p><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">This indicates that Nginx was unable to connect to the Gunicorn socket because of permissions problems. This can happen when the procedure is followed using the<span> </span><strong style="box-sizing: border-box; margin: 0px; font-weight: bolder;">root</strong><span> </span>user instead of a<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">sudo</code><span> </span>user. While systemd is able to create the Gunicorn socket file, Nginx is unable to access it.</p><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">This can happen if there are limited permissions at any point between the root directory (<code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">/</code>) and the<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">gunicorn.sock</code><span> </span>file. You can review the permissions and ownership values of the socket file and each of its parent directories by passing the absolute path to your socket file to the<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">namei</code><span> </span>command:</p><div class="code-toolbar" style="box-sizing: border-box; margin: 1em 0px; position: relative;"><pre class="prefixed command language-bash" style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: rgb(17, 25, 46); border-radius: 16px; color: rgb(247, 248, 251); line-height: 22px; padding: 24px; box-shadow: rgba(17, 25, 46, 0.1) 0px 0px 0px 2px inset; display: block; overflow: auto; white-space: normal; overflow-wrap: normal;"><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: transparent; border-radius: 0px; color: inherit; line-height: 22px; padding: 0px; white-space: normal; text-shadow: none;"><ol style="box-sizing: border-box; margin: 0px; list-style: none; padding: 0px;"><li data-prefix="$" style="box-sizing: border-box; margin: 0px; padding: 0px; white-space: pre;">namei <span class="token parameter variable" style="box-sizing: border-box; margin: 0px; text-shadow: none; background: none; border-radius: 0px; display: inline; font-weight: 400; padding: 0px; color: rgb(177, 228, 144);">-l</span> /run/gunicorn.sock </li></ol></code></pre><div class="toolbar" style="box-sizing: border-box; margin: 0px; position: absolute; right: 24px; top: 24px;"><div class="toolbar-item" style="box-sizing: border-box; margin: 0px; display: inline-block;"><button type="button" style="box-sizing: border-box; margin: 0px; font-family: inherit; font-size: 0.9em; line-height: normal; overflow: visible; text-transform: none; appearance: button; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-optical-sizing: inherit; font-kerning: inherit; font-feature-settings: inherit; font-variation-settings: inherit; background: rgb(105, 111, 176); border: 0px; cursor: pointer; user-select: none; border-radius: 10px; color: rgb(255, 255, 255); padding: 6px 8px; transition: color 0.25s ease 0s, background 0.25s ease 0s;">Copy</button></div></div></div><pre style="box-sizing: border-box; margin: 1em 0px; font-family: monospace, monospace; font-size: 14px; background: rgb(17, 25, 46); border-radius: 16px; color: rgb(247, 248, 251); line-height: 22px; padding: 24px; box-shadow: rgba(17, 25, 46, 0.1) 0px 0px 0px 2px inset; display: block; overflow: auto; white-space: normal; overflow-wrap: normal;"><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: 0px center; border-radius: 0px; color: inherit; line-height: 22px; padding: 0px; white-space: pre;"><div class="secondary-code-label" title="Output" style="box-sizing: border-box; margin: 0px 0px 4px; color: rgb(159, 221, 255);">Output</div>f: /run/gunicorn.sock drwxr-xr-x root root / drwxr-xr-x root root run srw-rw-rw- root root gunicorn.sock </code></pre><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">The output displays the permissions of each of the directory components. By reviewing the permissions (first column), owner (second column), and group owner (third column), you can figure out what type of access is allowed to the socket file.</p><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">In this example, the socket file and each of the directories leading up to it have world read and execute permissions (the permissions column for the directories end with<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">r-x</code><span> </span>instead of<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">---</code>). The Nginx process should be able to access the socket successfully.</p><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">If any of the directories leading up to the socket do not have world read and execute permission, Nginx will not be able to access the socket without allowing world read and execute permissions, or by giving group ownership to a group that Nginx is a part of.</p><h3 id="django-is-displaying-could-not-connect-to-server-connection-refused" style="box-sizing: border-box; margin: 1em 0px; font-family: Epilogue, sans-serif;"><a href="https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-18-04#django-is-displaying-could-not-connect-to-server-connection-refused" style="box-sizing: border-box; margin: 0px; background: linear-gradient(rgb(0, 105, 255), rgb(0, 105, 255)) center bottom / 0px 0px repeat-x; color: inherit; text-decoration: none; border-bottom: none; transition: background 0.3s ease-in-out 0s, color 0.3s ease-in-out 0s;">Django Is Displaying: “could not connect to server: Connection refused”</a><a class="hash-anchor" href="https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-18-04#django-is-displaying-could-not-connect-to-server-connection-refused" aria-hidden="true" style="box-sizing: border-box; margin: 0px 0px 0px 0.5em; background: linear-gradient(rgb(0, 105, 255), rgb(0, 105, 255)) center bottom / 0px 0px repeat-x; color: rgb(138, 150, 181); text-decoration: none; border-bottom: none; transition: opacity 0.2s ease-in-out 0s, visibility 0s linear 0.2s; font-size: 0.85em; opacity: 0; visibility: hidden;"></a></h3><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">One message that you may receive from Django when attempting to access parts of the application in the web browser is:</p><pre style="box-sizing: border-box; margin: 1em 0px; font-family: monospace, monospace; font-size: 14px; background: rgb(17, 25, 46); border-radius: 16px; color: rgb(247, 248, 251); line-height: 22px; padding: 24px; box-shadow: rgba(17, 25, 46, 0.1) 0px 0px 0px 2px inset; display: block; overflow: auto; white-space: normal; overflow-wrap: normal;"><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: 0px center; border-radius: 0px; color: inherit; line-height: 22px; padding: 0px; white-space: pre;">OperationalError at /admin/login/ could not connect to server: Connection refused Is the server running on host "localhost" (127.0.0.1) and accepting TCP/IP connections on port 5432? </code></pre><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">This indicates that Django is unable to connect to the Postgres database. Check that the Postgres instance is running:</p><div class="code-toolbar" style="box-sizing: border-box; margin: 1em 0px; position: relative;"><pre class="prefixed command language-bash" style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: rgb(17, 25, 46); border-radius: 16px; color: rgb(247, 248, 251); line-height: 22px; padding: 24px; box-shadow: rgba(17, 25, 46, 0.1) 0px 0px 0px 2px inset; display: block; overflow: auto; white-space: normal; overflow-wrap: normal;"><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: transparent; border-radius: 0px; color: inherit; line-height: 22px; padding: 0px; white-space: normal; text-shadow: none;"><ol style="box-sizing: border-box; margin: 0px; list-style: none; padding: 0px;"><li data-prefix="$" style="box-sizing: border-box; margin: 0px; padding: 0px; white-space: pre;"><span class="token function" style="box-sizing: border-box; margin: 0px; text-shadow: none; background: none; border-radius: 0px; display: inline; font-weight: 400; padding: 0px; color: rgb(255, 175, 140);">sudo</span> systemctl status postgresql </li></ol></code></pre><div class="toolbar" style="box-sizing: border-box; margin: 0px; position: absolute; right: 24px; top: 24px;"><div class="toolbar-item" style="box-sizing: border-box; margin: 0px; display: inline-block;"><button type="button" style="box-sizing: border-box; margin: 0px; font-family: inherit; font-size: 0.9em; line-height: normal; overflow: visible; text-transform: none; appearance: button; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-optical-sizing: inherit; font-kerning: inherit; font-feature-settings: inherit; font-variation-settings: inherit; background: rgb(105, 111, 176); border: 0px; cursor: pointer; user-select: none; border-radius: 10px; color: rgb(255, 255, 255); padding: 6px 8px; transition: color 0.25s ease 0s, background 0.25s ease 0s;">Copy</button></div></div></div><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">If it’s not, you can start it and enable it to start automatically at boot (if it’s not already configured to do so):</p><div class="code-toolbar" style="box-sizing: border-box; margin: 1em 0px; position: relative;"><pre class="prefixed command language-bash" style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: rgb(17, 25, 46); border-radius: 16px; color: rgb(247, 248, 251); line-height: 22px; padding: 24px; box-shadow: rgba(17, 25, 46, 0.1) 0px 0px 0px 2px inset; display: block; overflow: auto; white-space: normal; overflow-wrap: normal;"><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: transparent; border-radius: 0px; color: inherit; line-height: 22px; padding: 0px; white-space: normal; text-shadow: none;"><ol style="box-sizing: border-box; margin: 0px; list-style: none; padding: 0px;"><li data-prefix="$" style="box-sizing: border-box; margin: 0px; padding: 0px; white-space: pre;"><span class="token function" style="box-sizing: border-box; margin: 0px; text-shadow: none; background: none; border-radius: 0px; display: inline; font-weight: 400; padding: 0px; color: rgb(255, 175, 140);">sudo</span> systemctl start postgresql </li><li data-prefix="$" style="box-sizing: border-box; margin: 0px; padding: 0px; white-space: pre;"><span class="token function" style="box-sizing: border-box; margin: 0px; text-shadow: none; background: none; border-radius: 0px; display: inline; font-weight: 400; padding: 0px; color: rgb(255, 175, 140);">sudo</span> systemctl <span class="token builtin class-name" style="box-sizing: border-box; margin: 0px; text-shadow: none; background: none; border-radius: 0px; display: inline; font-weight: 400; padding: 0px; color: rgb(255, 175, 140);">enable</span> postgresql </li></ol></code></pre><div class="toolbar" style="box-sizing: border-box; margin: 0px; position: absolute; right: 24px; top: 24px;"><div class="toolbar-item" style="box-sizing: border-box; margin: 0px; display: inline-block;"><button type="button" style="box-sizing: border-box; margin: 0px; font-family: inherit; font-size: 0.9em; line-height: normal; overflow: visible; text-transform: none; appearance: button; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-optical-sizing: inherit; font-kerning: inherit; font-feature-settings: inherit; font-variation-settings: inherit; background: rgb(105, 111, 176); border: 0px; cursor: pointer; user-select: none; border-radius: 10px; color: rgb(255, 255, 255); padding: 6px 8px; transition: color 0.25s ease 0s, background 0.25s ease 0s;">Copy</button></div></div></div><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">If you are still having issues, make sure the database settings defined in the<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">~/myprojectdir/myproject/settings.py</code><span> </span>file are correct.</p><h3 id="further-troubleshooting" style="box-sizing: border-box; margin: 1em 0px; font-family: Epilogue, sans-serif;"><a href="https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-18-04#further-troubleshooting" style="box-sizing: border-box; margin: 0px; background: linear-gradient(rgb(0, 105, 255), rgb(0, 105, 255)) center bottom / 0px 0px repeat-x; color: inherit; text-decoration: none; border-bottom: none; transition: background 0.3s ease-in-out 0s, color 0.3s ease-in-out 0s;">Further Troubleshooting</a><a class="hash-anchor" href="https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-18-04#further-troubleshooting" aria-hidden="true" style="box-sizing: border-box; margin: 0px 0px 0px 0.5em; background: linear-gradient(rgb(0, 105, 255), rgb(0, 105, 255)) center bottom / 0px 0px repeat-x; color: rgb(138, 150, 181); text-decoration: none; border-bottom: none; transition: opacity 0.2s ease-in-out 0s, visibility 0s linear 0.2s; font-size: 0.85em; opacity: 0; visibility: hidden;"></a></h3><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">For additional troubleshooting, the logs can help narrow down root causes. Check each of them and take note of any messages indicating problem areas.</p><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">The following logs may be helpful:</p><ul style="box-sizing: border-box; margin: 0px 0px 1.7em; list-style: disc; padding: 0px 0px 0px 2.5em;"><li style="box-sizing: border-box; margin: 0px;">Check the Nginx process logs:<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">sudo journalctl -u nginx</code></li><li style="box-sizing: border-box; margin: 0px;">Check the Nginx access logs:<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">sudo less /var/log/nginx/access.log</code></li><li style="box-sizing: border-box; margin: 0px;">Check the Nginx error logs:<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">sudo less /var/log/nginx/error.log</code></li><li style="box-sizing: border-box; margin: 0px;">Check the Gunicorn application logs:<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">sudo journalctl -u gunicorn</code></li><li style="box-sizing: border-box; margin: 0px;">Check the Gunicorn socket logs:<span> </span><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background-color: rgb(227, 232, 244); border-radius: 8px; color: rgb(36, 51, 90); line-height: 22px; padding: 3px;">sudo journalctl -u gunicorn.socket</code></li></ul><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">As you update your configuration or application, you will likely need to restart the processes to adjust to your changes.</p><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">If you update your Django application, you can restart the Gunicorn process to pick up the changes by running the following:</p><div class="code-toolbar" style="box-sizing: border-box; margin: 1em 0px; position: relative;"><pre class="prefixed command language-bash" style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: rgb(17, 25, 46); border-radius: 16px; color: rgb(247, 248, 251); line-height: 22px; padding: 24px; box-shadow: rgba(17, 25, 46, 0.1) 0px 0px 0px 2px inset; display: block; overflow: auto; white-space: normal; overflow-wrap: normal;"><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: transparent; border-radius: 0px; color: inherit; line-height: 22px; padding: 0px; white-space: normal; text-shadow: none;"><ol style="box-sizing: border-box; margin: 0px; list-style: none; padding: 0px;"><li data-prefix="$" style="box-sizing: border-box; margin: 0px; padding: 0px; white-space: pre;"><span class="token function" style="box-sizing: border-box; margin: 0px; text-shadow: none; background: none; border-radius: 0px; display: inline; font-weight: 400; padding: 0px; color: rgb(255, 175, 140);">sudo</span> systemctl restart gunicorn </li></ol></code></pre><div class="toolbar" style="box-sizing: border-box; margin: 0px; position: absolute; right: 24px; top: 24px;"><div class="toolbar-item" style="box-sizing: border-box; margin: 0px; display: inline-block;"><button type="button" style="box-sizing: border-box; margin: 0px; font-family: inherit; font-size: 0.9em; line-height: normal; overflow: visible; text-transform: none; appearance: button; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-optical-sizing: inherit; font-kerning: inherit; font-feature-settings: inherit; font-variation-settings: inherit; background: rgb(105, 111, 176); border: 0px; cursor: pointer; user-select: none; border-radius: 10px; color: rgb(255, 255, 255); padding: 6px 8px; transition: color 0.25s ease 0s, background 0.25s ease 0s;">Copy</button></div></div></div><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">If you change Gunicorn socket or service files, reload the daemon and restart the process by running the following:</p><div class="code-toolbar" style="box-sizing: border-box; margin: 1em 0px; position: relative;"><pre class="prefixed command language-bash" style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: rgb(17, 25, 46); border-radius: 16px; color: rgb(247, 248, 251); line-height: 22px; padding: 24px; box-shadow: rgba(17, 25, 46, 0.1) 0px 0px 0px 2px inset; display: block; overflow: auto; white-space: normal; overflow-wrap: normal;"><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: transparent; border-radius: 0px; color: inherit; line-height: 22px; padding: 0px; white-space: normal; text-shadow: none;"><ol style="box-sizing: border-box; margin: 0px; list-style: none; padding: 0px;"><li data-prefix="$" style="box-sizing: border-box; margin: 0px; padding: 0px; white-space: pre;"><span class="token function" style="box-sizing: border-box; margin: 0px; text-shadow: none; background: none; border-radius: 0px; display: inline; font-weight: 400; padding: 0px; color: rgb(255, 175, 140);">sudo</span> systemctl daemon-reload </li><li data-prefix="$" style="box-sizing: border-box; margin: 0px; padding: 0px; white-space: pre;"><span class="token function" style="box-sizing: border-box; margin: 0px; text-shadow: none; background: none; border-radius: 0px; display: inline; font-weight: 400; padding: 0px; color: rgb(255, 175, 140);">sudo</span> systemctl restart gunicorn.socket gunicorn.service </li></ol></code></pre><div class="toolbar" style="box-sizing: border-box; margin: 0px; position: absolute; right: 24px; top: 24px;"><div class="toolbar-item" style="box-sizing: border-box; margin: 0px; display: inline-block;"><button type="button" style="box-sizing: border-box; margin: 0px; font-family: inherit; font-size: 0.9em; line-height: normal; overflow: visible; text-transform: none; appearance: button; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-optical-sizing: inherit; font-kerning: inherit; font-feature-settings: inherit; font-variation-settings: inherit; background: rgb(105, 111, 176); border: 0px; cursor: pointer; user-select: none; border-radius: 10px; color: rgb(255, 255, 255); padding: 6px 8px; transition: color 0.25s ease 0s, background 0.25s ease 0s;">Copy</button></div></div></div><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">If you change the Nginx server block configuration, test the configuration and then Nginx by running the following:</p><div class="code-toolbar" style="box-sizing: border-box; margin: 1em 0px; position: relative;"><pre class="prefixed command language-bash" style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: rgb(17, 25, 46); border-radius: 16px; color: rgb(247, 248, 251); line-height: 22px; padding: 24px; box-shadow: rgba(17, 25, 46, 0.1) 0px 0px 0px 2px inset; display: block; overflow: auto; white-space: normal; overflow-wrap: normal;"><code style="box-sizing: border-box; margin: 0px; font-family: monospace, monospace; font-size: 14px; background: transparent; border-radius: 0px; color: inherit; line-height: 22px; padding: 0px; white-space: normal; text-shadow: none;"><ol style="box-sizing: border-box; margin: 0px; list-style: none; padding: 0px;"><li data-prefix="$" style="box-sizing: border-box; margin: 0px; padding: 0px; white-space: pre;"><span class="token function" style="box-sizing: border-box; margin: 0px; text-shadow: none; background: none; border-radius: 0px; display: inline; font-weight: 400; padding: 0px; color: rgb(255, 175, 140);">sudo</span> nginx <span class="token parameter variable" style="box-sizing: border-box; margin: 0px; text-shadow: none; background: none; border-radius: 0px; display: inline; font-weight: 400; padding: 0px; color: rgb(177, 228, 144);">-t</span> <span class="token operator" style="box-sizing: border-box; margin: 0px; text-shadow: none; background: none; border-radius: 0px; display: inline; font-weight: 400; padding: 0px; color: rgb(247, 248, 251);">&&</span> <span class="token function" style="box-sizing: border-box; margin: 0px; text-shadow: none; background: none; border-radius: 0px; display: inline; font-weight: 400; padding: 0px; color: rgb(255, 175, 140);">sudo</span> systemctl restart nginx </li></ol></code></pre><div class="toolbar" style="box-sizing: border-box; margin: 0px; position: absolute; right: 24px; top: 24px;"><div class="toolbar-item" style="box-sizing: border-box; margin: 0px; display: inline-block;"><button type="button" style="box-sizing: border-box; margin: 0px; font-family: inherit; font-size: 0.9em; line-height: normal; overflow: visible; text-transform: none; appearance: button; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-optical-sizing: inherit; font-kerning: inherit; font-feature-settings: inherit; font-variation-settings: inherit; background: rgb(105, 111, 176); border: 0px; cursor: pointer; user-select: none; border-radius: 10px; color: rgb(255, 255, 255); padding: 6px 8px; transition: color 0.25s ease 0s, background 0.25s ease 0s;">Copy</button></div></div></div><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">These commands are helpful for applying changes as you adjust your configuration.</p><h2 id="conclusion" style="box-sizing: border-box; margin: 1em 0px; font-family: Epilogue, sans-serif;"><a href="https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-18-04#conclusion" style="box-sizing: border-box; margin: 0px; background: linear-gradient(rgb(0, 105, 255), rgb(0, 105, 255)) center bottom / 0px 0px repeat-x; color: inherit; text-decoration: none; border-bottom: none; transition: background 0.3s ease-in-out 0s, color 0.3s ease-in-out 0s;">Conclusion</a><a class="hash-anchor" href="https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-18-04#conclusion" aria-hidden="true" style="box-sizing: border-box; margin: 0px 0px 0px 0.5em; background: linear-gradient(rgb(0, 105, 255), rgb(0, 105, 255)) center bottom / 0px 0px repeat-x; color: rgb(138, 150, 181); text-decoration: none; border-bottom: none; transition: opacity 0.2s ease-in-out 0s, visibility 0s linear 0.2s; font-size: 0.85em; opacity: 0; visibility: hidden;"></a></h2><p style="box-sizing: border-box; margin: 1em 0px; line-height: 1.5;">In this guide, you set up a Django project in its own virtual environment and configured Gunicorn to translate client requests so that Django can handle them. Afterward, you set up Nginx to act as a reverse proxy to handle client connections and serve the correct project depending on the client request.</p><p style="box-sizing: border-box; margin: 1em 0px 0px; line-height: 1.5;">Django makes creating projects and applications convenient by providing many of the common pieces, allowing you to focus on the unique elements. By leveraging the general tool chain described in this tutorial, you can serve the applications you create from a single server.</p></div><div class="Markdown_markdown___3RCz TutorialTemplateStyles__StyledConclusionCTA-sc-1gdp4d7-38 fIsdwR" style="box-sizing: border-box; margin: 32px 0px 0px; color: rgb(77, 91, 124); font-size: medium; position: relative; z-index: 0; border: 1px solid rgb(107, 113, 177); border-radius: 16px; line-height: 24px; padding: 24px 32px; font-family: Inter, sans-serif; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><br class="Apple-interchange-newline"><br></div><p></p>
Posted by
Visibility*
PUBLIC
DRAFT
PRIVATE
Categories
Django
Nginx
Gunicorn
Postgres
Ubuntu
Deployment
Django Production Server Deployement
IIDM
Digital Marketing
Concept Explained
Insights
Marketing Strategy
Artificial Intelligent
Discussion
Branding
Paid Marketing
SEO
Tags
Django
Ubuntu
Nginx
Gunicorn
Postgres
IIDM
Digital Marketing
SEO
Social Media
Email Marketing
Content Marketing
Digital Marketing Concept
Digital Marketing Strategy
Influencer Marketing
Insights
Featured image*
Minutes read*
Stick to top of blog
Allow comments
Allow social sharing