0% found this document useful (0 votes)
3 views3 pages

Ubuntu Server Setup with Caddy & Apache

This Bash script sets up a server by validating a network interface, setting a fixed password for the 'ubuntu' user, and modifying SSH configurations. It installs Caddy and Apache2 web servers, configures their directories and settings, and restarts the necessary services. Finally, it provides access information for both web servers and SSH login details.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

Ubuntu Server Setup with Caddy & Apache

This Bash script sets up a server by validating a network interface, setting a fixed password for the 'ubuntu' user, and modifying SSH configurations. It installs Caddy and Apache2 web servers, configures their directories and settings, and restarts the necessary services. Finally, it provides access information for both web servers and SSH login details.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

#!

/bin/bash

set -e # Exit on error

# Validate network interface

INTERFACE="ens33"

if ! ip -4 addr show "$INTERFACE" > /dev/null 2>&1; then

echo "Error: Network interface $INTERFACE not found."

exit 1

fi

# Set fixed password for ubuntu user

echo "ubuntu:root" | chpasswd

echo "Password for ubuntu user set to: root" >&2

# Modify SSH configuration

sed -i 's/^Include.*//' /etc/ssh/ssh_config

sed -i 's/^#\?PasswordAuthentication.*/PasswordAuthentication yes/' /etc/ssh/sshd_config

systemctl restart ssh || { echo "Error: Failed to restart SSH."; exit 1; }

# Add Caddy repository for Ubuntu 18.04

apt-get update

apt-get install -y debian-keyring debian-archive-keyring apt-transport-https curl

curl -1sLf '[Link] | apt-key add -

curl -1sLf '[Link] | tee


/etc/apt/[Link].d/[Link]

apt-get update

# Install Caddy and Apache2

apt-get install -y caddy apache2 || { echo "Error: Failed to install Caddy or Apache2."; exit 1; }

# Set up web directories


mkdir -p /var/www/html/caddy

mkdir -p /var/www/html/apache2

chown caddy:caddy /var/www/html/caddy

chown www-data:www-data /var/www/html/apache2

chmod 755 /var/www/html/caddy /var/www/html/apache2

# Get IP and create index pages

ip_addr=$(ip -4 addr show "$INTERFACE" | grep -oP '(?<=inet\s)\d+(\.\d+){3}')

echo "Server IP (Caddy): $ip_addr" > /var/www/html/caddy/[Link]

echo "Server IP (Apache2): $ip_addr" > /var/www/html/apache2/[Link]

# Configure Caddy

cat <<EOF > /etc/caddy/Caddyfile

:80 {

root * /var/www/html/caddy

file_server

EOF

# Configure Apache2 to listen on port 8080

sed -i 's/Listen 80/Listen 8080/' /etc/apache2/[Link]

cat <<EOF > /etc/apache2/sites-available/[Link]

<VirtualHost *:8080>

ServerAdmin homepage@localhost

DocumentRoot /var/www/html/apache2

ErrorLog \${APACHE_LOG_DIR}/[Link]

CustomLog \${APACHE_LOG_DIR}/[Link] combined

</VirtualHost>

EOF

# Enable Apache2 site and restart services


a2ensite [Link]

systemctl enable apache2

systemctl restart apache2 || { echo "Error: Failed to restart Apache2."; exit 1; }

systemctl enable caddy

systemctl restart caddy || { echo "Error: Failed to restart Caddy."; exit 1; }

echo "Setup complete."

echo "Access Caddy web server at [Link]

echo "Access Apache2 web server at [Link]

echo "SSH into the server with: ssh ubuntu@$ip_addr"

ssh “ubuntu@$ [Link] r ‘’

r”

You might also like