Install NextCloud On Ubuntu 22.04 LTS – Complete Guide

Install NextCloud On Ubuntu 22.04 LTS – Complete Guide

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

Nextcloud is a file hosting system that allows us to store our content like documents, pictures, videos, etc, and share it 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.

Below we provided a detailed step-by-step guidelines on how to install Nextcloud on Ubuntu 22.04 LTS. To maintain high performance and security, we have added necessary steps to add more features on the system. Each steps are highly recommended to setup a production grade Nextcloud environement.

Table of Contents

    Step1: 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-zip \
    php-mbstring php-imagick 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

    Step2. Configure MySQL Server

    1. Login to MySQL Prompt, Just type


    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';

    Step3. Download, Extract, and Apply Permissions.

    Now download the latest Nextcloud archive file, Go to the Nextcloud Download Page. Or you can download from direct link:

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

    cd /var/www/

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

    rm -rf

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

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

    Step4. 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 => '',   // 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
            ErrorLog ${APACHE_LOG_DIR}/error.log
            CustomLog ${APACHE_LOG_DIR}/access.log combined
    // 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.

    Step5. 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

    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
    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
    	<FilesMatch ".php$"> 
             SetHandler "proxy:unix:/var/run/php/php8.1-fpm.sock|fcgi://localhost/"          
    	ErrorLog ${APACHE_LOG_DIR}/error.log
    	CustomLog ${APACHE_LOG_DIR}/access.log combined
    service apache2 restart

    Step6. 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(); ?>

    Now Browse, it will show “Server API FPM/FastCGI” if the php-fpm is enabled on the PHP.

    Step7. 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
    // Now, Restart apache and php-fpm
    systemctl restart php8.1-fpm
    systemctl restart apache2

    Now, Check again, it will show the “Opcache is Up and Running”

    Step8. 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',
    // Restart php-fpm and apache
    systemctl restart php8.1-fpm
    systemctl restart apache2

    Now, Check the again, it will show the “APCu support Enabled”

    Step9. 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
    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

    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,

    5. Enable Redis session locking in PHP

    vi /etc/php/8.1/fpm/php.ini
    // 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.

    Step10. 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
    Saving debug log to /var/log/letsencrypt/letsencrypt.log
    Enter email address (used for urgent renewal and security notices)
     (Enter 'c' to cancel): [email protected]   // Input Email Address
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    Please read the Terms of Service at 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
    Successfully received certificate.
    Certificate is saved at: /etc/letsencrypt/live/
    Key is saved at:         /etc/letsencrypt/live/
    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 to /etc/apache2/sites-available/000-default-le-ssl.conf
    Congratulations! You have successfully enabled HTTPS on
    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
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    If you like Certbot, please consider supporting our work by:
     * Donating to ISRG / Let's Encrypt:
     * Donating to EFF:          
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

    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
    // Now, Restart Apache
    systemctl restart apache2

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

    curl -I --http2 -s | 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>
    <IfModule mod_headers.c>
        Header always set Strict-Transport-Security "max-age=15552000; includeSubDomains"

    Step11. 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' => '/',
    // 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 Complete Install NextCloud On Ubuntu 22.04 LTS.

    Thanks !!


    Get Special Free Tips, Tricks, Tutorials, and Case Studies, that I Only Share with Email Subscribers.


    We respect your privacy. Unsubscribe at any time.

    Related Articles


    Leave a Reply

    Your email address will not be published. Required fields are marked *