8 Commits

Author SHA1 Message Date
ducoterra
0c1542605e remove color 2019-04-16 17:26:49 -04:00
ducoterra
8e0e693fcb strip first level of sqlite and python 2019-04-16 17:26:22 -04:00
ducoterra
f4b9bdfb39 change tar name 2019-04-16 17:21:28 -04:00
ducoterra
083808394a update wget 2019-04-16 17:18:51 -04:00
ducoterra
2934d7978a don't check vars 2019-04-16 17:16:57 -04:00
ducoterra
67435af172 update python link, sqlite link 2019-04-16 17:15:52 -04:00
ducoterra
e3f0427e58 remove ufw setup 2019-04-16 17:12:03 -04:00
ducoterra
4e89aa2035 test install 2019-04-16 17:05:49 -04:00
3 changed files with 260 additions and 236 deletions

View File

@@ -14,29 +14,13 @@ fi
# source vars # source vars
source vars source vars
if [[ $user == "" ]] ; then
echo -e "User missing from vars."
exit 1
fi
if [[ $projectname == "" ]] ; then
echo -e "Project Name missing from vars."
exit 1
fi
if [[ $hostname == "" ]] ; then
echo -e "Hostname missing from vars."
exit 1
fi
tput setaf 2
echo -e "Installing dependencies" echo -e "Installing dependencies"
tput setaf 9
# create necessary dirs # create necessary dirs
if [ ! -d $logdir ] ; then if [ ! -d $logdir ] ; then
mkdir $logdir mkdir $logdir
fi; fi;
# install dependencies # install dependencies
packages=(gcc wget nginx ufw make sqlite3 bzip2 openssl libffi-dev libssl-dev libsqlite3-dev zlib1g-dev libbz2-dev libpq-dev);
echo -e "\tRunning apt update"; echo -e "\tRunning apt update";
apt update 1> $aptlogloc 2>> $aptlogloc 3>> $aptlogloc; apt update 1> $aptlogloc 2>> $aptlogloc 3>> $aptlogloc;
for package in ${packages[@]}; for package in ${packages[@]};
@@ -49,259 +33,295 @@ do
fi fi
done done
# firewall ###############################################################################
ports=(22 80 443) # Sqlite3
for port in ${ports[@]}; # Download and setup Sqlite3 and modules
do ################################################################################
echo -e "ufw allow $port/tcp" 1>> $firelogloc 2>> $firelogloc echo -e "Install Sqlite3"
ufw allow $port/tcp 1>> $firelogloc 2>> $firelogloc
if [ $? -ne 0 ] ; then # fetch Sqlite
echo -e "\tfirewall failed to update port $port correctly. $firelogmsg" wget -O sqlite.tar.gz $sqlink 1> $pylogloc 2>> $pylogloc 3>> $pylogloc
exit 1 if [ $? -ne 0 ] ; then
fi echo -e "Failed to fetch sqlite, make sure wget is installed and $pylink is what you're after. $pylogmsg"
done exit 1
echo -e "\tEnabling ufw" fi
ufw --force enable 1>> $firelogloc 2>> $firelogloc
mkdir sqlite
tar xf sqlite.tar.gz -C sqlite --strip-components=1 1>> $pylogloc 2>> $pylogloc 3>> $pylogloc
if [ $? -ne 0 ] ; then
echo -e "Failed to unzip sqlite. $pylogmsg"
exit 1
fi
cd sqlite
if [ $? -ne 0 ] ; then
echo -e "Something went wrong while unzipping. $pylogmsg"
exit 1
fi
# install Sqlite3
echo -e "\tConfigure"
./configure 1>> $pylogloc 2>> $pylogloc 3>> $pylogloc
if [ $? -ne 0 ] ; then
echo -e "./configure failed. $pylogmsg"
exit 1
fi
echo -e "\tMake"
make 1>> $pylogloc 2>> $pylogloc 3>> $pylogloc
if [ $? -ne 0 ] ; then
echo -e "make failed. $pylogmsg"
exit 1
fi
echo -e "\tMake install"
make install 1>> $pylogloc 2>> $pylogloc 3>> $pylogloc
if [ $? -ne 0 ] ; then
echo -e "make install failed. $pylogmsg"
exit 1
fi
################################################################################ ################################################################################
# Python # Python
# Download and setup Python and modules # Download and setup Python and modules
################################################################################ ################################################################################
tput setaf 2
echo -e "Installing Python" echo -e "Installing Python"
tput setaf 9
# fetch Python wget -O python.tar.gz $pylink 1> $pylogloc 2>> $pylogloc 3>> $pylogloc
if [ ! -d $pyinstalldir ] ; then # if we already installed python if [ $? -ne 0 ] ; then
wget $pylink 1> $pylogloc 2>> $pylogloc 3>> $pylogloc echo -e "Failed to fetch python, make sure wget is installed and $pylink is what you're after. $pylogmsg"
if [ $? -ne 0 ] ; then exit 1
echo -e "Failed to fetch python, make sure wget is installed and $pylink is what you're after. $pylogmsg"
exit 1
fi
tar xf Python-3.7.0.tar.xz 1>> $pylogloc 2>> $pylogloc 3>> $pylogloc
if [ $? -ne 0 ] ; then
echo -e "Failed to unzip python. $pylogmsg"
exit 1
fi
cd Python-3.7.0
if [ $? -ne 0 ] ; then
echo -e "Missing python directory. $pylogmsg"
exit 1
fi
# install Python
echo -e "\tConfigure"
./configure --prefix $pyinstalldir 1>> $pylogloc 2>> $pylogloc 3>> $pylogloc
if [ $? -ne 0 ] ; then
echo -e "./configure failed. $pylogmsg"
exit 1
fi
echo -e "\tMake"
make 1>> $pylogloc 2>> $pylogloc 3>> $pylogloc
if [ $? -ne 0 ] ; then
echo -e "make failed. $pylogmsg"
exit 1
fi
echo -e "\tMake altinstall"
make altinstall 1>> $pylogloc 2>> $pylogloc 3>> $pylogloc
if [ $? -ne 0 ] ; then
echo -e "make altinstall failed. $pylogmsg"
exit 1
fi
fi fi
mkdir python
tar xf python.tar.gz -C python --strip-components=1 1>> $pylogloc 2>> $pylogloc 3>> $pylogloc
if [ $? -ne 0 ] ; then
echo -e "Failed to unzip python. $pylogmsg"
exit 1
fi
cd python
if [ $? -ne 0 ] ; then
echo -e "Something went wrong while unzipping. $pylogmsg"
exit 1
fi
# install Python
echo -e "\tConfigure"
./configure 1>> $pylogloc 2>> $pylogloc 3>> $pylogloc
if [ $? -ne 0 ] ; then
echo -e "./configure failed. $pylogmsg"
exit 1
fi
echo -e "\tMake"
make 1>> $pylogloc 2>> $pylogloc 3>> $pylogloc
if [ $? -ne 0 ] ; then
echo -e "make failed. $pylogmsg"
exit 1
fi
echo -e "\tMake install"
make install 1>> $pylogloc 2>> $pylogloc 3>> $pylogloc
if [ $? -ne 0 ] ; then
echo -e "make install failed. $pylogmsg"
exit 1
fi
# upgrade pip and install virtualenv # upgrade pip and install virtualenv
echo -e "\tUpgrading pip" echo -e "\tUpgrading pip"
/usr/src/python37/bin/python3.7 -m pip install --upgrade pip 1>> $pylogloc 2>> $pylogloc 3>> $pylogloc python3 -m pip install --upgrade pip 1>> $pylogloc 2>> $pylogloc 3>> $pylogloc
if [ $? -ne 0 ] ; then if [ $? -ne 0 ] ; then
echo -e "pip upgrade failed. $pylogmsg" echo -e "pip upgrade failed. $pylogmsg"
exit 1 exit 1
fi fi
echo -e "\tInstalling virtualenv" echo -e "\tInstalling virtualenv"
/usr/src/python37/bin/python3.7 -m pip install virtualenv 1>> $pylogloc 2>> $pylogloc 3>> $pylogloc python3 -m pip install virtualenv 1>> $pylogloc 2>> $pylogloc 3>> $pylogloc
if [ $? -ne 0 ] ; then if [ $? -ne 0 ] ; then
echo -e "installing virtualenv failed. $pylogmsg" echo -e "installing virtualenv failed. $pylogmsg"
exit 1 exit 1
fi fi
cd /root/ cd /root/
rm Python-3.7.0.tar.xz 1>> $pylogloc 2>> $pylogloc rm python.tar.xz 1>> $pylogloc 2>> $pylogloc
rm -rf Python-3.7.0 1>> $pylogloc 2>> $pylogloc rm -rf python 1>> $pylogloc 2>> $pylogloc
if [ $? -ne 0 ] ; then if [ $? -ne 0 ] ; then
echo -e "Failed to remove Python install files. $pylogmsg" echo -e "Failed to remove Python install files. $pylogmsg"
fi fi
################################################################################ # ################################################################################
# Django # # Django
# Download, setup and configure Django # # Download, setup and configure Django
################################################################################ # ################################################################################
tput setaf 2 # tput setaf 2
echo -e "Starting Django project" # echo -e "Starting Django project"
tput setaf 9 # tput setaf 9
adduser $user 1> /dev/null 2> /dev/null 3> /dev/null # adduser $user 1> /dev/null 2> /dev/null 3> /dev/null
if [ ! -d "/home/$user" ] ; then # if [ ! -d "/home/$user" ] ; then
mkdir /home/$user # mkdir /home/$user
chown -R $user:$user /home/$user # chown -R $user:$user /home/$user
fi # fi
mkdir /home/$user/$projectname # mkdir /home/$user/$projectname
if [ $? -ne 0 ] ; then # if [ $? -ne 0 ] ; then
echo -e "Failed to create $projectname directory. $djamsg" # echo -e "Failed to create $projectname directory. $djamsg"
exit 1 # exit 1
fi # fi
# virtual environment # # virtual environment
echo -e "\tCreating venv" # echo -e "\tCreating venv"
cd /home/$user/$projectname # cd /home/$user/$projectname
/usr/src/python37/bin/python3.7 -m virtualenv venv 1>> $djalogloc 2>> $djalogloc 3>> $djalogloc # /usr/src/python37/bin/python3.7 -m virtualenv venv 1>> $djalogloc 2>> $djalogloc 3>> $djalogloc
if [ $? -ne 0 ] ; then # if [ $? -ne 0 ] ; then
echo -e "Failed to create virtual environment. $djamsg" # echo -e "Failed to create virtual environment. $djamsg"
exit 1 # exit 1
fi # fi
source venv/bin/activate 1>> $djalogloc 2>> $djalogloc 3>> $djalogloc # source venv/bin/activate 1>> $djalogloc 2>> $djalogloc 3>> $djalogloc
if [ $? -ne 0 ] ; then # if [ $? -ne 0 ] ; then
echo -e "Failed to source virtual environment. $djamsg" # echo -e "Failed to source virtual environment. $djamsg"
exit 1 # exit 1
fi # fi
# pip installs # # pip installs
pips=(django gunicorn psycopg2-binary) # pips=(django gunicorn psycopg2-binary)
for pip in ${pips[@]}; # for pip in ${pips[@]};
do # do
echo -e "\tInstalling $pip" # echo -e "\tInstalling $pip"
pip install $pip 1>> $djalogloc 2>> $djalogloc 3>> $djalogloc # pip install $pip 1>> $djalogloc 2>> $djalogloc 3>> $djalogloc
if [ $? -ne 0 ] ; then # if [ $? -ne 0 ] ; then
echo -e "Failed to install $pip. $djamsg" # echo -e "Failed to install $pip. $djamsg"
exit 1 # exit 1
fi # fi
done # done
if [ $usegit = False ] ; then # if [ $usegit = False ] ; then
# start django project in the project directory # # start django project in the project directory
echo -e "\tStarting django project" # echo -e "\tStarting django project"
django-admin startproject config . 1>> $djalogloc 2>> $djalogloc 3>> $djalogloc # django-admin startproject config . 1>> $djalogloc 2>> $djalogloc 3>> $djalogloc
if [ $? -ne 0 ] ; then # if [ $? -ne 0 ] ; then
echo -e "Failed to start project $projectname with django-admin. $djamsg" # echo -e "Failed to start project $projectname with django-admin. $djamsg"
exit 1 # exit 1
fi # fi
# update allowed hosts # # update allowed hosts
echo -e "echo -e sed -i 's/ALLOWED_HOSTS = []/ALLOWED_HOSTS = ['localhost', os.environ.get('HOST')'] config/settings.py" >> $djalogloc # echo -e "echo -e sed -i 's/ALLOWED_HOSTS = []/ALLOWED_HOSTS = ['localhost', os.environ.get('HOST')'] config/settings.py" >> $djalogloc
# | FROM | TO | # # | FROM | TO |
sed -i "s/ALLOWED_HOSTS = \[\]/ALLOWED_HOSTS = \['localhost', os.environ.get('HOST')\]/" config/settings.py # sed -i "s/ALLOWED_HOSTS = \[\]/ALLOWED_HOSTS = \['localhost', os.environ.get('HOST')\]/" config/settings.py
if [ $? -ne 0 ] ; then # if [ $? -ne 0 ] ; then
echo -e "Failed to change ALLOWED_HOSTS. $djamsg" # echo -e "Failed to change ALLOWED_HOSTS. $djamsg"
exit 1 # exit 1
fi # fi
echo -e "echo -e STATIC_ROOT = os.path.join(BASE_DIR, 'static') >> config//settings.py" >> $djalogloc # echo -e "echo -e STATIC_ROOT = os.path.join(BASE_DIR, 'static') >> config//settings.py" >> $djalogloc
echo -e "STATIC_ROOT = os.path.join(BASE_DIR, 'static')" >> config/settings.py # echo -e "STATIC_ROOT = os.path.join(BASE_DIR, 'static')" >> config/settings.py
if [ $? -ne 0 ] ; then # if [ $? -ne 0 ] ; then
echo -e "Failed to append STATIC_ROOT. $djamsg" # echo -e "Failed to append STATIC_ROOT. $djamsg"
exit 1 # exit 1
fi # fi
fi # fi
if [ $usegit = True ] ; then # if [ $usegit = True ] ; then
echo -e "\tCloning django project" # echo -e "\tCloning django project"
git init 1>> $gitlogloc 2>> $gitlogloc 3>> $gitlogloc; # git init 1>> $gitlogloc 2>> $gitlogloc 3>> $gitlogloc;
git remote add origin $giturl 1>> $gitlogloc 2>> $gitlogloc 3>> $gitlogloc; # git remote add origin $giturl 1>> $gitlogloc 2>> $gitlogloc 3>> $gitlogloc;
ssh-agent bash -c 'ssh-add $gitkey 1>> $gitlogloc 2>> $gitlogloc 3>> $gitlogloc; # ssh-agent bash -c 'ssh-add $gitkey 1>> $gitlogloc 2>> $gitlogloc 3>> $gitlogloc;
git pull origin master 1>> $gitlogloc 2>> $gitlogloc 3>> $gitlogloc;' # git pull origin master 1>> $gitlogloc 2>> $gitlogloc 3>> $gitlogloc;'
echo -e "\tInstalling pip requirements" # echo -e "\tInstalling pip requirements"
pip install -r requirements.txt 1>> $gitlogloc 2>> $gitlogloc 3>> $gitlogloc # pip install -r requirements.txt 1>> $gitlogloc 2>> $gitlogloc 3>> $gitlogloc
fi # fi
# collect static, migrate # # collect static, migrate
echo -e "\tCollecting static" # echo -e "\tCollecting static"
python manage.py collectstatic 1>> $djalogloc 2>> $djalogloc 3>> $djalogloc # python manage.py collectstatic 1>> $djalogloc 2>> $djalogloc 3>> $djalogloc
if [ $? -ne 0 ] ; then # if [ $? -ne 0 ] ; then
echo -e "Failed to collect static files. $djamsg" # echo -e "Failed to collect static files. $djamsg"
exit 1 # exit 1
fi # fi
echo -e "\tMaking migrations" # echo -e "\tMaking migrations"
python manage.py makemigrations 1>> $djalogloc 2>> $djalogloc 3>> $djalogloc # python manage.py makemigrations 1>> $djalogloc 2>> $djalogloc 3>> $djalogloc
if [ $? -ne 0 ] ; then # if [ $? -ne 0 ] ; then
echo -e "Failed to make migrations. $djamsg" # echo -e "Failed to make migrations. $djamsg"
exit 1 # exit 1
fi # fi
echo -e "\tMigrating" # echo -e "\tMigrating"
python manage.py migrate 1>> $djalogloc 2>> $djalogloc 3>> $djalogloc # python manage.py migrate 1>> $djalogloc 2>> $djalogloc 3>> $djalogloc
if [ $? -ne 0 ] ; then # if [ $? -ne 0 ] ; then
echo -e "Failed to migrate. You do not survive the winter. $djamsg" # echo -e "Failed to migrate. You do not survive the winter. $djamsg"
exit 1 # exit 1
fi # fi
deactivate # deactivate
if [ $? -ne 0 ] ; then # if [ $? -ne 0 ] ; then
echo -e "Failed to deactivate virtual environment, (this may not be an issue). $djamsg" # echo -e "Failed to deactivate virtual environment, (this may not be an issue). $djamsg"
fi # fi
chown -R $user:$user . # chown -R $user:$user .
if [ $? -ne 0 ] ; then # if [ $? -ne 0 ] ; then
echo -e "Failed to change permissions of $projectname. $djamsg" # echo -e "Failed to change permissions of $projectname. $djamsg"
exit 1 # exit 1
fi # fi
################################################################################ # ################################################################################
# Gunicorn # # Gunicorn
# configure gunicorn.conf file # # configure gunicorn.conf file
################################################################################ # ################################################################################
tput setaf 2 # tput setaf 2
echo -e "Setting up gunicorn" # echo -e "Setting up gunicorn"
tput setaf 9 # tput setaf 9
echo -e " # echo -e "
[Unit] # [Unit]
Description=gunicorn daemon # Description=gunicorn daemon
After=network.target # After=network.target
[Service] # [Service]
User=$user # User=$user
Group=www-data # Group=www-data
WorkingDirectory=/home/$user/$projectname # WorkingDirectory=/home/$user/$projectname
ExecStart=/home/$user/$projectname/venv/bin/gunicorn --workers 3 --bind unix:/home/$user/$projectname/$projectname.sock config.wsgi:application # ExecStart=/home/$user/$projectname/venv/bin/gunicorn --workers 3 --bind unix:/home/$user/$projectname/$projectname.sock config.wsgi:application
Environment=HOST=$hostname # Environment=HOST=$hostname
[Install] # [Install]
WantedBy=multi-user.target" > /etc/systemd/system/gunicorn.service # WantedBy=multi-user.target" > /etc/systemd/system/gunicorn.service
echo -e "\tStarting gunicorn" # echo -e "\tStarting gunicorn"
systemctl daemon-reload > $gunicornlogloc # systemctl daemon-reload > $gunicornlogloc
systemctl start gunicorn >> $gunicornlogloc # systemctl start gunicorn >> $gunicornlogloc
systemctl status gunicorn >> $gunicornlogloc # systemctl status gunicorn >> $gunicornlogloc
echo -e "\tEnabling gunicorn at startup" # echo -e "\tEnabling gunicorn at startup"
systemctl enable gunicorn 1>> $gunicornlogloc 2>> $gunicornlogloc # systemctl enable gunicorn 1>> $gunicornlogloc 2>> $gunicornlogloc
################################################################################ # ################################################################################
# Nginx # # Nginx
# configure nginx # # configure nginx
################################################################################ # ################################################################################
tput setaf 2 # tput setaf 2
echo -e "Configuring Nginx" # echo -e "Configuring Nginx"
tput setaf 9 # tput setaf 9
echo -e " # echo -e "
server { # server {
listen 80; # listen 80;
server_name $hostname; # server_name $hostname;
location = /favicon.ico { access_log off; log_not_found off; } # location = /favicon.ico { access_log off; log_not_found off; }
location /static/ { # location /static/ {
root /home/$user/$projectname; # root /home/$user/$projectname;
} # }
location / { # location / {
proxy_set_header Host \$http_host; # proxy_set_header Host \$http_host;
proxy_set_header X-Real-IP \$remote_addr; # proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for; # proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto \$scheme; # proxy_set_header X-Forwarded-Proto \$scheme;
proxy_pass http://unix:/home/$user/$projectname/$projectname.sock; # proxy_pass http://unix:/home/$user/$projectname/$projectname.sock;
} # }
}" > /etc/nginx/conf.d/$projectname.conf # }" > /etc/nginx/conf.d/$projectname.conf
# set the nginx user # # set the nginx user
if [ ! $user = "nginx" ] ; then # if [ ! $user = "nginx" ] ; then
sed -i "s/user nginx/user $user nginx/" /etc/nginx/nginx.conf # sed -i "s/user nginx/user $user nginx/" /etc/nginx/nginx.conf
fi # fi
echo -e "\tStarting nginx" # echo -e "\tStarting nginx"
systemctl restart nginx >> $nginxlogloc # systemctl restart nginx >> $nginxlogloc
systemctl status nginx >> $nginxlogloc # systemctl status nginx >> $nginxlogloc
echo -e "\tEnabling nginx" # echo -e "\tEnabling nginx"
systemctl enable nginx 1>> $nginxlogloc 2>> $nginxlogloc # systemctl enable nginx 1>> $nginxlogloc 2>> $nginxlogloc
tput setaf 2 # tput setaf 2
echo -e "Done! Navigate to $hostname to access the site. Logs can be found in $logdir" # echo -e "Done! Navigate to $hostname to access the site. Logs can be found in $logdir"
tput setaf 9 # tput setaf 9
exit 0 # exit 0

0
update.sh Normal file
View File

8
vars
View File

@@ -24,5 +24,9 @@ export gitlogloc=$logdir"git.log"
export gitlogmsg="See $gitlogloc for more info." export gitlogmsg="See $gitlogloc for more info."
# python settings # python settings
export pylink="https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tar.xz" export pylink="https://www.python.org/ftp/python/3.7.3/Python-3.7.3.tar.xz"
export pyinstalldir="/usr/src/python37" export sqlink="https://sqlite.org/2019/sqlite-autoconf-3280000.tar.gz"
export pyinstalldir="/usr/src/python37"
# don't change this by hand
export packages=(gcc wget nginx make bzip2 openssl libffi-dev libssl-dev libsqlite3-dev zlib1g-dev libbz2-dev libpq-dev);