Skip to content

📜 Apache web server in cPouta script

Summary
This script will create a local SSH key pair and a Linux instance in the cPouta environment. It will then upgrade the Linux, install the Apache2 web server, add the SSH key pair, create HTTP and SSH security groups, add a public IP and enable SSH and HTTP access to the server. Finally, it will print out instructions on how to test the installation and connect the machine.
Internet
https://a3s.fi/cgi/webserver.sh

Code

Disclaimer of Warranty

The software is provided "as is", without warranty of any kind, express or implied, including but not limited to the warranties of merchantability, fitness for a particular purpose and noninfringement. In no event shall the authors or copyright holders be liable for any claim, damages or other liability, whether in an action of contract, tort or otherwise, arising from, out of or in connection with the software or the use or other dealings in the software.

#!/bin/bash

arg="$1"

arg="${arg:-default}"

case "$arg" in
    ubuntu|default)
       IMAGE="Ubuntu-24.04"
       cat << EOF > /tmp/init.sh
#!/bin/bash
apt-get update && apt-get upgrade -y
apt-get install apache2 -y
EOF
        ;;
    alma|centos)
        if [ "$arg" = "alma" ]; then
            IMAGE="AlmaLinux-9"
        else
            IMAGE="CentOS-9-Stream"
        fi
        cat << EOF > /tmp/init.sh
#!/bin/bash
yum update -y
dnf install httpd -y
systemctl start httpd
systemctl enable httpd
EOF
        ;;
    *)
        echo "Invalid option: $arg"
        echo "Valid options are: ubuntu, centos, alma, or leave empty for default"
        exit 2
        ;;
esac

INSTANCE="$(echo "${IMAGE%%-*}" | tr '[:upper:]' '[:lower:]')-apache-webserver"

# Check that we are authenticated with an OpenStack environment
openstack token issue &> /dev/null
if [ $? -ne 0 ]; then
    echo "No valid authentication with cPouta found. Source OpenStack RC File to log in."
    exit 1
fi

# Generate an SSH key pair
ssh-keygen -t rsa -N "" -f $INSTANCE
openstack keypair create --public-key ${INSTANCE}.pub ${INSTANCE}-key

# Create the instance
openstack server create --flavor standard.tiny --image $IMAGE --key-name ${INSTANCE}-key --user-data /tmp/init.sh $INSTANCE

# Create and attach HTTP rule to the instance
openstack security group list -f value -c Name | grep -qw "^HTTP$"
if [ $? -eq 0 ]; then
    echo "Security group HTTP exists."
else
    echo "Creating security group HTTP."
    openstack security group create HTTP
    openstack security group rule create --proto tcp --remote-ip 0.0.0.0/0 --dst-port 80 HTTP
fi
openstack server add security group $INSTANCE HTTP

# Create and attach SSH rule to the instance
openstack security group list -f value -c Name | grep -qw "^SSH$"
if [ $? -eq 0 ]; then
    echo "Security group SSH exists."
else
    echo "Creating security group SSH."
    openstack security group create SSH
    openstack security group rule create --proto tcp --remote-ip 0.0.0.0/0 --dst-port 22 SSH
fi
openstack server add security group $INSTANCE SSH

# Assign a public floating IP
FLOATING_IP=$(openstack floating ip create public --format value --column floating_ip_address)
openstack server add floating ip $INSTANCE $FLOATING_IP

echo "Finishing installation of $IMAGE may take some time, please wait"
echo "You can connect the virtual machine with command: ssh -i $INSTANCE ${INSTANCE%%-*}@$FLOATING_IP"
echo "The apache installation can be tested with command: curl $FLOATING_IP"
echo "Or you can check the logs and status of the servers at https://pouta.csc.fi"