Skip to content

Nextcloud Occ Command

DodaTech 3 min read

In this tutorial, you'll learn about Nextcloud occ Command Fails. We cover key concepts, practical examples, and best practices.

Hook

You SSH into your Nextcloud server to run occ maintenance:mode --on and get "sudo: occ: command not found." You are in the Nextcloud directory. The file exists. But the command does not run.

The Wrong Way

Running php occ as root without switching to the web server user creates files owned by root, causing permission errors in the Nextcloud web UI.

# BAD: Running occ as root
sudo php /var/www/nextcloud/occ maintenance:mode --on
Maintenance mode enabled
Nextcloud web UI: "Can't write to config"
Config.php owned by root — web server cannot write

Running occ as root writes cache files and updates config.php with root ownership, breaking the web interface.

The Right Way

Always run occ as the web server user.

# Find the correct user
grep -r "^User\|^Group" /etc/apache2/envvars
# Or for Nginx:
ps aux | grep nginx | head -1
export APACHE_RUN_USER=www-data
export APACHE_RUN_GROUP=www-data
# Run occ as www-data
sudo -u www-data php /var/www/nextcloud/occ maintenance:mode --on
Nextcloud is currently in maintenance mode
Config.php permissions: www-data ✓

If php is not in the www-data PATH, use the full path:

sudo -u www-data /usr/bin/php8.3 /var/www/nextcloud/occ maintenance:mode --off

For convenience, create an alias:

alias ncc='sudo -u www-data php /var/www/nextcloud/occ'
ncc status

Prevention

  • Always prefix occ commands with sudo -u www-data (or your web server user).
  • Never run occ as root — it breaks file permissions.
  • Set up a shell alias (ncc) to avoid typing the full wrapper each time.
  • If you use Docker, exec into the container as the www-data user.
  • Keep php in the system PATH for the web server user.

Advanced Troubleshooting

Check the Logs

Most TOOL errors are logged to stdout or a dedicated log file. Check your logs first:

# Check system logs
journalctl -u tool --since "1 hour ago"

# Or check the application log
tail -50 ~/.tool/logs/error.log

Test with a Minimal Example

Create the simplest possible tool configuration to verify the base setup works:

tool --version
tool --help

If the minimal test passes, add configuration options one at a time until you find the breaking change.

Common Configuration Mistakes

  • Using the wrong file path or URL in configuration
  • Forgetting to restart TOOL after changing config files
  • Mixing tabs and spaces in YAML configuration files
  • Setting incorrect permissions on configuration directories

When to Reinstall

If none of the above resolves the issue, consider a clean reinstall:

# Backup your configuration
cp -r ~/.tool ~/.tool.bak

# Remove and reinstall
# Follow the official TOOL installation guide

This ensures you start from a known good state and can isolate the issue.

Common Mistakes with occ command

  1. Using foldl instead of foldl' causing stack overflow on large lists
  2. Forgetting deriving (Show, Eq) on custom data types needed for debugging
  3. Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable

These mistakes appear frequently in real-world NEXTCLOUD code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

What is the occ command?

occ (OwnCloud Console) is Nextcloud's command-line interface for server management. It handles app management, user administration, maintenance mode, database migrations, and configuration changes.

Why does occ say 'PHP not found'?

PHP may not be installed, or the CLI binary is in a different location than the web server's PHP. Check with which php and php --version. On some distros, the CLI and FPM PHP are separate packages.

Can I run occ from any directory?

No — you must be in the Nextcloud installation directory (usually /var/www/nextcloud). Alternatively, specify the full path to the occ file: php /var/www/nextcloud/occ.


DodaTech — command your Nextcloud server with confidence.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro