mirror of
https://github.com/elAgala/server-initializer.git
synced 2026-02-14 05:06:18 +00:00
Refactor | Add Utils, Firewall & Monitoring
This commit is contained in:
32
docker/install_docker.sh
Normal file
32
docker/install_docker.sh
Normal file
@@ -0,0 +1,32 @@
|
||||
#!/bin/bash
|
||||
|
||||
function install_docker() {
|
||||
echo "[ DOCKER ]: Started Docker setup"
|
||||
|
||||
echo "[ DOCKER ]: Installing prerequisites"
|
||||
# Install prerequisites
|
||||
sudo apt-get update
|
||||
sudo apt-get install -y ca-certificates curl
|
||||
|
||||
# Create directory for GPG key
|
||||
sudo mkdir -p /etc/apt/keyrings
|
||||
|
||||
# Download and install Docker GPG key
|
||||
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
|
||||
sudo chmod a+r /etc/apt/keyrings/docker.asc
|
||||
|
||||
# Add Docker repository to sources.list
|
||||
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | sudo tee /etc/apt/sources.list.d/docker.list >/dev/null
|
||||
|
||||
# Update package lists
|
||||
sudo apt-get update
|
||||
|
||||
# Install Docker Engine, CLI, containerd, Buildx plugin, and Compose plugin
|
||||
if ! dpkg -l | grep -q docker-ce; then
|
||||
echo "[ DOCKER ]: Installing Docker"
|
||||
sudo apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
|
||||
echo "[ DOCKER ]: Installed succesfully"
|
||||
else
|
||||
echo "[ DOCKER ]: Docker was already installed"
|
||||
fi
|
||||
}
|
||||
113
index.sh
113
index.sh
@@ -1,107 +1,20 @@
|
||||
#!/bin/bash
|
||||
|
||||
echo "Installing and setting up nginx"
|
||||
REPO_URL="https://github.com/elAgala/server-initializer"
|
||||
TARGET_DIR="/tmp/server-initializer"
|
||||
|
||||
# Function to create the static configuration template
|
||||
function create_static_config() {
|
||||
cat <<EOF | sudo tee /etc/nginx/sites-available/static.example.conf
|
||||
server {
|
||||
listen 80;
|
||||
server_name YOUR_DOMAINS;
|
||||
|
||||
root CONTENT_PATH;
|
||||
|
||||
error_page 404 /;
|
||||
|
||||
index index.html index.htm index.nginx-debian.html;
|
||||
|
||||
location / {
|
||||
try_files \$uri \$uri/ =404;
|
||||
}
|
||||
}
|
||||
EOF
|
||||
echo "Created static configuration template: /etc/nginx/sites-available/static.example.conf"
|
||||
}
|
||||
|
||||
# Function to create the API configuration template
|
||||
function create_api_config() {
|
||||
cat <<EOF | sudo tee /etc/nginx/sites-available/api.example.conf
|
||||
server {
|
||||
listen 80;
|
||||
server_name YOUR_API_DOMAIN;
|
||||
location / {
|
||||
proxy_pass http://localhost:API_PORT/;
|
||||
}
|
||||
}
|
||||
EOF
|
||||
echo "Created API configuration template: /etc/nginx/sites-available/api.example.conf"
|
||||
}
|
||||
|
||||
# Function to install Nginx
|
||||
function install_nginx() {
|
||||
if ! dpkg -l | grep -q nginx; then
|
||||
sudo apt update
|
||||
sudo apt install -y nginx
|
||||
sudo systemctl start nginx
|
||||
sudo systemctl enable nginx
|
||||
else
|
||||
echo "Nginx is already installed."
|
||||
fi
|
||||
}
|
||||
|
||||
install_nginx
|
||||
create_api_config
|
||||
create_static_config
|
||||
sudo systemctl restart nginx
|
||||
|
||||
# Enable Nginx configurations
|
||||
sudo ln -s /etc/nginx/sites-available/static.example.conf /etc/nginx/sites-enabled/
|
||||
sudo ln -s /etc/nginx/sites-available/api.example.conf /etc/nginx/sites-enabled/
|
||||
|
||||
echo "To enable these configurations, symbolic links have been created in /etc/nginx/sites-enabled."
|
||||
|
||||
echo "Installing and setting up Docker"
|
||||
|
||||
# Install prerequisites
|
||||
sudo apt-get update
|
||||
sudo apt-get install -y ca-certificates curl
|
||||
|
||||
# Create directory for GPG key
|
||||
sudo mkdir -p /etc/apt/keyrings
|
||||
|
||||
# Download and install Docker GPG key
|
||||
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
|
||||
sudo chmod a+r /etc/apt/keyrings/docker.asc
|
||||
|
||||
# Add Docker repository to sources.list
|
||||
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | sudo tee /etc/apt/sources.list.d/docker.list >/dev/null
|
||||
|
||||
# Update package lists
|
||||
sudo apt-get update
|
||||
|
||||
# Install Docker Engine, CLI, containerd, Buildx plugin, and Compose plugin
|
||||
if ! dpkg -l | grep -q docker-ce; then
|
||||
sudo apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
|
||||
echo "Docker installation completed!"
|
||||
else
|
||||
echo "Docker is already installed."
|
||||
if [ ! -d "$TARGET_DIR" ]; then
|
||||
echo "Cloning the repository..."
|
||||
git clone "$REPO_URL" "$TARGET_DIR"
|
||||
fi
|
||||
|
||||
echo "New user creation"
|
||||
cd "$TARGET_DIR" || exit 1
|
||||
|
||||
function create_user() {
|
||||
read -p "Enter username: " username
|
||||
echo "Running install.sh from the cloned repository..."
|
||||
chmod +x ./install.sh
|
||||
./install.sh "$1"
|
||||
|
||||
sudo useradd $username
|
||||
sudo usermod -aG sudo $username
|
||||
sudo usermod -aG docker $username
|
||||
|
||||
sudo mkdir -p /var/www/apps /var/www/static
|
||||
|
||||
echo "User $username created with sudo privileges & included in docker user group"
|
||||
echo "Apps directory created: /var/www/apps/"
|
||||
echo "Static files directory: /var/www/static/"
|
||||
echo "Next step: Set up auth via SSH keys. Refer to: [https://github.com/elAgala/server-initializer/blob/main/create_ssh_key.md]"
|
||||
}
|
||||
|
||||
create_user
|
||||
echo "Cleaning up..."
|
||||
cd /
|
||||
rm -rf "$TARGET_DIR"
|
||||
echo "Cleanup complete!"
|
||||
|
||||
42
install.sh
Normal file
42
install.sh
Normal file
@@ -0,0 +1,42 @@
|
||||
#!/bin/bash
|
||||
|
||||
source ./user/create_user.sh
|
||||
source ./user/ssh_config.sh
|
||||
source ./web/install_nginx.sh
|
||||
source ./web/setup_ufw.sh
|
||||
source ./docker/install_docker.sh
|
||||
source ./utils/install_vim.sh
|
||||
source ./utils/install_zsh.sh
|
||||
source ./monitoring/install_prometehus.sh
|
||||
|
||||
chmod +x ./user/create_user.sh
|
||||
chmod +x ./user/ssh_config.sh
|
||||
chmod +x ./web/install_nginx.sh
|
||||
chmod +x ./web/setup_ufw.sh
|
||||
chmod +x ./docker/install_docker.sh
|
||||
chmod +x ./utils/install_vim.sh
|
||||
chmod +x ./utils/install_zsh.sh
|
||||
chmod +x ./monitoring/install_prometehus.sh
|
||||
|
||||
if [ -z "$1" ]; then
|
||||
echo "Usage: $0 <username>"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Docker
|
||||
install_docker
|
||||
|
||||
# Web
|
||||
install_nginx
|
||||
setup_ufw
|
||||
|
||||
# User
|
||||
create_user $1
|
||||
config_ssh $1
|
||||
|
||||
# Utils
|
||||
install_vim
|
||||
install_zsh $1
|
||||
|
||||
# Monitoring
|
||||
install_prometehus $1
|
||||
16
monitoring/install_prometehus.sh
Normal file
16
monitoring/install_prometehus.sh
Normal file
@@ -0,0 +1,16 @@
|
||||
#!/bin/bash
|
||||
|
||||
function install_prometehus() {
|
||||
REPO_URL = "https://raw.githubusercontent.com/elAgala/monitoring-template/main"
|
||||
username="$1"
|
||||
monitoring_dir="/home/$username/monitoring"
|
||||
|
||||
echo "[ MONITOR ]: Starting Prometehus setup"
|
||||
mkdir -p "$monitoring_dir"
|
||||
curl -L "$REPO_URL/docker-compose.yml" -o "$monitoring_dir/docker-compose.yml"
|
||||
curl -L "$REPO_URL/prometheus.yml" -o "$monitoring_dir/prometheus.yml"
|
||||
cd "$monitoring_dir"
|
||||
echo "[ MONITOR ]: Prometheus Installed. Starting on docker container"
|
||||
sudo docker-compose up -d
|
||||
echo "[ MONITOR ]: Prometehus up & running on port 9090"
|
||||
}
|
||||
29
setup.sh
29
setup.sh
@@ -1,29 +0,0 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Check for required arguments
|
||||
if [ $# -ne 3 ]; then
|
||||
echo "Usage: $0 <username> <server_ip> <port>"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Extract arguments
|
||||
username="$1"
|
||||
server_ip="$2"
|
||||
port="$3"
|
||||
|
||||
# Script path
|
||||
script_path="index.sh"
|
||||
|
||||
# Transfer the script to the server
|
||||
scp -P $port "$script_path" "$username@$server_ip:/tmp/" || {
|
||||
echo "Error transferring script"
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Execute the script on the server
|
||||
ssh -P $port "$username@$server_ip" "bash /tmp/"$script_path"" || {
|
||||
echo "Error executing script on server"
|
||||
exit 1
|
||||
}
|
||||
|
||||
echo "Script execution completed on $username@$server_ip"
|
||||
19
user/create_user.sh
Normal file
19
user/create_user.sh
Normal file
@@ -0,0 +1,19 @@
|
||||
#!/bin/bash
|
||||
|
||||
function create_user() {
|
||||
username = $1
|
||||
|
||||
echo "[ USER ]: Starting user setup"
|
||||
sudo useradd $username
|
||||
echo "[ USER ]: Set a password for $username:"
|
||||
sudo passwd "$username"
|
||||
echo "[ USER ]: User created succesfully"
|
||||
|
||||
echo "[ USER ]: Adding user to groups"
|
||||
sudo usermod -aG sudo $username
|
||||
sudo usermod -aG www-data $username
|
||||
sudo usermod -aG docker $username
|
||||
echo "[ USER ]: User added to the following groupps (sudo, www-data, docker)"
|
||||
|
||||
echo "[ USER ]: User setup finished"
|
||||
}
|
||||
40
user/ssh_config.sh
Normal file
40
user/ssh_config.sh
Normal file
@@ -0,0 +1,40 @@
|
||||
#!/bin/bash
|
||||
|
||||
function config_ssh() {
|
||||
username = $1
|
||||
|
||||
echo "[ SSH ]: Starting setup"
|
||||
ssh_dir = "/home/$username/.ssh"
|
||||
|
||||
sudo mkdir -p $ssh_dir
|
||||
sudo chmod 700 $ssh_dir
|
||||
|
||||
sudo touch "$ssh_dir/authorized_leys"
|
||||
sudo chmod 600 "$ssh_dir/authorized_leys"
|
||||
sudo chown -R "$username:$username" $ssh_dir
|
||||
echo "[ SSH ]: Created ~/.ssh/authorized_leys"
|
||||
|
||||
echo "[ SSH ]: Paste the public key for $username (leave empty to skip)"
|
||||
read -r public_key
|
||||
if [ -n "$public_key" ]; then
|
||||
echo "$public_key" | sudo tee -a "$ssh_dir/authorized_keys" >/dev/null
|
||||
echo "[ SSH ]: Public key added to $ssh_dir/authorized_keys."
|
||||
else
|
||||
echo "[ SSH ]: No public key provided, skipping..."
|
||||
fi
|
||||
|
||||
echo "[ SSH ]: Disabling root login"
|
||||
sudo sed -i 's/^#PermitRootLogin yes/PermitRootLogin no/' /etc/ssh/sshd_config
|
||||
echo "[ SSH ]: Root login disabled"
|
||||
|
||||
echo "[ SSH ]: Adding $username to allowed users"
|
||||
if grep -q "^AllowUsers" /etc/ssh/sshd_config; then
|
||||
sudo sed -i "s/^AllowUsers.*/& $username/" /etc/ssh/sshd_config
|
||||
else
|
||||
echo "AllowUsers $username" | sudo tee -a /etc/ssh/sshd_config >/dev/null
|
||||
fi
|
||||
echo "[ SSH ]: User added to allowed users"
|
||||
|
||||
sudo systemctl restart sshd
|
||||
echo "[ SSH ]: Finished succesfully!"
|
||||
}
|
||||
9
utils/install_vim.sh
Normal file
9
utils/install_vim.sh
Normal file
@@ -0,0 +1,9 @@
|
||||
#!/bin/bash
|
||||
|
||||
function install_vim() {
|
||||
# TODO: Add .config
|
||||
|
||||
echo "[ UTILS ]: Installing Vim"
|
||||
sudo apt-get install -y vim
|
||||
echo "[ UTILS ]: Vim installed succesfully"
|
||||
}
|
||||
9
utils/install_zsh.sh
Normal file
9
utils/install_zsh.sh
Normal file
@@ -0,0 +1,9 @@
|
||||
#!/bin/bash
|
||||
|
||||
function install_zsh() {
|
||||
username = $1
|
||||
echo "[ UTILS ]: Installing zsh"
|
||||
sudo apt-get install -y zsh
|
||||
sudo chsh -s /usr/bin/zsh "$username"
|
||||
echo "[ UTILS ]: Zsh installed succesfully and set as default shell for $username"
|
||||
}
|
||||
14
web/install_nginx.sh
Normal file
14
web/install_nginx.sh
Normal file
@@ -0,0 +1,14 @@
|
||||
#!/bin/bash
|
||||
|
||||
function install_nginx() {
|
||||
echo "[ WEB ]: Starting NginX setup"
|
||||
if ! dpkg -l | grep -q nginx; then
|
||||
sudo apt update
|
||||
sudo apt install -y nginx
|
||||
sudo systemctl start nginx
|
||||
sudo systemctl enable nginx
|
||||
echo "[ WEB ]: Installed NginX succesfully"
|
||||
else
|
||||
echo "[ WEB ]: NginX already installed, skipping..."
|
||||
fi
|
||||
}
|
||||
10
web/setup_ufw.sh
Normal file
10
web/setup_ufw.sh
Normal file
@@ -0,0 +1,10 @@
|
||||
#!/bin/bash
|
||||
|
||||
function setup_ufw() {
|
||||
echo "[ WEB ]: Started UFW Firewall setup"
|
||||
sudo apt-get install -y ufw
|
||||
sudo ufw allow 22
|
||||
sudo ufw allow 443
|
||||
sudo ufw enable
|
||||
echo "[ WEB ]: UFW Installed succesfully. Open ports SSH:22 - HTTPS:443"
|
||||
}
|
||||
Reference in New Issue
Block a user