Build Your Social Network With Diaspora on Debian 9

Published on: Fri, Aug 10, 2018 at 11:13 am EST

Diaspora is a privacy-aware, open source social network. In this tutorial, you will learn how to set up and configure a Diaspora pod on Debian 9.

Prerequisites

  • A Debian 9 server instance.
  • Running an average-sized pod, your server should have, at the very least, 512MB of RAM (+1GB swap space) and a decent multi-core CPU.
  • A sudo user.

Install Prerequisite Packages

First, update the system and install the necessary packages.

sudo apt-get update
sudo apt-get install build-essential libssl-dev libcurl4-openssl-dev libxml2-dev libxslt-dev imagemagick ghostscript curl libmagickwand-dev git libpq-dev redis-server nodejs

Install PostgreSQL

Diaspora supports MySQL, MariaDB, and PostgreSQL. In this guide, we will use PostgreSQL.

Install PostgreSQL.

sudo apt-get install PostgreSQL-server

Connect to PostgreSQL with the postgres user.

sudo -u postgres psql

Create a Diaspora user.

CREATE USER diaspora WITH CREATEDB PASSWORD '<password>';

Add a Dedicated Diaspora User

This is the user account that will run Diaspora.

sudo adduser --disabled-login diaspora

Switch to the new user.

sudo  su - diaspora

Install Ruby

There are several ways to install Ruby. We will use rbenv to manage the environment and the versions.

First, you will need to install the packages Ruby requires.

sudo apt-get install autoconf bison build-essential libssl-dev libyaml-dev libreadline6-dev zlib1g-dev libncurses5-dev libffi-dev libgdbm3 libgdbm-dev

Install rbenv.

git clone https://github.com/rbenv/rbenv.git ~/.rbenv
cd ~/.rbenv && src/configure && make -C src
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
echo 'eval "$(rbenv init -)"' >> ~/.bash_profile

Reconnect to reload the path.

exit
sudo su - diaspora

Install the ruby-build plugin for rbenv to compile Ruby:

git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build

Install Ruby.

rbenv install 2.4.3
rbenv global 2.4.3

Install Exim4

We will use Exim4 as an SMTP relay to send emails to users.

Install and configure the package.

sudo apt-get install exim4
sudo dpkg-reconfigure exim4-config

Install and configure Diaspora

Clone the source code for Diaspora.

cd ~
git clone -b master https://github.com/diaspora/diaspora.git
cd diaspora

Copy the example database configuration file to the location required by Diaspora.

cp config/database.yml.example config/database.yml
cp config/diaspora.yml.example config/diaspora.yml

Open the database configuration file in a text editor to edit some of the settings.

nano config/database.yml

Change the database settings to match the PostgreSQL user and password that you created earlier.

postgresql: &postgresql
adapter: postgresql
host: localhost
port: 5432
username: diaspora
password: __password__
encoding: unicode

Open the Diaspora configuration file.

nano config/diaspora.yml

You will need to update a few settings in this file for Diaspora to work properly.

  • url: Set the public facing URL to your pod here.
  • certificate_authorities: Remove the leading # to uncomment it.
  • rails_environment: You must set this to production.
  • require_ssl: Set this to false to prevent a redirect from http:// to https://.

Install Required Gems

Install Bundle, the Ruby library manager.

gem install bundler
script/configure_bundler

Note: If you have errors concerning your Ruby version, edit .ruby-version and put your own (here 2.4.3 instead of 2.4).

Setup Database

Create and configure the database.

RAILS_ENV=production bin/rake db:create db:migrate

Pre-compile the assets

This rake command will precompile the assets.

RAILS_ENV=production bin/rake assets:precompile

Diaspora systemd Services

There are many ways to manage Diaspora as a service. In this tutorial, we will use Systemd.

First, create the following files.

  • systemd target file: touch /etc/systemd/system/diaspora.target
  • systemd web service file: touch /etc/systemd/system/diaspora-web.service
  • systemd sidekiq service file: touch /etc/systemd/system/diaspora-sidekiq.service

Paste in the following configuration text for each file that you created earlier.

target file:

[Unit]
Description=Diaspora social network
Wants=postgresql.service
Wants=redis-server.service
After=redis-server.service
After=postgresql.service

[Install]
WantedBy=multi-user.target

web service file:

[Unit]
Description=Diaspora social network (unicorn)
PartOf=diaspora.target
StopWhenUnneeded=true

[Service]
User=diaspora
Environment=RAILS_ENV=production
WorkingDirectory=/home/diaspora/diaspora
ExecStart=/bin/bash -lc "bin/bundle exec unicorn -c config/unicorn.rb -E production"
Restart=always

[Install]
WantedBy=diaspora.target

sidekiq service file:

[Unit]
Description=Diaspora social network (sidekiq)
PartOf=diaspora.target
StopWhenUnneeded=true

[Service]
User=diaspora
Environment=RAILS_ENV=production
WorkingDirectory=/home/diaspora/diaspora
ExecStart=/bin/bash -lc "bin/bundle exec sidekiq"
Restart=always

[Install]
WantedBy=diaspora.target

Enable boot services.

sudo systemctl enable diaspora.target diaspora-sidekiq.service diaspora-web.service

Restart the services.

sudo systemctl restart diaspora.target

Ensure that they are running correctly.

sudo systemctl status diaspora-web.service
sudo systemctl status diaspora-sidekiq.service

Nginx Reverse Proxy

We will use Nginx as a reverse proxy to serve static resources.

We will use acme.sh to get a Let's Encrypt certificate.

Download the acme.sh source code.

git clone https://github.com/Neilpang/acme.sh.git

Generate a Let's Encrypt certificate.

./.acme.sh/acme.sh --issue --log \
--dns \
--keylength ec-256 \
--cert-file /etc/nginx/https/cert.pem \
--key-file /etc/nginx/https/key.pem \
--fullchain-file /etc/nginx/https/fullchain.pem \
-d example.com \
-d www.example.com

Install Nginx.

sudo apt-get install nginx

Create a new Nginx configuration file for our Diaspora pod.

nano /etc/nginx/conf.d/diaspora.conf

Populate the file with the following content.

upstream diaspora_server {
 server unix:/home/diaspora/diaspora/tmp/diaspora.sock;
}

server {
  listen 80;
  listen [::]:80; 
  server_name www.example.com example.com;
  return 301 https://example.com$request_uri;

  access_log /dev/null;
  error_log /dev/null;
}

server {
  listen 443 ssl http2;
  listen [::]:443 ssl http2;
  server_name www.example.com example.com;

  if ($host = www.example.com) {
   return 301 https://example.com$request_uri;
  }

  access_log /var/log/nginx/dspr-access.log;
  error_log /var/log/nginx/dspr-error.log;

  ssl_certificate /etc/nginx/https/fullchain.pem;
  ssl_certificate_key /etc/nginx/https/key.pem;

  ssl_protocols TLSv1.2;
  ssl_ciphers EECDH+CHACHA20:EECDH+AESGCM:EECDH+AES;
  ssl_ecdh_curve X25519:P-521:P-384:P-256;
  ssl_prefer_server_ciphers on;
  ssl_stapling on;
  ssl_stapling_verify on;
  resolver 80.67.169.40 80.67.169.12 valid=300s;
  resolver_timeout 5s;
  ssl_session_cache shared:SSL:10m;

  root /home/diaspora/diaspora/public;

  client_max_body_size 5M;
  client_body_buffer_size 256K;

  try_files $uri @diaspora;

  location /assets/ {
    expires max;
    add_header Cache-Control public;
  }

  location @diaspora {
    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 https;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://diaspora_server;
  }
}

Note: change example.com to your own registered domain name.

After all modifications have been completed, check the configuration file for any errors.

sudo nginx -t

Restart Nginx to apply the changes.

sudo systemctl restart nginx

If you now visit your Diaspora pod's domain name in your browser (example: https://example.com), you will reach the Diaspora welcome page.

Create a Diaspora User

Click the link in Start by creating an account., and fill in the details to create a new Diaspora user. Then, you will be able to view your user's home page and start using the Diaspora social network.

After you create an account, give it admin rights:.

Role.add_admin User.where(username: "your_username").first.person

You now have access to the admin dashboard.

https://example.com/admins/dashboard

Sidekiq

Sidekiq, which handles background jobs processing, has a web interface available at https://example.com/sidekiq. The pod stats are available at https://example.com/statistics.

Logrotate

We will use logrotate to manage Diaspora logs.

Create a new logrotate file for Diaspora.

nano /etc/logrotate/diaspora

Then, add the following lines.

/home/diaspora/diaspora/log/*.log {
  notifempty
  copytruncate
  missingok
  compress
  weekly
  rotate 52
}

This will rotate the logs weekly, compress them, and keep them for 52 weeks.

Update Diaspora

When it comes time to update Diaspora, follow these steps.

First, update the system.

sudo apt-get update
sudo apt-get dist-upgrade

Update the Diaspora source code with git.

su - diaspora
cd diaspora
git pull

Update the gems.

gem install bundler
bin/bundle --full-index

Migrate the database and recompile the assets.

RAILS_ENV=production bin/rake db:migrate
RAILS_ENV=production bin/rake assets:precompile

Finally, restart Diaspora.

systemctl restart diaspora.target