DEV Community

Cover image for Deploy multiple Django websites with a simple command

Posted on

Deploy multiple Django websites with a simple command

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.

For my personal side projects and MVP, I usually use Digital Ocean droplet 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.

What does the script do?

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

The bash script is available on Github. 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:

curl -o addsite && chmod +x addsite && sudo ./addsite

The sudo password will be requested as the script needs to install dependencies on your server.

Deploy multiple Django websites easily

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 (MariaDB, MySQL, or PostgreSQL). After installation of all dependencies, the script will create a directory /var/www/$SITENAME and a log file deploy.log with bash script output and MySQL password.

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

The script creates two configuration files for Nginx and Gunicorn:

  • Nginx config located at: /etc/nginx/sites-available/{SITE_NAME}.conf
  • Gunicorn config: /etc/systemd/system/gunicorn_{SITE_NAME}.service

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

If you would like to add a second website, just run the ./addsite command again.

That's it. A $10 Digital Ocean droplet 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.

Discussion (0)