Steps to host your own Nextcloud service.
The Task
The Goals
- Manual deploy, i.e. no Docker or Snap.
- Support for online previewing for pictures, videos and documents.
- Nginx for reverse proxy
The Steps
1. Install Nextcloud
-
Install dependencies.
sudo apt install apache2 mariadb-server libapache2-mod-php7.2 sudo apt install php7.2-gd php7.2-json php7.2-mysql php7.2-curl php7.2-mbstring sudo apt install php7.2-intl php-imagick php7.2-xml php7.2-zip
Note
-
miradb
can be replaced withmysql
.
-
- Download
nextcloud-15.0.5.zip
from official website andunzip
it. - Copy site files to webserver’s data root:
sudo cp -r nextcloud /var/www
. - Change owner / executor to user
www-data
. - Create database
nextcloud
and database usernextclouduser@localhost
, and grant access. - Set-up default Apache server, in order to test reverse proxy (config file copied from here).
- Here I bound service to
localhost:80/nextcloud
.
- Here I bound service to
2. Install Nginx
sudo apt install nginx
-
cat /etc/passwd | grep www-data
to make sure we have an HTTP user.- Here it’s
www-data
under Ubuntu.
- Here it’s
sudo service nginx start
- Open a browser tab and visit
127.0.0.1
to check if Nginx is running properly.-
If there exists a previous installation of other web servers, say the Apache server we’ve just installed via
sudo apt install
, good chance is that anindex.html
already exists in/var/www/html/
. However, for the default Nginx configuration, the HelloWorld page is loaded in the following order …index index.html index.htm index.nginx-debian.html;
So don’t panic when you are greeted by Apache !
-
Also, do remember to change the default port configuration, so it doesn’t collide with the existing Apache server!
-
3. [Optional] Configure Nginx for Nextcloud
Nginx will be used for reverse proxy at the end of the day, therefore a dedicate Nginx-powered Nextcloud service is unnecessary! I just did this for fun !
- Create configuration file
/etc/nginx/conf.d/nextcloud.conf
.- Copied from here (or Nginx Configuration offered in reference).
- Here I bound service to
localhost:9001
.
-
sudo apt install php7.2-fpm
- Required for Nginx to run PHP files, or
502 Bad Gateway
.
- Required for Nginx to run PHP files, or
sudo systemctl start php7.2-fpm.service
sudo systemctl reload nginx.service
At this point, both localhost:80/nextcloud
(via Apache) and localhost:9001
(via Nginx) can reach our Nextcloud service.
4. Configure Nginx for Reverse Proxy
-
Replace
/etc/nginx/conf.d/nextcloud.conf
with following linesupstream localserver1 { server 127.0.0.1:80; # our local nextcloud service # you may have more than just one `server` statement in # this block, which enables Nginx to dynamically switch # between upstreams based on load. } server { listen 9001; server_name _; # redirect every subdir to our Apache-powered Nextcloud location / { proxy_pass http://localserver1/nextcloud/; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }
Note
- The
proxy_set_header
lines are required, or Nextcloud will identify the Nginx proxy, disallowing access.
- The
-
Visit
localhost:9001
.- After Nginx caching for sometime, you will see browser’s URL stops at
localhost/nextcloud/index.php/xxxx
, which indicates our Nginx reverse proxy is working as expected.
- After Nginx caching for sometime, you will see browser’s URL stops at
5. Final Touches
- Open browser tab and visit Nextcloud service.
- Walk through Nextcloud initialization step in its fancy WebApp.
- Which can be done in command line (see Installing from command line).
- Enjoy.