Published January 01, 2019 by

Shell Script for Install NextCloud in Ubuntu

This post shows you how to install NextCloud on Ubuntu 16.04 LTS with Apache2, MySQL, and PHP. NextCloud is a fork of OwnCloud. Like DropBox and other cloud storage services, NextCloud and OwnCloud provide similar functions and unlike the others, they both are free to use.
This guide will show you how to install and set up Nextcloud on Ubuntu using a single shell script.

Before Install nextcloud make sure you are properly pointed DNS record with your IP address. 

Install NextCloud

What this script does:
  1. Install Apache Web server
  2. Install MySQL
  3. Install PHP
  4. Install Redis Server
  5. Install Nextcloud
  6. Install SSL certificate for your nextcloud server
For Install Nextcloud Create a file named
 sudo vim  
Insert Below script in a file and save.
 # NextCloud Installation Script for Ubuntu  
 # with SSL certificate provided by Let's Encrypt (  
 # Author: Subhash (  
 # Check if running as root  
 if [ "$(id -u)" != "0" ]; then  
   echo "This script must be run as root" 1>&2  
   exit 1  
 datapath='/myData' # Path where user data is stored  
 read -p 'nextcloud_url [xx.xx or xx.xx.xx]: ' nextcloud_url  
 read -p 'nextcloud_version [x.x.x]: ' nextcloud_version  
 read -p 'letsencrypt_email [xx@xx.xx]: ' letsencrypt_email  
 read -p 'db_root_password [secretpasswd]: ' db_root_password  
 read -p 'db_user_password [passwd]: ' db_user_password  
 # Check All variable have a value  
 if [ -z $nextcloud_url ]|| [ -z $nextcloud_version ] || [ -z $letsencrypt_email ]|| [ -z $db_root_password ] || [ -z $db_user_password ]  
    echo run script again please insert all value. do not miss any value  
 # Installation start  
 ocpath='/var/www/html' # Path where NextCloud is installed  
 htuser='www-data' # User Apache runs as  
 htgroup='www-data' # Group Apache runs as  
 # Update System  
 apt-get update  
 # Install Apache  
 apt-get install apache2 -y  
 # Disable directory listing  
 sudo sed -i "s/Options Indexes FollowSymLinks/Options FollowSymLinks/" /etc/apache2/apache2.conf  
 # Install PHP  
 sudo apt-get install php libapache2-mod-php php-common libapache2-mod-php php-mbstring php-xmlrpc php-soap php-apcu php-smbclient php-ldap php-redis php-gd php-xml php-intl php-json php-imagick php-mysql php-cli php-ldap php-zip php-curl -y  
 # Install Redis  
 apt-get install redis-server -y  
 # Install MySQL database server  
 export DEBIAN_FRONTEND="noninteractive"  
 debconf-set-selections <<< "mysql-server mysql-server/root_password password $db_root_password"  
 debconf-set-selections <<< "mysql-server mysql-server/root_password_again password $db_root_password"  
 apt-get install mysql-server php-mysql -y  
 # Enable Apache extensions  
 a2enmod proxy_fcgi setenvif  
 a2enconf php-fpm  
 service apache2 reload  
 a2enmod rewrite  
 service apache2 reload  
 # Download Nextcloud and move to web directory  
 apt-get install unzip -y  
 unzip nextcloud-$  
 cd nextcloud  
 mv * $ocpath  
 mv .* $ocpath   
 # Delete Downloaded File  
 cd ..  
 rm -rf nextcloud nextcloud-$  
 # Create data directory  
 mkdir -p $datapath  
 # Set file and folder permissions  
 printf "Creating possible missing Directories\n"  
 mkdir -p $ocpath/data  
 mkdir -p $ocpath/assets  
 mkdir -p $ocpath/updater  
 printf "chmod Files and Directories\n"  
 find ${ocpath}/ -type f -print0 | xargs -0 chmod 0640  
 find ${ocpath}/ -type d -print0 | xargs -0 chmod 0750  
 printf "chown Directories\n"  
 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} ${datapath}/  
 chown -R ${htuser}:${htgroup} ${ocpath}/themes/  
 chown -R ${htuser}:${htgroup} ${ocpath}/updater/  
 chown -R ${htuser}:${htgroup} /tmp  
 chmod +x ${ocpath}/occ  
 printf "chmod/chown .htaccess\n"  
 if [ -f ${ocpath}/.htaccess ]  
  chmod 0644 ${ocpath}/.htaccess  
  chown ${rootuser}:${htgroup} ${ocpath}/.htaccess  
 if [ -f ${ocpath}/data/.htaccess ]  
  chmod 0644 ${ocpath}/data/.htaccess  
  chown ${rootuser}:${htgroup} ${ocpath}/data/.htaccess  
 # Configure Apache  
 touch /etc/apache2/sites-available/nextcloud.conf  
 printf "<VirtualHost *:80>\n\nServerName ${nextcloud_url}\nAlias /nextcloud "/var/www/html/"\n\n<Directory /var/www/html/>\n Options +FollowSymlinks\n AllowOverride All\n\n<IfModule mod_dav.c>\n Dav off\n</IfModule>\n\nSetEnv HOME /var/www/html\nSetEnv HTTP_HOME /var/www/html\n\n</Directory>\n\n</VirtualHost>" > /etc/apache2/sites-available/nextcloud.conf  
 ln -s /etc/apache2/sites-available/nextcloud.conf /etc/apache2/sites-enabled/nextcloud.conf  
 a2enmod headers  
 a2enmod env  
 a2enmod dir  
 a2enmod mime  
 service apache2 reload  
 # Configure MySQL database  
 mysql -uroot -p$db_root_password <<QUERY_INPUT  
 CREATE DATABASE nextcloud;  
 CREATE USER 'nextclouduser'@'localhost' IDENTIFIED BY '$db_user_password';  
 GRANT ALL PRIVILEGES ON nextcloud.* TO nextclouduser@localhost;  
 # Enable NextCloud cron job every 15 minutes  
 crontab -u www-data -l > cron  
 echo "*/15 * * * * php -f /var/www/html/cron.php" >> cron  
 crontab -u www-data cron  
 rm cron  
 # Enable HTTPS with Let's Encrypt SSL Certificate   
 apt-get install git -y  
 cd /etc  
 git clone  
 cd certbot  
 ./letsencrypt-auto --non-interactive --agree-tos --email $letsencrypt_email --apache -d $nextcloud_url --hsts  
 printf "<VirtualHost *:80>\n   ServerName $nextcloud_url\n   Redirect / https://$nextcloud_url/\n</VirtualHost>" > /etc/apache2/sites-enabled/nextcloud.conf  
 service apache2 reload  
 # Set up cron job for certificate auto-renewal every 90 days  
 crontab -l > cron  
 echo "* 1 * * 1 /etc/certbot/certbot-auto renew --quiet" >> cron  
 crontab cron  
 rm cron  
 # Install complete  
 printf "\n\nInstallation complete.\nNavigate to http://$nextcloud_url in a web browser to complete the setup wizard, before you run the optimization script.\n\n"  

Give execute permission to file
 sudo chmod +x  

Finally, now run the file
 sudo ./  

After successfully script execute, Go to a browser and hit http://localhost/ OR http://your_domain_name

Please follow the wizard carefully.

Click Finish setup and you’re done.

After Successfully configure Nextcloud you need to performance tuning and optimize your nextcloud server. For this run below script.
 # NextCloud Optimization Script  
 # with PHP Opcache and Redis Memcache  
 # Important: Do not run until the setup wizard in your browser is complete (has initialized the config.php file).  
 # Author: Subhash (  
 upload_max_filesize=4G # Largest filesize users may upload through the web interface  
 post_max_size=4G # Same as above  
 memory_limit=512M # Amount of memory NextCloud may consume  
 datapath='/data' # Path where user data is stored  
 dirpath='/var/www/html' # Path where NextCloud is installed  
 # Check if running as root  
 if [ "$(id -u)" != "0" ]; then  
   echo "This script must be run as root" 1>&2  
   exit 1  
 # Server Stop  
 service apache2 stop  
 # Enable PHP Opcache  
 cd /etc/php/7.2/apache2/  
 rm php.ini  
 # Enable Redis memory caching  
 sed -i '$i'"'"'memcache.local'"'"' => '"'"'\\OC\\Memcache\\Redis'"'"',''' ${dirpath}/config/config.php  
 sed -i '$i'"'"'memcache.locking'"'"' => '"'"'\\OC\\Memcache\\Redis'"'"',''' ${dirpath}/config/config.php  
 sed -i '$i'"'"'redis'"'"' => array('"\n""'"'host'"'"' => '"'"'localhost'"'"','"\n""'"'port'"'"' => 6379,'"\n"'),''' ${dirpath}/config/config.php  
 # Change the upload cache directory  
 # Makes it easier to exclude cache from rsync-style backups  
 sed -i '$i'"'"'cache_path'"'"' => '"'"${datapath}'/cache'"'"',''' ${dirpath}/config/config.php  
 # Change the PHP upload and memory limits  
 for key in upload_max_filesize post_max_size memory_limit  
 sed -i "s/^\($key\).*/\1=$(eval echo \${$key})/" ${dirpath}/.user.ini  
 # Server Start  
 service apache2 start  
 # Run OCC for DB optimization  
 cd /var/www/html/  
 echo y | sudo -u www-data php occ db:convert-filecache-bigint  
 printf "\n\nOptimization complete."  

Read More