How to Install Nextcloud on Ubuntu 22.04 With a LEMP Stack

Install Nextcloud on Ubuntu 22.04 with a LEMP stack: configure Nginx, MariaDB and PHP to self-host a private, open-source cloud storage alternative to Dropbox.

In this guide, we are going to set up Nextcloud on a Ubuntu 22.04 server hosted with Nginx, MariaDB and PHP (LEMP stack). We will be using MariaDB 10.6 from the Ubuntu repositories and PHP 8.4 for this guide.

Updated July 2026 — read this before you start. Ubuntu 22.04 ships PHP 8.1, and current Nextcloud releases (34.x at the time of writing) no longer support it — the supported versions are PHP 8.2 (deprecated), 8.3, 8.4 and 8.5. Installing the stock php package will leave you with an installer that refuses to run. The PHP section below therefore installs PHP 8.4 from the Sury repository instead. MariaDB 10.6, which Ubuntu 22.04 does ship, is still supported by Nextcloud 34, so the database section works as-is.

Note also that Ubuntu 22.04 LTS reaches the end of its standard five-year support in April 2027 (extended to 2032 with Ubuntu Pro). If you are building a new server rather than following along on an existing 22.04 box, prefer Ubuntu 24.04 LTS or 26.04 LTS — they ship newer PHP by default and you can skip the extra repository below.

Nextcloud is an Open Source suite of client-server software for creating and using file hosting services. It is a free self-hosted cloud storage solution similar to Dropbox, Google Drive, etc. With Nextcloud, you don’t have to worry about the pricey alternatives and since you will host your own files, you don’t have to worry about privacy or someone collecting your data.

Nextcloud can be installed on a private home server or a virtual private server in the cloud. Files can then be uploaded and then synced to a local desktop, laptop or even a smartphone. This way you have full control of your data.

Features of Nextcloud

  • Nextcloud has sync clients for Linux, Mac OS, Windows, Android and IOS
  • End to end encryption - files are encrypted while being uploaded to the server
  • Nextcloud is free and Open Source
  • Can be integrated with an online office suite (Collabora, OnlyOffice) so you can create and edit your doc, ppt, xls files directly from Nextcloud.
  • The app store contains hundreds of apps to extend functionality (like calendar app, notes-taking app, video conferencing app, etc).

Related Content:

Prerequisites

To follow along this guide, ensure that you have:

  • An up to date Ubuntu 22.04 Server
  • Internet access from the server
  • Root access from the server or user with sudo access
  • A domain name pointed at the server if you intend to reach Nextcloud over the internet

Ensure that the server packages are up to date

Let us make sure that our server packages are up to date with this command:

sudo apt update
sudo apt -y upgrade

While at it, let us also install some dependencies that we will require

sudo apt install -y unzip vim

Installing Mariadb in Ubuntu 22.04

MariaDB is a popular Opensource relational management system. It is available in the default repositories as mariadb-server. Ubuntu 22.04 ships MariaDB 10.6, which is still a supported database version for Nextcloud 34.

Install it with this command:

sudo apt install -y mariadb-server

MariaDB will be installed and started by default in Ubuntu 22.04. Check the status with this

$ sudo systemctl status mariadb

● mariadb.service - MariaDB 10.6.7 database server
     Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset: enabled)
     Active: active (running) since Sun 2022-10-02 11:37:36 UTC; 30s ago
       Docs: man:mariadbd(8)
             https://mariadb.com/kb/en/library/systemd/
    Process: 34957 ExecStartPre=/usr/bin/install -m 755 -o mysql -g root -d /var/run/mysqld (code=exited, status=0/SUCCESS)
    Process: 34958 ExecStartPre=/bin/sh -c systemctl unset-environment _WSREP_START_POSITION (code=exited, status=0/SUCCESS)
    Process: 34960 ExecStartPre=/bin/sh -c [ ! -e /usr/bin/galera_recovery ] && VAR= ||   VAR=`cd /usr/bin/..; /usr/bin/galera_recovery`; [ $? -eq 0 ]   && systemctl set-environment _WSREP_START_POSITION>
    Process: 35000 ExecStartPost=/bin/sh -c systemctl unset-environment _WSREP_START_POSITION (code=exited, status=0/SUCCESS)
    Process: 35002 ExecStartPost=/etc/mysql/debian-start (code=exited, status=0/SUCCESS)
   Main PID: 34989 (mariadbd)
     Status: "Taking your SQL requests now..."
      Tasks: 12 (limit: 4392)
     Memory: 57.2M
        CPU: 376ms
     CGroup: /system.slice/mariadb.service
             └─34989 /usr/sbin/mariadbd

Oct 02 11:37:36 unstable-ubuntusrv mariadbd[34989]: Version: '10.6.7-MariaDB-2ubuntu1.1'  socket: '/run/mysqld/mysqld.sock'  port: 3306  Ubuntu 22.04
Oct 02 11:37:36 unstable-ubuntusrv systemd[1]: Started MariaDB 10.6.7 database server.

To ensure that mariadb starts on boot, enable it with this systemd command:

sudo systemctl enable mariadb

Once the mysql service is running, you need to secure it. Mysql provides a commandline utility that will do that:

sudo mysql_secure_installation

The above command will take you throuh prompts to secure and set a root password for the mysql instance.

Check more info on Installing and setting up MariaDB in this guide here.

Creating mysql user for Nextcloud

It is always recommended to have a dedicated user for each app that connects to the DB. Check out this guide on managing mysql permissions here. We will set up a database, user and password to be used by nextcloud:

Connect to the MariaDB server. On Ubuntu 22.04 the root database user authenticates through the unix socket, so sudo gets you in without a password:

sudo mariadb

If you set a root password during mysql_secure_installation, or you are connecting to a remote server, use mysql -u root -p instead.

Enter the following at the prompt. Use a database character set of utf8mb4 — Nextcloud requires it for full Unicode (including emoji) support:

create database nextcloud character set utf8mb4 collate utf8mb4_general_ci;
create user 'nextcloud_user'@'localhost' identified by 'S0mStrongPa$$word';
grant all privileges on nextcloud.* to 'nextcloud_user'@'localhost';
flush privileges;

Replace S0mStrongPa$$word with a strong password of your own — you will need it in the setup wizard later. We scope the user to localhost because the database runs on the same host as Nextcloud; only widen it to 'nextcloud_user'@'%' if your database lives on a separate server.

Now that we have configured our mysql connection, let’s go to the next section where we install and configure PHP and Nginx.

Installing PHP 8.4 in Ubuntu 22.04

This is the step that has changed most since this guide was first published. Ubuntu 22.04’s default php package is PHP 8.1, which current Nextcloud releases no longer support — as of Nextcloud 34 the supported versions are 8.2 (deprecated), 8.3, 8.4 and 8.5. Installing the stock package will get you as far as the setup wizard and then stop with an unsupported-PHP error.

We therefore pull PHP 8.4 from the well-known Sury PHP repository, which packages current PHP for Ubuntu LTS releases. Add it with a keyring rather than the long-removed apt-key:

sudo apt install -y ca-certificates apt-transport-https software-properties-common
sudo add-apt-repository -y ppa:ondrej/php
sudo apt update

add-apt-repository handles the signing key for you. If you prefer to add the repository by hand, fetch the key with gpg --dearmor into /usr/share/keyrings/ and reference it with signed-by= in the .list file — apt-key add, which older guides use, has been deprecated and removed from modern APT.

Then install PHP 8.4 and the modules Nextcloud requires:

sudo apt install -y \
  php8.4-fpm \
  php8.4-cli \
  php8.4-gd \
  php8.4-curl \
  php8.4-zip \
  php8.4-xml \
  php8.4-mbstring \
  php8.4-intl \
  php8.4-bcmath \
  php8.4-gmp \
  php8.4-mysql

A few notes on that list versus older versions of this guide:

  • php-json is gone — JSON has been compiled into PHP core since 8.0, and there is no separate package to install.
  • php-dom and php-simplexml are both provided by php8.4-xml, so listing them separately is unnecessary.
  • bcmath and gmp are not strictly required but Nextcloud’s setup checks warn about them; installing them up front keeps the admin overview clean.

Optionally, add APCu for local caching (Nextcloud will warn if no memory cache is configured) and Imagick for richer preview generation:

sudo apt install -y php8.4-apcu php8.4-imagick

Nextcloud also expects a PHP memory_limit of at least 512MB. Set it for the FPM pool:

sudo sed -i 's/^memory_limit = .*/memory_limit = 512M/' /etc/php/8.4/fpm/php.ini
sudo systemctl restart php8.4-fpm

Confirm the installed php version:

$ php8.4 -v

PHP 8.4.11 (cli) (built: Jul  3 2026 10:12:44) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.4.11, Copyright (c) Zend Technologies
    with Zend OPcache v8.4.11, Copyright (c), by Zend Technologies

If php -v still reports 8.1, the CLI default has not switched. Select it with sudo update-alternatives --config php. This only affects the command line — Nginx talks to the FPM socket directly, which we point at 8.4 below.

For more info on setting up PHP and Nginx, check out this guide on How to Install and set up PHP and Nginx (LEMP) on Ubuntu 22.04.

Installing Nginx in Ubuntu 22.04

Next, let us install Nginx in our system. Use this command to install nginx

sudo apt install -y nginx

Nginx will be started by default. Check the status using this command:

$ sudo systemctl status nginx

● nginx.service - A high performance web server and a reverse proxy server
     Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
     Active: active (running) since Sun 2022-10-02 12:04:57 UTC; 7s ago
       Docs: man:nginx(8)
    Process: 49919 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
    Process: 49920 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
   Main PID: 49921 (nginx)
      Tasks: 2 (limit: 4392)
     Memory: 2.5M
        CPU: 26ms
     CGroup: /system.slice/nginx.service
             ├─49921 "nginx: master process /usr/sbin/nginx -g daemon on; master_process on;"
             └─49922 "nginx: worker process"

Oct 02 12:04:57 unstable-ubuntusrv systemd[1]: Starting A high performance web server and a reverse proxy server...
Oct 02 12:04:57 unstable-ubuntusrv systemd[1]: Started A high performance web server and a reverse proxy server.

Active: active (running) indicate that the service is now up and running.

To enable the service on boot, use this command:

sudo systemctl enable nginx

Download and configure Nextcloud

Now that we have successfully configured the LEMP server in our system, let us download and set up Nextcloud installer. Nextcloud provides a web installer that can be found in its site for web installer here.

First let us create our directory structure. We will create a directory /var/www/nextcloud and download the installer there. Then we will make sure that the web user owns that directory:

sudo mkdir -p /var/www/nextcloud
cd /var/www/nextcloud
sudo curl -LO https://download.nextcloud.com/server/installer/setup-nextcloud.php
sudo chown www-data:www-data -R /var/www/nextcloud

The sudo on the download and chown matters — /var/www is root-owned, so both commands fail with “Permission denied” without it. The web installer needs to write into this directory as www-data, which is why we hand ownership over.

Configure Nginx to serve Nextcloud from a virtual host

We managed to download the Nextcloud content to /var/www/nextcloud. The next bit it to set up Nginx to serve it.

Let’s create virtual site in the directory that nginx serves content here /etc/nginx/conf.d:

sudo vim /etc/nginx/conf.d/nextcloud.conf

Add the following content:

server {
    listen 80;

    server_name nextcloud.citizix.com;
    root /var/www/nextcloud;
    index index.php index.html;

    # Nextcloud uploads are large; the 1MB nginx default will break them.
    client_max_body_size 512M;
    client_body_timeout 300s;
    fastcgi_buffers 64 4K;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    # Keep the data directory and internal files unreachable over HTTP.
    location ~ ^/(?:\.|data|config|db_structure\.xml|README) {
        deny all;
    }

    location = /favicon.ico {
        log_not_found off;
        access_log off;
    }

    location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff2?)$ {
        expires max;
        log_not_found off;
    }

    location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
    }

    location ~ \.php$ {
        include /etc/nginx/fastcgi_params;
        fastcgi_pass unix:/run/php/php8.4-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_read_timeout 300;
    }
}

A few things to note in that config:

  • fastcgi_pass must point at the socket the FPM version you installed actually creates. With PHP 8.4 that is /run/php/php8.4-fpm.sock — a generic /run/php/php-fpm.sock does not exist and yields a 502 Bad Gateway. Confirm yours with ls /run/php/.
  • client_max_body_size is the single most common cause of failed Nextcloud uploads. Nginx defaults to 1MB; raise it to whatever the largest file you expect to sync is, and keep PHP’s upload_max_filesize and post_max_size at least as high.
  • The deny all block keeps /data and /config off the web. Nextcloud ships an .htaccess for this, but Nginx ignores .htaccess entirely, so it has to be expressed here.

This is a deliberately minimal virtual host to get you running. For a production deployment, start from the official Nginx example configuration in the Nextcloud admin manual, which adds the .well-known CalDAV/CardDAV redirects, security headers and PHP tuning that the setup checks look for.

Validate the config and, if it passes, restart Nginx and PHP-FPM:

sudo nginx -t
sudo systemctl restart php8.4-fpm nginx

Accessing Nextcloud installer from the web url

Then head to your set domain with the path for the set up, http://nextcloud.citizix.com/setup-nextcloud.php for me to initiate nextcloud set up.

You will get a set up wizard that will take you through installing dependencies and setting up admin user and database connection credentials.

In the database step, use the credentials we created earlier: database nextcloud, user nextcloud_user, the password you chose, and localhost as the host.

After that Click Finish. You will be redirected to the login page where you will use the details for the created user to login. On Successful login you will get the Dashboard.

Secure the site with HTTPS

The virtual host above listens on port 80 only, which means your login credentials and every file you sync travel in clear text. That is fine while you are testing on a private network, but before putting anything real into this server, terminate TLS in front of it. On Ubuntu 22.04 the quickest route is Certbot, which will obtain a Let’s Encrypt certificate and rewrite the Nginx server block for you:

sudo apt install -y certbot python3-certbot-nginx
sudo certbot --nginx -d nextcloud.citizix.com

Certbot installs a systemd timer that handles renewal, so there is nothing further to schedule. Nextcloud’s own security check will also stop warning about the missing HSTS header once you are on HTTPS.

Conclusion

In this guide, we managed to set up LEMP on Ubuntu 22.04 to serve Nextcloud — MariaDB 10.6 from the Ubuntu repositories, PHP 8.4 from the Sury repository to meet Nextcloud’s current requirements, and Nginx as the front end.

Keep an eye on the Nextcloud system requirements when you upgrade: the supported PHP and database versions move with each major release, and PHP is the component most likely to need attention on a long-lived Ubuntu 22.04 host.

comments powered by Disqus
Citizix Ltd
Built with Hugo
Theme Stack designed by Jimmy