How to Install NextCloud on Ubuntu 22.04 with Apache (LAMP Stack)

Choose a different version or distribution

Introduction

Before we begin talking about how to install NextCloud on Ubuntu 22.04 with Apache (Lamp Stack), let’s briefly understand – What is NextCloud?

Nextcloud is a powerful and secure open-source cloud storage solution. It allows you to store, share, and access your files, documents, and media from anywhere, using any device. With Nextcloud, you can collaborate with others, sync your files across multiple devices, and protect your data with strong encryption. It's a versatile and user-friendly alternative to popular cloud storage services, giving you full control over your data.

In this tutorial, you will learn how to install Nextcloud on Ubuntu 22.04. We will also address a few FAQs on how to install NextCloud on Ubuntu 22.04.

Advantages of Nextcloud

  1. Control: Nextcloud gives you complete control over your data, allowing you to host it on your own server and decide who has access.
  2. Collaboration: It enables seamless collaboration, allowing multiple users to edit and share files in real-time.
  3. Accessibility: With Nextcloud, you can access your files from any device with internet connectivity.
  4. Security: Nextcloud prioritizes data security, offering strong encryption and privacy measures to protect your sensitive information.
  5. Customization: It provides a flexible platform that can be customized with various apps and extensions to meet your specific needs.

Prerequisites to Install NextCloud on Ubuntu 22.04 with Apache (LAMP Stack)

  • The major requirement for this tutorial is Lamp Stack since NextCloud is written in PHP.
  • You can install NexCloud on your home server or a Virtual Private Server (VPS). You'd also need a domain name in order to enable HTTPS and encrypt HTTP traffic. You can even register your domain name at NameCheap since they offer low-price services along with privacy protection for life.

Step 1 - Download NextCloud

1) Start by downloading the latest stable version of NextCloud. You can check for the latest version here. Then click download for server.

2) You can run the following command to download it:

wget https://download.nextcloud.com/server/releases/nextcloud-27.0.0.zip

3) Next, download the archive using unzip.

sudo apt install unzip
sudo unzip nextcloud-27.0.0.zip -d /var/www/
ℹ️
The above URL format can be used to download NextCloud. Simply replace 18.0.4 with the new version number if a new version comes out.

4) -d specifies the directory. The NextCloud files are extracted to /var/www/nextcloud/. Next, change the owner of the directory to www-data in order enable Apache to write to this directory.

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

Step 2 - Create a Database in MariaDB

1) You then need to log in to MariaDB using the following command. MariaDB uses unix_socket plugin for login authentication, therefore there is no requirement for a root password. All you need to do is prefix the mysql command with sudo.

sudo mysql

2) Then, create a database. Here, the name used is NextCloud, you can use a name of your choice.

create database nextcloud;

3) Next, create a database user and make sure to replace your-password with your own desired password.

create user nextclouduser@localhost identified by 'your-password';

4) Then, grant all permissions to nextcloud database

grant all privileges on nextcloud.* to nextclouduser@localhost identified by 'your-password';

5) You then need to flush privileges and exit.

flush privileges;
exit;

Step 3 - Create an Apache Virtual Host

1) This is followed by creating a nextcloud.conf file in /etc/apache2/sites-available/ directory, with the following command:

sudo nano /etc/apache2/sites-available/nextcloud.conf

2) Next, copy and paste the following text. Don't forget to replace nextcloud.example.com with your own domain name. Also, do not forget to create a DNS A record for the sub-domain in the DNS-zone editor.

<VirtualHost *:80>
        DocumentRoot "/var/www/nextcloud"
        ServerName nextcloud.example.com

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

        <Directory /var/www/nextcloud/>
            Require all granted
            Options FollowSymlinks MultiViews
            AllowOverride All

           <IfModule mod_dav.c>
               Dav off
           </IfModule>

        SetEnv HOME /var/www/nextcloud
        SetEnv HTTP_HOME /var/www/nextcloud
        Satisfy Any

       </Directory>

</VirtualHost>

3) You can press Ctrl+O to initiate saving in the Nano Text editor, then press Enter to confirm. To exit, press Ctrl+X.

4) You then have to enable the virtual host and disable the default page.

sudo a2ensite nextcloud.conf
sudo a2dissite 000-default

5) Then test Apache configurations:

sudo apache2ctl -t

6) If there are no syntax errors, reload Apache for the changes to be applied.

sudo systemctl restart apache2

Step 4 - Install and Enable PHP Modules

1) You need to run the following command in order to install the modules required and recommended by NextCloud.

sudo apt install php-imagick php-common php-mysql php-fpm php-gd php-json php-curl php-zip php-xml php-mbstring php-bz2 php-intl php-bcmath php-gmp -y

2) Next, reload Apache in order to use these modules

sudo systemctl reload apache2

Step 5 - Enable HTTPS

1) Now, you can enter the NextCloud installation domain name and access the install wizard.

nextcloud.example.com

2) In case the webpage can't load, try opening port 80 in the firewall:

sudo iptables -I INPUT -p tcp --dport 80 -j ACCEPT

3) Also, open port 443:

sudo iptables -I INPUT -p tcp --dport 443 -j ACCEPT

4) It is recommended that you enable a secure HTTPS connection on NextCloud before entering any sensitive information. You need to install Let's Encrypt for obtaining a free TSL certificate. Install it with Certbot (client) from Ubuntu 20.04 repository.

sudo apt install certbot python3-certbot-apache

5) The Apache plug-in is Python3-certbot-apache. Now, run the command given below to obtain a free TSL

sudo certbot --apache --agree-tos --redirect --staple-ocsp --email you@example.com -d nextcloud.example.com

6) The following are the locations:

  • -apache2: Use Apache authenticator and installer
  • -agree-tos: Agree to Let’s Encrypt terms of service
  • -redirect: Enforce HTTPS by adding 301 redirects.
  • -staple-ocsp: Enable OCSP Stapling.
  • -email: Email used for registration and recovery contact.
  • -d flag is followed by a list of domain names, separated by a comma. You can add up to 100 domain names.

You will then be asked whether you want to receive emails from EFF (Electronic Frontier Foundation). You can select either Y or N. Then, you will get your free TSL and the following message will testify the same.

7) Certbot can't automatically add the HSTS header in the Apache config file for NextCloud. To enable HTTP Strict Transport Security(HSTS), edit the file:

sudo nano /etc/apache2/sites-enabled/nextcloud-le-ssl.conf

8) You need to add the following line in the SSL server block to enable the HSTS header:

Header always set Strict-Transport-Security "max-age=31536000"

It will look something like this:

9) Save and Close the file. Then, see the Apache configurations:

sudo apache2ctl -t

10) Next, reload Apache for the changes to take effect:

sudo systemctl reload apache2

11) This will get you an A+ score on SSL Test

Step 6 - Finish the Web Setup

1) You can now access the NextCloud web install wizard using an HTTPS connection.

https://nextcloud.example.com

2) You then have to create an admin account, thus you need to enter the path of the NextCloud folder along with the database details. You can also use the default localhost as a host address or simply enter localhost:3306 (MariaDB listens on port 3306).

3) It would be better to change /var/www/nextcloud/data/ to /var/www/nextcloud-data so that the data is out of NextCloud's Web root directory.

sudo mkdir /var/www/nextcloud-data

4) After this, make sure that the Apache user (www-data) has write permission to the directory.

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

5) Then click the Finish Setup button and you'll be all set to use NextCloud.

How to Set Up NextCloud Email Notification

1) Go to Settings -> Personal Info and then add an email address for your account so that you can receive emails for all activities performed through the NextCloud account. This is helpful when multiple users use the same account.

2) Next, go to Settings -> Basic settings, you'll find two options sendmail and smtp. You can choose sendmail if the SMTP server is running.

3) In case you want an SMTP server, choose the smtp option

How to Reset NextCloud User Password from Command Line

1) In case you lose your password and didn't set up email delivery in NextCloud, you'll have to run the following command. Replace nextcloud_username with your username:

sudo -u www-data php /var/www/nextcloud/occ user:resetpassword nextcloud_username

How to Move the Data Directory

1) You need to run the following commands to move the NextCloud data directory. Use the cp command to copy the data directory to the new location. For instance, for the mount point of an external hard driver /media/linuxbabe/b43e4eea-9796-4ac6-9c48-2bcaa46353731:

sudo mkdir /media/linuxbabe/b43e4eea-9796-4ac6-9c48-2bcaa46353731/nextcloud-data/

2) Then the original data directory is to be copied onto the data directory. -R flag refers to the copy operation being Recursive

sudo cp /var/www/nextcloud-data/* /media/linuxbabe/b43e4eea-9796-4ac6-9c48-2bcaa46353731/nextcloud-data/ -R

3) Then copy the .ocdata file:

sudo cp /var/www/nextcloud-data/.ocdata /media/linuxbabe/b43e4eea-9796-4ac6-9c48-2bcaa46353731/nextcloud-data/

4) After that, set www-data (Apache user) as the owner:

sudo chown www-data:www-data /media/linuxbabe/b43e4eea-9796-4ac6-9c48-2bcaa46353731/nextcloud-data/ -R

5) You then need to edit the config.php file.

sudo nano /var/www/nextcloud/config/config.php

6) Next, find the line given below and change the value of datadirectory:

'datadirectory' => '/var/www/nextcloud-data',

Save and close the file. Reload NextCloud to apply the changes made.

Step 7 - Increase PHP Memory limit

1) By default, the PHP memory limit is 128MB, while NextCloud recommends 512MB for better functioning. For this, you have to edit the php.ini file:

sudo nano /etc/php/8.1/apache2/php.ini

2) Find the following (line 409)

memory_limit = 128M

3) Change its value, then save and close the file.

memory_limit = 512M

4) Alternatively, you can run the following commands in order to change the value without having to change it manually.

sudo sed -i 's/memory_limit = 128M/memory_limit = 512M/g' /etc/php/8.1/apache2/php.ini

5) Reload Apache for the changes to take effect:

sudo systemctl reload apache2

6) If the server has the /etc/php8.1/fpm/php.ini file, your server also runs PHP FPM. It is recommended to change the memory_limit in PHP-FPM too.

sudo nano /etc/php/8.1/fpm/php.ini

7) Look for the memory_limit parameter and change its value. Save the file, then reload PHP FPM for the changes to take effect.

sudo systemctl reload php8.1-fpm

Step 8 - Configure Redis Cache for NextCloud

1) Now go to settings -> overview page, and you should be seeing the following warning:

No memory cache has been configured. To enhance your performance please configure a memcache if available.

2) Run the command given below to enable memory caching by Redis

sudo apt install redis-server

3) Check the version using:

redis-server -v

4) The following will be the output:

Redis server v=5.0.7 sha=00000000:0 malloc=jemalloc-5.2.1 bits=64 build=636cde3b5c7a3923

5) Then, check if the Redis server is running:

systemctl status redis
ℹ️
In case the above command doesn't quit immediately, you can press Q to gain back control of the terminal.

6) The above result substantiates that Redis is running and auto-start is enabled. In case that is not the case, execute the following:

sudo systemctl start redis-server

7) Use the following command to enable auto-start:

sudo systemctl enable redis-server

8) Now, you have to install PHP extensions for Redis interfacing in order to configure it as a cache for NextCloud.

sudo apt install php-redis

9) Check whether the extension is enabled:

php --ri redis

10) Redis extension is enabled and that can be seen in the output. If not, use:

sudo phpenmod redis

11) You may reload Apache if the Redis extension is still not enabled:

sudo systemctl reload apache2

12) Then, edit the NextCloud configuration file:

sudo nano /var/www/nextcloud/config/config.php

13) Then, add the following lines above the ending ); line

'memcache.distributed' => '\OC\Memcache\Redis',
'memcache.local' => '\OC\Memcache\Redis',
'memcache.locking' => '\OC\Memcache\Redis',
'redis' => array(
     'host' => 'localhost',
     'port' => 6379,
     ),
'appstoreenabled' => true,
'appstoreurl' => 'https://apps.nextcloud.com/api/v1',
'appsallowlist' => [],
'apps_paths' => [
        [
                'path'=> '/var/www/nextcloud/apps',
                'url' => '/apps',
                'writable' => true,
        ],
],
'appcodechecker' => true,

14) Save and close, then restart Apache and PHP FPM for the changes to take effect.

sudo systemctl restart apache2 php8.1-fpm

15) If you visit the NextCloud settings -> overview page, you won't find the Warning.

Adding missing indexes

1) You may also see the following message if you visit NextCloud settings -> overview page:

The database is missing some indexes. Due to the fact that adding indexes on big tables could take some time they were not added automatically.

2) You need to manually add those indexes, Change them to the NextCloud webroot directory.

cd /var/www/nextcloud/

3) Run the following in order to add indices to the NextCloud database:

sudo -u www-data php occ db:add-missing-indices

4) If you refresh the NextCloud Settings -> Overview page, you won't find the Warning.

Conversion to Big Int

1) You may also see the following message if you visit NextCloud settings -> overview page:

Some columns in the database are missing a conversion to big int. Due to the fact that changing column types on big tables could take some time they were not changed automatically.

2) Manually change the column type and change to the NextCloud webroot directory.

cd /var/www/nextcloud/

3) Change NextCloud to Maintenance Mode so that other users can't log in and make changes.

sudo -u www-data php occ maintenance:mode --on

4) Next, use the following command to change the column type:

sudo -u www-data php occ db:convert-filecache-bigint

5) On completion, switch off the Maintenance Mode.

sudo -u www-data php occ maintenance:mode --off

6) If you refresh the NextCloud Settings -> Overview page, you won't find the Warning.

How to install NextCloud Client on Ubuntu 22.04 Desktop

1) You can install NextCloud Client on Ubuntu 22.04 Desktop with the help of the following command:

sudo apt install nextcloud-desktop

2) NextCloud Client, Ubuntu 22.04, You may find them here.

How to Enable OnlyOffice

1) NextCloud comes with support for OnlyOffice, which as the name suggests allows you to edit doc, xls, and ppt files directly from NextCloud. For this you need to go toApps -> Office & Text. Find and enable the community document server app.

2) When you click (+) in Nextcloud, you'll be able to edit spreadsheets, documents, etc.

Increase Upload File Size Limit

1) You need to change the file size limit if you are using PHP FPM to run PHP scripts. The default size is 2MB. You can change it with the following commands:

sudo nano /etc/php/8.1/fpm/php.ini

2) Find the following (line 846)

upload_max_filesize = 2M

3) Change its value as shown below, then save and close.

upload_max_filesize = 1024M

4) Alternatively, you can run the following commands in order to change the value without having to change it manually.

sudo sed -i 's/upload_max_filesize = 2M/upload_max_filesize = 1024M/g' /etc/php/8.1/fpm/php.ini

5) Restart PHP FPM

sudo systemctl restart php8.1-fpm

Troubleshooting

You can check for certain common errors mentioned below and solve them:

  • Apache error log: /var/log/apache2/error.log
  • Apache error logs for the Nextcloud virtual host: /var/log/apache2/nextcloud.error
  • Nextcloud application log: /var/www/nextcloud/data/nextcloud.log

FAQs to Install NextCloud on Ubuntu 22.04 with Apache (LAMP Stack)

What is the LAMP stack?

LAMP stack stands for Linux, Apache, MySQL/MariaDB, and PHP. It is a popular combination of software used for web development and hosting.

What are the system requirements for installing Nextcloud?

Nextcloud has minimum system requirements that include a Linux server, Apache web server, MySQL/MariaDB database, and PHP.

Can I access Nextcloud remotely after installation?

Yes, you can access Nextcloud remotely by setting up port forwarding on your router and configuring your firewall to allow external access.

Is Nextcloud secure?

Yes, Nextcloud takes security seriously and provides features like encryption, two-factor authentication, and regular security updates to protect your data.

Can I integrate Nextcloud with other applications?

Yes, Nextcloud offers a wide range of integrations and apps that allow you to connect it with various services like email, calendars, and more.

Can I customize the look and functionality of Nextcloud?

Yes, Nextcloud is highly customizable, allowing you to change the theme, add or remove features, and extend its functionality using available apps and plugins.

Conclusion

We hope this detailed tutorial helped you understand how to install and configure NextCloud on Ubuntu 22.04. To learn more about NextCloud installation on Ubuntu 22.04 server, check out the official NextCloud installation document.

If you have any queries, please leave a comment below and we’ll be happy to respond to them for sure.