Newer
Older
nc-env / templates / template09-web-server-node / provision.sh
@pmarini pmarini on 13 Apr 2022 7 KB initial commit
#!/bin/bash

timedatectl set-timezone Europe/Madrid

start_time=`date`
  
echo "provisioning started: ${start_time}" 

MACHINE_HOSTNAME=web-server-XX.localenv.com

NEXTCLOUD_USER=usrv

NETWORK_INTERFACE=eth0

############################
## Add the installer archive name here
############################
NEXTCLOUD_INSTALLER_ARCHIVE=

############################
## Database Host
############################
DATABASE_MACHINE_HOSTNAME=

############################
## If the database already exists, set to 1 (this has no effect momentarily)
############################
DATABASE_ALREADY_EXIST=1

############################
## Redis Host
############################
REDIS_MACHINE_HOSTNAME=

############################
## Redis Port
############################
REDIS_MACHINE_PORT=6379

############################
## GlusterFs Host
############################
GLUSTERFS_MACHINE_HOSTNAME=

############################
## GlusterFs Volume
############################
GLUSTERFS_VOLUME=


PHP_INI=/etc/php/7.4/apache2/php.ini

hostnamectl set-hostname ${MACHINE_HOSTNAME}

# Print some information about the container OS
hostnamectl

# Print some information about the container timezone
timedatectl

#####################################################################
## Get the IP address into an environment variable. This command outputs 
## an empty variable if the network interface name is not ${NETWORK_INTERFACE}
#####################################################################
ip_address=`ip -4 addr show ${NETWORK_INTERFACE} | grep -oP '(?<=inet\s)\d+(\.\d+){3}'`

adduser --disabled-password --gecos GECOS ${NEXTCLOUD_USER}
 
usermod -p "`openssl passwd -1  -salt 5RPVAd ${NEXTCLOUD_USER}`" ${NEXTCLOUD_USER}
 
adduser ${NEXTCLOUD_USER} sudo


## Install Nextcloud: https://docs.nextcloud.com/server/stable/admin_manual/installation/example_ubuntu.html

#####
## Step 001
## Check the MD5 of the Nextcloud Installer 


##(cd to the folder to avoid a 'File Not Found' error
cd /vagrant/artifacts

md5sum -c ${NEXTCLOUD_INSTALLER_ARCHIVE}.md5 < ${NEXTCLOUD_INSTALLER_ARCHIVE}

md5sum_exit_status=$?

cd --

echo "Exit status for md5sum check for Nextcloud installer: ${md5sum_exit_status}"

if [ "${md5sum_exit_status}" == "0" ]; then
    echo "Check OK"
else
    echo "Check KO"
    echo "Aborting"
    exit 1
fi

## Install the needed packages from apt repositories
apt update

apt install -y apache2 libapache2-mod-php7.4 imagemagick

apt install -y php7.4-gd php7.4-mysql php7.4-curl php7.4-mbstring php7.4-intl

apt install -y php7.4-gmp php7.4-bcmath php-imagick php7.4-xml php7.4-zip

apt install -y php-redis php-apcu

apt install -y glusterfs-client

apt install -y mysql-client


## Expand the installer archive and move the content to the web server root folder (/var/www)

echo "START - Expand the installer archive and move the content to the web server root folder (/var/www)"

cd /vagrant/artifacts

tar -xjf ${NEXTCLOUD_INSTALLER_ARCHIVE}

cp -r nextcloud /var/www

echo "END - Expand the installer archive and move the content to the web server root folder (/var/www)"

## Apache Web Server Configuration

echo "START - Apache Web Server Configuration"

systemctl stop apache2

cp /vagrant/artifacts/nextcloud.conf /etc/apache2/sites-available/nextcloud.conf

## Putting the machine hostname in the Apache site configuration file (nextcloud.conf)
sed -i "s|#MACHINE_HOSTNAME#|${MACHINE_HOSTNAME}|g" /etc/apache2/sites-available/nextcloud.conf

## Activate nextcloud.conf
a2ensite nextcloud.conf

## Disactivate 000-default.conf
a2dissite 000-default.conf

## For Nextcloud to work correctly, we need the module mod_rewrite. Enable it by running:
a2enmod rewrite

## Additional recommended modules are mod_headers, mod_env, mod_dir and mod_mime:
a2enmod headers

a2enmod env

a2enmod dir

a2enmod mime

a2enmod ssl

chown -R www-data:www-data /var/www/nextcloud/

echo "END - Apache Web Server Configuration"

echo "START - Mounting GlusterFS filesystem"

cp /vagrant/artifacts/data.mount /etc/systemd/system

mkdir -p /data/nextcloud

chown www-data.www-data /data/nextcloud

cp /vagrant/artifacts/data-nextcloud.mount /etc/systemd/system/data-nextcloud.mount

sed -i "s|#GLUSTERFS_MACHINE_HOSTNAME#|${GLUSTERFS_MACHINE_HOSTNAME}|g" /etc/systemd/system/data-nextcloud.mount

sed -i "s|#GLUSTERFS_VOLUME#|${GLUSTERFS_VOLUME}|g" /etc/systemd/system/data-nextcloud.mount

systemctl daemon-reload

systemctl enable data-nextcloud.mount

systemctl start data-nextcloud.mount

systemctl status data-nextcloud.mount

df -hT /data/nextcloud

chown www-data.www-data /data/nextcloud

touch /data/nextcloud/.ocdata

chown www-data.www-data /data/nextcloud/.ocdata

echo "END - Mounting GlusterFS filesystem"

## Installing Nextcloud from CLI (https://docs.nextcloud.com/server/stable/admin_manual/installation/command_line_installation.html)
echo "START - Installing Nextcloud from CLI"

## Create a local wrapper occ command
cp /vagrant/artifacts/occ /usr/local/bin/occ

chmod +x /usr/local/bin/occ

## If it is a node in a cluster, the admin user should have a random part, to avoid an "Already existing" user
admin_usr=admin-`echo ${RANDOM} | md5sum | head -c 4`

occ  maintenance:install 	--database       		"mysql"\
											--database-name  "nextcloud_db"\
											--database-host  	"${DATABASE_MACHINE_HOSTNAME}"\
											--database-user  	"nextcloud_usr"\
											--database-pass  	"nextcloud_usr"\
											--admin-user 	 	"${admin_usr}"\
											--admin-pass 	 	"${admin_usr}"

## Adding the Hostname in the trusted_domains config key
occ config:system:set trusted_domains 1 --value "${MACHINE_HOSTNAME}"

echo "END - Installing Nextcloud from CLI"

## Tweaking some Nextcloud settings
echo "START - Tweaking some Nextcloud settings"

## Increasing PHP memory_limit to 512M
sed -i "s|memory_limit =.*|memory_limit = 512M|g" "$PHP_INI"

## Setting up pretty URLs
occ config:system:set htaccess.RewriteBase --value '/'

occ config:system:set overwrite.cli.url --value "https://${MACHINE_HOSTNAME}"

occ maintenance:update:htaccess

## Order is important here. The 'redis' array must be configured first.
occ config:system:set redis host --value "${REDIS_MACHINE_HOSTNAME}" 

occ config:system:set redis port --value "${REDIS_MACHINE_PORT}"

#https://help.nextcloud.com/t/occ-wont-run-with-memcache-apcu/119724
sudo -u www-data php --define apc.enable_cli=1 /var/www/nextcloud/occ config:system:set memcache.local --value '\OC\Memcache\APCu'

sudo -u www-data php --define apc.enable_cli=1 /var/www/nextcloud/occ config:system:set memcache.distributed --value '\OC\Memcache\Redis'

sudo -u www-data php --define apc.enable_cli=1 /var/www/nextcloud/occ config:system:set memcache.locking --value '\OC\Memcache\Redis'

sudo -u www-data php --define apc.enable_cli=1 /var/www/nextcloud/occ config:system:set filelocking.enabled --value 'true'

sudo -u www-data php --define apc.enable_cli=1 /var/www/nextcloud/occ config:system:set datadirectory --value '/data/nextcloud'

echo "END - Tweaking some Nextcloud settings"

systemctl restart apache2

systemctl status apache2

## Adding system user to www-data group (to ease command line operations)
adduser ${NEXTCLOUD_USER} www-data

echo "START - Cleanup"

rm -rf /vagrant/artifacts/nextcloud

echo "END - Cleanup"

end_time=`date`

echo "This container has IP (interface: ${NETWORK_INTERFACE}): ${ip_address}"

echo "If you add this IP to the hostname (${MACHINE_HOSTNAME}) in your hosts file:"

echo "	You can connect with user ${NEXTCLOUD_USER} via ssh (password ${NEXTCLOUD_USER}): ssh ${NEXTCLOUD_USER}@${MACHINE_HOSTNAME}"

echo "	You can connect to this Nextcloud instance with the following URL: http://${MACHINE_HOSTNAME}/nextcloud. Login with ${admin_usr}/$admin_usr}"

echo "provisioning started: ${start_time}" 
 
echo "provisioning ended: ${end_time}"