#!/bin/bash ################################################################################s # Dependencies # Installs Dependencies automatically ################################################################################ # check if root if [ $EUID -ne 0 ] ; then echo -e "This script must be run as root" exit 1 fi # 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" tput setaf 9 # create necessary dirs if [ ! -d $logdir ] ; then mkdir $logdir fi; # install dependencies packages=(gcc wget nginx ufw make bzip2 openssl libffi-dev libssl-dev libsqlite3-dev zlib1g-dev libbz2-dev libpq-dev); echo -e "\tRunning apt update"; apt update 1> $aptlogloc 2>> $aptlogloc 3>> $aptlogloc; for package in ${packages[@]}; do echo -e "\tInstalling $package" 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 # firewall ports=(22 80 443) for port in ${ports[@]}; do echo -e "ufw allow $port/tcp" 1>> $firelogloc 2>> $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 echo -e "\tEnabling ufw" ufw --force enable 1>> $firelogloc 2>> $firelogloc ############################################################################### # Sqlite3 # Download and setup Sqlite3 and modules ################################################################################ tput setaf 2 echo -e "Install Sqlite3" tput setaf 9 # fetch Sqlite wget https://sqlite.org/2019/sqlite-autoconf-3280000.tar.gz sqlite.tar.gz 1> $pylogloc 2>> $pylogloc 3>> $pylogloc if [ $? -ne 0 ] ; then echo -e "Failed to fetch sqlite, make sure wget is installed and $pylink is what you're after. $pylogmsg" exit 1 fi tar xf sqlite.tar.xz 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 # Download and setup Python and modules ################################################################################ tput setaf 2 echo -e "Installing Python" tput setaf 9 wget $pylink python.tar.gz 1> $pylogloc 2>> $pylogloc 3>> $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 tar xf python.tar.xz 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 echo -e "\tUpgrading pip" python3 -m pip install --upgrade pip 1>> $pylogloc 2>> $pylogloc 3>> $pylogloc if [ $? -ne 0 ] ; then echo -e "pip upgrade failed. $pylogmsg" exit 1 fi echo -e "\tInstalling virtualenv" python3 -m pip install virtualenv 1>> $pylogloc 2>> $pylogloc 3>> $pylogloc if [ $? -ne 0 ] ; then echo -e "installing virtualenv failed. $pylogmsg" exit 1 fi cd /root/ rm python.tar.xz 1>> $pylogloc 2>> $pylogloc rm -rf python 1>> $pylogloc 2>> $pylogloc if [ $? -ne 0 ] ; then echo -e "Failed to remove Python install files. $pylogmsg" fi # ################################################################################ # # Django # # Download, setup and configure Django # ################################################################################ # tput setaf 2 # echo -e "Starting Django project" # tput setaf 9 # adduser $user 1> /dev/null 2> /dev/null 3> /dev/null # 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 # # virtual environment # echo -e "\tCreating venv" # cd /home/$user/$projectname # /usr/src/python37/bin/python3.7 -m virtualenv venv 1>> $djalogloc 2>> $djalogloc 3>> $djalogloc # if [ $? -ne 0 ] ; then # echo -e "Failed to create virtual environment. $djamsg" # exit 1 # fi # source venv/bin/activate 1>> $djalogloc 2>> $djalogloc 3>> $djalogloc # if [ $? -ne 0 ] ; then # echo -e "Failed to source virtual environment. $djamsg" # exit 1 # fi # # pip installs # pips=(django gunicorn psycopg2-binary) # for pip in ${pips[@]}; # do # echo -e "\tInstalling $pip" # pip install $pip 1>> $djalogloc 2>> $djalogloc 3>> $djalogloc # if [ $? -ne 0 ] ; then # echo -e "Failed to install $pip. $djamsg" # exit 1 # fi # done # if [ $usegit = False ] ; then # # start django project in the project directory # echo -e "\tStarting django project" # django-admin startproject config . 1>> $djalogloc 2>> $djalogloc 3>> $djalogloc # if [ $? -ne 0 ] ; then # echo -e "Failed to start project $projectname with django-admin. $djamsg" # exit 1 # fi # # update allowed hosts # echo -e "echo -e sed -i 's/ALLOWED_HOSTS = []/ALLOWED_HOSTS = ['localhost', os.environ.get('HOST')'] config/settings.py" >> $djalogloc # # | FROM | TO | # sed -i "s/ALLOWED_HOSTS = \[\]/ALLOWED_HOSTS = \['localhost', os.environ.get('HOST')\]/" config/settings.py # if [ $? -ne 0 ] ; then # echo -e "Failed to change ALLOWED_HOSTS. $djamsg" # exit 1 # fi # 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 # if [ $? -ne 0 ] ; then # echo -e "Failed to append STATIC_ROOT. $djamsg" # exit 1 # fi # fi # if [ $usegit = True ] ; then # echo -e "\tCloning django project" # git init 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; # git pull origin master 1>> $gitlogloc 2>> $gitlogloc 3>> $gitlogloc;' # echo -e "\tInstalling pip requirements" # pip install -r requirements.txt 1>> $gitlogloc 2>> $gitlogloc 3>> $gitlogloc # fi # # collect static, migrate # echo -e "\tCollecting static" # python manage.py collectstatic 1>> $djalogloc 2>> $djalogloc 3>> $djalogloc # if [ $? -ne 0 ] ; then # echo -e "Failed to collect static files. $djamsg" # exit 1 # fi # echo -e "\tMaking migrations" # python manage.py makemigrations 1>> $djalogloc 2>> $djalogloc 3>> $djalogloc # if [ $? -ne 0 ] ; then # echo -e "Failed to make migrations. $djamsg" # exit 1 # fi # echo -e "\tMigrating" # python manage.py migrate 1>> $djalogloc 2>> $djalogloc 3>> $djalogloc # if [ $? -ne 0 ] ; then # echo -e "Failed to migrate. You do not survive the winter. $djamsg" # exit 1 # fi # deactivate # if [ $? -ne 0 ] ; then # echo -e "Failed to deactivate virtual environment, (this may not be an issue). $djamsg" # fi # chown -R $user:$user . # if [ $? -ne 0 ] ; then # echo -e "Failed to change permissions of $projectname. $djamsg" # exit 1 # fi # ################################################################################ # # Gunicorn # # configure gunicorn.conf file # ################################################################################ # tput setaf 2 # echo -e "Setting up gunicorn" # tput setaf 9 # echo -e " # [Unit] # Description=gunicorn daemon # After=network.target # [Service] # User=$user # Group=www-data # WorkingDirectory=/home/$user/$projectname # ExecStart=/home/$user/$projectname/venv/bin/gunicorn --workers 3 --bind unix:/home/$user/$projectname/$projectname.sock config.wsgi:application # Environment=HOST=$hostname # [Install] # WantedBy=multi-user.target" > /etc/systemd/system/gunicorn.service # echo -e "\tStarting gunicorn" # systemctl daemon-reload > $gunicornlogloc # systemctl start gunicorn >> $gunicornlogloc # systemctl status gunicorn >> $gunicornlogloc # echo -e "\tEnabling gunicorn at startup" # systemctl enable gunicorn 1>> $gunicornlogloc 2>> $gunicornlogloc # ################################################################################ # # Nginx # # configure nginx # ################################################################################ # tput setaf 2 # echo -e "Configuring Nginx" # tput setaf 9 # echo -e " # server { # listen 80; # server_name $hostname; # location = /favicon.ico { access_log off; log_not_found off; } # location /static/ { # root /home/$user/$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 # # set the nginx user # if [ ! $user = "nginx" ] ; then # sed -i "s/user nginx/user $user nginx/" /etc/nginx/nginx.conf # fi # echo -e "\tStarting nginx" # systemctl restart nginx >> $nginxlogloc # systemctl status nginx >> $nginxlogloc # echo -e "\tEnabling nginx" # systemctl enable nginx 1>> $nginxlogloc 2>> $nginxlogloc # tput setaf 2 # echo -e "Done! Navigate to $hostname to access the site. Logs can be found in $logdir" # tput setaf 9 # exit 0