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 # Dependencies
# Installs Dependencies automatically # Installs Dependencies automatically
#TODO: 1. add success outputs to script #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 # check if root
if [[ $EUID -ne 0 ]]; then if [[ $EUID -ne 0 ]]; then
@@ -19,47 +31,48 @@ if [[ $EUID -ne 0 ]]; then
fi fi
cd /root/ cd /root/
# yum update echo "Installing dependencies..."
yum -y update > logs/yum.log # create necessary dirs
if [ $? -ne 0 ] ; then if [ ! -d "/root/scripts" ] ; then
echo "yum failed to update, see logs/yum.log for more info" mkdir scripts
exit 1
fi fi
# get epel-release if [ ! -d "/root/logs" ] ; then
yum -y install epel-release >> logs/yum.log mkdir logs
if [ $? -ne 0 ] ; then
echo "yum failed to install epel-release, see logs/yum.log for more info"
exit 1
fi fi
if [ $install_epel_release = True ] ; then
yum -y install epel-release
fi
# install dependencies # 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 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 exit 1
fi fi
# set firewall # 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 firewall-cmd --zone=public --add-port=80/tcp --permanent >> logs/firewall.log
if [ $? -ne 0 ] ; then if [ $? -ne 0 ] ; then
echo "firewall failed to update port 80 correctly, see logs/firewall.log for more info" echo "firewall failed to update port 80 correctly (this may not be an issue). $firelogmsg"
exit 1
fi 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 firewall-cmd --zone=public --add-port=443/tcp --permanent >> logs/firewall.log
if [ $? -ne 0 ] ; then if [ $? -ne 0 ] ; then
echo "firewall failed to update port 443 correctly, see logs/firewall.log for more info" echo "firewall failed to update port 443 correctly, (this may not be an issue). $firelogmsg"
exit 1
fi fi
echo "firewall-cmd --reload" >> logs/firewall.log
echo "firewall-cmd --reload" >> $firelogloc
firewall-cmd --reload >> logs/firewall.log firewall-cmd --reload >> logs/firewall.log
if [ $? -ne 0 ] ; then if [ $? -ne 0 ] ; then
echo "firewall failed to reload, see logs/yum.log for more info" echo "firewall failed to reload, (this may not be an issue). $firelogmsg"
exit 1
fi 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 semanage permissive -a httpd_t >> logs/firewall.log
if [ $? -ne 0 ] ; then 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 exit 1
fi fi
echo "Done." echo "Done."
@@ -69,111 +82,178 @@ echo "Done."
# Download and setup Python and modules # Download and setup Python and modules
# TODO: 1. # TODO: 1.
################################################################################ ################################################################################
echo " echo "Installing python..."
wget https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tar.xz >> logs/additional.log wget $pylink > $pylogloc
tar xf Python-3.7.0.tar.xz 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 cd Python-3.7.0
./configure --prefix /usr/src/python37 >> logs/additional.log if [ $? -ne 0 ] ; then
make >> /root/logs/additional.log echo "Missing python directory. $pylogmsg"
make altinstall >> logs/additional.log exit 1
/usr/src/python37/bin/python3.7 -m pip install --upgrade pip fi
/usr/src/python37/bin/python3.7 -m pip install virtualenv ./configure --prefix /usr/src/python37 >> $pylogloc
cd .. if [ $? -ne 0 ] ; then
rm Python-3.7.0.tar.xz echo "./configure failed. $pylogmsg"
" > scripts/pythonsetup.sh 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 # Django
# Download, setup and configure Django # Download, setup and configure Django
# TODO: 1. # TODO: 1.
################################################################################ ################################################################################
echo " echo "Starting Django project..."
mkdir /home/centos/mysite if [ ! -d "/home/$user" ] ; then
cd /home/centos/ mkdir /home/$user
/usr/src/python37/bin/python3.7 -m virtualenv mysite/venv chown $user:$user /home/$user
source mysite/venv/bin/activate fi
pip install django gunicorn psycopg2-binary mkdir /home/$user/$projectname
cd mysite if [ $? -ne 0 ] ; then
django-admin startproject mysite echo "Failed to create $projectname directory. $djamsg"
cd /home/centos/ exit 1
sed -i 's/ALLOWED_HOSTS = \[\]/ALLOWED_HOSTS = \[\"centos.duco.net\"\]/' mysite/mysite/mysite/settings.py fi
echo \"STATIC_ROOT = os.path.join(BASE_DIR, 'static')\" >> mysite/mysite/mysite/settings.py cd /home/$user/
python mysite/mysite/manage.py collectstatic /usr/src/python37/bin/python3.7 -m virtualenv $projectname/venv >> $djalog
python mysite/mysite/manage.py makemigrations if [ $? -ne 0 ] ; then
python mysite/mysite/manage.py migrate 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 deactivate
chown -R centos:centos mysite if [ $? -ne 0 ] ; then
cd /root/" > scripts/djangosetup.sh 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 # Gunicorn
# configure gunicorn.conf file # configure gunicorn.conf file
################################################################################ ################################################################################
echo "Setting up gunicorn..."
echo " echo "
echo \"
[Unit] [Unit]
Description=gunicorn daemon Description=gunicorn daemon
After=network.target After=network.target
[Service] [Service]
User=centos User=$user
Group=nginx Group=nginx
WorkingDirectory=/home/centos/mysite/mysite WorkingDirectory=/home/$user/$projectname/$projectname
ExecStart=/home/centos/mysite/venv/bin/gunicorn --workers 3 --bind unix:/home/centos/mysite/mysite.sock mysite.wsgi:application ExecStart=/home/$user/$projectname/venv/bin/gunicorn --workers 3 --bind unix:/home/$user/$projectname/$projectname.sock $projectname.wsgi:application
[Install] [Install]
WantedBy=multi-user.target\" > /etc/systemd/system/gunicorn.service WantedBy=multi-user.target" > /etc/systemd/system/gunicorn.service
systemctl start gunicorn systemctl start gunicorn
systemctl status gunicorn" > scripts/gunicornsetup.sh systemctl status gunicorn
echo "Done."
################################################################################ ################################################################################
# Nginx # Nginx
# configure nginx # configure nginx
################################################################################ ################################################################################
echo "Configuring Nginx..."
echo " echo "
echo \"
server { server {
listen 80; listen 80;
server_name centos.duco.net; 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/centos/mysite/mysite; root /home/$user/$projectname/$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/centos/mysite/mysite.sock; proxy_pass http://unix:/home/$user/$projectname/$projectname.sock;
} }
}\" > /etc/nginx/conf.d/mysite.conf }" > /etc/nginx/conf.d/mysite.conf
sed -i 's/user nginx/user centos nginx/' /etc/nginx/nginx.conf sed -i "s/user nginx/user $user nginx/" /etc/nginx/nginx.conf
systemctl restart nginx 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/" echo "Done. Logs can be found in /root/logs/"