Skip to content

Instantly share code, notes, and snippets.

@GoodnessEzeokafor
Last active January 11, 2021 14:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GoodnessEzeokafor/3a1c520679a744a8e2f051e10e4dc320 to your computer and use it in GitHub Desktop.
Save GoodnessEzeokafor/3a1c520679a744a8e2f051e10e4dc320 to your computer and use it in GitHub Desktop.

SSH SETUP

  • copy and paste ssh-keygen on your local machine

initial server setup/user setup

  • ssh root@your_server_ip
  • adduser sammy
  • usermod -aG sudo sammy - setup root privileges for the user
  • ufw app list - setup a basic filewall
  • ufw allow OpenSSH - setup a basic filewall
  • ufw enable - setup a basic filewall
  • ufw status
  • rsync --archive --chown=sammy:sammy ~/.ssh /home/sammy

Django Setup

  • sudo apt update
  • sudo apt install python3-pip python3-dev libpq-dev postgresql postgresql-contrib nginx curl
  • sudo apt update
  • sudo apt install python-pip python-dev libpq-dev postgresql postgresql-contrib nginx curl

postgresql

  • sudo -u postgres psql
  • CREATE USER db_user WITH PASSWORD 'userpassword';
  • CREATE DATABASE db_name OWNER db_user;
  • ALTER ROLE db_user SET client_encoding TO 'utf8';
  • ALTER ROLE db_user SET default_transaction_isolation TO 'read committed';
  • ALTER ROLE db_user SET timezone TO 'UTC';
  • GRANT ALL PRIVILEGES ON DATABASE icoach18th TO icoachdbuser;

installing pip and virtualenv

  • sudo -H pip3 install --upgrade pip
  • sudo -H pip3 install virtualenv

create project folder

  • mkdir ~/myprojectdir
  • cd ~/myprojectdir

create virtual environment

  • within the project folder run
    • virtualenv myprojectenv
    • source myprojectenv/bin/activate - activate
    • pip install django gunicorn psycopg2-binary

running django

  • sudo ufw allow 8000
  • python manage.py makemigrations
  • python manage.py migrate
  • python manage.py runserver 0.0.0.0:8000

testing gunicord ability to host the django project

  • gunicorn --bind 0.0.0.0:8000 icoach.wsgi

implementing a more robust way of starting and stopping the application server

  • sudo nano /etc/systemd/system/gunicorn.socket
  • copy and paste this code on the empty file
    Description=gunicorn socket
    
    [Socket]
    ListenStream=/run/gunicorn.sock
    
    [Install]
    WantedBy=sockets.target 
    
  • ctrl +x to close and save
  • sudo nano /etc/systemd/system/gunicorn.service
  • copy and paste this code
    [Unit]
    Description=gunicorn daemon
    Requires=gunicorn.socket
    After=network.target
    
    [Service]
    User=sammy
    Group=www-data
    WorkingDirectory=/home/sammy/myprojectdir
    ExecStart=/home/sammy/myprojectdir/myprojectenv/bin/gunicorn \
           --access-logfile - \
           --workers 3 \
           --bind unix:/run/gunicorn.sock \
           myproject.wsgi:application
     [Install]
     WantedBy=multi-user.target
    
  • sudo systemctl start gunicorn.socket
  • sudo systemctl enable gunicorn.socket
  • sudo systemctl status gunicorn.socket
  • file /run/gunicorn.sock
  • sudo journalctl -u gunicorn.socket
  • sudo systemctl status gunicorn
  • curl --unix-socket /run/gunicorn.sock localhost
  • sudo systemctl status gunicorn
  • sudo systemctl status gunicorn
  • sudo systemctl daemon-reload
  • sudo systemctl restart gunicorn

Configure Nginx to Proxy Pass to Gunicorn

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment