aboutsummaryrefslogtreecommitdiff
path: root/setup.sh
blob: 87591f6004b808f723f5757b85705363937c9f9c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
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