To install Nextcloud from the command line, prepare a LEMP or LAMP stack, create a database, download the Nextcloud release, configure your web server and TLS, then run the non interactive “occ maintenance:install” command. Finally, enable cron, Redis caching, and hardening settings. The steps below cover Ubuntu/Debian (Nginx/Apache), Snap, and Docker options.
If you want a private, self hosted cloud, installing Nextcloud from the command line is the fastest, most controllable method. This guide shows how to Install NextCloud From Command Line on Ubuntu/Debian using Nginx (LEMP) or Apache (LAMP), plus quick alternatives with Snap and Docker—fully optimized, secure, and production ready.
What You’ll Need (Prerequisites)
Before you begin, ensure the following are in place:
- A fresh Ubuntu 22.04/24.04 or Debian 12 server (VPS or dedicated)
- Sudo or root access via SSH
- A domain (e.g., cloud.example.com) with DNS A/AAAA records pointing to your server
- Open ports: 80 (HTTP) and 443 (HTTPS)
- At least 2 GB RAM (4+ GB recommended for larger teams), SSD storage
- Time synchronized (systemd-timesyncd or chrony)
Tip: For reliable performance, a VPS with dedicated vCPU and NVMe SSD helps. YouStable’s SSD VPS plans provide stable resources and easy OS templates ideal for Nextcloud.
Quick Install Paths (Choose Your Method)
- LEMP (Nginx + PHP-FPM + MariaDB/PostgreSQL): Best performance, flexible hardening
- LAMP (Apache + PHP + MariaDB/PostgreSQL): Simpler for Apache users
- Snap package: Fast all in one, minimal tuning needed
- Docker/Compose: Portable and reproducible deployments
Below we deep dive into LEMP on Ubuntu/Debian, then show Apache, Snap, and Docker variants.
Install Nextcloud from Command Line (Ubuntu/Debian, Nginx) – Step-by-Step
1) Update system and install core packages
sudo apt update && sudo apt -y upgrade
sudo apt -y install nginx mariadb-server redis-server unzip wget curl gnupg2 ufw software-properties-common
sudo ufw allow OpenSSH
sudo ufw allow "Nginx Full"
sudo ufw enable
2) Install PHP-FPM and required extensions
sudo apt -y install php-fpm php-cli php-mysql php-gd php-curl php-xml php-zip php-mbstring php-intl php-bcmath php-gmp php-imagick php-apcu php-redis php-ldap
Ensure PHP-FPM is running and note the socket path, e.g., /run/php/php8.2-fpm.sock.
3) Secure MariaDB and create the Nextcloud database
sudo mysql_secure_installation
sudo mysql -u root -p
# Inside the MariaDB shell:
CREATE DATABASE nextcloud CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
CREATE USER 'ncuser'@'localhost' IDENTIFIED BY 'StrongPasswordHere';
GRANT ALL PRIVILEGES ON nextcloud.* TO 'ncuser'@'localhost';
FLUSH PRIVILEGES;
EXIT;
Alternatively, you can use PostgreSQL for better scalability. The “occ” installer supports both.
4) Download and verify Nextcloud
wget https://download.nextcloud.com/server/releases/latest.zip
wget https://download.nextcloud.com/server/releases/latest.zip.asc
wget https://nextcloud.com/nextcloud.asc
gpg --import nextcloud.asc
gpg --verify latest.zip.asc latest.zip # verify signature (recommended)
unzip latest.zip
sudo mv nextcloud /var/www/nextcloud
sudo mkdir -p /var/www/nextcloud-data
sudo chown -R www-data:www-data /var/www/nextcloud /var/www/nextcloud-data
sudo find /var/www/nextcloud/ -type d -exec chmod 750 {} \;
sudo find /var/www/nextcloud/ -type f -exec chmod 640 {} \;
5) Configure Nginx server block
sudo nano /etc/nginx/sites-available/nextcloud
server {
listen 80;
server_name cloud.example.com;
root /var/www/nextcloud;
client_max_body_size 512M;
fastcgi_buffers 64 4K;
add_header X-Content-Type-Options nosniff;
add_header X-Frame-Options SAMEORIGIN;
add_header X-XSS-Protection "1; mode=block";
add_header Referrer-Policy no-referrer;
add_header X-Download-Options noopen;
add_header X-Permitted-Cross-Domain-Policies none;
index index.php index.html /index.php$request_uri;
location = /robots.txt { allow all; log_not_found off; access_log off; }
location ~ ^/(?:build|tests|config|lib|3rdparty|templates|data)/ { deny all; }
location ~ ^/(?:\.|autotest|occ|issue|indie|db_|console) { deny all; }
location / {
try_files $uri $uri/ /index.php$request_uri;
}
location ~ \.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 modHeadersAvailable true;
fastcgi_param front_controller_active true;
fastcgi_pass unix:/run/php/php8.2-fpm.sock;
fastcgi_intercept_errors on;
fastcgi_request_buffering off;
}
location ~ \.(?:css|js|woff2?|svg|gif|map)$ {
try_files $uri /index.php$request_uri;
add_header Cache-Control "public, max-age=15778463";
access_log off;
}
location ~ \.(?:png|html|ttf|ico|jpg|jpeg)$ {
try_files $uri /index.php$request_uri;
access_log off;
}
}
sudo ln -s /etc/nginx/sites-available/nextcloud /etc/nginx/sites-enabled/nextcloud
sudo nginx -t && sudo systemctl reload nginx
6) Obtain a Let’s Encrypt SSL certificate
sudo apt -y install certbot python3-certbot-nginx
sudo certbot --nginx -d cloud.example.com --redirect --agree-tos -m admin@example.com -n
sudo systemctl reload nginx
This adds HTTPS with automatic redirects and renewal timers.
7) Run the non interactive Nextcloud installer (occ)
sudo -u www-data php /var/www/nextcloud/occ maintenance:install \
--database "mysql" \
--database-name "nextcloud" \
--database-user "ncuser" \
--database-pass "StrongPasswordHere" \
--admin-user "ncadmin" \
--admin-pass "AnotherStrongPassword" \
--data-dir "/var/www/nextcloud-data"
For PostgreSQL, use “–database pgsql” and supply a PostgreSQL user and DB.
8) Enable caching, trusted domains, and security settings
# Set the trusted domain and URL
sudo -u www-data php /var/www/nextcloud/occ config:system:set trusted_domains 1 --value=cloud.example.com
sudo -u www-data php /var/www/nextcloud/occ config:system:set overwrite.cli.url --value=https://cloud.example.com
# Enable APCu (local cache) and Redis (file locking/memcache)
sudo -u www-data php /var/www/nextcloud/occ config:system:set memcache.local --value='\OC\Memcache\APCu'
sudo -u www-data php /var/www/nextcloud/occ config:system:set memcache.locking --value='\OC\Memcache\Redis'
sudo -u www-data php /var/www/nextcloud/occ config:system:set redis --value='{"host":"127.0.0.1","port":6379}' --type=json
# Set recommended defaults
sudo -u www-data php /var/www/nextcloud/occ background:cron
sudo -u www-data php /var/www/nextcloud/occ config:system:set default_phone_region --value=US
sudo -u www-data php /var/www/nextcloud/occ config:system:set logtimezone --value=UTC
9) Configure cron for background jobs
sudo crontab -u www-data -e
*/5 * * * * php -f /var/www/nextcloud/cron.php >/dev/null 2>&1
Background jobs handle previews, cleanups, and federation tasks efficiently.
10) Verify the installation
sudo -u www-data php /var/www/nextcloud/occ status
sudo tail -f /var/log/nginx/access.log /var/log/nginx/error.log
Open https://cloud.example.com in your browser. You should see the Nextcloud login.
Apache Variant (LAMP) in Brief
Prefer Apache? Replace Nginx steps with the following essentials.
sudo apt -y install apache2 libapache2-mod-php
sudo a2enmod rewrite headers env dir mime ssl http2 proxy_fcgi setenvif
sudo a2dismod php* # use PHP-FPM with Apache for performance
sudo a2enconf php8.2-fpm
sudo nano /etc/apache2/sites-available/nextcloud.conf
<VirtualHost *:80>
ServerName cloud.example.com
DocumentRoot /var/www/nextcloud
<Directory /var/www/nextcloud/>
Require all granted
AllowOverride All
Options FollowSymLinks MultiViews
</Directory>
<FilesMatch "\.php$">
SetHandler "proxy:unix:/run/php/php8.2-fpm.sock|fcgi://localhost/"
</FilesMatch>
Header always set Strict-Transport-Security "max-age=15552000; includeSubDomains"
ErrorLog ${APACHE_LOG_DIR}/nextcloud-error.log
CustomLog ${APACHE_LOG_DIR}/nextcloud-access.log combined
</VirtualHost>
sudo a2ensite nextcloud
sudo a2dissite 000-default
sudo systemctl reload apache2
sudo apt -y install certbot python3-certbot-apache
sudo certbot --apache -d cloud.example.com --redirect -m admin@example.com --agree-tos -n
Then run the same “occ maintenance:install” and hardening commands shown earlier.
Optional: Snap or Docker from Command Line
Snap (fastest single node install)
sudo apt -y install snapd
sudo snap install core
sudo snap install nextcloud
# Create admin:
sudo nextcloud.manual-install ncadmin "AnotherStrongPassword"
# Add domain and TLS:
sudo nextcloud.occ config:system:set trusted_domains 1 --value=cloud.example.com
sudo nextcloud.enable-https lets-encrypt
Docker Compose (portable)
mkdir -p ~/nextcloud && cd ~/nextcloud
nano docker-compose.yml
services:
db:
image: mariadb:11
command: --transaction-isolation=READ-COMMITTED --binlog-format=ROW
restart: unless-stopped
environment:
- MYSQL_ROOT_PASSWORD=RootPass
- MYSQL_DATABASE=nextcloud
- MYSQL_USER=ncuser
- MYSQL_PASSWORD=StrongPasswordHere
volumes:
- db:/var/lib/mysql
redis:
image: redis:7-alpine
restart: unless-stopped
app:
image: nextcloud:latest
restart: unless-stopped
ports:
- "8080:80"
depends_on:
- db
- redis
environment:
- MYSQL_HOST=db
- MYSQL_DATABASE=nextcloud
- MYSQL_USER=ncuser
- MYSQL_PASSWORD=StrongPasswordHere
volumes:
- nextcloud:/var/www/html
links:
- redis
volumes:
db:
nextcloud:
docker compose up -d
# Access via http://server-ip:8080 and finalize setup. Add a proper reverse proxy and TLS (e.g., Caddy/Traefik/Nginx) for production.
Performance Tuning and Hardening
PHP and upload limits
sudo nano /etc/php/8.2/fpm/php.ini
memory_limit = 512M
upload_max_filesize = 512M
post_max_size = 512M
opcache.enable=1
opcache.enable_cli=1
opcache.memory_consumption=256
opcache.interned_strings_buffer=16
opcache.max_accelerated_files=10000
opcache.validate_timestamps=1
opcache.revalidate_freq=60
expose_php = Off
sudo systemctl restart php8.2-fpm
Nginx security headers and HSTS
Add or confirm these in your server block for better security and caching control.
add_header Strict-Transport-Security "max-age=15552000; includeSubDomains" always;
add_header X-Content-Type-Options nosniff;
add_header X-Frame-Options SAMEORIGIN;
add_header X-XSS-Protection "1; mode=block";
add_header Referrer-Policy no-referrer;
Database basics
- Use utf8mb4 for full Unicode support
- Run mysql_secure_installation and limit remote root access
- Schedule backups (logical dump + offsite copy)
Backups and updates
# Files backup (example)
rsync -Aax /var/www/nextcloud/ /backup/nextcloud-app/
rsync -Aax /var/www/nextcloud-data/ /backup/nextcloud-data/
# MariaDB backup
mysqldump -u root -p nextcloud > /backup/nextcloud_$(date +%F).sql
Test restore procedures regularly. Keep the OS and PHP packages updated.
Common Errors and Fixes from the CLI
- 403 or 404 after install: Check Nginx/Apache vhost root path and try_files rules.
- File upload size too small: Increase upload_max_filesize and post_max_size, reload PHP/Nginx.
- “Can’t write into config directory”: Fix ownership to www data and correct permissions.
- Background jobs not running: Ensure crontab for www data is set to run every 5 minutes.
- Redis not used: Confirm php redis installed and Redis settings set via occ.
- SELinux (RHEL based): Set proper contexts or temporarily permissive for testing.
Upgrade Nextcloud Safely from the Command Line
- Create backups of app, data, and database
- Put Nextcloud in maintenance mode
sudo -u www-data php /var/www/nextcloud/occ maintenance:mode --on
sudo -u www-data php /var/www/nextcloud/occ upgrade
sudo -u www-data php /var/www/nextcloud/occ maintenance:mode --off
For major upgrades, read the official changelog and upgrade step-by-step across supported versions.
Why Host Nextcloud on YouStable
- SSD or NVMe backed VPS with dedicated vCPU for faster PHP and database performance
- Clean IPv4, modern data centers, and quick OS templates (Ubuntu/Debian)
- 24×7 expert support that understands web hosting, WordPress, and self hosted stacks like Nextcloud
If you want predictable performance and simple scaling, YouStable’s VPS and dedicated servers are a strong fit for Nextcloud deployments of any size.
FAQ’s
1. Is Nginx or Apache better for a Nextcloud CLI install?
Both work well. Nginx with PHP-FPM typically offers better resource usage and performance, while Apache can be simpler to configure for some admins. For large instances, Nginx + PHP-FPM + Redis is a popular choice.
2. What are the minimum PHP modules for Nextcloud?
At minimum: php-fpm, php-cli, php-mysql or php-pgsql, php-xml, php-zip, php-gd, php-curl, php-mbstring, php-intl. For better caching and file locking, add php-apcu and php-redis, and php-imagick for previews.
3. How do I enable HTTPS during a command line install?
Use Certbot with the Nginx or Apache plugin to issue Let’s Encrypt certificates, then auto configure redirects and renewal. Example: “certbot –nginx -d cloud.example.com –redirect -n”. For Docker, terminate TLS in your reverse proxy.
4. Can I install Nextcloud with PostgreSQL from the CLI?
Yes. Install PostgreSQL, create a database and user, then run the installer with “–database pgsql” and the correct credentials. Many large deployments prefer PostgreSQL for concurrency and stability.
5. What’s the fastest way to deploy Nextcloud for testing?
Snap is the quickest single node method. Run “snap install nextcloud”, set the admin user, and enable Let’s Encrypt. For production grade portability, Docker with a reverse proxy (Caddy/Traefik/Nginx) is also fast and reproducible.
With these steps, you can Install NextCloud From Command Line confidently, optimize performance, and harden security from day one. As your storage and users grow, consider scaling up on a YouStable VPS or dedicated server for consistent, reliable performance.