<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: jdbit</title>
    <description>The latest articles on DEV Community by jdbit (@jdbit).</description>
    <link>https://dev.to/jdbit</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F434768%2Faf834725-5e70-4d3a-87ac-c0b20350142f.png</url>
      <title>DEV Community: jdbit</title>
      <link>https://dev.to/jdbit</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jdbit"/>
    <language>en</language>
    <item>
      <title>Deploy multiple Django websites with a simple command</title>
      <dc:creator>jdbit</dc:creator>
      <pubDate>Sat, 21 May 2022 15:21:01 +0000</pubDate>
      <link>https://dev.to/jdbit/deploy-multiple-django-websites-with-a-simple-command-h3d</link>
      <guid>https://dev.to/jdbit/deploy-multiple-django-websites-with-a-simple-command-h3d</guid>
      <description>&lt;p&gt;Setting up a web server for Django websites can be time-consuming and boring as you need to set up Nginx, database server, database connector, Python virtual environment, WSGI server, etc., and set all the config files. When you want to set up a few websites on your server, you would need to configure these services for each website.&lt;/p&gt;

&lt;p&gt;For my personal side projects and MVP, I usually use &lt;a href="https://m.do.co/c/008d3315ed7b"&gt;Digital Ocean droplet&lt;/a&gt; which costs me $10 per month and allows me to host 5-8 Django websites. So I decided to write a bash script that helps deploy as many Django websites as your server can handle with one simple command. I know there is Docker, but I was too lazy to learn it, and my solution better suits my needs as it is lightweight and is perfect for a cheap droplet with 1-2Gb of RAM.&lt;/p&gt;

&lt;h2&gt;
  
  
  What does the script do?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Generates SFTP/database passwords&lt;/li&gt;
&lt;li&gt;Creates a working directory in &lt;code&gt;/var/www/$SITENAME&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Creates a new Linux user and adds it to the SFTP group&lt;/li&gt;
&lt;li&gt;Assigns user permissions to the working directory&lt;/li&gt;
&lt;li&gt;Installs all necessary dependencies: &lt;em&gt;nginx, python3-pip, mysql-server/mariadb-server/postgresql, virtualenv, gunicorn, django, mysql-connector-python/psycopg2&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;Creates a new Django project, database, and configures Django to work with the &lt;em&gt;MySQL/MariaDB/PostgreSQL&lt;/em&gt; database
Creates NGINX and Gunicorn config files (a separate config file for each site)&lt;/li&gt;
&lt;li&gt;Creates &lt;code&gt;.gitignore&lt;/code&gt; file&lt;/li&gt;
&lt;li&gt;Adds settings for static and media files&lt;/li&gt;
&lt;li&gt;Collects static files after installation&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The bash script &lt;a href="https://github.com/jdbit/django-auto-deploy"&gt;is available on Github&lt;/a&gt;. I have tested it on an Ubuntu droplet, but it should work on other Debian-based Linux distributions. You can download it from GitHub and run the script on your server with a single command:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;curl -o addsite https://raw.githubusercontent.com/jdbit/django-auto-deploy/master/addsite &amp;amp;&amp;amp; chmod +x addsite &amp;amp;&amp;amp; sudo ./addsite&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The sudo password will be requested as the script needs to install dependencies on your server.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--sHyW6OKa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/itkjr20zilt05ivakkzi.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--sHyW6OKa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/itkjr20zilt05ivakkzi.gif" alt="Deploy multiple Django websites easily" width="600" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When you execute the command, you will be prompted to enter the website name (no whitespaces, single word) and domain name. It also asks you which database you are planning to use (&lt;em&gt;MariaDB&lt;/em&gt;, &lt;em&gt;MySQL&lt;/em&gt;, or &lt;em&gt;PostgreSQL&lt;/em&gt;). After installation of all dependencies, the script will create a directory &lt;code&gt;/var/www/$SITENAME&lt;/code&gt; and a log file deploy.log with bash script output and MySQL password.&lt;/p&gt;

&lt;p&gt;In &lt;code&gt;/var/www/{SITENAME}/env&lt;/code&gt; directory a Python virtual environment will be created. You can activate it by &lt;code&gt;source env/bin/activate&lt;/code&gt; command.&lt;/p&gt;

&lt;p&gt;The script creates two configuration files for Nginx and Gunicorn:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Nginx config located at: &lt;code&gt;/etc/nginx/sites-available/{SITE_NAME}.conf&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Gunicorn config: &lt;code&gt;/etc/systemd/system/gunicorn_{SITE_NAME}.service&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you changed the Nginx config file, don't forget to reload Nginx with &lt;code&gt;sudo service nginx restart&lt;/code&gt; command. If you changed any files in your Django project, you should reload the Gunicorn service with &lt;code&gt;sudo systemctl restart gunicorn_YOUR_SITE_NAME&lt;/code&gt; command to apply changes.&lt;/p&gt;

&lt;p&gt;If you would like to add a second website, just run the &lt;code&gt;./addsite&lt;/code&gt; command again.&lt;/p&gt;

&lt;p&gt;That's it. A &lt;a href="https://m.do.co/c/008d3315ed7b"&gt;$10 Digital Ocean droplet&lt;/a&gt; can easily handle dozens of thousands visitors per day from a 5-7 websites. Hope this script will be useful for you. Let me know in the comments what you think about the script and how you deploy your Django websites.&lt;/p&gt;

</description>
      <category>django</category>
      <category>bash</category>
      <category>webdev</category>
      <category>linux</category>
    </item>
  </channel>
</rss>
