Setting up a self-hosted Dropbox-like Cloud Storage Service in 5 min. using AWS EC2 & AWS S3.
Dropbox is one of the market leaders in Cloud Storage. It has more than 500 million users and around 1.2 billion files are being uploaded daily Dropbox has a free basic plan with 2 GB space.
We can set up our own Dropbox-like Cloud Storage service using NextCloud. NextCloud is an open-source Cloud Storage platform that has many features like Files, Chat, Preview, Collaboration and many more. There are dozens of Apps are available to make NextCloud more powerful and extensively useful for personal as well as enterprise usages.
We’ll be using AWS S3 for storing the files and NextCloud installed on the EC2 machine to host our self-hosted Cloud Storage Service. We’ll be using NextCloud Docker Image to run the application on the EC2 machine.
Prerequisites:
- AWS Console Access
- Ubuntu 18.04 LTS
- Docker & Docker-Compose installed on the VM
- AWS S3 Bucket
- Nginx installed on the VM (reverse proxy)
- Domain Name to be mapped
- SSL certificate for the Domain
Step 1: Create an EC2 Instance on AWS
We need to create an EC2 instance on AWS. We can use any instance type (depending upon the requirement, I used t2.micro). Make sure that we configure everything properly especially key management, security group configuration, storage configuration, etc. Make sure that we allow only HTTP/HTTPS ports publicly and block all other ports. We can allow ssh for our IP Addresses to configure the application server.
Step 2: Install Docker & Docker-Compose
After the successful SSH on the VM, we need to install the required applications on the VM. Please use the following commands to install and setup Docker on the Ubuntu VM.
sudo apt update
sudo apt upgrade
sudo apt install software-properties-common && sudo apt install docker.io && apt install docker-compose
Please check the version of Docker & Docker-Compose
sudo docker — version
sudo docker-compose — version
Step 3: Set up an S3 Bucket
We need to create an S3 Bucket for storing our data on S3 securely. We need to set up an IAM user with appropriate permissions on the S3 Bucket and would require the programmatic access (Access Key & Secret Access Key), this would be required to set up S3 as external storage on NextCloud. Make sure we store the access key & secret access key to a safe place.
Step 4: Domain Name for the Application
We can use any domain name that we already have. The domain name or sub-domain name needs to be pointed to the EC2 instance.
Step 5 Setup & Configuration of NextCloud using Docker-Compose
Now, we’ll set up the NextCloud Application using Docker-Compose. NextCloud official images are available on the Docker Hub and we can download/pull the latest images from Docker Hub and use them to configure our setup. We will use the docker-compose.yml file to set up the application. There are 2 parts of the docker-compose.yml file first the App (Nextcloud Image) and the second one is DB (MySQL/MariaDB Image). Please create a folder and put the docker-compose.yml file there.
sudo mkdir -p /opt/docker-projects/nextcloud
sudo cd /opt/docker-projects/nextcloud
sudo vi docker-compose.yml
Paste following the following to the docker-compose.yml file. Please change the “custompassword”.
version: ‘2’volumes:
nextcloud:
db:services:
db:
image: mariadb
command: — transaction-isolation=READ-COMMITTED — binlog-format=ROW
restart: always
volumes:
— db:/var/lib/mysql
environment:
— MYSQL_ROOT_PASSWORD=custompassword
— MYSQL_PASSWORD=custompassword
— MYSQL_DATABASE=nextcloud
— MYSQL_USER=nextcloudapp:
image: nextcloud
ports:
— 8080:80
links:
— db
volumes:
— nextcloud:/var/www/htmlrestart: always
Save the file and run the following command to run the Docker-Compose based NextCloud Application & Database.
sudo docker-compose up -d (Run the docker in the detach mode)
sudo docker-compose ps (See the running containers)
Now, our NextCloud Docker Container is up & running and the database for the same is also configured properly. This will store the data on the volumes we mentioned in the docker-compose.yml file for both App & DB.
We can check the NextCloud Application on http://remote-ip:8080 as we mentioned the port 8080 to be mapped with the port 80 of the NextCloud App container. We need to create an admin account and password for this.
Note: Before doing anything, let’s map the domain name and configure SSL certificates on the same.
Step 6: Map the Domain
We need to Map a Domain Name with the Static IP Address of the EC2 machine using the control panel of Domain Registrar.
Step 7: Install Ngnix Web Server & configure SSL Certificate on the Application Server
As we have set up Docker-based NextCloud Application and Domain Name is pointed to the EC2 machine. We need to install Nginx because NextCloud Application is running on port 8080 and we want it to run on port 80/443 (HTTP/HTTPS) so we need the Ngnix web server run as a reverse proxy. Let us install Ngnix and configure the same. Also, we need to install LetsEncrpt to get a free SSL Certificate if we don’t have an SSL Certificate for the Domain.
sudo apt install nginx
sudo apt install letsencrypt
sudo service nginx start
sudo vi /etc/nginx/sites-available/nextcloud.conf
Put the following to the nextcloud.conf
server {
listen 80;
listen [::]:80 ipv6only=on;server_name nextcloud.customdomain.com;
root /var/www/customdomain.com;index index.html;
location / {
try_files $uri $uri/ =404;
}
}
Run the following command to enable the site.
sudo ln -s /etc/nginx/sites-available/nextcloud.conf /etc/nginx/sites-enabled/nextcloud.conf
sudo nginx -t
sudo service nginx reload
Let’s obtain a free SSL Certificate from LetsEncrypt.
sudo service nginx stop && sudo certbot certonly
sudo nginx -t
sudo nginx start
As we get the SSL Certificate from LetsEncrypt. We will modify the nextcloud.conf file to configure Nginx as a reverse proxy with HTTPS redirection.
sudo vi /etc/nginx/sites-available/nextcloud.conf
Put the following in the nextcloud.conf
server {
if ($host = nextcloud.customdomain.com) {
return 301 https://$host$request_uri;
}listen 80;server_name nextcloud.customdomain.com;
return 404;
}server {
server_name nextcloud.customdomain.com;
root /var/www/customdomain.com;index index.html;location / {
proxy_pass http://localhost:8080/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;add_header Strict-Transport-Security “max-age=31536000; includeSubDomains; preload”;
client_max_body_size 0;access_log /var/log/nginx/nextcloud.access.log;
error_log /var/log/nginx/nextcloud.error.log;
}location /.well-known/carddav {
return 301 $scheme://$host/remote.php/dav;
}location /.well-known/caldav {
return 301 $scheme://$host/remote.php/dav;
}listen 443 ssl;
ssl_certificate /etc/letsencrypt/live/nextcloud.customdomain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/nextcloud.customdomain.com/privkey.pem;
ssl_trusted_certificate /etc/letsencrypt/live/nextcloud.customdomain.com/chain.pem;
}
sudo nginx -t
sudo service nginx reload
Now, we can start using our Cloud Storage Service on https://customdomain.com and configure the admin user and password.
Step 8: Configure NextCloud
After a successful login, we can configure NextCloud as per our requirements like users, email setup, theme setup, etc.
Step 9: Configure NextCloud to use S3 as external storage. We’ll use the “External Storage Support” App to enable S3 integration with NextCloud. Go to App in by clicking the admin user on the top-right pane. Search “external” and enable “External Storage Support”.
After this, we can find the “External Storage Support” in the Settings →Administration section, where we can configure S3 integration.
Now, let us configure the S3 in NextCloud as external storage. We would require S3 Bucket Name, Region, Access Key, and Secret Access Key to configure the same. Also, enable SSL to make sure that the traffic in-transit is encrypted. We can assign the S3 storage to all users, specific users or a single user as well. We can also configure multiple buckets and assign them to specific users depending upon the requirement.
We can explore more Apps and features to enhance the experience and usability of Nextcloud.
Add-ons:
Update Nextcloud Image:
As we are storing/mounting our volume to the EC2 instance, we can update NextCloud/MySQL Images at any time. Data will be intact. However, we can make sure the proper data backup/snapshot procedures are in place to avoid any kind of discrepancies.
sudo docker-compose stop && sudo docker-compose rm --force && sudo docker-compose pull && sudo docker-compose build --pull && sudop docker-compose up -d
Please let me know any feedback & comments.