Bash script

Home

Server Set Up

The bash script at the bottom of this page can be copied and pasted into the user data section of the EC2 launch process.

Notes on the Bash script:

#!/bin/bash

# After launching an instance with this script, the script will be stored at:
# /var/lib/cloud/instances/i-<Instance ID>/user-data.txt
# Substitute <Instance ID> with the actual Instance ID. 

set -x #echos commands to output

{

# I chose an existing key pair

# Create Security Group. Allow SSH (from anywhere), HTTPS, HTTP

apt-get update
echo -e "******************************************************** \n"

apt-get -y upgrade
echo -e "******************************************************** \n"

# this includes sqlite. Forget for now
# apt-get -y install apache2 php libapache2-mod-php sqlite3 php8.3-sqlite3



apt-get -y install apache2 php libapache2-mod-php
echo -e "******************************************************** \n"

# this might be necessary so apache restarts after rebooting server
# could you use apache2 instead of httpd as with the reload command below?
# systemctl enable httpd

mkdir -p /var/www/stevespwas.org.uk/public_html
echo -e "******************************************************** \n"

echo "<html><head><title>Steve's PWAs</title></head><body><h1>Steve's PWAs</h1><?php echo '<p>From PHP!!!</p>'; ?></body></html>" > /var/www/stevespwas.org.uk/public_html/index.php
echo -e "******************************************************** \n"

chown -R ubuntu:ubuntu /var/www
echo -e "******************************************************** \n"

cat << EOF > /etc/apache2/sites-available/stevespwas.org.uk.conf
<VirtualHost *:80>
ServerName stevespwas.org.uk
ServerAlias www.stevespwas.org.uk
DocumentRoot /var/www/stevespwas.org.uk/public_html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
EOF
echo -e "******************************************************** \n"

a2ensite stevespwas.org.uk.conf
echo -e "******************************************************** \n"

a2dissite 000-default.conf
echo -e "******************************************************** \n"

systemctl restart apache2
echo -e "******************************************************** \n"

# Use snap not apt as apt version of certbot may be old
# The snap version of certbot obviates need for...
# ...python3-certbot-apache
# snap is already on ubuntu but update the core
snap install core && sudo snap refresh core
echo -e "******************************************************** \n"

# Install certbot. Agree to t&cs when you use it after launch
snap install --classic certbot
echo -e "******************************************************** \n"

# Make certbot command available
ln -s /snap/bin/certbot /usr/bin/certbot
echo -e "******************************************************** \n"

# After this bash script has run and the instance is running:
# 1. Check http://IpV-4 works
# 2. SSH into instance and vim launch-script-output.txt
# 3. Configure a domain name's DNS records to point to this server
# 4. Check http://domain-name works
# 5. SSH into instance and run: certbot --apache
# 6. Check https://domain-name works

} &> /home/ubuntu/launch-script-output.txt