#!/bin/bash
######系統配合
#下載ISO檔
#https://ftp.ubuntu-tw.org/ubuntu-releases/22.04/ 安裝
######鄭亮星 jls@mail.jls.idv.tw , jls550225@gmail.com , jls@acfs.tw
##安裝 nextcloud 雲端
ETH0=$(ls /proc/net/dev_snmp6 |awk '{ print $1 }' |sed -n '1p')
ETH1=$(ls /proc/net/dev_snmp6 |awk '{ print $1 }' |sed -n '2p')
IP=$(ifconfig $ETH0 |grep 'inet ' |awk '{print $2}')
DOMAINNAME=$(/bin/hostname -d)
DATE=$(date "+%Y%m%d%H")
sync
apt -y install imagemagick php-imagick php-fpm php-curl php-bz2 php-intl php-bcmath php-gmp ffmpeg redis-server php-redis
### NextCloud 各個版本下載處 https://download.nextcloud.com/server/releases
#wget https://download.nextcloud.com/server/releases/nextcloud-26.0.1.zip
unzip nextcloud-26.0.1.zip -d /var/www/
ocpath='/var/www/nextcloud/'
htuser='www-data'
htgroup='www-data'
rootuser='root'
mkdir -p $ocpath/data
mkdir -p $ocpath/assets
mkdir -p $ocpath/updater
find ${ocpath}/ -type f -print0 | xargs -0 chmod 0640
find ${ocpath}/ -type d -print0 | xargs -0 chmod 0750
chown -R ${rootuser}:${htgroup} ${ocpath}/
chown -R ${htuser}:${htgroup} ${ocpath}/apps/
chown -R ${htuser}:${htgroup} ${ocpath}/assets/
chown -R ${htuser}:${htgroup} ${ocpath}/config/
chown -R ${htuser}:${htgroup} ${ocpath}/data/
chown -R ${htuser}:${htgroup} ${ocpath}/themes/
chown -R ${htuser}:${htgroup} ${ocpath}/updater/
chmod +x ${ocpath}/occ
if [ -f ${ocpath}/.htaccess ]
then
chmod 0644 ${ocpath}/.htaccess
chown ${rootuser}:${htgroup} ${ocpath}/.htaccess
fi
if [ -f ${ocpath}/data/.htaccess ]
then
chmod 0644 ${ocpath}/data/.htaccess
chown ${rootuser}:${htgroup} ${ocpath}/data/.htaccess
fi
##### PHP 設定
cp -arf /etc/php/8.1/apache2/php.ini /etc/php/8.1/apache2/php.ini-$DATE
#find /etc/php/8.1/apache2/php.ini -type f -exec perl -i -pe s/"session.auto_start = 1"/"session.auto_start = 0"/g {} \;
find /etc/php/8.1/apache2/php.ini -type f -exec perl -i -pe s/"output_buffering = 4096"/"output_buffering = Off"/g {} \;
cat >> /etc/php/8.1/apache2/php.ini << EOF
[opcache]
opcache.enable = 1
opcache.enable_cli = 1
opcache.memory_consumption = 512
opcache.interned_strings_buffer = 16
opcache.max_accelerated_files = 10000
opcache.revalidate_freq = 1
opcache.save_comments = 1
EOF
##### Apache 設定
cp -arf /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/000-default.conf-$DATE
cat > /etc/apache2/sites-available/000-default.conf << EOF
#------------------------------------------------------------------------------------------------------------------------
ServerAdmin root@$DOMAINNAME
DocumentRoot /var/www/nextcloud
ServerName nextcloud.$DOMAINNAME
Require all granted
Options FollowSymlinks MultiViews
AllowOverride All
Dav off
#
#### From the Apache version 2.4.10 and above, use the SetHandler to run PHP as a fastCGI process server
# SetHandler "proxy:unix:/run/php/php7.4-fpm.sock|fcgi://localhost"
#
# CBandSpeed 40960 200 200
# CBandRemoteSpeed 40960kb/s 100 100
# CBandLimit 10000Mb
# CBandPeriod 4W
# CBandScoreboard /var/log/scoreboard/nextcloud.$DOMAINNAME
# CBandScoreFlushPeriod /var/log/scoreboard/nextcloud.$DOMAINNAME
# CBandExceededURL http://$DOMAINNAME/cband_error.html
SetEnv HOME /var/www/nextcloud
SetEnv HTTP_HOME /var/www/nextcloud
Satisfy Any
RewriteEngine On
RewriteRule ^/\.well-known/carddav http://$IP/remote.php/dav/ [R=301,L]
RewriteRule ^/\.well-known/caldav http://$IP/remote.php/dav/ [R=301,L]
Header always set Strict-Transport-Security: "max-age=63072000; includeSubDomains; preload"
EOF
cp -arf /etc/apache2/sites-available/http_80.conf /etc/apache2/sites-available/http_80.conf-$DATE
cat >> /etc/apache2/sites-available/http_80.conf << EOF
#------------------------------------------------------------------------------------------------------------------------
ServerAdmin root@$DOMAINNAME
DocumentRoot /var/www/nextcloud
ServerName nextcloud.$DOMAINNAME
Require all granted
Options FollowSymlinks MultiViews
AllowOverride All
Dav off
#
#### From the Apache version 2.4.10 and above, use the SetHandler to run PHP as a fastCGI process server
# SetHandler "proxy:unix:/run/php/php7.4-fpm.sock|fcgi://localhost"
#
# CBandSpeed 40960 200 200
# CBandRemoteSpeed 40960kb/s 100 100
# CBandLimit 10000Mb
# CBandPeriod 4W
# CBandScoreboard /var/log/scoreboard/nextcloud.$DOMAINNAME
# CBandScoreFlushPeriod /var/log/scoreboard/nextcloud.$DOMAINNAME
# CBandExceededURL http://$DOMAINNAME/cband_error.html
SetEnv HOME /var/www/nextcloud
SetEnv HTTP_HOME /var/www/nextcloud
Satisfy Any
RewriteEngine On
RewriteRule ^/\.well-known/carddav http://nextcloud.$DOMAINNAME/remote.php/dav/ [R=301,L]
RewriteRule ^/\.well-known/caldav http://nextcloud.$DOMAINNAME/remote.php/dav/ [R=301,L]
Header always set Strict-Transport-Security: "max-age=63072000; includeSubDomains; preload"
EOF
cp -arf /etc/apache2/sites-available/https_443.conf /etc/apache2/sites-available/https_443.conf-$DATE
cat >> /etc/apache2/sites-available/https_443.conf << EOF
#------------------------------------------------------------------------------------------------------------------------
ServerAdmin root@$DOMAINNAME
DocumentRoot /var/www/nextcloud
ServerName nextcloud.$DOMAINNAME
Require all granted
Options FollowSymlinks MultiViews
AllowOverride All
Dav off
#
#### From the Apache version 2.4.10 and above, use the SetHandler to run PHP as a fastCGI process server
# SetHandler "proxy:unix:/run/php/php7.4-fpm.sock|fcgi://localhost"
#
# CBandSpeed 40960 200 200
# CBandRemoteSpeed 40960kb/s 100 100
# CBandLimit 10000Mb
# CBandPeriod 4W
# CBandScoreboard /var/log/scoreboard/nextcloud.$DOMAINNAME
# CBandScoreFlushPeriod /var/log/scoreboard/nextcloud.$DOMAINNAME
# CBandExceededURL https://$DOMAINNAME/cband_error.html
SetEnv HOME /var/www/nextcloud
SetEnv HTTP_HOME /var/www/nextcloud
Satisfy Any
RewriteEngine On
RewriteRule ^/\.well-known/carddav https://nextcloud.$DOMAINNAME/remote.php/dav/ [R=301,L]
RewriteRule ^/\.well-known/caldav https://nextcloud.$DOMAINNAME/remote.php/dav/ [R=301,L]
Header always set Strict-Transport-Security: "max-age=63072000; includeSubDomains; preload"
EOF
cp -arf /etc/redis/redis.conf /etc/redis/redis.conf-$DATE
/usr/bin/sed -i '/^# unixsocketperm 700/iunixsocket /var/run/redis/redis-server.sock\nunixsocketperm 770' /etc/redis/redis.conf
echo "vm.overcommit_memory = 1" >> /etc/sysctl.conf
sysctl -p
echo never > /sys/kernel/mm/transparent_hugepage/enabled
usermod -aG redis www-data
a2enmod headers env dir mime rewrite
systemctl restart redis-server.service
systemctl restart named.service
systemctl restart apache2.service
clear
while [ "$yn" != "yes" -a "$yn" != "YES" ]
do
echo "請開啟瀏覽器,在網址列輸入 http://$IP"
read -p "待完成一些設定後,再輸入 yes 或 YES,繼續執行:" yn
clear
done
sudo -u www-data php /var/www/nextcloud/occ db:add-missing-indices
cp -arf /var/www/nextcloud/config/config.php /var/www/nextcloud/config/config.php-$DATE
sed -i '$d' /var/www/nextcloud/config/config.php
cat >> /var/www/nextcloud/config/config.php << EOF
'htaccess.RewriteBase' => '/',
'default_language' => 'zh_TW',
'default_locale' => 'zh_Hant_TW',
'default_phone_region' => 'TW',
'memcache.distributed' => '\OC\Memcache\Redis',
'memcache.locking' => '\OC\Memcache\Redis',
'memcache.local' => '\OC\Memcache\Redis',
'redis' => array(
'host' => 'localhost',
'port' => 6379,
),
);
EOF
chown www-data: /var/www/nextcloud/.htaccess
sudo -u www-data php /var/www/nextcloud/occ maintenance:update:htaccess
sudo -u www-data php -d memory_limit=512M /var/www/nextcloud/occ app:install richdocumentscode
systemctl restart named.service
systemctl restart php8.1-fpm.service
systemctl restart apache2.service
#####安裝 OxOFFICE 來搭配 NextCloud 服務
#wget --no-check-certificate https://github.com/nextcloud/richdocuments/releases/download/v4.1.1/richdocuments.tar.gz
#tar zxvf richdocuments.tar.gz -C /var/www/nextcloud/apps/
#sudo -u www-data php /var/www/nextcloud/occ config:app:set richdocuments wopi_url --value="https://nextcloud.$DOMAINNAME"