1. Prerequisites
In this example we assume that there is already a webserver with ssl enabled running on your server and you have configured a subdomain to be used by nextcloud in your DNS service provider. If your site is example.com, you can create a subdomain nc.example.com to access to it. If you prefer not to have a subdomain, you will have to point the nextcloud’s root folder in the configuration file of your main site.
- Server: You need a server (VPS, dedicated server, or local server) running a Linux distribution (e.g., Ubuntu, CentOS, Debian).
- Web Server: Apache should be installed and running.
- PHP: Ensure PHP is installed (Nextcloud requires PHP 7.1 or higher) and
php-gd
,php-curl
,php-xml
,php-mbstring
,php-zip
. - Other optional but very useful packages, fail2ban and a front end firewall manager like ufw.
sudo apt install certbot python3-certbot-apache wget
Apache modules for enhanced security and for php integration
sudo apt install libapache2-mod-php libapache2-mod-security2
Certbot will provide the certificate for your website’s subdomain while python3-certbot-apache will facilitate the installation of the certificate in your system by integrating apache in the installation process. It will add the necessary lines where the certificates can be accessed into the nc.examle.conf file in folder sites-available and deploy the certificate, among other things
2. Install Nextcloud Server Community edition
https://download.nextcloud.com/server/releases/latest.zip
Step 1: Unzip latest.zip to /var/www. I like to name those kind of folders as the name of the website they are holding in so you should rename it as example.com.
Step 2: Set the correct permissions:
sudo chown -R www-data:www-data /var/www/html/nc.example.com
OPTIONAL INSTALL
Alternatively, you can download the zip file and decompress the folder named nextcloud into your /var/www folder like so:
Download the zip file to your home folder:
wget https://download.nextcloud.com/server/releases/latest.zip
Decompress:
sudo unzip latest.zip -d /var/www/
Rename folder according to the settings in your web server:
mv /var/www/nextcloud /var/www/nc.example.com
Set permissions to the www-data user
Go to your ip address or domain setup for nextcloud after you had created an database user and the database itself.
sudo chown -R www-data:www-data /var/www/nc.example.com
3. Configure Apache for Nextcloud
- Step 1: Enable necessary Apache modules:
sudo a2enmod rewrite headers env dir mime ssl
sudo systemctl restart apache2
Step 2: Create a new Apache configuration file for Nextcloud:
sudo nano /etc/apache2/sites-available/nc.example.com.conf
Add the following configuration (modify paths if necessary):
apache
<VirtualHost *:80>
ServerName nc.example.com
Redirect permanent / https://nc.example.com/
</VirtualHost>
#########################################################
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerName nc.RootDomain
DocumentRoot /var/www/nc.example.com
# NextCloud folder directives
<Directory /var/www/nc.example.com/>
Options +FollowSymlinks
AllowOverride All
Require all granted
Satisfy Any
</Directory>
# Certificates
SSLEngine on
SSLCertificateFile /etc/letsencrypt/live/nc.example.com/cert.pem
SSLCertificateKeyFile /etc/letsencrypt/live/nc.example.com/privkey.pem
# logging
ErrorLog ${APACHE_LOG_DIR}/nc.example.com_error.log
CustomLog ${APACHE_LOG_DIR}/nc.example.com_access.log combined
# Reverse Proxy Directives. End edit appropriately before uncommenting.
# <Location />
# ProxyPass http://localhost:50000/
# ProxyPassReverse http://localhost:50000/
# ProxyPreserveHost On
# RequestHeader set X-Forwarded-Proto "https"
# RequestHeader set X-Forwarded-Port "443"
# </Location>
</VirtualHost>
</IfModule>
I’ve left some reverse proxy directives in the config file. Those are not going to be executed as long as they have the # at the beginning of the line. Remove them if you want.
Once the config file is done and you are planing to have the nextcloud website in a subdomain, get the appropriate certificate with this command:
sudo certbot certonly --webroot -w /var/www/example.com -d nc.example.com
Step 3: Enable the Nextcloud site and restart Apache:
sudo a2ensite nc.example.com
sudo systemctl restart apache2
4. If your plan is to use it for LAN only…
<VirtualHost LAN_IP_ADDRESS:80>
ServerAdmin admin@example.com
ServerName nextcloud.example.com
DocumentRoot /var/www/nextcloud
<Directory /var/www/nextcloud>
Options +FollowSymlinks
AllowOverride All
Require local
# If you want to allow access from specific LAN IP ranges, use:
# Require ip 192.168.1.0/24
</Directory>
ErrorLog ${APACHE_LOG_DIR}/nextcloud_error.log
CustomLog ${APACHE_LOG_DIR}/nextcloud_access.log combined
</VirtualHost>
5. Using MySQL/MariaDB Command Line:
The pre final step is to create a database for nextcloud. Download the appropriate packages if you dont have them already installed on your system. A secure installation of mariadb must have been already performed. I also prefer to name the database the same as the website from whic it receives the data.
- Access MySQL/MariaDB:bash
mysql -u root -p
Create a Database:
CREATE DATABASE
nc.example.com;
Replace nc.example.com
with the name you want for your Nextcloud database.
Create a Database User:
CREATE USER 'nextcloud_user'@'localhost' IDENTIFIED BY 'your_password';
Replace nextcloud_user
with the desired username and your_password
with a strong password.
Grant Permissions:
GRANT ALL PRIVILEGES ON nc.example.com.* TO 'nextcloud_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;
Ensure to replace nc.example.com
and nextcloud_user
with your actual database name and username.
6. Finalize Installation
- Step 1: Open your web browser and navigate to
http://nc.example.com/
- Step 2: Follow the on-screen instructions to complete the Nextcloud setup.
Leave a Reply