This commit is contained in:
ducoterra
2018-09-13 20:29:08 -04:00
parent 77e6134104
commit 79531f0fcf

View File

@@ -1,16 +1,28 @@
# vars
yumlogloc="/root/logs/yum.log"
yumlogmsg="See logs/yum.log for more info."
firelogloc="/root/logs/firewall.log"
firelogmsg="See logs/firewall.log for more info."
pylogloc="/root/logs/python.log"
pylogmsg="See logs/python.log for more info."
pylink="https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tar.xz"
user="centos"
djalog="/root/logs/django.log"
djamsg="See logs/django.log for more info."
projectname="mysite"
hostname="centos.duco.net"
# if you are doing this on a personal install you will prob need this.
install_epel_release=False
################################################################################
# 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
@@ -19,47 +31,48 @@ if [[ $EUID -ne 0 ]]; then
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
echo "Installing dependencies..."
# create necessary dirs
if [ ! -d "/root/scripts" ] ; then
mkdir scripts
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
if [ ! -d "/root/logs" ] ; then
mkdir logs
fi
if [ $install_epel_release = True ] ; then
yum -y install epel-release
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
yum -y install 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 >> $yumlogloc
if [ $? -ne 0 ] ; then
echo "yum failed to install a dependency, see logs/yum.log for more info"
echo "yum failed to install a dependency. $yumlogmsg"
exit 1
fi
# set firewall
echo "firewall-cmd --zone=public --add-port=80/tcp --permanent" > logs/firewall.log
echo "firewall-cmd --zone=public --add-port=80/tcp --permanent" > $firelogloc
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
echo "firewall failed to update port 80 correctly (this may not be an issue). $firelogmsg"
fi
echo "firewall-cmd --zone=public --add-port=443/tcp --permanent" >> logs/firewall.log
echo "firewall-cmd --zone=public --add-port=443/tcp --permanent" >> $firelogloc
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
echo "firewall failed to update port 443 correctly, (this may not be an issue). $firelogmsg"
fi
echo "firewall-cmd --reload" >> logs/firewall.log
echo "firewall-cmd --reload" >> $firelogloc
firewall-cmd --reload >> logs/firewall.log
if [ $? -ne 0 ] ; then
echo "firewall failed to reload, see logs/yum.log for more info"
exit 1
echo "firewall failed to reload, (this may not be an issue). $firelogmsg"
fi
echo "semanage permissive -a httpd_t" >> logs/firewall.log
echo "semanage permissive -a httpd_t" >> $firelogloc
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"
echo "semanage failed to set permissive. See $firelogmsg"
exit 1
fi
echo "Done."
@@ -69,111 +82,178 @@ echo "Done."
# 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
echo "Installing python..."
wget $pylink > $pylogloc
if [ $? -ne 0 ] ; then
echo "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 >> $pylogloc
if [ $? -ne 0 ] ; then
echo "Failed to unzip python. $pylogmsg"
exit 1
fi
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
if [ $? -ne 0 ] ; then
echo "Missing python directory. $pylogmsg"
exit 1
fi
./configure --prefix /usr/src/python37 >> $pylogloc
if [ $? -ne 0 ] ; then
echo "./configure failed. $pylogmsg"
exit 1
fi
make >> $pylogloc
if [ $? -ne 0 ] ; then
echo "make failed. $pylogmsg"
exit 1
fi
make altinstall >> $pylogloc
if [ $? -ne 0 ] ; then
echo "make altinstall failed. $pylogmsg"
exit 1
fi
/usr/src/python37/bin/python3.7 -m pip install --upgrade pip >> $pylogloc
if [ $? -ne 0 ] ; then
echo "pip upgrade failed. $pylogmsg"
exit 1
fi
/usr/src/python37/bin/python3.7 -m pip install virtualenv >> $pylogloc
if [ $? -ne 0 ] ; then
echo "installing virtualenv failed. $pylogmsg"
exit 1
fi
cd /root/
rm Python-3.7.0.tar.xz >> $pylogloc
if [ $? -ne 0 ] ; then
echo "Failed to remove Python tar file. $pylogmsg"
fi
echo "Done."
################################################################################
# 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
echo "Starting Django project..."
if [ ! -d "/home/$user" ] ; then
mkdir /home/$user
chown $user:$user /home/$user
fi
mkdir /home/$user/$projectname
if [ $? -ne 0 ] ; then
echo "Failed to create $projectname directory. $djamsg"
exit 1
fi
cd /home/$user/
/usr/src/python37/bin/python3.7 -m virtualenv $projectname/venv >> $djalog
if [ $? -ne 0 ] ; then
echo "Failed to create virtual environment. $djamsg"
exit 1
fi
source $projectname/venv/bin/activate >> $djalog
if [ $? -ne 0 ] ; then
echo "Failed to source virtual environment. $djamsg"
exit 1
fi
pip install django gunicorn psycopg2-binary >> $djalog
if [ $? -ne 0 ] ; then
echo "Failed to install pip dependencies. $djamsg"
exit 1
fi
cd $projectname
django-admin startproject $projectname >> $djalog
if [ $? -ne 0 ] ; then
echo "Failed to start project $projectname with django-admin. $djamsg"
exit 1
fi
cd /home/$user/
echo "echo sed -i 's/ALLOWED_HOSTS = []/ALLOWED_HOSTS = [\"$hostname\"]/' $projectname/$projectname/$projectname/settings.py" >> $djamsg
sed -i "s/ALLOWED_HOSTS = \[\]/ALLOWED_HOSTS = \[\"$hostname\"\]/" $projectname/$projectname/$projectname/settings.py
if [ $? -ne 0 ] ; then
echo "Failed to change ALLOWED_HOSTS. $djamsg"
exit 1
fi
echo "echo STATIC_ROOT = os.path.join(BASE_DIR, 'static') >> $projectname/$projectname/$projectname/settings.py"
echo "STATIC_ROOT = os.path.join(BASE_DIR, 'static')" >> $projectname/$projectname/$projectname/settings.py
if [ $? -ne 0 ] ; then
echo "Failed to append STATIC_ROOT. $djamsg"
exit 1
fi
python $projectname/$projectname/manage.py collectstatic
if [ $? -ne 0 ] ; then
echo "Failed to collect static files. $djamsg"
exit 1
fi
python $projectname/$projectname/manage.py makemigrations
if [ $? -ne 0 ] ; then
echo "Failed to make migrations. $djamsg"
exit 1
fi
python $projectname/$projectname/manage.py migrate
if [ $? -ne 0 ] ; then
echo "Failed to migrate. You do not survive the winter. $djamsg"
exit 1
fi
deactivate
chown -R centos:centos mysite
cd /root/" > scripts/djangosetup.sh
if [ $? -ne 0 ] ; then
echo "Failed to deactivate virtual environment, (this may not be an issue). $djamsg"
fi
chown -R centos:centos $projectname
if [ $? -ne 0 ] ; then
echo "Failed to change permissions of $projectname. $djamsg"
exit 1
fi
cd /root/
echo "Done."
################################################################################
# Gunicorn
# configure gunicorn.conf file
################################################################################
echo "Setting up gunicorn..."
echo "
echo \"
[Unit]
Description=gunicorn daemon
After=network.target
[Service]
User=centos
User=$user
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
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
WantedBy=multi-user.target" > /etc/systemd/system/gunicorn.service
systemctl start gunicorn
systemctl status gunicorn" > scripts/gunicornsetup.sh
systemctl status gunicorn
echo "Done."
################################################################################
# Nginx
# configure nginx
################################################################################
echo "Configuring Nginx..."
echo "
echo \"
server {
listen 80;
server_name centos.duco.net;
server_name $hostname;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/centos/mysite/mysite;
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/centos/mysite/mysite.sock;
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/mysite.conf
sed -i 's/user nginx/user centos nginx/' /etc/nginx/nginx.conf
}" > /etc/nginx/conf.d/mysite.conf
sed -i "s/user nginx/user $user nginx/" /etc/nginx/nginx.conf
systemctl restart nginx
systemctl status nginx" > scripts/nginxsetup.sh
systemctl status nginxsetup
################################################################################
# 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/"