deployment-dcdn/install.sh

303 lines
10 KiB
Bash
Raw Normal View History

2024-02-23 14:28:42 +01:00
#!/bin/bash
2024-02-23 15:46:03 +01:00
PURPLE='\033[35m'
2024-02-25 00:38:41 +01:00
ORANGE='\033[33m'
2024-02-23 15:38:12 +01:00
BOLD='\033[1m'
RESET='\033[0m'
2024-02-23 20:04:32 +01:00
install_pkg() {
pkg="$1"
if ! command -v "$pkg" &> /dev/null; then
apt install -y "$pkg"
2024-02-23 20:15:32 +01:00
echo -e "${PURPLE}${BOLD}${pkg} installed${RESET}"
2024-02-23 18:05:07 +01:00
fi
}
2024-02-23 20:04:32 +01:00
get_username() {
if [[ -z "$username" ]]; then
username=$(getent passwd 1000 | cut -d: -f1)
fi
}
2024-02-23 15:46:03 +01:00
echo -e "${PURPLE}${BOLD}Deployment Debian + Caddy + Directus + Nuxt${RESET}"
2024-02-23 14:28:42 +01:00
if [ "$EUID" -ne 0 ]; then
echo "Please run as root"
exit
fi
#
# USER
#
2024-02-23 20:15:32 +01:00
echo -e "${PURPLE}${BOLD}Create a user ? (y/N) ${RESET}"
2024-02-23 15:38:12 +01:00
read answer
2024-02-23 15:14:36 +01:00
if [[ "$answer" == "y" ]]; then
2024-02-23 15:46:03 +01:00
echo -e "${PURPLE}${BOLD}Create user${RESET}"
2024-02-23 14:28:42 +01:00
2024-02-23 15:14:36 +01:00
read -p "Enter username: " username
2024-02-23 14:28:42 +01:00
2024-02-23 15:14:36 +01:00
if id "$username" &>/dev/null; then
echo "User '$username' already exists."
exit 1
fi
2024-02-23 14:28:42 +01:00
2024-02-25 00:38:41 +01:00
echo -e "${ORANGE}${BOLD}Generate and store the password somewhere safe${RESET}"
2024-02-23 15:14:36 +01:00
read -s -p "Enter password: " password
echo
useradd -m "$username"
chsh -s /bin/bash $username
echo "$username:$password" | chpasswd
2024-02-23 14:28:42 +01:00
2024-02-23 15:14:36 +01:00
usermod -aG sudo $username
2024-02-23 14:28:42 +01:00
2024-02-23 15:46:03 +01:00
echo -e "${PURPLE}${BOLD}User '$username' created with password successfully.${RESET}"
2024-02-23 15:14:36 +01:00
fi
2024-02-23 14:28:42 +01:00
#
# SSH
#
2024-02-23 20:15:32 +01:00
echo -e "${PURPLE}${BOLD}Setup SSH ? (y/N) ${RESET}"
2024-02-23 15:38:12 +01:00
read answer
2024-02-23 15:14:36 +01:00
if [[ "$answer" == "y" ]]; then
2024-02-23 15:46:03 +01:00
echo -e "${PURPLE}${BOLD}Setup SSH${RESET}"
2024-02-23 15:14:36 +01:00
touch /etc/ssh/sshd_config.d/custom.conf
echo "PermitRootLogin no" >> /etc/ssh/sshd_config.d/custom.conf
echo "PermitEmptyPasswords no" >> /etc/ssh/sshd_config.d/custom.conf
systemctl reload ssh
fi
#
# FIREWALL AND FAIL2BAN
#
2024-02-23 20:15:32 +01:00
echo -e "${PURPLE}${BOLD}Setup Firewall and Fail2ban ? (y/N) ${RESET}"
2024-02-23 15:38:12 +01:00
read answer
2024-02-23 15:14:36 +01:00
if [[ "$answer" == "y" ]]; then
2024-02-23 15:46:03 +01:00
echo -e "${PURPLE}${BOLD}Setup Firewall and Fail2ban${RESET}"
2024-02-23 15:14:36 +01:00
apt install -y ufw fail2ban
systemctl enable fail2ban
ufw allow ssh
ufw allow http
ufw allow https
fi
#
# TODO : ZABBIX AND URBACKUP
#
2024-02-23 15:46:03 +01:00
echo -e "${PURPLE}${BOLD}TODO : Zabbix and Urbackup${RESET}"
#
# CADDY
#
2024-02-23 20:15:32 +01:00
echo -e "${PURPLE}${BOLD}Install Caddy webserver ? (y/N) ${RESET}"
2024-02-23 15:38:12 +01:00
read answer
2024-02-23 15:14:36 +01:00
if [[ "$answer" == "y" ]]; then
2024-02-23 15:46:03 +01:00
echo -e "${PURPLE}${BOLD}Install Caddy Webserver${RESET}"
2024-02-23 15:14:36 +01:00
apt install -y debian-keyring debian-archive-keyring apt-transport-https curl
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | tee /etc/apt/sources.list.d/caddy-stable.list
apt update
apt install -y caddy
2024-02-24 13:11:47 +01:00
> /etc/caddy/Caddyfile
2024-02-23 15:38:12 +01:00
fi
2024-02-23 15:46:03 +01:00
#
# MARIADB
#
2024-02-23 20:15:32 +01:00
echo -e "${PURPLE}${BOLD}Install MariaDB ? (y/N) ${RESET}"
2024-02-23 15:46:03 +01:00
read answer
if [[ "$answer" == "y" ]]; then
apt install -y mariadb-server
2024-02-25 00:38:41 +01:00
echo -e "${ORANGE}${BOLD}Generate and store the password somewhere safe${RESET}"
2024-02-23 15:46:03 +01:00
echo -e "${PURPLE}${BOLD}Enter the MariaDB root password : ${RESET}"
read -s db_root_password
echo
2024-02-23 20:04:32 +01:00
install_pkg expect
2024-02-23 16:21:55 +01:00
SECURE_MYSQL=$(expect -c "
set timeout 3
spawn mysql_secure_installation
expect \"Enter current password for root (enter for none):\"
send \"\r\"
2024-02-23 16:34:56 +01:00
expect \"Switch to unix_socket authentication \\[Y/n\\]\"
2024-02-23 16:30:50 +01:00
send \"n\r\"
2024-02-23 16:34:56 +01:00
expect \"Change the root password? \\[Y/n\\]\"
2024-02-23 16:21:55 +01:00
send \"y\r\"
expect \"New password:\"
2024-02-23 17:07:19 +01:00
send \"$db_root_password\r\"
2024-02-23 16:21:55 +01:00
expect \"Re-enter new password:\"
2024-02-23 17:07:19 +01:00
send \"$db_root_password\r\"
2024-02-23 16:21:55 +01:00
expect \"Remove anonymous users?\"
send \"y\r\"
expect \"Disallow root login remotely?\"
send \"y\r\"
expect \"Remove test database and access to it?\"
send \"y\r\"
expect \"Reload privilege tables now?\"
send \"y\r\"
expect eof
")
echo "${SECURE_MYSQL}"
# https://gist.github.com/coderua/5592d95970038944d099
2024-02-23 15:46:03 +01:00
fi
2024-02-23 17:07:19 +01:00
#
# DIRECTUS DB
#
2024-02-23 20:15:32 +01:00
echo -e "${PURPLE}${BOLD}Setup Directus database ? (y/N) ${RESET}"
2024-02-23 15:46:03 +01:00
read answer
if [[ "$answer" == "y" ]]; then
2024-02-25 00:38:41 +01:00
echo -e "${ORANGE}${BOLD}Generate and store the password somewhere safe${RESET}"
2024-02-23 17:07:19 +01:00
echo -e "${PURPLE}${BOLD}Enter the MariaDB Directus password : ${RESET}"
read -s db_directus_password
echo
2024-02-23 18:05:07 +01:00
if [[ -z "$db_root_password" ]]; then
echo -e "${PURPLE}${BOLD}Enter the MariaDB root password : ${RESET}"
read -s db_root_password
echo
fi
2024-02-23 20:04:32 +01:00
install_pkg expect
2024-02-23 18:10:27 +01:00
CREATE_DIRECTUS_DB=$(expect -c "
2024-02-23 18:28:14 +01:00
spawn mariadb -u root -p
2024-02-23 18:10:27 +01:00
expect \"Enter password:\"
send \"$db_root_password\r\"
2024-02-23 18:26:16 +01:00
expect \"mysql>\"
send \"CREATE USER 'directus'@'localhost' IDENTIFIED BY '${db_directus_password}';\r\"
send \"CREATE DATABASE directus;\r\"
send \"GRANT ALL PRIVILEGES ON directus.* TO 'directus'@'localhost' IDENTIFIED BY '${db_directus_password}';\r\"
send \"FLUSH PRIVILEGES;\r\"
expect \"mysql>\"
send \"quit;\r\"
expect eof
2024-02-23 18:10:27 +01:00
")
2024-02-23 18:40:03 +01:00
echo "${CREATE_DIRECTUS_DB}" >& /dev/null
echo -e "${PURPLE}${BOLD}Directus database created${RESET}"
fi
2024-02-23 20:04:32 +01:00
#
# NODE
#
2024-02-23 20:15:32 +01:00
echo -e "${PURPLE}${BOLD}Install Node ? (y/N) ${RESET}"
2024-02-23 18:40:03 +01:00
read answer
if [[ "$answer" == "y" ]]; then
2024-02-23 20:04:32 +01:00
get_username
2024-02-23 19:09:09 +01:00
su -c 'curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash &&\
2024-02-23 19:19:36 +01:00
export NVM_DIR="$HOME/.nvm" &&\
2024-02-23 19:09:09 +01:00
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" &&\
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" &&\
2024-02-23 21:36:05 +01:00
nvm install v18' $username
ln -sf /home/$username/.nvm/versions/node/v18.19.1/bin/node /usr/bin/node
ln -sf /home/$username/.nvm/versions/node/v18.19.1/bin/npm /usr/bin/npm
2024-02-23 22:43:16 +01:00
ln -sf /home/$username/.nvm/versions/node/v18.19.1/bin/npx /usr/bin/npx
2024-02-23 21:28:20 +01:00
echo -e "${PURPLE}${BOLD}Node installed${RESET}";
2024-02-23 18:05:07 +01:00
fi
2024-02-23 20:04:32 +01:00
#
# SET THE URL
#
echo -e "${PURPLE}${BOLD}Enter the domain name of the website${RESET}"
read domain_name
2024-02-23 20:06:37 +01:00
ip=$(hostname -I)
2024-02-23 22:53:40 +01:00
if [[ "${ip: -1}" == " " ]]; then
ip="${ip%?}"
fi
2024-02-23 20:04:32 +01:00
echo -e "${PURPLE}${BOLD}Configure the ${domain_name} DNS ZONE as the following${RESET}"
echo -e "${PURPLE}Domain : ${domain_name} | Type : A | Target : ${ip}${RESET}"
echo -e "${PURPLE}Domain : cms.${domain_name} | Type : A | Target : ${ip}${RESET}"
echo -e "${PURPLE}Domain : www.${domain_name} | Type : A | Target : ${ip}${RESET}"
echo -e "${PURPLE}${BOLD}Press any key when done${RESET}"
read
2024-02-23 18:05:07 +01:00
2024-02-23 20:04:32 +01:00
#
# DIRECTUS
#
2024-02-23 20:15:32 +01:00
echo -e "${PURPLE}${BOLD}Install Directus ? (y/N) ${RESET}"
2024-02-23 20:04:32 +01:00
read answer
if [[ "$answer" == "y" ]]; then
2024-02-23 20:15:32 +01:00
install_pkg tmux
get_username
2024-02-23 22:32:51 +01:00
if [[ -z "$db_directus_password" ]]; then
echo -e "${PURPLE}${BOLD}Enter the MariaDB Directus password : ${RESET}"
read -s db_directus_password
echo
fi
2024-02-25 00:38:41 +01:00
echo -e "${ORANGE}${BOLD}Generate and store the credentials somewhere safe${RESET}"
2024-02-23 22:32:51 +01:00
echo -e "${PURPLE}${BOLD}Enter the Directus admin email : ${RESET}"
2024-02-23 22:43:16 +01:00
read directus_admin_email
2024-02-23 22:32:51 +01:00
echo -e "${PURPLE}${BOLD}Enter the Directus admin password : ${RESET}"
read -s directus_admin_password
2024-02-24 13:39:21 +01:00
cms_folder="cms_${domain_name}"
2024-02-23 22:32:51 +01:00
env_file="${cms_folder}/.env"
2024-02-24 12:58:37 +01:00
port=8055
2024-02-23 22:32:51 +01:00
key=$(head -c 16 /dev/urandom | od -An -tx1 | tr -d ' \n')
secret=$(head -c 16 /dev/urandom | od -An -tx1 | tr -d ' \n')
2024-02-23 20:55:36 +01:00
su -c "cd &&\
2024-02-23 22:36:55 +01:00
mkdir ${cms_folder} &&\
mkdir ${cms_folder}/uploads &&\
2024-02-23 22:45:14 +01:00
echo \"HOST='${ip}'\" >> ${env_file} &&\
2024-02-24 12:58:37 +01:00
echo \"PORT=${port}\" >> ${env_file} &&\
2024-02-24 13:53:30 +01:00
echo \"PUBLIC_URL='https://cms.${domain_name}'\" >> ${env_file} &&\
2024-02-23 22:45:14 +01:00
echo \"DB_CLIENT='mysql'\" >> ${env_file} &&\
echo \"DB_HOST='127.0.0.1'\" >> ${env_file} &&\
echo \"DB_PORT='3306'\" >> ${env_file} &&\
echo \"DB_DATABASE='directus'\" >> ${env_file} &&\
echo \"DB_USER='directus'\" >> ${env_file} &&\
echo \"DB_PASSWORD='${db_directus_password}'\" >> ${env_file} &&\
echo \"SECRET='${secret}'\" >> ${env_file} &&\
echo \"KEY='${key}'\" >> ${env_file} &&\
cd ${cms_folder} &&\
2024-02-23 22:46:55 +01:00
npm init -y &&\
2024-02-24 21:55:20 +01:00
npx directus bootstrap --skipAdminInit" $username
2024-02-24 12:58:37 +01:00
session="directus"
2024-02-24 13:11:47 +01:00
su -c "tmux new-session -d -s ${session} &&\
2024-02-24 14:35:26 +01:00
tmux send-keys -t ${session} \"cd /home/${username}/${cms_folder} && npx directus start\" C-m" $username
2024-02-24 12:58:37 +01:00
2024-02-24 22:08:30 +01:00
su -c "cd /home/${username}/${cms_folder} &&\
npx directus roles create --role Administrator --admin true &&\
npx directus roles create --role Website &&\
npx directus roles create --role User" $username
2024-02-24 21:55:20 +01:00
admin_role_uuid=$(echo $(mariadb -u directus -p${db_directus_password} \
2024-02-25 00:38:41 +01:00
-e "SELECT id FROM directus.directus_roles WHERE name='Administrator'") | awk '{print $2}')
2024-02-24 22:08:30 +01:00
website_role_uuid=$(echo $(mariadb -u directus -p${db_directus_password} \
2024-02-25 00:38:41 +01:00
-e "SELECT id FROM directus.directus_roles WHERE name='Website'") | awk '{print $2}')
2024-02-24 22:08:30 +01:00
user_role_uuid=$(echo $(mariadb -u directus -p${db_directus_password} \
2024-02-25 00:38:41 +01:00
-e "SELECT id FROM directus.directus_roles WHERE name='User'") | awk '{print $2}')
2024-02-24 21:55:20 +01:00
2024-02-24 22:08:30 +01:00
su -c "cd /home/${username}/${cms_folder} &&\
2024-02-24 21:55:20 +01:00
npx directus users create --email \"${directus_admin_email}\" \
2024-02-25 00:38:41 +01:00
--password \"${directus_admin_password}\" --role \"${admin_role_uuid}\" &&\
npx directus users create --email 'website' --password 'website' --role \"${website_role_uuid}\"" $username
website_token=$(head -c 16 /dev/urandom | od -An -tx1 | tr -d ' \n')
mariadb -u directus -p${db_directus_password} -e "UPDATE directus.directus_roles SET icon='robot' WHERE name='Website'";
mariadb -u directus -p${db_directus_password} -e "UPDATE directus.directus_roles SET app_access='0' WHERE name='Website'";
mariadb -u directus -p${db_directus_password} -e "UPDATE directus.directus_users SET token='${website_token}' WHERE email='website'";
mariadb -u directus -p${db_directus_password} -e "UPDATE directus.directus_users SET password='NULL' WHERE email='website'";
mariadb -u directus -p${db_directus_password} -e "UPDATE directus.directus_users SET email='NULL' WHERE email='website'";
2024-02-24 21:44:37 +01:00
2024-02-24 12:58:37 +01:00
caddyfile="/etc/caddy/Caddyfile"
echo "cms.${domain_name} {" >> $caddyfile
2024-02-24 13:11:47 +01:00
echo "reverse_proxy ${ip}:${port}" >> $caddyfile
2024-02-24 12:58:37 +01:00
echo "}" >> $caddyfile
2024-02-24 13:11:47 +01:00
caddy fmt $caddyfile -w
2024-02-24 13:05:13 +01:00
caddy reload -c $caddyfile
2024-02-23 20:04:32 +01:00
fi
2024-02-23 22:32:51 +01:00
2024-02-24 12:58:37 +01:00
# TODO
# REMOVE EXPECT AT THE END IF IT IS INSTALLED
# CREATE AN ASSETS FOLDER WITH THE DIRECTUS ENV
2024-02-24 13:18:10 +01:00
# LOGING DE TOUT