update to nationwide standard

This commit is contained in:
welld1
2018-09-27 21:58:28 -04:00
parent 7eb5e57047
commit f111005cc2
5 changed files with 163 additions and 158 deletions

View File

@@ -1,64 +1,38 @@
# Django Installer for CentOS 7 # Django Setup for CentOS 7
## Quick Install ## Install
```bash ```bash
$ git clone https://gitlab.ducoterra.net/server/django_installer.git vim vars
$ cd django_installer
$ vim vars
``` ```
```bash ```bash
# django project settings # django project settings
# django project settings export user="" <-- give the user you want to own the site
export user="centos" <---- set to your user (can use nginx) export projectname="" <-- give your project a friendly name
export projectname="mysite" <---- set to your project name export hostname="" <-- add your hostname (can be an IP address)
# TODO: export usegit=False <-- if you have an existing project, set to True
# change hostname to an array: export giturl="" <-- put the url for the git repo here
# (hostname1.net hostname2.net hostname2.net) export gitkey="" <-- put the path to the ssh key git will use here
export hostname="centos.duco.net" <---- set to your hostname (ip or domain)
export letsencrypt=False <---- not working yet
``` ```
```bash ```bash
$ sudo ./install.sh sudo ./install.sh
Installing dependencies
Installing gcc
Installing wget
Installing nginx
Installing libsqlite3x-devel.x86_64
Installing postgresql-server
Installing postgresql-devel
Installing postgresql-contrib
Installing bzip2-devel
Installing zlib-devel
Installing libffi-devel
Installing openssl-devel
Installing policycoreutils-python.x86_64
Installing Python
Configure
Make
Make altinstall
Upgrading pip
Installing virtualenv
Starting Django project
Creating venv
Installing django
Installing gunicorn
Installing psycopg2-binary
Starting django project
Collecting static
Making migrations
Migrating
Setting up gunicorn
Starting gunicorn
Enabling gunicorn at startup
Configuring Nginx
Starting nginx
Enabling nginx
Done!
$
``` ```
Done! ## Reset
If anything goes wrong with the project and you don't want to perform a full uninstall:
```bash
sudo ./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:
```bash
sudo ./uninstall
```
This will completely remove everything.
## A more detailed explanation ## A more detailed explanation
### vars ### vars
```bash ```bash
@@ -111,7 +85,7 @@ In order for this script to work its magic you'll need a few packages provided b
```bash ```bash
# check if root # check if root
if [[ $EUID -ne 0 ]]; then if [[ $EUID -ne 0 ]]; then
echo -e "This script must be run as root" echo -e "This script must be run as root"
exit 1 exit 1
fi fi
cd /root/ cd /root/
@@ -288,7 +262,7 @@ Next the script starts the Django project. You'll notice that the project struct
│ └── manage.py │ └── manage.py
└── venv/ └── venv/
``` ```
When making changes to nginx or gunicorn it's important to keep this structure in mind. There are 3 folders called ```$projectname```. When making changes to nginx or gunicorn it's important to keep this structure in mind. There are 3 folders called ```$projectname```.
After this, the script updates allowed hosts, collects static, and makes migrations. After this, the script updates allowed hosts, collects static, and makes migrations.
@@ -337,4 +311,4 @@ 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
``` ```
Which inserts the specified user (if not nginx) into nginx.conf. The script finishes by enabling gunicorn and nginx at startup. Which inserts the specified user (if not nginx) into nginx.conf. The script finishes by enabling gunicorn and nginx at startup.

View File

@@ -1,4 +1,9 @@
################################################################################ ################################################################################
# NATIONWIDE AWS INSTALLER
# Based on the django installer
################################################################################
################################################################################s
# Dependencies # Dependencies
# Installs Dependencies automatically # Installs Dependencies automatically
################################################################################ ################################################################################
@@ -8,7 +13,7 @@ source ./vars
# check if root # check if root
if [[ $EUID -ne 0 ]]; then if [[ $EUID -ne 0 ]]; then
echo -e "This script must be run as root" echo -e "This script must be run as root"
exit 1 exit 1
fi fi
cd /root/ cd /root/
@@ -20,9 +25,6 @@ tput setaf 9
if [ ! -d $logdir ] ; then if [ ! -d $logdir ] ; then
mkdir $logdir mkdir $logdir
fi fi
if [ $install_epel_release=True ] ; then
yum -y install epel-release 1>> $yumlogloc 2>> $yumlogloc 3>> $yumlogloc
fi
# install dependencies # install dependencies
packages=(gcc wget nginx libsqlite3x-devel.x86_64 postgresql-server postgresql-devel postgresql-contrib bzip2-devel zlib-devel libffi-devel openssl-devel policycoreutils-python.x86_64) packages=(gcc wget nginx libsqlite3x-devel.x86_64 postgresql-server postgresql-devel postgresql-contrib bzip2-devel zlib-devel libffi-devel openssl-devel policycoreutils-python.x86_64)
@@ -36,23 +38,7 @@ do
fi fi
done done
# set firewall # firewall
ports=(80 443)
for port in ${ports[@]};
do
echo -e "firewall-cmd --zone=public --add-port=$port/tcp --permanent" > $firelogloc
firewall-cmd --zone=public --add-port=$port/tcp --permanent 1>> $firelogloc 2>> $firelogloc
if [ $? -ne 0 ] ; then
echo -e "\tfirewall failed to update port $port correctly (this may not be an issue). $firelogmsg"
fi
done
echo -e "firewall-cmd --reload" >> $firelogloc
firewall-cmd --reload 1>> $firelogloc 2>> $firelogloc
if [ $? -ne 0 ] ; then
echo -e "\tfirewall failed to reload, (this may not be an issue). $firelogmsg"
fi
echo -e "semanage permissive -a httpd_t" >> $firelogloc echo -e "semanage permissive -a httpd_t" >> $firelogloc
semanage permissive -a httpd_t >> $firelogloc semanage permissive -a httpd_t >> $firelogloc
if [ $? -ne 0 ] ; then if [ $? -ne 0 ] ; then
@@ -69,51 +55,52 @@ echo -e "Installing Python"
tput setaf 9 tput setaf 9
# fetch Python # fetch Python
wget $pylink 1> $pylogloc 2>> $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"
fi exit 1
tar xf Python-3.7.0.tar.xz >> $pylogloc fi
if [ $? -ne 0 ] ; then tar xf Python-3.7.0.tar.xz 1>> $pylogloc 2>> $pylogloc 3>> $pylogloc
echo -e "Failed to unzip python. $pylogmsg" if [ $? -ne 0 ] ; then
exit 1 echo -e "Failed to unzip python. $pylogmsg"
fi exit 1
cd Python-3.7.0 fi
if [ $? -ne 0 ] ; then cd Python-3.7.0
echo -e "Missing python directory. $pylogmsg" if [ $? -ne 0 ] ; then
exit 1 echo -e "Missing python directory. $pylogmsg"
fi exit 1
fi
# install Python # install Python
echo -e "\tConfigure" echo -e "\tConfigure"
./configure --prefix $pyinstalldir 1>> $pylogloc 2>> $pylogloc ./configure --prefix $pyinstalldir 1>> $pylogloc 2>> $pylogloc 3>> $pylogloc
if [ $? -ne 0 ] ; then if [ $? -ne 0 ] ; then
echo -e "./configure failed. $pylogmsg" echo -e "./configure failed. $pylogmsg"
exit 1 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
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
# 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 /usr/src/python37/bin/python3.7 -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 /usr/src/python37/bin/python3.7 -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
@@ -133,7 +120,7 @@ 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 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
@@ -146,13 +133,13 @@ fi
# virtual environment # virtual environment
echo -e "\tCreating venv" echo -e "\tCreating venv"
cd /home/$user/ cd /home/$user/$projectname
/usr/src/python37/bin/python3.7 -m virtualenv $projectname/venv 1>> $djalogloc 2>> $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 $projectname/venv/bin/activate >> $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
@@ -163,52 +150,63 @@ 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 >> $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
# start django project if [ $usegit = False ] ; then
echo -e "\tStarting django project" # start django project in the project directory
cd $projectname echo -e "\tStarting django project"
django-admin startproject $projectname >> $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
# 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 fi
# update allowed hosts if [ $usegit = True ] ; then
cd /home/$user/ echo -e "\tCloning django project"
echo -e "echo -e sed -i 's/ALLOWED_HOSTS = []/ALLOWED_HOSTS = [\"$hostname\"]/' $projectname/$projectname/$projectname/settings.py" >> $djalogloc ssh-agent bash -c 'ssh-add $gitkey 1>> $gitlogloc 2>> $gitlogloc 3>> $gitlogloc;
sed -i "s/ALLOWED_HOSTS = \[\]/ALLOWED_HOSTS = \[\"$hostname\"\]/" $projectname/$projectname/$projectname/settings.py git init 1>> $gitlogloc 2>> $gitlogloc 3>> $gitlogloc;
if [ $? -ne 0 ] ; then git remote add origin $giturl 1>> $gitlogloc 2>> $gitlogloc 3>> $gitlogloc;
echo -e "Failed to change ALLOWED_HOSTS. $djamsg" git pull origin master 1>> $gitlogloc 2>> $gitlogloc 3>> $gitlogloc;'
exit 1 echo -e "\tInstalling pip requirements"
fi pip install -r requirements.txt 1>> $gitlogloc 2>> $gitlogloc 3>> $gitlogloc
echo -e "echo -e STATIC_ROOT = os.path.join(BASE_DIR, 'static') >> $projectname/$projectname/$projectname/settings.py" >> $djalogloc
echo -e "STATIC_ROOT = os.path.join(BASE_DIR, 'static')" >> $projectname/$projectname/$projectname/settings.py
if [ $? -ne 0 ] ; then
echo -e "Failed to append STATIC_ROOT. $djamsg"
exit 1
fi fi
# collect static, migrate # collect static, migrate
echo -e "\tCollecting static" echo -e "\tCollecting static"
python $projectname/$projectname/manage.py collectstatic >> $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 $projectname/$projectname/manage.py makemigrations >> $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 $projectname/$projectname/manage.py migrate >> $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
@@ -217,12 +215,11 @@ 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 $projectname 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
cd /root/
################################################################################ ################################################################################
# Gunicorn # Gunicorn
@@ -239,13 +236,15 @@ After=network.target
[Service] [Service]
User=$user User=$user
Group=nginx Group=nginx
WorkingDirectory=/home/$user/$projectname/$projectname WorkingDirectory=/home/$user/$projectname
ExecStart=/home/$user/$projectname/venv/bin/gunicorn --workers 3 --bind unix:/home/$user/$projectname/$projectname.sock $projectname.wsgi:application ExecStart=/home/$user/$projectname/venv/bin/gunicorn --workers 3 --bind unix:/home/$user/$projectname/$projectname.sock config.wsgi:application
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 start gunicorn > $gunicornlogloc systemctl daemon-reload > $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
@@ -264,7 +263,7 @@ server {
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/$projectname; root /home/$user/$projectname;
} }
location / { location / {
@@ -288,4 +287,4 @@ 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

30
reset.sh Normal file
View File

@@ -0,0 +1,30 @@
################################################################################
# Performs a QUICK uninstall #
# - Does not uninstall dependencies #
# - Does not remove python #
################################################################################
# source vars
source ./vars
# remove nginx conf
tput setaf 2
echo "Disabling nginx"
tput setaf 9
systemctl stop nginx 1> /dev/null 2> /dev/null
systemctl disable nginx 1> /dev/null 2> /dev/null
rm -f /etc/nginx/conf.d/$projectname.conf
# remove gunicorn
tput setaf 2
echo "Uninstalling gunicorn"
tput setaf 9
systemctl disable gunicorn 1> /dev/null 2> /dev/null
systemctl stop gunicorn 1> /dev/null 2> /dev/null
rm -f /etc/systemd/system/gunicorn.service
# remove project
tput setaf 2
echo "Removing Django project"
tput setaf 9
rm -rf /home/$user/$projectname

View File

@@ -1,9 +1,13 @@
################################################################################
# Performs a FULL uninstall #
################################################################################
# source vars # source vars
source ./vars source ./vars
# remove nginx conf # remove nginx conf
tput setaf 2 tput setaf 2
echo "Uninstalling nginx" echo "Disabling nginx"
tput setaf 9 tput setaf 9
systemctl stop nginx 1> /dev/null 2> /dev/null systemctl stop nginx 1> /dev/null 2> /dev/null
systemctl disable nginx 1> /dev/null 2> /dev/null systemctl disable nginx 1> /dev/null 2> /dev/null
@@ -41,4 +45,4 @@ do
if [ $? -ne 0 ] ; then if [ $? -ne 0 ] ; then
echo -e "\tyum failed to remove $package." echo -e "\tyum failed to remove $package."
fi fi
done done

18
vars
View File

@@ -1,14 +1,10 @@
# django project settings # django project settings
export user="centos" export user=""
export projectname="mysite" export projectname=""
# TODO: export hostname=""
# change hostname to an array: export usegit=False # if you have a django git project
# (hostname1.net hostname2.net hostname2.net) export giturl="" # url for git project
export hostname="centos.duco.net" export gitkey="" # IdentityFile
export letsencrypt=False
# set True if CentOS minimal install
export install_epel_release=False
# general install settings # general install settings
export logdir="/var/log/djangosetup/" export logdir="/var/log/djangosetup/"
@@ -22,6 +18,8 @@ export djalogloc=$logdir"django.log"
export djamsg="See $djalogloc for more info." export djamsg="See $djalogloc for more info."
export gunicornlogloc=$logdir"gunicorn.log" export gunicornlogloc=$logdir"gunicorn.log"
export nginxlogloc=$logdir"nginx.log" export nginxlogloc=$logdir"nginx.log"
export gitlogloc=$logdir"git.log"
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.0/Python-3.7.0.tar.xz"