Nextcloud lets you store files, photos, contacts, and calendars on your own hardware, keeping data out of third‑party clouds. Installing it on an Android phone gives you a portable, always‑on personal server that can be accessed over Wi‑Fi or, with proper routing, from the internet. Because the process uses Termux, no rooting or custom recovery is required, making it safe for most users.
Safety warning: The server will be reachable by any device on your local network. Do not expose it to the public internet without configuring proper TLS certificates and a firewall.
pkg update && pkg upgrade -y
This updates the package repository and upgrades existing tools.
Nextcloud needs a web server, PHP, and a database. In Termux we’ll use nginx, php-fpm, and mariadb. Install them with a single command:
pkg install nginx php-fpm mariadb proot tar wget openssl -y
Explanation of each package:
nginx – lightweight HTTP server.php-fpm – PHP FastCGI Process Manager, required by Nextcloud.mariadb – MySQL‑compatible database engine.proot – provides a chroot‑like environment that lets us run Linux binaries without root.tar and wget – for downloading and extracting the Nextcloud archive.openssl – used later to generate a self‑signed certificate.~/nextcloud:mkdir -p ~/nextcloud && cd ~/nextcloud
wget https://download.nextcloud.com/server/releases/nextcloud-28.0.0.tar.bz2
tar -xjf nextcloud-28.0.0.tar.bz2 --strip-components=1
The --strip-components=1 flag removes the top‑level folder from the archive, placing all files directly into ~/nextcloud.
Nextcloud stores all metadata in a database. We’ll create a dedicated database and user.
mysqld_safe --datadir=$PREFIX/var/lib/mysql &
Wait a few seconds, then secure the installation:
mysql_secure_installation
You’ll be prompted for a root password and a series of security questions. Choose a strong password and answer “Y” to all prompts.
mysql -u root -p
CREATE DATABASE nextcloud CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
CREATE USER 'nc_user'@'localhost' IDENTIFIED BY 'StrongPassword123!';
GRANT ALL PRIVILEGES ON nextcloud.* TO 'nc_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;
Replace StrongPassword123! with a password you can remember securely.
Nextcloud requires a few PHP extensions. Most are already bundled with the php-fpm package, but we need to enable them in the configuration file.
$PREFIX/etc/php/php.ini (or create it if missing):nano $PREFIX/etc/php/php.ini
Add or uncomment the following lines:
memory_limit = 512M
upload_max_filesize = 1024M
post_max_size = 1024M
max_execution_time = 360
extension=mysqli.so
extension=gd.so
extension=zip.so
extension=xml.so
extension=mbstring.so
extension=intl.so
extension=curl.so
Save with Ctrl+O then exit with Ctrl+X.
We’ll create a simple server block that points to the Nextcloud directory and forwards PHP requests to php-fpm.
mkdir -p $PREFIX/etc/nginx/conf.d
nano $PREFIX/etc/nginx/conf.d/nextcloud.conf
Paste the following content (adjust server_name if you plan to use a custom hostname):
server {
listen 8080 default_server;
server_name localhost;
root /data/data/com.termux/files/home/nextcloud;
index index.php index.html /index.php;
client_max_body_size 1024M;
fastcgi_buffers 64 4K;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ ^/(?:build|tests|config|lib|3rdparty|templates|data)/ {
deny all;
}
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_pass unix:/data/data/com.termux/files/usr/var/run/php-fpm.sock;
}
location ~ \\.{php|phar}$ {
deny all;
}
}
This configuration listens on port 8080 – you can change it later if needed.
We need three daemons running: MariaDB, PHP‑FPM, and Nginx. Termux provides the termux-services package to manage them, but for simplicity we’ll start them manually.
mysqld_safe --datadir=$PREFIX/var/lib/mysql &
php-fpm -y $PREFIX/etc/php/php-fpm.conf &
nginx -c $PREFIX/etc/nginx/nginx.conf
If you receive “permission denied” for the socket, make sure the directory $PREFIX/var/run exists:
mkdir -p $PREFIX/var/run && php-fpm -y $PREFIX/etc/php/php-fpm.conf &
Open a browser on any device connected to the same Wi‑Fi network and navigate to:
http://PHONE_IP_ADDRESS:8080
Replace PHONE_IP_ADDRESS with the IP shown by ifconfig or ip addr inside Termux. You should see the Nextcloud setup wizard.
/data/data/com.termux/files/home/nextcloud/data) or point to a location on external storage (requires proper permissions).nc_usernextcloudlocalhostWhen the dashboard appears, you have a working Nextcloud instance!
Running on plain HTTP is fine for local use, but if you ever expose the server to the internet you’ll want TLS.
openssl req -newkey rsa:2048 -nodes -keyout nextcloud.key -x509 -days 365 -out nextcloud.crt -subj "/C=US/ST=State/L=City/O=MyNextcloud/OU=IT/CN=$(hostname)"
mkdir -p $PREFIX/etc/nginx/ssl
mv nextcloud.key nextcloud.crt $PREFIX/etc/nginx/ssl/
nextcloud.conf to listen on 443 and use the certificate:nano $PREFIX/etc/nginx/conf.d/nextcloud.conf
Replace the existing listen 8080 default_server; line with:
listen 443 ssl http2;
ssl_certificate $PREFIX/etc/nginx/ssl/nextcloud.crt;
ssl_certificate_key $PREFIX/etc/nginx/ssl/nextcloud.key;
Optionally keep the HTTP block and add a redirect:
server {
listen 80;
server_name localhost;
return 301 https://$host$request_uri;
}
nginx -s reload
Now access the server via https://PHONE_IP_ADDRESS. Your browser will warn about a self‑signed cert – accept it for local testing.
Termux sessions stop when you close the app. To keep Nextcloud alive, use the termux-wake-lock command or install the termux-services package and create a service script.
pkg install termux-services
~/.termux/boot/mariadb.sh):#!/data/data/com.termux/files/usr/bin/sh
mysqld_safe --datadir=$PREFIX/var/lib/mysql &
Make it executable:
chmod +x ~/.termux/boot/mariadb.sh
php-fpm.sh and nginx.sh).termux-setup-storage # grants storage permission if not yet done
termux-wake-lock # prevents Doze mode from killing the process
Now, whenever you open Termux, run the three scripts or add them to ~/.bashrc for automatic start.
https://PHONE_IP_ADDRESS or http://PHONE_IP_ADDRESS:8080).Because the server runs on your phone, files are stored on the device’s internal storage or on an attached SD card (if you configured the data folder there).
If Nginx fails to start with “bind() to 0.0.0.0:8080 failed (98: Address already in use)”, another app is occupying the port. Either stop that app or change the listen directive to an unused port (e.g., 8081) and adjust the URL accordingly.
Make sure all required PHP extensions are enabled in php.ini. If you see messages about missing intl or gd, install them via:
pkg install php-intl php-gd
Then restart php-fpm.
Double‑check the username, password, and database name you entered in the web installer. You can test the connection manually:
mysql -u nc_user -p -e "SHOW DATABASES;"
If the command fails, revisit the steps in Step 4.
Nextcloud needs write access to the data folder and the configuration directory. Set the correct ownership:
chmod -R 750 ~/nextcloud
chmod -R 750 ~/nextcloud/data
If you store data on external SD card, ensure Termux has the READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE permissions (run termux-setup-storage).
Android’s Doze mode can pause background processes. Use termux-wake-lock while the server is needed, or whitelist Termux from battery optimizations:
settings put global ignore_battery_optimize 1
Note: The exact path may differ per manufacturer; you can also manually disable battery optimisation for Termux in Settings → Battery → Battery optimisation.
pkg upgrade -y inside Termux.mysqldump -u nc_user -p nextcloud > nextcloud.sql and store the dump on another device or cloud.config and data folders), and run the web updater or the occ command:php occ upgrade
Run the command from the Nextcloud directory:
cd ~/nextcloud && php occ upgrade
By following this guide you now have a fully functional Nextcloud server running directly on your Android phone, without rooting or flashing custom firmware. You can store files, share links, sync calendars, and even install Nextcloud apps (e.g., Talk, Photos) just as you would on a dedicated NAS.
The same principles apply to other lightweight web services – you’ve essentially turned Termux into a mini Linux environment capable of hosting PHP‑based applications.









