Comprehensive Guide to Installing Nextcloud from Source

Nextcloud is a powerful, open-source file sharing and collaboration platform. This guide will walk you through the process of installing Nextcloud from source, giving you maximum control and flexibility over your installation.

Prerequisites

Before we begin, ensure you have:

  1. A server running a supported operating system (e.g., Ubuntu 20.04 LTS)
  2. Root or sudo access to your server
  3. A web server (Apache or Nginx)
  4. PHP 7.4 or newer with required modules
  5. A database (MySQL/MariaDB, PostgreSQL, or SQLite)

Need a reliable server to host your Nextcloud instance? Check out Servers Guru for high-performance servers optimized for web applications.

Step 1: Prepare Your Web Server

Ensure your web server is properly configured. For Apache, enable the following modules:

sudo a2enmod rewrite
sudo a2enmod headers
sudo a2enmod env
sudo a2enmod dir
sudo a2enmod mime

For Nginx, ensure you have a proper server block configuration.

Step 2: Install Required PHP Modules

Install the necessary PHP modules:

sudo apt install php-cli php-mysql php-zip php-gd php-mbstring php-curl php-xml php-bcmath

Step 3: Set Up the Database

Create a database and user for Nextcloud. For MySQL/MariaDB:

CREATE DATABASE nextcloud;
CREATE USER 'nextcloud'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON nextcloud.* TO 'nextcloud'@'localhost';
FLUSH PRIVILEGES;

Replace ‘password’ with a strong, unique password.

Step 4: Download Nextcloud

Download the latest version of Nextcloud:

wget https://download.nextcloud.com/server/releases/latest.tar.bz2
tar -xjf latest.tar.bz2
sudo mv nextcloud /var/www/

Step 5: Set Permissions

Set the correct permissions for the Nextcloud directory:

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

Step 6: Configure Web Server

For Apache, create a new configuration file:

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

Add the following content:

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

<Directory /var/www/nextcloud/>
    Options +FollowSymlinks
    AllowOverride All
    Require all granted
    <IfModule mod_dav.c>
        Dav off
    </IfModule>
</Directory>

Enable the site:

sudo a2ensite nextcloud.conf
sudo systemctl restart apache2

For Nginx, add a server block to your Nginx configuration:

server {
    listen 80;
    server_name your_domain.com;
    
    root /var/www/nextcloud;
    index index.php index.html /index.php$request_uri;
    
    location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
    }
    
    location = /.well-known/carddav {
        return 301 $scheme://$host/remote.php/dav;
    }

    location = /.well-known/caldav {
        return 301 $scheme://$host/remote.php/dav;
    }
    
    location ~ ^/(?:build|tests|config|lib|3rdparty|templates|data)/ {
        deny all;
    }

    location ~ ^/(?:\.|autotest|occ|issue|indie|db_|console) {
        deny all;
    }
    
    location ~ ^/(?:index|remote|public|cron|core/ajax/update|status|ocs/v[12]|updater/.+|ocs-provider/.+)\.php(?:$|/) {
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_param HTTPS on;
        #Avoid sending the security headers twice
        fastcgi_param modHeadersAvailable true;
        fastcgi_param front_controller_active true;
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        fastcgi_intercept_errors on;
        fastcgi_request_buffering off;
    }
    
    location ~ ^/(?:updater|ocs-provider)(?:$|/) {
        try_files $uri/ =404;
        index index.php;
    }
    
    location ~ \.(?:css|js|woff|svg|gif)$ {
        try_files $uri /index.php$request_uri;
        add_header Cache-Control "public, max-age=15778463";
        add_header X-Content-Type-Options nosniff;
        add_header X-XSS-Protection "1; mode=block";
        add_header X-Robots-Tag none;
        add_header X-Download-Options noopen;
        add_header X-Permitted-Cross-Domain-Policies none;
        add_header Referrer-Policy no-referrer;
        access_log off;
    }

    location ~ \.(?:png|html|ttf|ico|jpg|jpeg)$ {
        try_files $uri /index.php$request_uri;
        access_log off;
    }
}

Remember to replace your_domain.com with your actual domain name.

Step 7: Complete the Installation

Navigate to http://your_domain.com/nextcloud in your web browser to complete the installation process. You’ll need to:

  1. Create an admin account
  2. Configure the database connection
  3. Choose your data directory (ensure it’s outside the web root for security)

Step 8: Configure PHP

Optimize your PHP configuration for Nextcloud. Edit your php.ini file:

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

Make the following changes:

memory_limit = 512M
upload_max_filesize = 500M
post_max_size = 500M
max_execution_time = 300
date.timezone = Your/Timezone

Restart your web server after making these changes.

Step 9: Set Up HTTPS

For production use, it’s crucial to secure your Nextcloud instance with HTTPS. You can use Let’s Encrypt to obtain a free SSL certificate:

sudo apt install certbot python3-certbot-apache
sudo certbot --apache -d your_domain.com

Optimizing Your Nextcloud Instance

For the best performance:

  1. Allocate Sufficient Resources: Ensure your server has enough CPU and RAM. Servers Guru offers a range of high-performance options suitable for Nextcloud hosting.

  2. Use Redis for Caching: Install and configure Redis to improve Nextcloud’s performance.

  3. Enable PHP OpCache: This can significantly speed up PHP execution.

  4. Regular Backups: Implement a robust backup strategy for your Nextcloud data and database.

  5. Keep Updated: Regularly update Nextcloud and all its components to ensure security and performance.

By installing Nextcloud from source, you have full control over your instance. This method allows for maximum customization and optimization. Remember to keep your instance updated and secure.

Enjoy your new, self-hosted cloud storage and collaboration platform!