2019-04-16 17:34:26 -04:00
2019-04-16 17:34:26 -04:00
2018-09-19 20:56:13 -04:00
2019-04-16 17:34:26 -04:00
2018-12-12 21:54:26 -05:00
2018-12-12 21:54:26 -05:00
2019-04-16 17:05:49 -04:00
2019-04-16 17:15:52 -04:00

Django Setup for Debian 9, Ubuntu. 18.04, and Raspbian Stretch

Install

vim vars
# django project settings
export user=""              <-- give the user you want to own the site
export projectname=""       <-- give your project a friendly name
export hostname=""          <-- add your hostname (can be an IP address)
export usegit=False         <-- if you have an existing project, set to True
export giturl=""            <-- put the url for the git repo here
export gitkey=""            <-- put the path to the ssh key git will use here
sudo -s                     <-- you must be root to run script
./install.sh

Reset

If anything goes wrong with the project and you don't want to perform a full uninstall:

sudo -s
./reset

will reset the project but leave the dependencies installed. You can rerun the installer with different variables to fix what was incorrect. WARNING: This will delete your project (including your database)

Uninstall

To completely remove every trace of the project from your server:

sudo -s
./uninstall

This will completely remove everything.

A more detailed explanation

vars

# Django project settings
user=""
projectname=""
hostname=""
letsencrypt=False

# general install settings
logdir="/var/log/djangosetup/"
aptlogloc=$logdir"apt.log"
aptlogmsg="See $aptlogloc for more info."
firelogloc=$logdir"firewall.log"
firelogmsg="See $firelogloc for more info."
pylogloc=$logdir"python.log"
pylogmsg="See $pylogloc for more info."
djalogloc=$logdir"django.log"
djamsg="See $djalogloc for more info."
gunicornlogloc=$logdir"gunicorn.log"
nginxlogloc=$logdir"nginx.log"

# Python settings
pylink="https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tar.xz"

These variables can (and should) be changed to match your configuration needs. For example: any of the "logloc" log location variables can be changed to log somewhere else. The "logmsg" log message variables can be changed to reflect different log locations or output a custom message.

The Django Project Settings variables should be changed to match your configuration. You have (essentially) 3 options for user configuration:

  1. user = "[primary (sudo) user here]"
  2. user = "nginx"
  3. user = "[other user]"

My recommendation is to use nginx as the user. The installer takes care of creating a home directory for nginx and setting permissions. Of course there is no problem with specifying another user. The installer will automatically add a user and assign permissions. Setting the user to the primary sudo account is risky but the installer won't complain if you do it quietly.

The projectname variable can be whatever you want. When Django starts the project it will use this name.

The hostname will be added as an environment variable for gunicorn. Multiple hosts are not supported at the moment so set this to the address you'll test with. You can add additional hosts by manually editing settings.py.

Install

Dependencies

In order for this script to work its magic you'll need a few packages provided by apt.

# check if root
if [[ $EUID -ne 0 ]]; then
   echo -e "This script must be run as root" 
   exit 1
fi
cd /root/

tput setaf 2
echo -e "Installing dependencies"
tput setaf 0
# create necessary dirs
if [ ! -d $logdir ] ; then
	mkdir $logdir
fi

The first part of the script is fairly straight forward. If you aren't root user, the installer can't use apt and won't work. After checking if you are the root user it creates the necessary directories to prevent future complaints.

# install dependencies
packages=(gcc wget nginx ufw make sqlite3 bzip2 openssl libffi-dev libssl-dev libsqlite3-dev zlib1g-dev libbz2-dev)
for package in ${packages[@]};
do
    echo -e "\tInstalling $package"
    touch $aptlogloc
    apt install -y $package 1>> $aptlogloc 2>> $aptlogloc 3>> $aptlogloc
    if [ $? -ne 0 ] ; then
        echo -e "apt failed to install $package. $aptlogmsg"
        exit 1
    fi
done

The next part of the script installs apt dependencies. The script verifies that the installation goes smoothly and exits if an error occurs.

Firewall

# firewall
ports=(22 80 443)
for port in ${ports[@]};
do
    echo -e "ufw allow $port/tcp" > $firelogloc
    ufw allow $port/tcp 1>> $firelogloc 2>> $firelogloc
    if [ $? -ne 0 ] ; then
    	echo -e "\tfirewall failed to update port $port correctly. $firelogmsg"
        exit 1
    fi
done

The next part of the script opens the necessary ports through ufw.

Python

wget $pylink 1> $pylogloc 2>> $pylogloc
if [ $? -ne 0 ] ; then
	echo -e "Failed to fetch python, make sure wget is installed and $pylink is what you're after. $pylogmsg"
    exit 1
fi

After configuring the firewall the script will fetch Python from the link provided.

echo -e "\tConfigure"
./configure --prefix /usr/src/python37 1>> $pylogloc 2>> $pylogloc
if [ $? -ne 0 ] ; then
	echo -e "./configure failed. $pylogmsg"
    exit 1
fi
echo -e "\tMake"
make 1>> $pylogloc 2>> $pylogloc
if [ $? -ne 0 ] ; then
	echo -e "make failed. $pylogmsg"
    exit 1
fi
echo -e "\tMake altinstall"
make altinstall 1>> $pylogloc 2>> $pylogloc
if [ $? -ne 0 ] ; then
	echo -e "make altinstall failed. $pylogmsg"
    exit 1
fi

If you've ever installed Python from source this part should look familiar. The script uses wget to download the version of python provided by $pylink. It uses alt install to prevent conflict with previous versions of Python (I've been told CentOS will break if it can't find Python 2, a real problem if you use make install).

You could get rid of this section and do it yourself with another version of Python. As long as Python can be found at $pyinstalldir and it has Django support the installer will continue to chug along happily.

The script will automatically update pip and install virtualenv.

Django

id -u $user > /dev/null
if [ $? = 0 ]; then
    adduser $user
fi
if [ ! -d "/home/$user" ] ; then
	mkdir /home/$user
    chown -R $user:$user /home/$user
fi
mkdir /home/$user/$projectname
if [ $? -ne 0 ] ; then
	echo -e "Failed to create $projectname directory. $djamsg"
    exit 1
fi

Installing Django starts by checking if the user you provided exists. If it doesn't then it adds the user and creates a home directory. It then creates the project directory using the provided project name.

echo -e "\tCreating venv"
cd /home/$user/
/usr/src/python37/bin/python3.7 -m virtualenv $projectname/venv 1>> $djalogloc 2>> $djalogloc
if [ $? -ne 0 ] ; then
	echo -e "Failed to create virtual environment. $djamsg"
    exit 1
fi
source $projectname/venv/bin/activate >> $djalogloc
if [ $? -ne 0 ] ; then
	echo -e "Failed to source virtual environment. $djamsg"
    exit 1
fi

The next part of the script creates a virtual environment based on your Python install.

# pip installs
pips=(django gunicorn psycopg2-binary)
for pip in ${pips[@]};
do
    echo -e "\tInstalling $pip"
    pip install $pip >> $djalogloc
    if [ $? -ne 0 ] ; then
    	echo -e "Failed to install $pip. $djamsg"
      exit 1
    fi
done

The script then installs the necessary pip packages.

# start django project
echo -e "\tStarting django project"
cd $projectname
django-admin startproject $projectname >> $djalogloc
if [ $? -ne 0 ] ; then
	echo -e "Failed to start project $projectname with django-admin. $djamsg"
    exit 1
fi

Next the script starts the Django project. You'll notice that the project structure looks like this:


[projectname]/
├── [config]/
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── manage.py
└── venv/

When making changes to nginx or gunicorn it's important to keep this structure in mind.

After starting the Django project, the script updates allowed hosts, collects static, and makes migrations.

Gunicorn

echo -e "
[Unit]
Description=gunicorn daemon
After=network.target

[Service]
User=$user
Group=www-data
WorkingDirectory=/home/$user/$projectname/$projectname
ExecStart=/home/$user/$projectname/venv/bin/gunicorn --workers 3 --bind unix:/home/$user/$projectname/$projectname.sock $projectname.wsgi:application

[Install]
WantedBy=multi-user.target" > /etc/systemd/system/gunicorn.service

The gunicorn and nginx installs are very similar. The gunicorn "install" is just a .service file that gets placed in /etc/systemd/system/. As seen above, the install command just echoes the service file into the right place.

Nginx

echo -e "
server {
    listen 80;
    server_name $hostname;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/$user/$projectname/$projectname;
    }

    location / {
        proxy_set_header Host \$http_host;
        proxy_set_header X-Real-IP \$remote_addr;
        proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto \$scheme;
        proxy_pass http://unix:/home/$user/$projectname/$projectname.sock;
    }
}" > /etc/nginx/conf.d/$projectname.conf

The nginx install copies a configuration file to the nginx configuration directory. The only complication here is:

if [ ! $user = "nginx" ] ; then
    sed -i "s/user nginx/user $user nginx/" /etc/nginx/nginx.conf
fi

Which inserts the specified user (if not nginx) into nginx.conf. The script finishes by enabling gunicorn and nginx at startup.

Description
Automatic django-gunicorn-nginx installer for CentOS
Readme MIT 108 KiB