Install Nextcloud on CentOS: Step-by-Step Guide
In this article
This guide walks you through installing and configuring Nextcloud on CentOS (7, 8, or 9). By the end, you'll have a self-hosted file sync and sharing server accessible via HTTPS, with a database backend and proper file permissions. You'll need a CentOS server with root or sudo access, a domain name or IP address, and a basic comfort with the command line.
What You'll Need
- A CentOS server (7, 8, or 9) with at least 2 GB RAM and 20 GB free disk space
- Root or sudo access to the server
- A domain name pointing to your server's IP (optional but recommended for SSL)
- Open ports 80 and 443 in your firewall
Step-by-Step
- Update the system
Log in via SSH and run:
Reboot if the kernel was updated.sudo yum update -y # CentOS 7/8 sudo dnf update -y # CentOS 9 - Install the Remi repository (for PHP 8.1+)
Nextcloud 27+ requires PHP 8.1. CentOS's default PHP is too old. Install Remi:
Then enable PHP 8.1:sudo yum install -y epel-release sudo yum install -y https://rpms.remirepo.net/enterprise/remi-release-7.rpm # CentOS 7 sudo dnf install -y epel-release sudo dnf install -y https://rpms.remirepo.net/enterprise/remi-release-8.rpm # CentOS 8 sudo dnf install -y epel-release sudo dnf install -y https://rpms.remirepo.net/enterprise/remi-release-9.rpm # CentOS 9sudo yum module reset php -y # CentOS 8/9 sudo yum module enable php:remi-8.1 -y - Install required packages
Install Apache, PHP, and necessary extensions:
For MariaDB (database) also install:sudo yum install -y httpd php php-gd php-mbstring php-intl php-mysqlnd php-xml php-json php-zip php-curl php-bz2 php-imagick php-gmp php-bcmath php-apcusudo yum install -y mariadb-server - Start and enable services
sudo systemctl start httpd mariadb sudo systemctl enable httpd mariadb - Secure MariaDB
Set a root password, remove anonymous users, disable remote root login, and remove test databases.sudo mysql_secure_installation - Create the Nextcloud database and user
Then run:sudo mysql -u root -pCREATE DATABASE nextcloud; CREATE USER 'nextclouduser'@'localhost' IDENTIFIED BY 'strong-password'; GRANT ALL PRIVILEGES ON nextcloud.* TO 'nextclouduser'@'localhost'; FLUSH PRIVILEGES; EXIT; - Download Nextcloud
Get the latest version from nextcloud.com/install. Replace the version number with the current one:
If wget is missing, install it:cd /var/www/html sudo wget https://download.nextcloud.com/server/releases/nextcloud-28.0.1.zipsudo yum install -y wget unzip - Extract and set permissions
sudo unzip nextcloud-28.0.1.zip sudo chown -R apache:apache nextcloud sudo chmod -R 755 nextcloud - Configure Apache virtual host
Create/etc/httpd/conf.d/nextcloud.confwith:
Then restart Apache:<VirtualHost *:80> DocumentRoot /var/www/html/nextcloud ServerName your-domain.com <Directory /var/www/html/nextcloud> Require all granted Options FollowSymlinks AllowOverride All <IfModule mod_dav.c> Dav off </IfModule> </Directory> </VirtualHost>sudo systemctl restart httpd - Set up SSL (HTTPS)
Use Let's Encrypt with certbot:
Follow the prompts. Certbot will edit your Apache config and set up auto-renewal.sudo yum install -y certbot python2-certbot-apache # CentOS 7 sudo dnf install -y certbot python3-certbot-apache # CentOS 8/9 sudo certbot --apache -d your-domain.com - Complete installation via web browser
Navigate tohttps://your-domain.com. Create an admin account, enter the database details (nextcloud, nextclouduser, your password), and click "Finish setup". - Configure trusted domains
If you access via IP or a different domain, edit/var/www/html/nextcloud/config/config.phpand add your domain to thetrusted_domainsarray. Then restart Apache.
Common Mistakes
- Forgetting to enable PHP extensions – Nextcloud will complain about missing modules. Double-check with
php -m. - Wrong file permissions – If the web server can't write to the config or data directory, you'll see errors. Ensure
chown -R apache:apacheis run. - Using an outdated PHP version – CentOS's default PHP is 5.4 (CentOS 7) or 7.2 (CentOS 8). You must enable the Remi repo.
- SELinux blocking access – SELinux often blocks Apache from writing to certain directories. Check with
getenforce; if enforcing, either set it to permissive or apply the correct contexts. - Not setting a strong database password – Use a complex password and avoid reusing the admin password.
Tips
- Use a dedicated data directory outside the web root (e.g.,
/var/ncdata) for better security and easier backups. - Set up a cron job for Nextcloud's background jobs instead of AJAX. Edit
config.phpand set'backgroundjobs_mode' => 'cron', then add a cron entry:*/5 * * * * php -f /var/www/html/nextcloud/cron.php. - Enable OPcache for better performance. Add to
/etc/php.d/10-opcache.ini:opcache.enable=1,opcache.memory_consumption=128,opcache.max_accelerated_files=10000. - Regularly back up the
configanddatadirectories, and the database. - Use Redis for caching if you have many users. Install redis and php-pecl-redis, then add the config entries.
Troubleshooting
- Web page shows "502 Bad Gateway" – Usually PHP-FPM isn't running. Check
systemctl status php-fpmand start it. - Nextcloud says "Can't write into config directory" – Fix permissions:
sudo chown -R apache:apache /var/www/html/nextcloud. - HTTPS certificate not renewing – Check certbot timer:
sudo systemctl list-timers | grep certbot. If missing, add a cron job. - Database connection error – Verify MariaDB is running and the credentials are correct. Test with
mysql -u nextclouduser -p. - Blank page after installation – Check Apache error logs:
sudo tail -f /var/log/httpd/error_log. Often a missing PHP extension. - SELinux blocks access – Temporarily set to permissive with
sudo setenforce 0to test. If it works, add proper contexts:sudo semanage fcontext -a -t httpd_sys_rw_content_t '/var/www/html/nextcloud(/.*)?'thenrestorecon -Rv /var/www/html/nextcloud.
Frequently Asked Questions
Can I install Nextcloud on CentOS 6?
No. CentOS 6 is end-of-life and doesn't meet Nextcloud's PHP and database requirements. Use CentOS 7 or newer.
What is the default data directory?
By default, Nextcloud stores data in /var/www/html/nextcloud/data. It's strongly recommended to move it outside the web root.
How do I update Nextcloud?
Use the built-in updater in the admin panel, or manually replace the files. Always back up before updating.
Can I use Nginx instead of Apache?
Yes, but the configuration differs. This guide focuses on Apache, which is the default on CentOS.
Why do I get a "Trusted domain" error?
You accessed Nextcloud via an IP or domain not listed in config.php. Add it to the trusted_domains array.
Is it safe to run Nextcloud on the default Apache port?
Yes, but you should use HTTPS (port 443) to encrypt traffic. The HTTP port can be redirected to HTTPS.