Skip to content

Instantly share code, notes, and snippets.

@summersab
Last active August 16, 2021 18:01
Show Gist options
  • Save summersab/18dc6543015a8e1c39402023a092d665 to your computer and use it in GitHub Desktop.
Save summersab/18dc6543015a8e1c39402023a092d665 to your computer and use it in GitHub Desktop.
Quick and dirty Nextcloud installation
#!/bin/bash
# Change these to suit
export DBUSER=nextcloud
export DBPASS=
# Install MariaDB
apt-get update
apt-get install mariadb-server
# Allow remote access to the server
sed -i "s/^bind-address *=.*/bind-address = 0.0.0.0/g" /etc/mysql/mariadb.conf.d/50-server.cnf
# Create the default user and grant permissions
mysql -e "CREATE USER '$DBUSER'@'' IDENTIFIED BY '$DBPASS';"
mysql -e "GRANT ALL PRIVILEGES ON *.* TO '$DBUSER'@'' IDENTIFIED BY '$DBPASS';"
# I'm not ENTIRELY sure if this line is necessary. I think it is
mysql -e "UPDATE mysql.user SET Grant_priv = 'Y' WHERE User = '$DBUSER';"
mysql -e "FLUSH PRIVILEGES;"
service mysql restart
#!/bin/bash
# Change these to suit
export DBNAME=nextcloud
export DBUSER=nextcloud
export DBPASS=
export DBHOST=
export ADMINLOGIN=nextcloud
export ADMINPASS=
# Variables to be used in various places throughout the script
export PHP_MEMORY_LIMIT=512M
export PHP_POST_LIMIT=0
export PHP_UPLOAD_LIMIT=10G
export OPCACHE_INTERNED_STRINGS_BUFFER=16
export OPCACHE_MAX_ACCELERATED_FILES=20000
export OPCACHE_MEMORY_CONSUMPTION=256
export OPCACHE_SAVE_COMMENTS=1
export OPCACHE_REVALIDATE_FREQ=1
export OPCACHE_FAST_SHUTDOWN=1
export OPCACHE_JIT=1255
export OPCACHE_JIT_BUFFER_SIZE=256M
apt update
# This sets up the repository for PHP 8.0
apt install -y lsb-release apt-transport-https ca-certificates wget
wget -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg
echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" | tee /etc/apt/sources.list.d/php.list
# Install PHP, Apache, and all of the other packages required for NC
apt update
apt install -y vim curl apache2 imagemagick php php-mysql php-curl php-mbstring php-zip php-gd php-xml php-imagick php-intl php-json php-gmp php-bcmath php-apcu vim
apt upgrade -y
apt-get clean
# This enables some Apache modules that are required for NC
a2enmod rewrite headers env dir mime setenvif
# Change a few PHP settings
sed -i "s/^memory_limit *=.*/memory_limit = ${PHP_MEMORY_LIMIT}/g" /etc/php/$(php -v | grep --only-matching --perl-regexp "(PHP )\d+\.\\d+\.\\d+" | cut -c 5-7)/apache2/php.ini
sed -i "s/^post_max_size *=.*/post_max_size = ${PHP_POST_LIMIT}/g" /etc/php/$(php -v | grep --only-matching --perl-regexp "(PHP )\d+\.\\d+\.\\d+" | cut -c 5-7)/apache2/php.ini
sed -i "s/^upload_max_filesize *=.*/upload_max_filesize = ${PHP_UPLOAD_LIMIT}/g" /etc/php/$(php -v | grep --only-matching --perl-regexp "(PHP )\d+\.\\d+\.\\d+" | cut -c 5-7)/apache2/php.ini
sed -i "s/^output_buffering *=.*/output_buffering = Off/g" /etc/php/$(php -v | grep --only-matching --perl-regexp "(PHP )\d+\.\\d+\.\\d+" | cut -c 5-7)/apache2/php.ini
# PHP 8.0 introduces a JIT compiler (makes things go "vroom!") This creates a file to enable it
cat > /etc/php/8.0/apache2/conf.d/opcache-recommended.ini << EOF
opcache.enable=1
opcache.interned_strings_buffer=${OPCACHE_INTERNED_STRINGS_BUFFER}
opcache.max_accelerated_files=${OPCACHE_MAX_ACCELERATED_FILES}
opcache.memory_consumption=${OPCACHE_MEMORY_CONSUMPTION}
opcache.save_comments=${OPCACHE_SAVE_COMMENTS}
opcache.revalidate_freq=${OPCACHE_REVALIDATE_FREQ}
opcache.fast_shutdown=${OPCACHE_FAST_SHUTDOWN}
opcache.jit=${OPCACHE_JIT}
opcache.jit_buffer_size=${OPCACHE_JIT_BUFFER_SIZE}
EOF
# Overwrite the default Apache site config with a config for NC (may need to be edited later)
cat > /etc/apache2/sites-enabled/000-default.conf << EOF
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/nextcloud
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<IfModule mod_headers.c>
Header always set Strict-Transport-Security "max-age=15552000; includeSubDomains"
</IfModule>
</VirtualHost>
<Directory /var/www/nextcloud/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
EOF
# Download NC and install it to the webroot
cd /var/www
curl -sLO https://download.nextcloud.com/server/releases/latest.tar.bz2
tar xf latest.tar.bz2
rm -r /var/www/html latest.tar.bz2
# This creates an autoconfig file to automate the setup of NC
cat > /var/www/nextcloud/config/autoconfig.php << EOF
<?php
\$AUTOCONFIG = array(
'dbtype' => 'mysql',
'dbname' => '${DBNAME}',
'dbuser' => '${DBUSER}',
'dbpass' => '${DBPASS}',
'dbhost' => '${DBHOST}',
'dbtableprefix' => 'oc_',
'adminlogin' => '${ADMINLOGIN}',
'adminpass' => '${ADMINPASS}',
'directory' => '/var/www/nextcloud/data',
);
EOF
# Make sure the files in the webroot are owned by the webserver (Apache2) and restart the service
chown -R www-data:www-data /var/www
service apache2 restart
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment