Atlantic.Net Blog

How to Set Up a Seafile Server with Nginx on Ubuntu 18.04

Seafile is a free and open-source cloud-based file sharing platform that can be used for storing files and synchronizing the data across multiple devices. Seafile is a self-hosted and high-performance storage system that allows you to host on your own private servers. Seafile is free to use and very similar to other storage providers including OwnCloud, NextCloud, Google Drive and DropBox.

With Seafile, you can access and sync your files, contacts and data across PC and mobile devices.

Seafile comes with a rich set of features including support for client encryption, support for version control, LDAP authentication, two-factor authentication, antivirus  integration, Office web app integration, public link sharing, reliable file synicing, Drive Client support, and many more.

In this tutorial, we will explain how to install and configure a Seafile server with Nginx as a reverse proxy on Ubuntu 18.04.

Step 1 – Install Required Dependencies

First, you will need to install all the dependencies needed for Seafile server installation. You can install all of them by running the following command:

apt-get update -y
apt-get install python2.7 libpython2.7 python-setuptools python-pil python-ldap python-urllib3 ffmpeg python-pip python-mysqldb python-memcache python-requests unzip wget -y

Once all the dependencies are installed, you can proceed to the next step.

Step 2 – Install Nginx and MariaDB

Next, you will need to install the Nginx web server and MariaDB database server in your server. You can install them by running the following command:

apt-get install nginx mariadb-server mariadb-client -y

Once the installation is completed, start the Nginx and MariaDB service and enable it to start after system reboot:

systemctl start nginx
systemctl start mariadb
systemctl enable nginx
systemctl enable mariadb

Once you are finished, you can proceed to the next step.

Step 3 – Create a Database for Seafile

Seafile consists of three main components: Seahub, Seafile server and Ccnet server. Each component requires a separate database to store its data. You will need to create a separate database for each of these components.

To do so, login to the MariaDB shell with the following command:

mysql -u root -p

Provide your root password when prompt then create the required databases with the following commands:

CREATE DATABASE seafiledb character set = 'utf8';
CREATE DATABASE ccnetdb character set = 'utf8';
CREATE DATABASE seahubdb character set = 'utf8';

Next, create the database user with the following command:

CREATE USER 'seafileuser'@'localhost' IDENTIFIED BY 'password';

Next, grant all the privileges to the Seafile database user with the following command:

GRANT ALL ON seafiledb.* TO 'seafileuser'@'localhost' IDENTIFIED BY 'password' WITH GRANT OPTION;
GRANT ALL ON ccnetdb.* TO 'seafileuser'@'localhost' IDENTIFIED BY 'password' WITH GRANT OPTION;
GRANT ALL ON seahubdb.* TO 'seafileuser'@'localhost' IDENTIFIED BY 'password' WITH GRANT OPTION;

Next, flush the privileges and exit from the MariaDB shell with the following command:

FLUSH PRIVILEGES;
EXIT;

At this point, you have created all the databases required to store the Seafile data.

Step 4 – Install Seafile Server

Next, download the latest version of Seafile from their official website with the wget command:

wget https://download.seadrive.org/seafile-server_7.0.5_x86-64.tar.gz

Once downloaded, extract the downloaded file with the following command:

tar -xvzf seafile-server_7.0.5_x86-64.tar.gz

Next, copy the extracted directory to the Nginx web root directory:

cp -r seafile-server-7.0.5 /var/www/html/seafile

Next, start the Seafile installation script with the following command:

cd /var/www/html/seafile
./setup-seafile-mysql.sh

During the installation, you will be prompted to answer several questions as shown below:

Checking python on this machine ...
  Checking python module: python-mysqldb ... Done.

-----------------------------------------------------------------
This script will guide you to setup your seafile server using MySQL.
Make sure you have read seafile server manual at

        https://github.com/haiwen/seafile/wiki

Press ENTER to continue
-----------------------------------------------------------------


What is the name of the server? It will be displayed on the client.
3 - 15 letters or digits
[ server name ] seafile

What is the ip or domain of the server?
For example: www.mycompany.com, 192.168.1.101
[ This server's ip or domain ] seafile.example.com

Where do you want to put your seafile data?
Please use a volume with enough free space
[ default "/var/www/html/seafile-data" ]

Which port do you want to use for the seafile fileserver?
[ default "8082" ]

-------------------------------------------------------
Please choose a way to initialize seafile databases:
-------------------------------------------------------

[1] Create new ccnet/seafile/seahub databases
[2] Use existing ccnet/seafile/seahub databases

[ 1 or 2 ] 2

What is the host of mysql server?
[ default "localhost" ]

What is the port of mysql server?
[ default "3306" ]

Which mysql user to use for seafile?
[ mysql user for seafile ] seafileuser

What is the password for mysql user "seafileuser"?
[ password for seafileuser ]

verifying password of user seafileuser ...  done

Enter the existing database name for ccnet:
[ ccnet database ] ccnetdb

verifying user "seafileuser" access to database ccnetdb ...  done

Enter the existing database name for seafile:
[ seafile database ] seafiledb

verifying user "seafileuser" access to database seafiledb ...  done

Enter the existing database name for seahub:
[ seahub database ] seahubdb

verifying user "seafileuser" access to database seahubdb ...  done

---------------------------------
This is your configuration
---------------------------------

    server name:            seafile
    server ip/domain:       seafile.example.com

    seafile data dir:       /var/www/html/seafile-data
    fileserver port:        8082

    database:               use existing
    ccnet database:         ccnetdb
    seafile database:       seafiledb
    seahub database:        seahubdb
    database user:          seafileuser



---------------------------------
Press ENTER to continue, or Ctrl-C to abort
---------------------------------

Once the installation has been completed successfully, you should get the following output:

-----------------------------------------------------------------
Your seafile server configuration has been finished successfully.
-----------------------------------------------------------------

run seafile server:     ./seafile.sh { start | stop | restart }
run seahub  server:     ./seahub.sh  { start <port> | stop | restart <port> }

-----------------------------------------------------------------
If you are behind a firewall, remember to allow input/output of these tcp ports:
-----------------------------------------------------------------

port of seafile fileserver:   8082
port of seahub:               8000

When problems occur, Refer to

        https://github.com/haiwen/seafile/wiki

for information.

Next, give permissions to Seafile with the following command:

chown -R www-data:www-data /var/www/html/

Next, you will need to add a FILE_SERVER_ROOT setting in the file seahub_settings.py:

nano /var/www/html/conf/seahub_settings.py

Add the FILE_SERVER_ROOT settings as shown below:

SECRET_KEY = "x)0=j*l6b+4amq2n^&)c=q5p==exn13%s&6x!*48u4p0p97k)4"
FILE_SERVER_ROOT = 'http://seafile.example.com/seafhttp'
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
:        'NAME': 'seahubdb',
        'USER': 'seafileuser',
        'PASSWORD': 'password',
        'HOST': '127.0.0.1',
        'PORT': '3306'
    }
}

Save and close the file when you are finished.

Step 5 – Create Admin User for Seafile

Next, you will need to create an admin user and set a password for Seafile. To do so, change the directory to seafile and start the seafile service with the following command:

cd /var/www/html/seafile
su -p -l www-data -s /bin/bash -c "./seafile.sh start"

Next, start the seahub service and create an admin account with the following command:

su -p -l www-data -s /bin/bash -c "./seahub.sh start"

You will be asked to provide your email address and admin password as shown below:

LC_ALL is not set in ENV, set to en_US.UTF-8
Starting seahub at port 8000 ...

----------------------------------------
It's the first time you start the seafile server. Now let's create the admin account
----------------------------------------

What is the email for the admin account?
[ admin email ] [email protected]

What is the password for the admin account?
[ admin password ]

Enter the password again:
[ admin password again ]



----------------------------------------
Successfully created seafile admin
----------------------------------------

Seahub is started

Done.

Once you are done, stop the seafile and seahub services:

su -p -l www-data -s /bin/bash -c "./seafile.sh stop"
su -p -l www-data -s /bin/bash -c "./seahub.sh stop"

Step 6 – Create a Systemd File for Seafile and Seahub

Next, you will need to create a systemd unit file for Seafile and Seahub to manage the service.

First, create a seafile service file with the following command:

nano /etc/systemd/system/seafile.service

Add the following lines:

[Unit]
Description=Seafile
After= mysql.service
After=network.target

[Service]
User=www-data
Group=www-data

Type=forking
ExecStart=/var/www/html/seafile-server-latest/seafile.sh start
ExecStop=/var/www/html/seafile-server-latest/seafile.sh stop

[Install]
WantedBy=multi-user.target

Save and close the file. Then, create a systemd service file for Seahub with the following command:

nano /etc/systemd/system/seahub.service

Add the following lines:

[Unit]
Description=Seafile
After= mysql.service
After=network.target

[Service]
User=www-data
Group=www-data
Type=forking

ExecStart=/var/www/html/seafile-server-latest/seahub.sh start
ExecStop=/var/www/html/seafile-server-latest/seahub.sh stop

[Install]
WantedBy=multi-user.target

Save and close the file. Then, reload the systemd daemon with the following command:

systemctl daemon-reload

Next, restart Seafile and Seahub service and enable them to start after system reboot with the following command:

systemctl start seafile
systemctl enable seafile
systemctl start seahub
systemctl enable seahub

To verify the Seafile service, run the following command:

systemctl status seafile

Output:

● seafile.service - Seafile
   Loaded: loaded (/etc/systemd/system/seafile.service; disabled; vendor preset: enabled)
   Active: active (running) since Thu 2019-11-28 08:48:31 UTC; 8s ago
  Process: 15487 ExecStart=/var/www/html/seafile-server-latest/seafile.sh start (code=exited, status=0/SUCCESS)
    Tasks: 16 (limit: 1150)
   CGroup: /system.slice/seafile.service
           ├─15522 /var/www/html/seafile/seafile/bin/seafile-controller -c /var/www/html/ccnet -d /var/www/html/seafile-data -F /var/www/html/c
           ├─15524 ccnet-server -F /var/www/html/conf -c /var/www/html/ccnet -f /var/www/html/logs/ccnet.log -d -P /var/www/html/pids/ccnet.pid
           └─15527 seaf-server -F /var/www/html/conf -c /var/www/html/ccnet -d /var/www/html/seafile-data -l /var/www/html/logs/seafile.log -P

Nov 28 08:48:28 ubuntu1804 systemd[1]: Starting Seafile...

To verify the Seahub service run the following command:

systemctl status seahub

Output:

● seahub.service - Seafile
   Loaded: loaded (/etc/systemd/system/seahub.service; disabled; vendor preset: enabled)
   Active: active (running) since Thu 2019-11-28 08:50:49 UTC; 18s ago
  Process: 15547 ExecStart=/var/www/html/seafile-server-latest/seahub.sh start (code=exited, status=0/SUCCESS)
 Main PID: 15573 (python2.7)
    Tasks: 6 (limit: 1150)
   CGroup: /system.slice/seahub.service
           ├─15573 python2.7 /var/www/html/seafile/seahub/thirdpart/gunicorn seahub.wsgi:application -c /var/www/html/conf/gunicorn.conf --prel
           ├─15579 python2.7 /var/www/html/seafile/seahub/thirdpart/gunicorn seahub.wsgi:application -c /var/www/html/conf/gunicorn.conf --prel
           ├─15580 python2.7 /var/www/html/seafile/seahub/thirdpart/gunicorn seahub.wsgi:application -c /var/www/html/conf/gunicorn.conf --prel
           ├─15581 python2.7 /var/www/html/seafile/seahub/thirdpart/gunicorn seahub.wsgi:application -c /var/www/html/conf/gunicorn.conf --prel
           ├─15582 python2.7 /var/www/html/seafile/seahub/thirdpart/gunicorn seahub.wsgi:application -c /var/www/html/conf/gunicorn.conf --prel
           └─15583 python2.7 /var/www/html/seafile/seahub/thirdpart/gunicorn seahub.wsgi:application -c /var/www/html/conf/gunicorn.conf --prel

Nov 28 08:50:43 ubuntu1804 systemd[1]: Starting Seafile...

Step 7 – Configure Reverse Proxy with Nginx

Next, you will need to install and configure Nginx as a reverse proxy for Seafile to forward the client requests from ports 8000 and 8082 to the Nginx port 80.

To do so, create an Nginx virtual host configuration file with the following command:

nano /etc/nginx/sites-available/seafile

Add the following lines:

server {
    listen 80;
    listen [::]:80;
    root /var/www/html/seafile;
    server_name  seafile.example.com;

     client_max_body_size 100M;

     autoindex off;

     access_log /var/log/nginx/example.com.access.log;
     error_log /var/log/nginx/example.com.error.log;

     location / {
            proxy_pass         http://127.0.0.1:8000;
            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-Host $server_name;
            proxy_read_timeout  1200s;
        }

     location /seafhttp {
            rewrite ^/seafhttp(.*)$ $1 break;
            proxy_pass http://127.0.0.1:8082;
            proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_connect_timeout  36000s;
            proxy_read_timeout  36000s;
            proxy_send_timeout  36000s;
            send_timeout  36000s;
        }
    location /media {
            root /var/www/html/seafile-server-latest/seahub;
        }
}

Save and close the file when you are finished. Then, enable the Nginx virtual host with the following command:

ln -s /etc/nginx/sites-available/seafile /etc/nginx/sites-enabled/

Finally, restart the Nginx service with the following command:

systemctl restart nginx

Step 9 – Access Seafile Web Interface

Now, open your web browser and navigate the URL http://seafile.example.com. You should see the Seafile login page:

Provide your admin username, password and click on the Log in button. You should see the Seafile default dashboard:

Conclusion

In the above tutorial, we learned how to install the Seafile server with Nginx as a reverse proxy on Ubuntu 18.04 VPS. I hope you have now enough knowledge to host your own file sharing server using Seafile. Get started with Seafile today on a VPS from Atlantic.Net!

Get a $250 Credit and Access to Our Free Tier!

Free Tier includes:
G3.2GB Cloud VPS a Free to Use for One Year
50 GB of Block Storage Free to Use for One Year
50 GB of Snapshots Free to Use for One Year