Record of Django “voting app” deployed on vultr

pollapp django

I posted this content once before, but it was a chronological list of items only, so this time I will summarize it a bit more thoroughly while deploying the “voting application” I created yesterday.

(Later, I moved the above application to digitalOcean.)

Separate development and production environments

  1. Create a “.gitignore” file.
  2. Delete SECRET_KEY from “setting.py” and put it in “local_settings.py
  3. Set DEBUG to False for the production environment in “setting.py
  4. Add local settings to “setting.py

local_settings.py

In the development environment, set DEBUG = True. Paste the SECRET_KEY you just cut from settings.py

Create an instance in vultr

  1. Create an account
  2. Ubuntu 18.04 -> deploy now
  3. SSH key and firewall are assigned from the pre-created ones.

How to make SSH connection on VPS(Vultr), from VPS startup to firewall setting.

Configuring the Vultr Firewall

Create a user via SSH, grant administrative privileges, and once logged in as a user, update/upgrade to keep the environment up-to-date.

Install nginx, pip, venv

Create src folder and Remote SSH

After creating the “src” folder, connect to Remote SSH (Remote SSH: Connect to Host and use “ssh yamaco@xxx.xxx.xxx” to open the “src” folder you just created)

git clone

Copy the code from github and clone it to vultr

Virtual environment, upgrade, requirements.txt, nginx folder

After building the virtual environment (python3 -m venv myvenv), enter myvenv and update to the latest commands (pip3 install -upgrade pip setuptools). Install “requirements.txt”. Create a folder for “nginx

Used to set the “.env” environment variable and execute commands

Both “SECRET_KEY” and “ALLOWED_HOSTS” do not need spaces before and after “=”. Also, ” ‘ ‘” should be deleted.

django.core.exceptions.ImproperlyConfigured: Set the SECRET_KEY environment variable

I got an error with “python3 manage.py migrate”! 

I looked closely and found that the “.env” file was directly under “src” and was the same as “polls”. I stored it in “polls” and successfully migrated!

The css is not reflecting!

When I started the server with “python3 manage.py runserver”, the navbar looked strange. The css is not reflected!

  1. 「sudo python3 manage.py collectstatic」=>131 static files copied to ‘/usr/share/nginx/html/static’.
  2. 「sudo nano /etc/nginx/sites-available/default」(Fixed nginx)

nginx restart

When I restarted nginx, I was able to successfully view the vultr IP address.

Register server startup, shutdown, and restart with System Control.

sudo nano /etc/systemd/system/poll_project.service

gunicorn Official document

[Service]

WorkingDirectory=/home/yamco/src/poll_project

EcexStart=/home/yamaco/src/poll_project/myvenv/bin/bunicorn –bind 127.0.0.1:8000 poll_project.wsgi:application

  1. start the server:sudo systemctl start poll_project
  2. Status check:sudo systemctl statud poll_project
  3. stop the server:sudo systemctl stop poll_project
  4. restart the server:sudo systemctl restart poll_project

Get your Domein

I bought it before and had a domain I wasn’t using, so I set vultr on the name server.

  1. Set DNS and domain on the vultr side as well
  2. Add the acquired domain to “ALLOWED_HOSTS” in the “.env” file

I was able to view it in the domain, but “no security protection”

Make your site HTTPS

  1. sudo apt-get install letsencrypt
  2. sudo letsencrypt certonly –standalone -d ask946.info
  3. After rewriting nginx for Https (sudo nano / etc / nginx / sites-available / default), change the server to domain name

Later, I moved this app to Digital Ocean. Click here for the finished product

Recommend

How to create a Django blog site

タイトルとURLをコピーしました