Manual Installation of NextCloud on Ubuntu with Apache

Manual Installation of NextCloud on Ubuntu with Apache

This tutorial goes through the entire process of installing NextCloud on an Ubuntu Server. I tested it on Ubuntu 24.04 but it should work on older versions (at least 22.04). The only prerequisite to this tutorial is that you have a fresh Ubuntu install. The official NextCloud installation docs can be found here: https://docs.nextcloud.com/server/latest/admin_manual/installation/source_installation.html and Ubuntu 22.04 specific docs found here: https://docs.nextcloud.com/server/latest/admin_manual/installation/example_ubuntu.html (I based this tutorial almost entirely off of these two docs). Let’s dive!

Installing Apache & MariaDB & Other Packages

We will first begin by installing the necessary dependencies to run NextCloud via: 

							
							
					sudo apt update && sudo apt upgrade
sudo apt install apache2 mariadb-server libapache2-mod-php php-gd php-mysql \
php-curl php-mbstring php-intl php-gmp php-bcmath php-xml php-imagick php-zip				
			

This installs the necessary dependencies for NextCloud, addon NextCloud apps may require additional dependencies see: https://docs.nextcloud.com/server/latest/admin_manual/installation/source_installation.html#prerequisites-for-manual-installation for more info.

Creating a MariaDB User & DB

Next we will create a database for NextCloud and a database user that NextCloud will run as to interact with the database. We will enter the MySQL cli mode via:

							
							
					sudo mysql				
			

Then run the following command, be sure to replace “username” with the username you desire and “password” with your password.

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

To exit the mysql cli mode enter: 

							
							
					quit;				
			

Downloading the Latest NextCloud Version

Next we will download the latest NextCloud archive version via: 

							
							
					wget https://download.nextcloud.com/server/releases/latest.tar.bz2
#or 
wget https://download.nextcloud.com/server/releases/latest.zip				
			

You can find the checksum values / files and pgp key to verify authenticity and download integrity on the NextCloud install page here: https://nextcloud.com/install/ just go to Download for Server -> Community Projects -> Archive.  For speed and simplicity I skipped this step, although in production or according to company policies you may need to verify these details.

Next we will unpack the archive via: 

							
							
					tar -xjvf latest.tar.bz2
#or
unzip latest.zip				
			

If you do not have unzip installed you can install it via:

							
							
					sudo apt-get install unzip				
			

Next we will copy the NextCloud directory to the Apache document root /var/www.

							
							
					sudo cp -r nextcloud /var/www				
			

And finally we will transfer the NextCloud directory ownership to our HTTP user via:

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

Apache Configuration

Next we will configure the Apache web server. We need to first create the Apache site config file for our nextcloud site at /etc/apache/sites-available/nextcloud.conf 

We can either set it up to be a directory install or a virtual host install. Basically if you want your NextCloud install to be accessible from a sub-directory of an existing site such as https://mydomain.com/nextcloud then use the directory install and if you want nextcloud to be accessible from a root domain or a sub-domain such as mydomain.com or cloud.mydomain.com then use the virtual host setup. 

Below is the directory setup:

							
							
					Alias /nextcloud "/var/www/nextcloud/"

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

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

And the virtual host setup

							
							
					<VirtualHost *:80>
  DocumentRoot /var/www/nextcloud/
  ServerName  your.server.com

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

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

We will then enable the site config via:

							
							
					a2ensite nextcloud.conf				
			

We need to install the following additional apache modules, run:

							
							
					a2enmod rewrite
a2enmod headers
a2enmod env
a2enmod dir
a2enmod mime				
			

Now we can restart apache:

							
							
					sudo systemctl restart apache2				
			

Then visit your.domain.com to see your nextcloud setup and finish the installation processes as directed. After you finish the installation according to the instructions we will install ssl certs for https connections.

Let's Encrypt SSL Setup

Finally we will setup HTTPS via Let’s Encrypt’s free ssl certificate service. We will use Certbot to streamline the setup, you can install it via:

							
							
					sudo apt update
sudo apt install certbot python3-certbot-apache
				
			

Then enter the certbot prompt to configure and install the ssl certificate via:

							
							
					sudo certbot --apache
				
			

You will have to enter your email, select the domain for which to setup https, and it is recommended to set it to redirect all http traffic to https traffic.

Let’s Encrypt certificates are valid for 90 days, Certbot automatically creates a cron job to renew the certificate, however just to be sure that automatic renewals are working run: 

							
							
					sudo certbot renew --dry-run
				
			

Also be sure that your domain is set in the NextCloud trusted domains setting, open the /var/www/nextcloud/config/config.php file and add your domain in the “trusted_domains” array key.

							
							
					'trusted_domains' =>
  array (
    0 => 'localhost',
    1 => 'your_domain.com',
  ),				
			

Restart apache via:

							
							
					sudo systemctl restart apache2				
			

And now navigate to https://your.domain.com to ensure https is enabled!

That’s a wrap! 

Walter Miely is a tech entrepreneur and CEO of Phoenix Ignited Tech You can find him on Linkedin. This material is licensed under the CC BY 4.0 License LEGAL DISCLAIMER: The content provided here is provided AS IS, and part of, or the entirety of this content may be incorrect. Please read the entireLegal Disclaimer here.