The Dual Kingdom Setup: Nginx + Apache Nextcloud Harmony

Table of Contents

The Dual Kingdom Setup: Nginx + Apache Nextcloud Harmony

๐Ÿฐ The Dual Kingdom Setup: Nginx + Apache Nextcloud Harmony

Why choose between kingdoms when you can rule them both? Let Nginx be the gatekeeper and Apache be the Nextcloud specialist! โš”๏ธ๐Ÿค

๐ŸŽญ Chapter 1: The Master Plan

The Strategy:

  • ๐Ÿฐ Nginx (Port 80/443) - The main fortress, handling all incoming requests
  • ๐Ÿ›๏ธ Apache (Port 8080) - The Nextcloud specialist, running your cloud
  • ๐Ÿ”„ Reverse Proxy - Nginx forwards cloud.thebigocommunity.org to Apache
๐ŸŒ Internet โ†’ ๐Ÿฐ Nginx (80/443) โ†’ ๐Ÿ›๏ธ Apache (8080) โ†’ โ˜๏ธ Nextcloud

๐Ÿ”ง Chapter 2: Revive the Apache Kingdom

๐ŸŒ… Wake Up Apache (But on a Different Port!)

# Check if Apache is installed but just stopped
sudo systemctl status apache2

# If stopped, don't start it yet! We need to configure first
# sudo systemctl start apache2  # DON'T DO THIS YET!

# If not installed, install it
sudo apt update
sudo apt install apache2 php libapache2-mod-php

โš™๏ธ Configure Apache to Use Port 8080

# Edit Apache ports configuration
sudo nano /etc/apache2/ports.conf

Change the ports.conf file:

# /etc/apache2/ports.conf
# Listen on port 8080 instead of 80
Listen 8080

<IfModule ssl_module>
    Listen 8443
</IfModule>

<IfModule mod_gnutls.c>
    Listen 8443
</IfModule>

๐Ÿ›๏ธ Configure Your Nextcloud Apache Site

# Edit your existing Nextcloud Apache configuration
sudo nano /etc/apache2/sites-available/nextcloud.conf

Nextcloud Apache Configuration:

# /etc/apache2/sites-available/nextcloud.conf
<VirtualHost *:8080>
    ServerName cloud.thebigocommunity.org
    DocumentRoot /var/www/html/nextcloud
    
    # Enable required modules for Nextcloud โœจ
    <Directory /var/www/html/nextcloud/>
        Require all granted
        AllowOverride All
        Options FollowSymLinks MultiViews
        
        <IfModule mod_dav.c>
            Dav off
        </IfModule>
    </Directory>
    
    # Security headers (though Nginx will add more) ๐Ÿ›ก๏ธ
    Header always set Strict-Transport-Security "max-age=15552000; includeSubDomains"
    Header always set X-Content-Type-Options nosniff
    Header always set X-Frame-Options "SAMEORIGIN"
    Header always set X-XSS-Protection "1; mode=block"
    Header always set Referrer-Policy "no-referrer"
    
    # Logs for debugging ๐Ÿ“
    ErrorLog ${APACHE_LOG_DIR}/nextcloud_error.log
    CustomLog ${APACHE_LOG_DIR}/nextcloud_access.log combined
    
    # PHP configuration for large file uploads ๐Ÿ“ค
    php_value upload_max_filesize 16G
    php_value post_max_size 16G
    php_value max_input_time 3600
    php_value max_execution_time 3600
    php_value memory_limit 512M
</VirtualHost>

๐Ÿ” Find Your Existing Nextcloud Installation

# Let's find where your Nextcloud is hiding
find /var/www -name "config.php" -path "*/nextcloud/config/*" 2>/dev/null
find /var/www -name "index.php" -path "*/nextcloud*" 2>/dev/null

# Check common locations
ls -la /var/www/html/nextcloud/ 2>/dev/null
ls -la /var/www/nextcloud/ 2>/dev/null
ls -la /var/www/html/ | grep -i next 2>/dev/null

๐Ÿ“‚ Update DocumentRoot in Apache Config

Once you find your Nextcloud directory, update the Apache config:

# If Nextcloud is in /var/www/html/nextcloud
sudo sed -i 's|DocumentRoot /var/www/nextcloud|DocumentRoot /var/www/html/nextcloud|g' /etc/apache2/sites-available/nextcloud.conf

# If it's somewhere else, edit manually
sudo nano /etc/apache2/sites-available/nextcloud.conf

๐Ÿš€ Chapter 3: Launch Apache Nextcloud Service

โœจ Enable Required Apache Modules

# Enable essential modules for Nextcloud
sudo a2enmod rewrite
sudo a2enmod headers
sudo a2enmod env
sudo a2enmod dir
sudo a2enmod mime
sudo a2enmod ssl

# Enable your Nextcloud site
sudo a2ensite nextcloud.conf

# Disable default site on port 8080 if it exists
sudo a2dissite 000-default.conf

# Test Apache configuration
sudo apache2ctl configtest

๐ŸŒ… Start Apache Kingdom

# Start Apache on port 8080
sudo systemctl start apache2
sudo systemctl enable apache2

# Check if it's running correctly
sudo systemctl status apache2

# Test Apache is responding on port 8080
curl -I http://localhost:8080

๐Ÿฐ Chapter 4: Configure Nginx Reverse Proxy

๐ŸŽฏ Create Nginx Configuration for Nextcloud Proxy

# Create the Nginx proxy configuration
sudo nano /etc/nginx/sites-available/nextcloud-proxy

The Ultimate Nginx โ†” Apache Proxy Configuration:

# /etc/nginx/sites-available/nextcloud-proxy
# The bridge between kingdoms! ๐ŸŒ‰

server {
    listen 80;
    server_name cloud.thebigocommunity.org;
    
    # Redirect HTTP to HTTPS ๐Ÿ”
    return 301 https://$server_name$request_uri;
}

server {
    listen 443 ssl http2;
    server_name cloud.thebigocommunity.org;

    # SSL Configuration ๐Ÿ”’
    ssl_certificate /etc/letsencrypt/live/cloud.thebigocommunity.org/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/cloud.thebigocommunity.org/privkey.pem;
    
    # Modern SSL settings
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384;
    ssl_prefer_server_ciphers off;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 1d;

    # Security headers ๐Ÿ›ก๏ธ
    add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload" always;
    add_header X-Content-Type-Options "nosniff" always;
    add_header X-Frame-Options "SAMEORIGIN" always;
    add_header X-XSS-Protection "1; mode=block" always;
    add_header Referrer-Policy "no-referrer" always;

    # Increase upload size for large files ๐Ÿ“ค
    client_max_body_size 16G;
    client_body_timeout 300s;
    client_body_buffer_size 512k;
    
    # Proxy settings for Apache backend ๐Ÿ”„
    location / {
        proxy_pass http://127.0.0.1:8080;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Server $host;
        
        # Important for Nextcloud! ๐ŸŽฏ
        proxy_set_header X-Forwarded-Ssl on;
        proxy_redirect off;
        
        # Timeouts for large file operations โฑ๏ธ
        proxy_connect_timeout 60s;
        proxy_send_timeout 300s;
        proxy_read_timeout 300s;
        
        # Buffer settings for better performance ๐Ÿš€
        proxy_buffering on;
        proxy_buffer_size 8k;
        proxy_buffers 32 8k;
        proxy_busy_buffers_size 16k;
    }
    
    # Special handling for WebDAV (Nextcloud sync) ๐Ÿ“ฑ
    location ~* ^/remote\.php/(dav|webdav|caldav|carddav) {
        proxy_pass http://127.0.0.1:8080;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Forwarded-Host $host;
        
        # WebDAV specific headers ๐Ÿ—‚๏ธ
        proxy_set_header Destination $http_destination;
        proxy_set_header Authorization $http_authorization;
        proxy_pass_request_headers on;
        
        # Longer timeouts for sync operations
        proxy_connect_timeout 300s;
        proxy_send_timeout 300s;
        proxy_read_timeout 300s;
    }
    
    # Well-known paths for clients ๐Ÿ”
    location /.well-known/carddav {
        return 301 $scheme://$host/remote.php/dav;
    }
    
    location /.well-known/caldav {
        return 301 $scheme://$host/remote.php/dav;
    }
    
    # Let's Encrypt ACME challenge ๐Ÿ”
    location /.well-known/acme-challenge {
        root /var/www/html;
        try_files $uri $uri/ =404;
    }

    # Logging for debugging ๐Ÿ“
    access_log /var/log/nginx/nextcloud-proxy.access.log;
    error_log /var/log/nginx/nextcloud-proxy.error.log;
}

๐Ÿ”— Enable Nginx Proxy Configuration

# Enable the proxy site
sudo ln -s /etc/nginx/sites-available/nextcloud-proxy /etc/nginx/sites-enabled/

# Remove any conflicting configurations
sudo rm -f /etc/nginx/sites-enabled/default

# Test Nginx configuration
sudo nginx -t

# If test passes, reload Nginx
sudo systemctl reload nginx

๐Ÿ” Chapter 5: SSL Certificate Quest

๐ŸŽฏ Get SSL Certificate for Your Domain

# Install Certbot if needed
sudo apt install certbot python3-certbot-nginx

# Get SSL certificate
sudo certbot --nginx -d cloud.thebigocommunity.org

# Test auto-renewal
sudo certbot renew --dry-run

โš™๏ธ Chapter 6: Nextcloud Configuration Updates

๐Ÿ”ง Update Nextcloud Trusted Domains

# Find your Nextcloud config directory
find /var/www -name "config.php" -path "*/nextcloud/config/*" 2>/dev/null

# Edit the config (replace path with your actual path)
sudo nano /var/www/nextcloud/config/config.php

Update the trusted_domains section:

<?php
$CONFIG = array (
  'trusted_domains' => 
  array (
    0 => 'localhost',
    1 => 'cloud.thebigocommunity.org',
    2 => '127.0.0.1:8080', // Add this for direct Apache access
  ),
  
  // Add these lines for proper proxy handling ๐Ÿ”„
  'trusted_proxies' => array('127.0.0.1'),
  'overwritehost' => 'cloud.thebigocommunity.org',
  'overwriteprotocol' => 'https',
  'overwritewebroot' => '',
  'overwrite.cli.url' => 'https://cloud.thebigocommunity.org',
  
  // ... rest of your config
);

๐Ÿงช Chapter 7: Testing Your Dual Kingdom

๐Ÿ” Test Everything Works

# Test Apache directly (should work)
curl -I http://localhost:8080

# Test Nginx proxy (should redirect to HTTPS)
curl -I http://cloud.thebigocommunity.org

# Test final HTTPS endpoint
curl -I https://cloud.thebigocommunity.org

# Check service status
sudo systemctl status nginx
sudo systemctl status apache2

๐ŸŒ Browser Test

  1. Go to https://cloud.thebigocommunity.org
  2. You should see your Nextcloud login page! ๐ŸŽ‰
  3. Login and check that everything works as expected

๐Ÿ“ฑ Test Mobile Apps

  • Try connecting with Nextcloud mobile apps
  • Use the URL: https://cloud.thebigocommunity.org

๐Ÿšจ Chapter 8: Troubleshooting Common Issues

๐Ÿ”ง If Nextcloud Shows “Trusted Domain” Error

# Add your domain to trusted domains
sudo -u www-data php /var/www/nextcloud/occ config:system:set trusted_domains 1 --value=cloud.thebigocommunity.org

๐Ÿ“Š Check Logs for Issues

# Nginx proxy logs
sudo tail -f /var/log/nginx/nextcloud-proxy.error.log

# Apache Nextcloud logs  
sudo tail -f /var/log/apache2/nextcloud_error.log

# Nextcloud logs
sudo tail -f /var/www/nextcloud/data/nextcloud.log

๐Ÿ” Port Conflicts Check

# Make sure no other service is using port 8080
sudo netstat -tuln | grep :8080
sudo lsof -i :8080

๐Ÿฅ If Apache Won’t Start on Port 8080

# Check what's using port 8080
sudo ss -tlnp | grep :8080

# Check Apache error logs
sudo tail -f /var/log/apache2/error.log

# Try a different port (like 8081) if needed
sudo sed -i 's/8080/8081/g' /etc/apache2/ports.conf
sudo sed -i 's/8080/8081/g' /etc/apache2/sites-available/nextcloud.conf
sudo sed -i 's/8080/8081/g' /etc/nginx/sites-available/nextcloud-proxy

๐ŸŽฏ Chapter 9: Performance Optimization

โšก Apache Performance Tuning

# Edit Apache configuration
sudo nano /etc/apache2/apache2.conf

Add these optimizations:

# Performance settings for Nextcloud ๐Ÿš€
<IfModule mpm_prefork_module>
    StartServers 8
    MinSpareServers 5
    MaxSpareServers 20
    ServerLimit 256
    MaxRequestWorkers 256
    MaxConnectionsPerChild 0
</IfModule>

# Enable compression
LoadModule deflate_module modules/mod_deflate.so
<Location />
    SetOutputFilter DEFLATE
    SetEnvIfNoCase Request_URI \
        \.(?:gif|jpe?g|png)$ no-gzip dont-vary
    SetEnvIfNoCase Request_URI \
        \.(?:exe|t?gz|zip|bz2|sit|rar)$ no-gzip dont-vary
</Location>

๐Ÿš€ Nginx Optimization

Add to your Nginx config:

# Inside http block in /etc/nginx/nginx.conf
client_body_buffer_size 512k;
client_max_body_size 16G;
proxy_buffering on;
proxy_buffer_size 8k;
proxy_buffers 32 8k;

๐Ÿ† Chapter 10: Victory Celebration!

โœ… Your Dual Kingdom is Now Live!

You now have the best of both worlds:

  • ๐Ÿฐ Nginx handling SSL/TLS, security headers, and acting as a powerful reverse proxy
  • ๐Ÿ›๏ธ Apache doing what it does best - serving your Nextcloud with all its PHP magic
  • ๐Ÿ”„ Seamless integration that users never see
  • ๐Ÿ›ก๏ธ Enhanced security with Nginx as the front-facing fortress
  • ๐Ÿš€ Better performance with optimized proxy settings

๐Ÿ“‹ Quick Status Check Commands

# Check both services
sudo systemctl status nginx apache2

# Monitor real-time logs
sudo tail -f /var/log/nginx/nextcloud-proxy.access.log /var/log/apache2/nextcloud_access.log

# Test the complete chain
curl -IL https://cloud.thebigocommunity.org

๐ŸŽ‰ What You’ve Achieved:

  • โœ… Nextcloud running on Apache (port 8080)
  • โœ… Nginx reverse proxy (port 443)
  • โœ… SSL/TLS encryption
  • โœ… Security headers
  • โœ… Large file upload support
  • โœ… WebDAV/CalDAV/CardDAV support
  • โœ… Mobile app compatibility

Your Nextcloud kingdom is restored and more powerful than ever! The dual monarchy of Nginx and Apache shall serve your cloud for ages to come! ๐Ÿ‘‘โ˜๏ธโœจ


๐Ÿš€ Pro Tips:

  • Both services run independently - if one goes down, you can troubleshoot separately
  • Apache handles all Nextcloud-specific stuff (PHP, .htaccess rules, etc.)
  • Nginx handles modern web stuff (HTTP/2, SSL, security, caching)
  • Perfect for scaling - you can add more Apache instances later and load balance them through Nginx!
Tags :