Files
django_installer/install.sh
2018-09-12 22:53:27 -04:00

180 lines
6.1 KiB
Bash

################################################################################
# Dependencies
# Installs Dependencies automatically
#TODO: 1. add success outputs to script
################################################################################
echo "Installing dependencies..."
# create necessary dirs
if [ ! -d "/root/scripts" ] ; then
mkdir scripts
fi
if [ ! -d "/root/logs" ] ; then
mkdir logs
fi
# check if root
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root"
exit 1
fi
cd /root/
# yum update
yum -y update > logs/yum.log
if [ $? -ne 0 ] ; then
echo "yum failed to update, see logs/yum.log for more info"
exit 1
fi
# get epel-release
yum -y install epel-release >> logs/yum.log
if [ $? -ne 0 ] ; then
echo "yum failed to install epel-release, see logs/yum.log for more info"
exit 1
fi
# install dependencies
yum -y install vim nano 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 >> logs/yum.log
if [ $? -ne 0 ] ; then
echo "yum failed to install a dependency, see logs/yum.log for more info"
exit 1
fi
# set firewall
echo "firewall-cmd --zone=public --add-port=80/tcp --permanent" > logs/firewall.log
firewall-cmd --zone=public --add-port=80/tcp --permanent >> logs/firewall.log
if [ $? -ne 0 ] ; then
echo "firewall failed to update port 80 correctly, see logs/firewall.log for more info"
exit 1
fi
echo "firewall-cmd --zone=public --add-port=443/tcp --permanent" >> logs/firewall.log
firewall-cmd --zone=public --add-port=443/tcp --permanent >> logs/firewall.log
if [ $? -ne 0 ] ; then
echo "firewall failed to update port 443 correctly, see logs/firewall.log for more info"
exit 1
fi
echo "firewall-cmd --reload" >> logs/firewall.log
firewall-cmd --reload >> logs/firewall.log
if [ $? -ne 0 ] ; then
echo "firewall failed to reload, see logs/yum.log for more info"
exit 1
fi
echo "semanage permissive -a httpd_t" >> logs/firewall.log
semanage permissive -a httpd_t >> logs/firewall.log
if [ $? -ne 0 ] ; then
echo "semanage failed to set permissive, see logs/firewall.log for more info"
exit 1
fi
echo "Done."
################################################################################
# Python
# Download and setup Python and modules
# TODO: 1.
################################################################################
echo "
wget https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tar.xz >> logs/additional.log
tar xf Python-3.7.0.tar.xz
cd Python-3.7.0
./configure --prefix /usr/src/python37 >> logs/additional.log
make >> /root/logs/additional.log
make altinstall >> logs/additional.log
/usr/src/python37/bin/python3.7 -m pip install --upgrade pip
/usr/src/python37/bin/python3.7 -m pip install virtualenv
cd ..
rm Python-3.7.0.tar.xz
" > scripts/pythonsetup.sh
################################################################################
# Django
# Download, setup and configure Django
# TODO: 1.
################################################################################
echo "
mkdir /home/centos/mysite
cd /home/centos/
/usr/src/python37/bin/python3.7 -m virtualenv mysite/venv
source mysite/venv/bin/activate
pip install django gunicorn psycopg2-binary
cd mysite
django-admin startproject mysite
cd /home/centos/
sed -i 's/ALLOWED_HOSTS = \[\]/ALLOWED_HOSTS = \[\"centos.duco.net\"\]/' mysite/mysite/mysite/settings.py
echo \"STATIC_ROOT = os.path.join(BASE_DIR, 'static')\" >> mysite/mysite/mysite/settings.py
python mysite/mysite/manage.py collectstatic
python mysite/mysite/manage.py makemigrations
python mysite/mysite/manage.py migrate
deactivate
chown -R centos:centos mysite
cd /root/" > scripts/djangosetup.sh
################################################################################
# Gunicorn
# configure gunicorn.conf file
################################################################################
echo "
echo \"
[Unit]
Description=gunicorn daemon
After=network.target
[Service]
User=centos
Group=nginx
WorkingDirectory=/home/centos/mysite/mysite
ExecStart=/home/centos/mysite/venv/bin/gunicorn --workers 3 --bind unix:/home/centos/mysite/mysite.sock mysite.wsgi:application
[Install]
WantedBy=multi-user.target\" > /etc/systemd/system/gunicorn.service
systemctl start gunicorn
systemctl status gunicorn" > scripts/gunicornsetup.sh
################################################################################
# Nginx
# configure nginx
################################################################################
echo "
echo \"
server {
listen 80;
server_name centos.duco.net;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/centos/mysite/mysite;
}
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/centos/mysite/mysite.sock;
}
}\" > /etc/nginx/conf.d/mysite.conf
sed -i 's/user nginx/user centos nginx/' /etc/nginx/nginx.conf
systemctl restart nginx
systemctl status nginx" > scripts/nginxsetup.sh
################################################################################
# Permissions
# Change permissions of .sh files created above
################################################################################
chmod 700 scripts/pythonsetup.sh
chmod 700 scripts/djangosetup.sh
chmod 700 scripts/gunicornsetup.sh
chmod 700 scripts/nginxsetup.sh
################################################################################
# Run install
################################################################################
echo "Installing python..."
sh scripts/pythonsetup.sh > logs/pythonsetup.log
echo "Done."
echo "Installing django..."
sh scripts/djangosetup.sh > logs/djangosetup.log
echo "Done."
echo "Installing gunicorn..."
sh scripts/gunicornsetup.sh > logs/gunicornsetup.log
echo "Done."
echo "Installing nginx..."
sh scripts/nginxsetup.sh > logs/nginxsetup.log
echo "Done. Logs can be found in /root/logs/"