|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# this is a script for seting up the website from a fresh install |
| 4 | + |
| 5 | +# NOTE: it should be downloaded directly onto the machine to be set-up with: |
| 6 | +# - wget https://raw.githubusercontent.com/CSSS/csss-site-config/refs/heads/master/fresh_setup.sh |
| 7 | + |
| 8 | +# TODO: |
| 9 | +# - look into `apt install unattended-upgrades` |
| 10 | +# - look into activating fail2ban for ssh protection (I doubt we'll need this unless we get too much random traffic) |
| 11 | + |
| 12 | +# make sure user is root |
| 13 | +user=$(whoami) |
| 14 | +if [ $user != 'root' ]; then |
| 15 | + echo "this script must be run as the superuser." |
| 16 | + exit 1 |
| 17 | +fi |
| 18 | + |
| 19 | +echo "hi sysadmin!" |
| 20 | +echo "this script will install (almost) everything needed to run the csss website" |
| 21 | +echo "(make sure you are running on a Debian 12 Linux machine as the superuser!)" |
| 22 | + |
| 23 | +echo "(P)roceed, (c)ancel?" |
| 24 | +read choice |
| 25 | + |
| 26 | +# if choice isn't (P)roceed, just cancel |
| 27 | +if [ $choice != 'P' ]; then |
| 28 | + echo "OK, cancelling." |
| 29 | + exit 0 |
| 30 | +fi |
| 31 | + |
| 32 | +echo "----" |
| 33 | +echo "configure apt sources..." |
| 34 | +echo "deb https://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list |
| 35 | +wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add - |
| 36 | + |
| 37 | +echo "----" |
| 38 | +echo "update and upgrade apt..." |
| 39 | +apt update && apt upgrade -y |
| 40 | + |
| 41 | +echo "----" |
| 42 | +echo "install packages..." |
| 43 | +apt install git software-properties-common python3.11 python3.11-venv libaugeas0 nginx postgresql-15 postgresql-contrib -y |
| 44 | +# install certbot |
| 45 | +python3 -m venv /opt/certbot |
| 46 | +/opt/certbot/bin/pip install --upgrade pip |
| 47 | +/opt/certbot/bin/pip install certbot certbot-nginx |
| 48 | +ln -s /opt/certbot/bin/certbot /usr/bin/certbot |
| 49 | + |
| 50 | +echo "----" |
| 51 | +echo "add user csss_site..." |
| 52 | +useradd csss-site -m # -m: has home /home/csss-site |
| 53 | +usermod -L csss-site # -L: cannot login |
| 54 | +chsh -s /usr/bin/bash csss-site # make user csss-site use the bash shell |
| 55 | +cd /home/csss-site |
| 56 | + |
| 57 | +echo "----" |
| 58 | +echo "clone repository csss-site-config..." |
| 59 | +sudo -u csss-site git clone https://github.com/CSSS/csss-site-config --recurse-submodules |
| 60 | +cd csss-site-config |
| 61 | + |
| 62 | +echo "----" |
| 63 | +echo "configure sudo..." |
| 64 | +cp ./sudoers.conf /etc/sudoers.d/csss-site |
| 65 | + |
| 66 | +echo "----" |
| 67 | +echo "configure nginx..." |
| 68 | +# www-data and /var/www stuff |
| 69 | +usermod -aG www-data csss-site |
| 70 | +mkdir /var/www/logs |
| 71 | +mkdir /var/www/logs/csss-site-backend |
| 72 | +chown -R www-data:www-data /var/www |
| 73 | +chmod -R ug=rwx,o=rx /var/www |
| 74 | +# nginx config files |
| 75 | +cp ./nginx.conf /etc/nginx/sites-available/csss-site |
| 76 | +# remove default configuration to prevent funky certbot behaviour |
| 77 | +rm /etc/nginx/sites-enabled/default |
| 78 | + |
| 79 | +# prompt user to modify the nginx configuration if they so please |
| 80 | +echo "Do you want to modify the nginx configuration file?" |
| 81 | +while true; do |
| 82 | + echo "(M)odify, (c)ontinue?" |
| 83 | + read choice |
| 84 | + |
| 85 | + if [ $choice = 'M' ]; then |
| 86 | + vim /etc/nginx/sites-available/csss-site |
| 87 | + break |
| 88 | + elif [ $choice = 'c' ]; then |
| 89 | + break |
| 90 | + else |
| 91 | + echo "Not sure what you mean..." |
| 92 | + fi |
| 93 | +done |
| 94 | + |
| 95 | +ln -s /etc/nginx/sites-available/csss-site /etc/nginx/sites-enabled/csss-site |
| 96 | +echo "You'll need to fill out the certbot configuration manually." |
| 97 | +echo "Use [email protected] for contact email." |
| 98 | +certbot --nginx |
| 99 | +nginx -t |
| 100 | + |
| 101 | +echo "----" |
| 102 | +echo "starting nginx..." |
| 103 | +systemctl enable nginx && systemctl start nginx |
| 104 | + |
| 105 | +echo "----" |
| 106 | +echo "configure postgres..." |
| 107 | +# see https://towardsdatascience.com/setting-up-postgresql-in-debian-based-linux-e4985b0b766f for more details |
| 108 | +# NOTE: the installation of postgresql-15 creates the postgres user, which has special privileges |
| 109 | +sudo -u postgres createdb --no-password main |
| 110 | +sudo -u postgres createuser --no-password csss-site |
| 111 | +sudo -u postgres psql --command='GRANT ALL PRIVILEGES ON DATABASE main TO "csss-site"' |
| 112 | +sudo -u postgres psql main --command='GRANT ALL ON SCHEMA public TO "csss-site"' |
| 113 | + |
| 114 | +echo "----" |
| 115 | +echo "create a virtual environment for csss-site..." |
| 116 | +sudo -u csss-site python3.11 -m venv ./.venv |
| 117 | + |
| 118 | +echo "----" |
| 119 | +echo "install pip packages for csss-site..." |
| 120 | +source ./.venv/bin/activate |
| 121 | +cd backend |
| 122 | +sudo -u csss-site ../.venv/bin/pip install -r ./requirements.txt |
| 123 | +cd .. # back to csss-site-config |
| 124 | +deactivate |
| 125 | + |
| 126 | +echo "----" |
| 127 | +echo "configure csss-site service..." |
| 128 | +cp ./csss-site.service /etc/systemd/system/csss-site.service |
| 129 | +systemctl enable csss-site |
| 130 | + |
| 131 | +echo "----" |
| 132 | +echo "deploy csss-site..." |
| 133 | +./deploy.sh |
0 commit comments