Install NextCloud From Command Line

There are various methods for setting up Nextcloud on Linux. Here, we will describe, how to Install NextCloud from the command line On Ubuntu 22.04. We used to perform the entire Nextcloud installation manually.

Preparing the server environment for installation comes first, secondly, the web interface is used to complete the setup. We can now install Nextcloud completely using the command line.

Many sysadmins like this method since it is very convenient for automatic installation with any automation system.


In this guide, we’ll demonstrate how to install NextCloud from the command line on Ubuntu 22.04. What will we do? In essence, we will run a few commands to finish the Nextcloud initial setups rather than using the web-based setup.


The steps we are going to follow:

1. Install PHP and MySQL packages
2. Configure MySQL Server
3. Run the Nextcloud Setup From the Command Line.

1. Install Packages

1. Update and Upgrade the Ubuntu Packages

# apt update && apt upgrade

2. install Apache and MySQL Server

# apt install apache2 mariadb-server 

3. Install PHP and other Dependencies and Restart Apache

# apt install libapache2-mod-php php-bz2 php-gd php-mysql php-curl php-mbstring php-imagick php-zip php-ctype php-curl php-dom php-json php-posix php-bcmath php-xml php-intl php-gmp zip unzip wget

4. Enable required Apache modules and restart Apache:

# a2enmod rewrite dir mime env headers
# systemctl restart apache2

2. Configure MySQL Server

1. Login to MySQL Prompt, Just type

# mysql

2. Create MySQL Database and User for Nextcloud and Provide Permissions.

CREATE USER 'nextcloud'@'localhost' IDENTIFIED BY 'passw@rd';
CREATE DATABASE IF NOT EXISTS nextcloud CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
GRANT ALL PRIVILEGES ON nextcloud.* TO 'nextcloud'@'localhost';
FLUSH PRIVILEGES;
quit;

3. Download and Install Nextcloud

Now download the latest Nextcloud archive file, Go to the Nextcloud Download Page. Or you can download from this direct link: https://download.nextcloud.com/server/releases/latest.zip


1. Download and unzip at the web root (/var/www/html) folder

# cd /var/www/html
# wget https://download.nextcloud.com/server/releases/latest.zip
# unzip latest.zip

2. Move all nextcloud content to the web root (/var/www/html) folder

# cd /var/www/html/nextcloud
# mv * .* ../

3. Remove empty nextcloud directory

# rmdir /var/www/html/nextcloud

4. Change the ownership of the nextcloud content directory to the HTTP user.

# chown -R www-data:www-data /var/www/html

5. Run the nextcloud installation CLI commands.

# cd /var/www/html
# sudo -u www-data php occ  maintenance:install --database \
"mysql" --database-name "nextcloud"  --database-user "nextcloud" --database-pass \
"passw@rd" --admin-user "admin" --admin-pass "admin123"

If everything goes well the command will output “Nextcloud was successfully installed”.

6. nextcloud allowed access only from localhost, it could through error “Access through untrusted domain”. we need to allow accessing nextcloud by using our ip or domain name.

# vi /var/www/html/config/config.php

<?php
$CONFIG = array (
  'passwordsalt' => 'VAXFa5LsahAWHK/CMPHC3QkTsnqK80',
  'secret' => 'ZWTuZMLpKVizET85i/NkcwYCPUQyjB/6ZjEYGdVgJeDhNXzR',
  'trusted_domains' =>
  array (
    0 => 'localhost',
    1 => 'nc.mailserverguru.com', // we added this line
  ),
  'datadirectory' => '/var/www/html/data',
  'dbtype' => 'mysql',

  .....
:x

Now save the file and restart apache2

# systemctl restart apache2

Now, Go to the Browser and type http:// [ ip or fqdn ] of the server, as the configuration is completed command line, the Login page will appear.

Thanks !!



Nextcloud is a file hosting system that allows us to store our content like documents, pictures, videos, etc, and share them with others.

Instead of relying on external service providers for our personal and business documents, Nextcloud gives us the freedom to store them on our servers or in trusted data centers.

it’s a self-managed centralized document and file management system. It is Open-Source hence enabling us to use and adapt the application as we need. We have full control over the application. So we can provide our security measures to secure our contents.




In this tutorial, we are going to Install NextCloud On Ubuntu 22.04 LTS, This will be a detail step-by-step high-performance Setup.

1. Install PHP and MySQL packages
2. Configure MySQL Server
3. Run the Nextcloud Setup on the Browser.


1. Install Packages


1. Update and Upgrade the Ubuntu Packages

# apt update && apt upgrade

2. install Apache and MySQL Server

# apt install apache2 mariadb-server 

3. Install PHP and other Dependencies and Restart Apache

# apt install libapache2-mod-php php-bz2 php-gd php-mysql php-curl php-mbstring php-imagick php-zip php-ctype php-curl php-dom php-json php-posix php-bcmath php-xml php-intl php-gmp zip unzip wget

4. Enable required Apache modules and restart Apache:

# a2enmod rewrite dir mime env headers
# systemctl restart apache2


2. Configure MySQL Server


1. Login to MySQL Prompt, Just type

# mysql

2. Create MySQL Database and User for Nextcloud and Provide Permissions.

CREATE USER 'nextcloud'@'localhost' IDENTIFIED BY 'passw@rd';
CREATE DATABASE IF NOT EXISTS nextcloud CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
GRANT ALL PRIVILEGES ON nextcloud.* TO 'nextcloud'@'localhost';
FLUSH PRIVILEGES;
quit;


3. Download and Install Nextcloud

Now download the latest Nextcloud archive file, Go to the Nextcloud Download Page. Or you can download from this direct link: https://download.nextcloud.com/server/releases/latest.zip


1. Download and unzip at the web root (/var/www/html) folder

# cd /var/www/html
# wget https://download.nextcloud.com/server/releases/latest.zip
# unzip latest.zip

2. Move all nextcloud content to the web root (/var/www/html) folder

# cd /var/www/html/nextcloud
# mv * .* ../

3. Remove the empty nextcloud directory

# rmdir /var/www/html/nextcloud

4. Change the ownership of the Nextcloud content directory to the HTTP user.

# chown -R www-data:www-data /var/www/html

Now, Go to the Browser and type http:// [ ip or fqdn ] of the server, The below Nextcloud install page will appear. On the page, we need to provide information for

1. Nextcloud admin username & password
2. Database Credentials (db name, db user and db Password)

– After Providing all the information click the Install button. it will install Nextcloud in a minute and provide the recommended app installation page, we can safely cancel the prompt for now.

Our installation and Initial Setup are complete so Next, the Nextcloud Dashboard will appear.

This is the fastest way to install Nextcloud on Ubuntu 22.04, after installation we should take care of the Nextcloud performance and security issues.

Nextcloud is a file hosting system that allows us to store our content like documents, pictures, videos, etc, and share it with others.

Here we will see How to Install NextCloud On Ubuntu 22.04 LTS this will be a complete guide.

Instead of relying on external service providers for our personal and business documents, Nextcloud gives us the freedom to store them on our servers or in trusted data centers. it’s a self-managed centralized document and file management system.

It is Open-Source hence enabling us to use and adapt the application as we need. We have full control over the application. So we can provide our security measures to secure our contents.



In this tutorial, we are going to see How to Install NextCloud On Ubuntu 22.04 LTS. This will be a detailed setup, we will increase the performance of Nextcloud and apply the security. the steps we are going to follow.

1. Install PHP and MySQL packages
2. Configure MySQL Server
3. Download, Extract and Apply Permission
4. Install Nextcloud from the Command Line
5. Install and Configure PHP-FPM with Apache

6. Create info.php Page for php feature check
7. Enable Opcache in php
8. Enable APCu in php
9. Install and Configure Redis

10. Install SSL and Enable HTTP2
11. Pretty URLs


1. Install Required Packages


1. Update and Upgrade the Ubuntu Packages

# apt update && apt upgrade

2. install Apache and MySQL Server

# apt install apache2 mariadb-server 

3. Install PHP and other Dependencies and Restart Apache

# apt install libapache2-mod-php php-bz2 php-gd php-mysql php-curl php-mbstring php-imagick php-zip php-ctype php-curl php-dom php-json php-posix php-bcmath php-xml php-intl php-gmp zip unzip wget

4. Enable required Apache modules and restart Apache:

# a2enmod rewrite dir mime env headers
# systemctl restart apache2

2. Configure MySQL Server

1. Login to MySQL Prompt, Just type

# mysql

2. Create MySQL Database and User for Nextcloud and Provide Permissions.

CREATE USER 'nextcloud'@'localhost' IDENTIFIED BY 'passw@rd';
CREATE DATABASE IF NOT EXISTS nextcloud CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
GRANT ALL PRIVILEGES ON nextcloud.* TO 'nextcloud'@'localhost';
FLUSH PRIVILEGES;
quit;

3. Download, Extract, and Apply Permissions.

Now download the latest Nextcloud archive file, Go to the Nextcloud Download Page. Or you can download from this direct link: https://download.nextcloud.com/server/releases/latest.zip

1. Download and unzip at the /var/www folder

# cd /var/www/
# wget https://download.nextcloud.com/server/releases/latest.zip
# unzip latest.zip

2. Remove the zip file, which is not necessary now.

# rm -rf latest.zip

3. Change the ownership of the nextcloud content directory to the HTTP user.

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

4. Install NextCloud From the Command Line

We are going to install Nextcloud on Ubuntu 22.04 from the command line, it will save us time as we are providing all the database and admin credentials for installation. It will install Nextcloud silently, we don’t have to go through web setup. For detailed Nextcloud Command line installation, visit this page.

1. Run the CLI Command

# cd /var/www/nextcloud
# sudo -u www-data php occ  maintenance:install --database \
"mysql" --database-name "nextcloud"  --database-user "nextcloud" --database-pass \
"passw@rd" --admin-user "admin" --admin-pass "admin123"

If everything goes well the command will output “Nextcloud was successfully installed”. We provided a very simple user/password, during production setup, this must be a complex password.

2. nextcloud allows access only from localhost, it could through error “Access through untrusted domain”. we need to allow accessing nextcloud by using ip or domain name.

# vi /var/www/nextcloud/config/config.php

  'trusted_domains' =>
  array (
    0 => 'localhost',
    1 => 'nc.mailserverguru.com',     // we Included the Sub Domain
  ),

  .....
:x    // saving the file

3. Configure Apache to load Nextcloud from the /var/www/nextcloud folder.

# vi /etc/apache2/sites-enabled/000-default.conf

<VirtualHost *:80>
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/nextcloud    // Chan
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Now, Restart Apache Server

# systemctl restart apache2

Now, Go to the Browser and type http:// [ ip or fqdn ] of the server, The below Nextcloud login page will appear.

The Basic NextCloud Installation on Ubuntu 22.04 is completed, Now we are going to Work on the Performance and Security.

5. Install and Configure PHP-FPM with Apache

Here we will install PHP-FPM, which is faster than the mpm-prefork module, which is the default method of executing php files on Apache.

1. Install php-fpm

# apt install php8.1-fpm
# service php8.1-fpm status    // Check the php-fpm is running

2. Check the php-fpm version and Socket.

# php-fpm8.1 -v
# ls -la /var/run/php/php8.1-fpm.sock

3. Disable Apache prefork module

# a2dismod php8.1
# a2dismod mpm_prefork

4. Enable php-fpm

# a2enmod mpm_event proxy_fcgi setenvif
# a2enconf php8.1-fpm

5. set required php.ini variables

# vi /etc/php/8.1/fpm/php.ini

upload_max_filesize = 64M 
post_max_size = 96M 
memory_limit = 512M 
max_execution_time = 600
max_input_vars = 3000 
max_input_time = 1000

:x

6. php-fpm pool Configurations

# vi /etc/php/8.1/fpm/pool.d/www.conf

pm.max_children = 64
pm.start_servers = 16
pm.min_spare_servers = 16
pm.max_spare_servers = 32

:x

# service php8.1-fpm restart

7. Apache directives for php files processing by php-fpm

# vi /etc/apache2/sites-enabled/000-default.conf 

<VirtualHost *:80>

	ServerAdmin webmaster@localhost
	DocumentRoot /var/www/nextcloud


	<Directory /var/www/nextcloud>
          Options Indexes FollowSymLinks
          AllowOverride All
          Require all granted
	</Directory>

	<FilesMatch ".php$"> 
         SetHandler "proxy:unix:/var/run/php/php8.1-fpm.sock|fcgi://localhost/"          
	</FilesMatch>


	ErrorLog ${APACHE_LOG_DIR}/error.log
	CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

:x

# service apache2 restart

6. Create info.php Page for php feature check

Create an info.php page, it will show us whether php-fpm, opcache, apcu are enabled with the php.

# cd /var/www/nextcloud

# vi info.php
    <?php phpinfo(); ?>
:x

Now Browse http://nc.mailserverguru.com/info.php, it will show “Server API FPM/FastCGI” if the php-fpm is enabled on the PHP.


7. Enable Opcache in php

Opcache is a caching engine for PHP. It stores precompiled script bytecode in shared memory, so parsing PHP scripts on each request won’t be necessary. It increases php file execution and website loading performance.

# vi /etc/php/8.1/fpm/php.ini

opcache.enable=1
opcache.enable_cli=1
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=10000
opcache.memory_consumption=128
opcache.save_comments=1
opcache.revalidate_freq=60

:x

// Now, Restart apache and php-fpm

# systemctl restart php8.1-fpm
# systemctl restart apache2

Now, Check http://nc.mailserverguru.com/info.php again, it will show the “Opcache is Up and Running”


8. Enable APCu in php

APCu is the user data caching. It is a local cache for systems. Nextcloud uses this for memory caching.

1. Install APCu

# apt install php8.1-apcu

2. Configure Nextcloud to use APCu for memory caching.
# vi /var/www/nextcloud/config/config.php

'memcache.local' => '\OC\Memcache\APCu',

:x

// Restart php-fpm and apache

# systemctl restart php8.1-fpm
# systemctl restart apache2

Now, Check the http://nc.mailserverguru.com/info.php again, it will show the “APCu support Enabled”

9. Install and Configure Redis

In Nextcloud, Redis is used for local and distributed caching as well as transactional file locking. we used APCu for Local Cahing which is faster than Redis. We will use Redis for File locking. Nextcloud’s Transactional File Locking mechanism locks files to avoid file corruption during normal operation.

1. Install Redis Server and Redis php extension

# apt-get install redis-server php-redis

// Start and Enable Redis Service

# systemctl start redis-server
# systemctl enable redis-server

2. Configure Redis to use Unix Socket than ports

# vi /etc/redis/redis.conf

port 0
unixsocket /var/run/redis/redis.sock
unixsocketperm 770

:x

3. Add Apache user to the Redis group

# usermod -a -G redis www-data

4. Configure Nextcloud for using Redis for File Locking

# vi /var/www/nextcloud/config/config.php

'filelocking.enabled' => 'true',
'memcache.locking' => '\OC\Memcache\Redis',
'redis' => [
     'host'     => '/var/run/redis/redis.sock',
     'port'     => 0,
     'dbindex'  => 0,
     'password' => '',
     'timeout'  => 1.5,
],

:x

5. Enable Redis session locking in PHP

# vi /etc/php/8.1/fpm/php.ini

redis.session.locking_enabled=1
redis.session.lock_retries=-1
redis.session.lock_wait_time=10000

:x

// Now, Restart php-fpm and apache

# systemctl restart php8.1-fpm
# systemctl restart apache2

Now, we can check Redis use, (by enabling the Redis port in the Redis configuration) by running the command “redis-cli MONITOR”, during Nextcloud loading it will show live data on the screen.

Now, that we have finished Performance improvement steps. We are going to work for the Security, First of all, we will install an SSL certificate for Nextcloud.

10. Install SSL and Enable HTTP2

1. We will install the LetsEncrypt certificate, so, first, we need the Certbot tools.

# apt-get install python3-certbot-apache -y

2. with the Certbot tool, let’s request a Certificate for our domain.

# certbot --apache -d nc.mailserverguru.com

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Enter email address (used for urgent renewal and security notices)
 (Enter 'c' to cancel): munnavai@gmail.com   // Input Email Address

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must
agree in order to register with the ACME server. Do you agree?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: Y   // Press Y here

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you be willing, once your first certificate is successfully issued, to
share your email address with the Electronic Frontier Foundation, a founding
partner of the Let's Encrypt project and the non-profit organization that
develops Certbot? We'd like to send you email about our work encrypting the web,
EFF news, campaigns, and ways to support digital freedom.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: Y   // Press Y here
Account registered.
Requesting a certificate for nc.mailserverguru.com

Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/nc.mailserverguru.com/fullchain.pem
Key is saved at:         /etc/letsencrypt/live/nc.mailserverguru.com/privkey.pem
This certificate expires on 2022-10-13.
These files will be updated when the certificate renews.
Certbot has set up a scheduled task to automatically renew this certificate in the background.

Deploying certificate
Successfully deployed certificate for nc.mailserverguru.com to /etc/apache2/sites-available/000-default-le-ssl.conf
Congratulations! You have successfully enabled HTTPS on https://nc.mailserverguru.com
We were unable to subscribe you the EFF mailing list because your e-mail address appears to be invalid. You can try again later by visiting https://act.eff.org.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
If you like Certbot, please consider supporting our work by:
 * Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
 * Donating to EFF:                    https://eff.org/donate-le
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

3. Enable apache HTTP2 module and configure site for the http2 protocols

# a2enmod http2

# vi /etc/apache2/sites-enabled/000-default-le-ssl.conf

<VirtualHost *:443>

        Protocols h2 h2c http/1.1

        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/nextcloud
  ......

:x

// Now, Restart Apache

# systemctl restart apache2


4. Test the http2 protocol, by sending an http2 request to the web server.

# curl -I --http2 -s https://nc.mailserverguru.com/ | grep HTTP
HTTP/2 200

Or, we can Inspect the Browser while accessing Nextcloud URL, we can easily see the protocol column from the Network tab, and it will show h2 as the protocol which is http2.


5. HTTP Strict Transport Security, which instructs browsers not to allow any connection to the Nextcloud instance using HTTP, prevents man-in-the-middle attacks.

<VirtualHost *:443>
  ServerName nc.mailserverguru.com

    <IfModule mod_headers.c>
      Header always set Strict-Transport-Security "max-age=15552000; includeSubDomains"
    </IfModule>

 </VirtualHost>


11. Pretty URL’s

Pretty URLs remove the "index.php” part in all Nextcloud URLs. It will make URLs shorter and prettier.

# vi /var/www/nextcloud/config/config.php

'htaccess.RewriteBase' => '/',
                                 
:x

// This command will update the .htaccess file for the redirection
# sudo -u www-data php --define apc.enable_cli=1 /var/www/nextcloud/occ maintenance:update:htaccess

So, this is it, we have done our Nextcloud complete setup on Ubuntu 22.04 LTS. Thanks !!


Nextcloud can be installed in multiple ways. Here, we will discuss how to Install Nextcloud on Web Panels via Web Installer.

For hosting websites and web-based applications we need a web server. Famous web panels like cPanel, ISPConfig, Cyberpanel, etc are web servers with user-friendly interfaces. We can install NextCloud on any web panel.

With the installation of web panels, all required services and modules are installed, so during the installation of Nextcloud, it should not be necessary to install any PHP modules. Moreover, we can quickly create databases for Nextcloud via the web interface.



So, how do we proceed? We will download a php install script from nextcloud. We will save and run it from the web panels hosting space. The script will check the dependencies, download Nextcloud from the official server, and unpack it, with the right permissions and the right user account.

Finally, it will be redirected to the Nextcloud installer page. rest of the setup needs to be done manually from the web interface.

In this guide, I will demonstrate how to install Nextcloud on the hestiaCP web hosting panel.

The steps we are going to follow:

1. Create Hosting Space on the HestiCP hosting Panel
2. Creating MySQL DB on the Panel
3. Download the Web installer Script from Nextcloud
4. Upload the script to the hosting space
5. Run the Setup Script from the Browser.


To Install Nextcloud on Web Panels via Web Installer, we need to create a hosting space for nextcloud, the space needs to be created with a web panel user, web space and database will be created with the user permission.


1. Create Hosting Space on the HestiCP hosting Panel

1. Login to HestiaCP Web Interface


2. Create an User for the Nextcloud Hosting Space, this will be a Web Panel Only User.


3. Provide User Name, Password, Email, etc. for the Web Panel User.

4. After the user is Created, log in with that user.


5. After logging in to the Panel, create the domain.


6. Provide the Domain Detail and Click the Save button.

2. Creating MySQL DB on the Panel

1. On the Web Panel, Go to DB Section. and Click add Database


2. Provide Database Details

3. Download the Web installer Script Nextcloud

– Download the script from Nextcloud Download Page.


4. Upload the script to the hosting space

1. To Upload files, we need to Click the Files tab.

2. The Files window will show the directory tree of the hosting space.


3. We need to travel to the root directory of the hosting space, then Click on “Add files”

4. Now, Upload the file.

Our upload is complete, we need to fire the script from the browser.


5. Run the Setup Script from the Browser.

1. Run the script from the browser: http://[ip or domain]/setup-nextcloud.php


2. Dependency check completed, now enter only “.” into the textbox to install Nextcloud on the current root directory. then click Next.


3. If all is OK, the Nextcloud installation will succeed.


4. Now, if we Click Next, it will show the Nextcloud Configuration Page. We need to provide the nextcloud admin user/pass, and database details to complete the setup process.


5. After providing all the details above, if we click next, the nextcloud login page will appear, that means, our installation is done and the system is ready for use.


Thanks !!