Installing Nextcloud on Ubuntu 22.04 LTS with Apache and Let’s Encrypt SSL

Nextcloud is a versatile, open-source file-sharing and synchronization platform that allows you to centrally store personal documents, files, photos, and more. It functions similarly to cloud storage services like Dropbox, Google Drive, and iCloud, while offering the added benefit of enhanced privacy. By deploying Nextcloud on your own server, you gain complete control over your data. Additionally, you can easily sync your files across various devices, including desktops, laptops, and smartphones.

This guide provides a step-by-step process to install Nextcloud on an Ubuntu 22.04 server.

Requirements

  • Ubuntu 22.04 server.
  • A valid domain name linked to your server’s IP address.
  • Root privileges configured on the server.

Installing Apache, PHP, and MariaDB

A LAMP stack is a prerequisite. To install it, execute the following command:

apt install apache2 mariadb-server php php-cli php-fpm php-json php-intl php-imagick php-pdo php-mysql php-zip php-gd php-mbstring php-curl php-xml php-pear php-bcmath apache2 libapache2-mod-php -y

Next, modify the PHP configuration to optimize settings:

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

Adjust the following parameters:

    date.timezone = UTC
    memory_limit = 512M
    upload_max_filesize = 500M
    post_max_size = 500M
    max_execution_time = 300

Save your changes and restart Apache:

systemctl restart apache2

Creating a Database for Nextcloud

MariaDB will serve as the database backend for Nextcloud. Begin by accessing the MariaDB shell:

mysql

Then, establish a new database and user:

CREATE DATABASE nextcloud;
CREATE USER 'nextcloud'@'localhost' IDENTIFIED BY 'password';

Grant privileges to the Nextcloud user:

GRANT ALL PRIVILEGES ON nextcloud.* TO 'nextcloud'@'localhost';

Apply changes and exit the MariaDB shell:

FLUSH PRIVILEGES;
QUIT;

Downloading Nextcloud

Retrieve the latest release of Nextcloud (e.g., version 24.0.1) using:

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

Extract the downloaded archive:

unzip nextcloud-24.0.1.zip

Move the resulting directory to Apache’s web root:

mv nextcloud /var/www/html/

Adjust directory ownership and permissions:

chown -R www-data:www-data /var/www/html/nextcloud
chmod -R 775 /var/www/html/nextcloud

Configuring an Apache Virtual Host for Nextcloud

Create a new virtual host configuration file for Nextcloud:

nano /etc/apache2/sites-available/next.conf

Insert the configuration:

<VirtualHost *:80>
     ServerAdmin admin@example.com
     DocumentRoot /var/www/html/nextcloud
     ServerName next.example.com
     ErrorLog /var/log/apache2/nextcloud-error.log
     CustomLog /var/log/apache2/nextcloud-access.log combined
  
    <Directory /var/www/html/nextcloud>
    Options +FollowSymlinks
    AllowOverride All
    Require all granted
    SetEnv HOME /var/www/html/nextcloud
    SetEnv HTTP_HOME /var/www/html/nextcloud
    <IfModule mod_dav.c>
      Dav off
    </IfModule>
    </Directory>
  </VirtualHost>

Save and close the file, then enable the virtual host and necessary Apache modules:

a2ensite next
a2enmod rewrite dir mime env headers

Restart Apache to load the new settings:

systemctl restart apache2

Check Apache’s status to confirm it’s running:

systemctl status apache2
? apache2.service - The Apache HTTP Server
     Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
     Active: active (running) since Fri 2022-06-17 15:04:27 UTC; 4s ago
       Docs: https://httpd.apache.org/docs/2.4/
     Process: 16746 ExecStart=/usr/sbin/apachectl start (code=exited, status=0/SUCCESS)
     Main PID: 16750 (apache2)
      Tasks: 6 (limit: 2292)
     Memory: 14.7M
     CPU: 98ms
     CGroup: /system.slice/apache2.service
             ??16750 /usr/sbin/apache2 -k start
             ??16751 /usr/sbin/apache2 -k start
             ??16752 /usr/sbin/apache2 -k start
             ??16753 /usr/sbin/apache2 -k start
             ??16754 /usr/sbin/apache2 -k start
             ??16755 /usr/sbin/apache2 -k start

  Jun 17 15:04:27 ubuntu2204 systemd[1]: Starting The Apache HTTP Server...

Accessing the Nextcloud Web Interface

Navigate to http://next.example.com in your browser. The Nextcloud installation screen should appear:

NextCloud Admin Login

Configure Nextcloud database

Create your admin account by entering a username, password, and database credentials, then click Install. Upon completion, you’ll see:

Recommended NextCloud apps

Install the recommended apps by clicking their respective button. The next screen should appear:

Tasks after install

Select All files to access the storage manager:

NextCloud GUI

To view your photos, click on Your photos:

NextCloud photo

Enabling SSL on Nextcloud

For secure communications, activate SSL. First, install Certbot:

apt-get install python3-certbot-apache -y

Run Certbot to obtain an SSL certificate from Let’s Encrypt:

certbot --apache -d next.example.com

Enter your email and accept the terms of service:

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator standalone, Installer None
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel): your-email@example.com

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
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 at
https://acme-v02.api.letsencrypt.org/directory
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
(A)gree/(C)ancel: A

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
Would you be willing 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

Select if you want HTTP to redirect to HTTPS:

Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.
  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
  1: No redirect - Make no further changes to the webserver configuration.
  2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for 
  new sites, or if you're confident your site works on HTTPS. You can undo this
  change by editing your web server's configuration.
  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
  Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 2

After selecting option 2 and hitting Enter, you will see:

Enabled Apache rewrite module
Redirecting vhost in /etc/apache2/sites-enabled/next.conf to ssl vhost in /etc/apache2/sites-available/next-le-ssl.conf

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Congratulations! You have successfully enabled https://next.example.com

You should test your configuration at:
https://www.ssllabs.com/ssltest/analyze.html?d=next.example.com
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/next.example.com/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/next.example.com/privkey.pem
   Your cert will expire on 2022-09-21. To obtain a new or tweaked
   version of this certificate in the future, simply run certbot again
   with the "certonly" option. To non-interactively renew *all* of
   your certificates, run "certbot renew"
 - 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

Conclusion

Congratulations! You have successfully installed Nextcloud with Apache and a Let’s Encrypt SSL certificate on Ubuntu 22.04. You can now easily upload your files, documents, and photos, and synchronize them across all your devices.

Frequently Asked Questions (FAQ)

1. Why should I use Nextcloud?

Nextcloud offers a secure, private alternative to other cloud storage solutions, allowing you to control your data on a self-hosted server.

2. Is Ubuntu 22.04 the only supported operating system?

While this guide is tailored for Ubuntu 22.04, Nextcloud is compatible with other Linux distributions and even certain Windows and macOS platforms, albeit with different setup procedures.

3. Can I install Nextcloud without a domain name?

While possible, it’s recommended to use a domain name for ease of access and to utilize SSL certificates effectively.

4. How can I ensure my Nextcloud installation remains secure?

Regularly update your Nextcloud instance and its dependencies. Consider additional security measures such as firewalls and regular security audits.

5. What should I do if my SSL certificate is about to expire?

Use Certbot’s renew command (certbot renew) to renew your SSL certificate before it expires.