From 863382e0c415c47f2e2117d50dfd49c3060d3739 Mon Sep 17 00:00:00 2001 From: Cody Hiar Date: Sat, 24 Mar 2018 13:52:46 -0600 Subject: Initial commit --- README.md | 9 +++++ setup.sh | 110 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 119 insertions(+) create mode 100644 README.md create mode 100644 setup.sh diff --git a/README.md b/README.md new file mode 100644 index 0000000..afbfcc1 --- /dev/null +++ b/README.md @@ -0,0 +1,9 @@ +# Setting up Django on a VPS + +`setup.sh` is a script you can use to setup your own working instance of django +on a VPS. It's farly self documenting but basically you follow these steps: + +1. Setup your own user with python 3, pip, and virtualenv (as root) +2. Create the virtualenv and install/setup djagno (as user) + * MAKE SURE EVERYTHING WORKS UP TILL THIS POINT +3. Setup nginx and uwsgi to serve the django site. diff --git a/setup.sh b/setup.sh new file mode 100644 index 0000000..87591f6 --- /dev/null +++ b/setup.sh @@ -0,0 +1,110 @@ +#!/bin/bash +################################# +# Execute as root +################################# +# Variables +NEW_USER='cody' +# Create the user with a home directory and a group matching the user's name +useradd -mU "$NEW_USER" +# Give the user sudo ability +usermod -aG sudo cody +# Install Python3 and Pip +apt-get update && apt-get install python3-pip python3-dev nginx -y +# Update Pip to the latest version +pip3 install --upgrade pip +# Install Virtualenv and Virtualwrapper +pip3 install virtualenv virtualenvwrapper +# Add environment variables to user's bashrc +echo "export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3" >> /home/"$NEW_USER"/.bashrc +echo "export WORKON_HOME=~/virtualenvs" >> /home/"$NEW_USER"/.bashrc +echo "source /usr/local/bin/virtualenvwrapper.sh" >> /home/"$NEW_USER"/.bashrc +# Setup a password for the user +passwd "$NEW_USER" + + + +################################# +# Execute as new user +################################# +# Make sure were in home directory +cd ~ +# Create virtual env +mkvirtualenv zeevirtualenv +# Install django +pip install django +# Create our project +django-admin startproject zeedjangosite +cd zeedjangosite +# Migrate databse +./manage.py migrate +# Create the super user +./manage.py createsuperuser +# vim ~/zeedjangosite/zeedjangosite/settings.py +# - Update ALLOWED_HOSTS (IP and www.zeewebsite.com) +# - Add "STATIC_ROOT = os.path.join(BASE_DIR, 'static/')" + + + +# Test the setup so far. +./manage.py collectstatic +./manage.py runserver 0.0.0.0:8000 +# - test /admin + + +################################# +# Execute as root +################################# +# Install uwsgi +pip3 install uwsgi +# Test uwsgi works +#uwsgi --http :8000 --home /home/cody/virtualenvs/zeevirtualenv/ --chdir /home/cody/zeedjangosite/ -w zeedjangosite.wsgi +# Create directory to store +mkdir -p /etc/uwsgi/sites +# Create the django site configuration for uwsgi +cat > /etc/uwsgi/sites/zeedjangosite.ini <<- EOM +[uwsgi] +uid = cody +chdir = /home/cody/zeedjangosite +home = /home/cody/virtualenvs/zeevirtualenv +module = zeedjangosite.wsgi:application +logto = /home/cody/uwsgi.log + +master = true +processes = 5 + +socket = /run/uwsgi/zeedjangosite.sock +chown-socket = %(uid):www-data +chmod-socket = 660 +vacuum = true +EOM +# Create a systemd configuration for uwsgi +cat > /etc/systemd/system/uwsgi.service <<- EOM +[Unit] +Description=uWSGI Emperor service + +[Service] +ExecStartPre=/bin/bash -c 'mkdir -p /run/uwsgi; chown cody:www-data /run/uwsgi' +ExecStart=/usr/local/bin/uwsgi --emperor /etc/uwsgi/sites +Restart=always +KillSignal=SIGQUIT +Type=notify +NotifyAccess=all +EOM +# Create nginx configuration for uwsgi +cat > /etc/nginx/sites-enabled/zeewebsite.conf <<- EOM +server { + listen 80; + server_name zeewebsite.com www.zeewebsite.com; + + location = /favicon.ico { access_log off; log_not_found off; } + location /static/ { + root /home/cody/zeedjangosite; + } + + location / { + include uwsgi_params; + uwsgi_pass unix:/run/uwsgi/zeedjangosite.sock; + } +} +EOM +systemctl restart nginx && systemctl restart uwsgi -- cgit v1.2.3