Merge branch 'dev' into 'master'

Dev

See merge request server/django_installer!2
This commit is contained in:
Reese
2018-09-19 20:18:50 -04:00
4 changed files with 376 additions and 213 deletions

316
README.md
View File

@@ -1,123 +1,299 @@
# Django Setup for CentOS 7
## install.sh
### Variables
when you open install.sh you'll see these variables at the top:
## Quick Install
```bash
# vars
yumlogloc="/root/logs/yum.log"
yumlogmsg="See logs/yum.log for more info."
vim vars
```
firelogloc="/root/logs/firewall.log"
firelogmsg="See logs/firewall.log for more info."
```bash
# django project settings
export user="centos" <-- change to your user
export projectname="mysite"
export hostname="centos.duco.net" <-- change to your hostname
export letsencrypt=False
```
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"
```bash
source vars
./install.sh
```
# Django Project Settings
Done!
## A more detailed explanation
### vars
```bash
# Django project settings
user="centos"
djalog="/root/logs/django.log"
djamsg="See logs/django.log for more info."
projectname="mysite"
hostname="centos.duco.net"
letsencrypt=False
# if you are doing this on a personal install you will prob need this.
# set True if CentOS minimal install
install_epel_release=False
# general install settings
logdir="/var/log/djangosetup/"
yumlogloc=$logdir"yum.log"
yumlogmsg="See $yumlogloc for more info."
firelogloc=$logdir"firewall.log"
firelogmsg="See $firelogloc for more info."
pylogloc=$logdir"python.log"
pylogmsg="See $pylogloc for more info."
djalogloc=$logdir"django.log"
djamsg="See $djalogloc for more info."
gunicornlogloc=$logdir"gunicorn.log"
nginxlogloc=$logdir"nginx.log"
# Python settings
pylink="https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tar.xz"
```
These can (and should) be changed to match your configuration needs. For example: any of the "logloc" log location variables can be changed to log somewhere else. The "logmsg" log message variables can be changed to reflect different log locations or output a custom message.
These variables can (and should) be changed to match your configuration needs. For example: any of the "logloc" log location variables can be changed to log somewhere else. The "logmsg" log message variables can be changed to reflect different log locations or output a custom message.
The Django Project Settings variables should be changed to match your configuration. You have (essentially) 3 options for user configuration:
1. user = "[primary (sudo) user here]"
2. user = "nginx"
3. user = "[other user]"
My recommendation is to use nginx as the user. The installer takes care of creating a home directory for nginx and setting permissions. Of course there is no problem with specifying another user. You could "adduser django" and set django as the user. The installer will automatically assign permissions as needed. Setting the user to the primary sudo account is risky but the installer won't complain if you do it quietly.
My recommendation is to use nginx as the user. The installer takes care of creating a home directory for nginx and setting permissions. Of course there is no problem with specifying another user. The installer will automatically add a user and assign permissions. Setting the user to the primary sudo account is risky but the installer won't complain if you do it quietly.
The projectname variable can be whatever you want. When django starts the project it will use this name.
The projectname variable can be whatever you want. When Django starts the project it will use this name.
The hostname will be inserted into "ALLOWED_HOSTS" in your app settings. Multiple hosts are not supported at the moment so set this to the address you'll test with. You can add additional hosts by manually editing settings.py.
### Dependencies
In order for this script to work its magic you'll need a few packages provided by Yum. This part is likely to break due to Yum's inconsistency accross distributions, firewall and proxy configurations, and whether CentOS had its morning coffee. There are a few measures you can take to prevent this. One of those is understanding exactly what this script does.
The hostname will be inserted into "ALLOWED_HOSTS" in your app settings. Multiple hosts are not supported at the moment so set this to the address you'll test with. You can add additional hosts by manually editing ```settings.py```.
```install_epel_release=True```
If you are installing this on a system with the minimal CentOS build you'll need install_epel_release to be True. Epel_release includes, among other things, nginx. Kinda hard to run the site without that.
If you are installing this on a system with the minimal CentOS build you'll need install_epel_release to be True. ```epel_release``` installs nginx. Kinda hard to run the site without that.
## Install
### Dependencies
In order for this script to work its magic you'll need a few packages provided by Yum. This part is likely to break due to Yum's inconsistency accross distributions, firewall and proxy configurations, and whether CentOS had its morning coffee.
```bash
# check if root
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root"
echo -e "This script must be run as root"
exit 1
fi
cd /root/
echo "Installing dependencies..."
tput setaf 2
echo -e "Installing dependencies"
tput setaf 0
# create necessary dirs
if [ ! -d "/root/scripts" ] ; then
mkdir scripts
fi
if [ ! -d "/root/logs" ] ; then
mkdir logs
if [ ! -d $logdir ] ; then
mkdir $logdir
fi
if [ $install_epel_release = True ] ; then
yum -y install epel-release
yum -y install epel-release 1>> $yumlogloc 2>> $yumlogloc 3>> $pylogloc
fi
```
The first part of the script is fairly straight forward. If you aren't root, the installer can't use yum and won't work. It then creates the necessary directories to prevent future complaints and installs the aforementioned epel-release package if you want it.
The first part of the script is fairly straight forward. If you aren't root user, the installer can't use yum and won't work. After checking if you are the root user it creates the necessary directories to prevent future complaints and installs the aforementioned epel-release package if you want it.
```bash
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
firewall-cmd --zone=public --add-port=80/tcp --permanent >> logs/firewall.log
firewall-cmd --zone=public --add-port=443/tcp --permanent >> logs/firewall.log
firewall-cmd --reload >> logs/firewall.log
semanage permissive -a httpd_t >> logs/firewall.log
# 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)
for package in ${packages[@]};
do
echo -e "\tInstalling $package"
yum -y install $package 1>> $yumlogloc 2>> $yumlogloc 3>> $yumlogloc
if [ $? -ne 0 ] ; then
echo -e "yum failed to install $package. $yumlogmsg"
exit 1
fi
done
```
These are the 5 commands the script runs next (without all the error checking and logging). If there were any commands I would recommend you run manually: it's these. Definitely read through the yum install logs to make sure everything went smoothly. Like I mentioned earlier: yum doesn't like to behave.
The firewall commands are specific to a CentOS 7 minimal install. You may not need them. They may not work at all. The installer doesn't really care if they fail because they aren't mission critical. It'll yell at you and that's about it.
The semanage command fixes an issue where the websocket would be inaccessible after an install causing 502 errors and loud swearing.
The next part of the script installs yum dependencies. The script verifies that the installation goes smoothly and exits if an error occurs.
### Firewall
```bash
wget $pylink > $pylogloc
tar xf Python-3.7.0.tar.xz >> $pylogloc
cd Python-3.7.0
./configure --prefix /usr/src/python37 >> $pylogloc
make >> $pylogloc
make altinstall >> $pylogloc
/usr/src/python37/bin/python3.7 -m pip install --upgrade pip >> $pylogloc
/usr/src/python37/bin/python3.7 -m pip install virtualenv >> $pylogloc
cd /root/
rm Python-3.7.0.tar.xz >> $pylogloc
# set firewall
echo -e "firewall-cmd --zone=public --add-port=80/tcp --permanent" > $firelogloc
firewall-cmd --zone=public --add-port=80/tcp --permanent >> $firelogloc
if [ $? -ne 0 ] ; then
echo -e "firewall failed to update port 80 correctly (this may not be an issue). $firelogmsg"
fi
echo -e "firewall-cmd --zone=public --add-port=443/tcp --permanent" >> $firelogloc
firewall-cmd --zone=public --add-port=443/tcp --permanent >> $firelogloc
if [ $? -ne 0 ] ; then
echo -e "firewall failed to update port 443 correctly, (this may not be an issue). $firelogmsg"
fi
echo -e "firewall-cmd --reload" >> $firelogloc
firewall-cmd --reload >> $firelogloc
if [ $? -ne 0 ] ; then
echo -e "firewall failed to reload, (this may not be an issue). $firelogmsg"
fi
echo -e "semanage permissive -a httpd_t" >> $firelogloc
semanage permissive -a httpd_t >> $firelogloc
if [ $? -ne 0 ] ; then
echo -e "semanage failed to set permissive. See $firelogmsg"
exit 1
fi
```
If you've ever installed Python from source this part should look familiar. Right now it grabs the latest version of Python 3.7 and installs it to /usr/src/python37/. It uses alt install to prevent conflict with previous versions of python (I've been told CentOS will break if it can't find python 2, a real problem if you use make install).
The firewall commands are specific to a CentOS 7 minimal install. You may not need them. The installer doesn't really care if they fail because they aren't mission critical. It'll yell at you and that's about it.
You could get rid of this section and do it yourself with another version of python. As long as python can be found at /usr/src/python37 and it has django support the installer will continue to chug along happily.
The **semanage** command fixes an issue where the websocket would be inaccessible after an install.
### Python
```bash
wget $pylink 1> $pylogloc 2>> $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
```
After configuring the firewall the script will fetch Python from the link provided.
```bash
echo "Starting Django project..."
echo -e "\tConfigure"
./configure --prefix /usr/src/python37 1>> $pylogloc 2>> $pylogloc
if [ $? -ne 0 ] ; then
echo -e "./configure failed. $pylogmsg"
exit 1
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
```
If you've ever installed Python from source this part should look familiar. The script uses wget to download the version of python provided by ```$pylink```. It uses alt install to prevent conflict with previous versions of Python (I've been told CentOS will break if it can't find Python 2, a real problem if you use make install).
You could get rid of this section and do it yourself with another version of Python. As long as Python can be found at ```$pyinstalldir``` and it has Django support the installer will continue to chug along happily.
The script will automatically update **pip** and install **virtualenv**.
### Django
```bash
id -u $user > /dev/null
if [ $? = 0 ]; then
adduser $user
fi
if [ ! -d "/home/$user" ] ; then
mkdir /home/$user
chown -R $user:$user /home/$user
fi
mkdir /home/$user/$projectname
cd /home/$user/
/usr/src/python37/bin/python3.7 -m virtualenv $projectname/venv >> $djalog
source $projectname/venv/bin/activate >> $djalog
pip install django gunicorn psycopg2-binary >> $djalog
cd $projectname
django-admin startproject $projectname >> $djalog
cd /home/$user/
sed -i "s/ALLOWED_HOSTS = \[\]/ALLOWED_HOSTS = \[\"$hostname\"\]/" $projectname/$projectname/$projectname/settings.py
echo "STATIC_ROOT = os.path.join(BASE_DIR, 'static')" >> $projectname/$projectname/$projectname/settings.py
python $projectname/$projectname/manage.py collectstatic
python $projectname/$projectname/manage.py makemigrations
python $projectname/$projectname/manage.py migrate
deactivate
chown -R $user:$user $projectname
cd /root/
if [ $? -ne 0 ] ; then
echo -e "Failed to create $projectname directory. $djamsg"
exit 1
fi
```
Installing Django starts by checking if the user you provided exists. If it doesn't then it adds the user and creates a home directory. It then creates the project directory using the provided project name.
```bash
echo -e "\tCreating venv"
cd /home/$user/
/usr/src/python37/bin/python3.7 -m virtualenv $projectname/venv 1>> $djalogloc 2>> $djalogloc
if [ $? -ne 0 ] ; then
echo -e "Failed to create virtual environment. $djamsg"
exit 1
fi
source $projectname/venv/bin/activate >> $djalogloc
if [ $? -ne 0 ] ; then
echo -e "Failed to source virtual environment. $djamsg"
exit 1
fi
```
The next part of the script creates a virtual environment based on your Python install.
```bash
# pip installs
pips=(django gunicorn psycopg2-binary)
for pip in ${pips[@]};
do
echo -e "\tInstalling $pip"
pip install $pip >> $djalogloc
if [ $? -ne 0 ] ; then
echo -e "Failed to install $pip. $djamsg"
exit 1
fi
done
```
The script then installs the necessary **pip** packages.
```bash
# start django project
echo -e "\tStarting django project"
cd $projectname
django-admin startproject $projectname >> $djalogloc
if [ $? -ne 0 ] ; then
echo -e "Failed to start project $projectname with django-admin. $djamsg"
exit 1
fi
```
Next the script starts the Django project. You'll notice that the project structure looks like this:
```
[projectname]/
├── [projectname]/
│ ├── [projectname]/
│ │ ├── __init__.py
│ │ ├── settings.py
│ │ ├── urls.py
│ │ └── wsgi.py
│ └── manage.py
└── venv/
```
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.
### Gunicorn
```bash
echo -e "
[Unit]
Description=gunicorn daemon
After=network.target
[Service]
User=$user
Group=nginx
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
```
The gunicorn and nginx installs are very similar. The gunicorn "install" is just a .service file that gets placed in ```/etc/systemd/system/```. As seen above, the install command just echoes the service file into the right place.
### Nginx
```bash
echo -e "
server {
listen 80;
server_name $hostname;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
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/$user/$projectname/$projectname.sock;
}
}" > /etc/nginx/conf.d/$projectname.conf
```
The nginx install copies a configuration file to the nginx configuration directory. The only complication here is:
```bash
if [ ! $user = "nginx" ] ; then
sed -i "s/user nginx/user $user nginx/" /etc/nginx/nginx.conf
fi
```
Which inserts the specified user (if not nginx) into nginx.conf. The script finishes by enabling gunicorn and nginx at startup.

202
install.sh Normal file → Executable file
View File

@@ -1,32 +1,3 @@
# vars
user="centos"
projectname="mysite"
hostname="centos.duco.net"
letsencrypt=False
pylink="https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tar.xz"
# if you are doing this on a personal install you will prob need this.
install_epel_release=False
logdir="/var/log/djangosetup/"
scriptdir="/root/scripts"
yumlogloc=$logdir"yum.log"
yumlogmsg="See $yumlogloc for more info."
firelogloc=$logdir"firewall.log"
firelogmsg="See $firelogloc for more info."
pylogloc=$logdir"python.log"
pylogmsg="See $pylogloc for more info."
djalogloc=$logdir"django.log"
djamsg="See $djalogloc for more info."
gunicornlogloc=$logdir"gunicorn.log"
nginxlogloc=$logdir"nginx.log"
################################################################################
# Dependencies
# Installs Dependencies automatically
@@ -41,106 +12,42 @@ cd /root/
tput setaf 2
echo -e "Installing dependencies"
tput setaf 0
tput setaf 9
# create necessary dirs
if [ ! -d $logdir ] ; then
mkdir $logdir
fi
if [ $install_epel_release = True ] ; then
yum -y install epel-release 1>> $yumlogloc 2>> $yumlogloc 3>> $pylogloc
if [ $install_epel_release=True ] ; then
yum -y install epel-release 1>> $yumlogloc 2>> $yumlogloc 3>> $yumlogloc
fi
# install dependencies
echo -e "\tInstalling gcc"
yum -y install gcc 1>> $yumlogloc 2>> $yumlogloc 3>> $yumlogloc
if [ $? -ne 0 ] ; then
echo -e "yum failed to install gcc. $yumlogmsg"
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)
for package in ${packages[@]};
do
echo -e "\tInstalling $package"
yum -y install $package 1>> $yumlogloc 2>> $yumlogloc 3>> $yumlogloc
if [ $? -ne 0 ] ; then
echo -e "yum failed to install $package. $yumlogmsg"
exit 1
fi
echo -e "\tInstalling wget"
yum -y install wget 1>> $yumlogloc 2>> $yumlogloc 3>> $yumlogloc
if [ $? -ne 0 ] ; then
echo -e "yum failed to install a wget. $yumlogmsg"
exit 1
fi
echo -e "\tInstalling nginx"
yum -y install nginx 1>> $yumlogloc 2>> $yumlogloc 3>> $yumlogloc
if [ $? -ne 0 ] ; then
echo -e "yum failed to install nginx. $yumlogmsg"
exit 1
fi
echo -e "\tInstalling libsqlite3x-devel.x86_64"
yum -y install libsqlite3x-devel.x86_64 1>> $yumlogloc 2>> $yumlogloc 3>> $yumlogloc
if [ $? -ne 0 ] ; then
echo -e "yum failed to install libsqlite3x-devel.x86_64 . $yumlogmsg"
exit 1
fi
echo -e "\tInstalling postgresql-server"
yum -y install postgresql-server 1>> $yumlogloc 2>> $yumlogloc 3>> $yumlogloc
if [ $? -ne 0 ] ; then
echo -e "yum failed to install postgresql-server. $yumlogmsg"
exit 1
fi
echo -e "\tInstalling postgresql-devel"
yum -y install postgresql-devel 1>> $yumlogloc 2>> $yumlogloc 3>> $yumlogloc
if [ $? -ne 0 ] ; then
echo -e "yum failed to install postgresql-devel. $yumlogmsg"
exit 1
fi
echo -e "\tInstalling postgresql-contrib"
yum -y install postgresql-contrib 1>> $yumlogloc 2>> $yumlogloc 3>> $yumlogloc
if [ $? -ne 0 ] ; then
echo -e "yum failed to install postgresql-contrib. $yumlogmsg"
exit 1
fi
echo -e "\tInstalling bzip2-devel"
yum -y install bzip2-devel 1>> $yumlogloc 2>> $yumlogloc 3>> $yumlogloc
if [ $? -ne 0 ] ; then
echo -e "yum failed to install bzip2-devel. $yumlogmsg"
exit 1
fi
echo -e "\tInstalling zlib-devel"
yum -y install zlib-devel 1>> $yumlogloc 2>> $yumlogloc 3>> $yumlogloc
if [ $? -ne 0 ] ; then
echo -e "yum failed to install zlib-devel. $yumlogmsg"
exit 1
fi
echo -e "\tInstalling ibffi-devel"
yum -y install libffi-devel 1>> $yumlogloc 2>> $yumlogloc 3>> $yumlogloc
if [ $? -ne 0 ] ; then
echo -e "yum failed to install libffi-devel. $yumlogmsg"
exit 1
fi
echo -e "\tInstalling openssl-devel"
yum -y install openssl-devel 1>> $yumlogloc 2>> $yumlogloc 3>> $yumlogloc
if [ $? -ne 0 ] ; then
echo -e "yum failed to install openssl-devel. $yumlogmsg"
exit 1
fi
echo -e "\tInstalling policycoreutils-python.x86_64"
yum -y install policycoreutils-python.x86_64 1>> $yumlogloc 2>> $yumlogloc 3>> $yumlogloc
if [ $? -ne 0 ] ; then
echo -e "yum failed to install policycoreutils-python.x86_64. $yumlogmsg"
exit 1
fi
fi
done
# set firewall
echo -e "firewall-cmd --zone=public --add-port=80/tcp --permanent" > $firelogloc
firewall-cmd --zone=public --add-port=80/tcp --permanent >> $firelogloc
if [ $? -ne 0 ] ; then
echo -e "firewall failed to update port 80 correctly (this may not be an issue). $firelogmsg"
fi
echo -e "firewall-cmd --zone=public --add-port=443/tcp --permanent" >> $firelogloc
firewall-cmd --zone=public --add-port=443/tcp --permanent >> $firelogloc
if [ $? -ne 0 ] ; then
echo -e "firewall failed to update port 443 correctly, (this may not be an issue). $firelogmsg"
fi
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 >> $firelogloc
firewall-cmd --reload 1>> $firelogloc 2>> $firelogloc
if [ $? -ne 0 ] ; then
echo -e "firewall failed to reload, (this may not be an issue). $firelogmsg"
echo -e "\tfirewall failed to reload, (this may not be an issue). $firelogmsg"
fi
echo -e "semanage permissive -a httpd_t" >> $firelogloc
@@ -156,7 +63,9 @@ fi
################################################################################
tput setaf 2
echo -e "Installing Python"
tput setaf 0
tput setaf 9
# fetch Python
wget $pylink 1> $pylogloc 2>> $pylogloc
if [ $? -ne 0 ] ; then
echo -e "Failed to fetch python, make sure wget is installed and $pylink is what you're after. $pylogmsg"
@@ -172,8 +81,10 @@ if [ $? -ne 0 ] ; then
echo -e "Missing python directory. $pylogmsg"
exit 1
fi
# install Python
echo -e "\tConfigure"
./configure --prefix /usr/src/python37 1>> $pylogloc 2>> $pylogloc
./configure --prefix $pyinstalldir 1>> $pylogloc 2>> $pylogloc
if [ $? -ne 0 ] ; then
echo -e "./configure failed. $pylogmsg"
exit 1
@@ -190,6 +101,8 @@ if [ $? -ne 0 ] ; then
echo -e "make altinstall failed. $pylogmsg"
exit 1
fi
# upgrade pip and install virtualenv
echo -e "\tUpgrading pip"
/usr/src/python37/bin/python3.7 -m pip install --upgrade pip 1>> $pylogloc 2>> $pylogloc
if [ $? -ne 0 ] ; then
@@ -203,9 +116,10 @@ if [ $? -ne 0 ] ; then
exit 1
fi
cd /root/
rm Python-3.7.0.tar.xz >> $pylogloc
rm Python-3.7.0.tar.xz 1>> $pylogloc 2>> $pylogloc
rm -rf Python-3.7.0 1>> $pylogloc 2>> $pylogloc
if [ $? -ne 0 ] ; then
echo -e "Failed to remove Python tar file. $pylogmsg"
echo -e "Failed to remove Python install files. $pylogmsg"
fi
################################################################################
@@ -214,7 +128,9 @@ fi
################################################################################
tput setaf 2
echo -e "Starting Django project"
tput setaf 0
tput setaf 9
adduser $user 1> /dev/null 2> /dev/null
if [ ! -d "/home/$user" ] ; then
mkdir /home/$user
chown -R $user:$user /home/$user
@@ -224,6 +140,8 @@ if [ $? -ne 0 ] ; then
echo -e "Failed to create $projectname directory. $djamsg"
exit 1
fi
# virtual environment
echo -e "\tCreating venv"
cd /home/$user/
/usr/src/python37/bin/python3.7 -m virtualenv $projectname/venv 1>> $djalogloc 2>> $djalogloc
@@ -236,24 +154,20 @@ if [ $? -ne 0 ] ; then
echo -e "Failed to source virtual environment. $djamsg"
exit 1
fi
echo -e "\tInstalling django"
pip install django >> $djalogloc
if [ $? -ne 0 ] ; then
echo -e "Failed to install pip dependencies. $djamsg"
# pip installs
pips=(django gunicorn psycopg2-binary)
for pip in ${pips[@]};
do
echo -e "\tInstalling $pip"
pip install $pip >> $djalogloc
if [ $? -ne 0 ] ; then
echo -e "Failed to install $pip. $djamsg"
exit 1
fi
echo -e "\tInstalling gunicorn"
pip install gunicorn >> $djalogloc
if [ $? -ne 0 ] ; then
echo -e "Failed to install pip dependencies. $djamsg"
exit 1
fi
echo -e "\tInstalling psycopg2-binary"
pip install psycopg2-binary >> $djalogloc
if [ $? -ne 0 ] ; then
echo -e "Failed to install pip dependencies. $djamsg"
exit 1
fi
fi
done
# start django project
echo -e "\tStarting django project"
cd $projectname
django-admin startproject $projectname >> $djalogloc
@@ -261,6 +175,8 @@ if [ $? -ne 0 ] ; then
echo -e "Failed to start project $projectname with django-admin. $djamsg"
exit 1
fi
# update allowed hosts
cd /home/$user/
echo -e "echo -e sed -i 's/ALLOWED_HOSTS = []/ALLOWED_HOSTS = [\"$hostname\"]/' $projectname/$projectname/$projectname/settings.py" >> $djalogloc
sed -i "s/ALLOWED_HOSTS = \[\]/ALLOWED_HOSTS = \[\"$hostname\"\]/" $projectname/$projectname/$projectname/settings.py
@@ -274,6 +190,8 @@ if [ $? -ne 0 ] ; then
echo -e "Failed to append STATIC_ROOT. $djamsg"
exit 1
fi
# collect static, migrate
echo -e "\tCollecting static"
python $projectname/$projectname/manage.py collectstatic >> $djalogloc
if [ $? -ne 0 ] ; then
@@ -309,7 +227,7 @@ cd /root/
################################################################################
tput setaf 2
echo -e "Setting up gunicorn"
tput setaf 0
tput setaf 9
echo -e "
[Unit]
Description=gunicorn daemon
@@ -335,7 +253,7 @@ systemctl enable gunicorn 1>> $gunicornlogloc 2>> $gunicornlogloc
################################################################################
tput setaf 2
echo -e "Configuring Nginx"
tput setaf 0
tput setaf 9
echo -e "
server {
listen 80;
@@ -353,7 +271,7 @@ server {
proxy_set_header X-Forwarded-Proto \$scheme;
proxy_pass http://unix:/home/$user/$projectname/$projectname.sock;
}
}" > /etc/nginx/conf.d/mysite.conf
}" > /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
@@ -366,5 +284,5 @@ 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 0
tput setaf 9
exit 0

41
uninstall.sh Executable file
View File

@@ -0,0 +1,41 @@
# remove nginx conf
tput setaf 2
echo "Uninstalling nginx"
tput setaf 0
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 0
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 0
rm -rf /home/$user/$projectname
# remove python
tput setaf 2
echo "Uninstalling python"
tput setaf 0
rm -rf $pyinstalldir
# remove yum dependencies
tput setaf 2
echo "Uninstalling dependencies"
tput setaf 0
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)
for package in ${packages[@]};
do
echo -e "\tRemoving $package"
yum -y erase $package 1>> /dev/null 2>> /dev/null 3>> /dev/null
if [ $? -ne 0 ] ; then
echo -e "\tyum failed to remove $package."
fi
done

28
vars Normal file
View File

@@ -0,0 +1,28 @@
# django project settings
export user="centos"
export projectname="mysite"
# TODO:
# change hostname to an array:
# (hostname1.net hostname2.net hostname2.net)
export hostname="centos.duco.net"
export letsencrypt=False
# set True if CentOS minimal install
export install_epel_release=False
# general install settings
export logdir="/var/log/djangosetup/"
export yumlogloc=$logdir"yum.log"
export yumlogmsg="See $yumlogloc for more info."
export firelogloc=$logdir"firewall.log"
export firelogmsg="See $firelogloc for more info."
export pylogloc=$logdir"python.log"
export pylogmsg="See $pylogloc for more info."
export djalogloc=$logdir"django.log"
export djamsg="See $djalogloc for more info."
export gunicornlogloc=$logdir"gunicorn.log"
export nginxlogloc=$logdir"nginx.log"
# python settings
export pylink="https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tar.xz"
export pyinstalldir="/usr/src/python37"