Ricerca nel sito web

Come installare Mastodon Social Network su Ubuntu 22.04


Su questa pagina

  1. Prerequisiti
  2. Per iniziare
  3. Installa Node.js
  4. Installa e configura PostgreSQL
  5. Installa Ruby
  6. Installa e configura Mastodon
  7. Crea un file di servizio Systemd per Mastodon
  8. Configura Nginx come proxy inverso
  9. Proteggi Mastodon con Lets Encrypt SSL
  10. Accedi all'interfaccia web di Mastodon

Mastodon è un software gratuito e open source per l'esecuzione di servizi di social networking self-hosted. Offre funzionalità di microblogging che ti consentono di seguire altri utenti e pubblicare messaggi e immagini con Mastodon. È scritto in Ruby e JavaScript e supporta post audio, video e immagini, descrizioni di accessibilità, sondaggi, avvisi sui contenuti, avatar animati, emoji personalizzati e altro ancora. Mastodon offre un'applicazione per varie piattaforme come Android e iOS.

In questo tutorial, ti mostreremo come installare Mastodon su Ubuntu 22.04.

Aggiornamento: la guida è stata aggiornata e testata nuovamente con successo il 30/11/2022 per chiarire l'installazione e risolvere i problemi menzionati nei commenti, poiché alcune versioni del software sono cambiate da quando è stata scritta la guida.

C'è anche una guida all'installazione alternativa per Mastodon su Ubuntu 22.04 disponibile qui che utilizza Docker per installare Mastodon su Ubuntu.

Prerequisiti

  • Un server che esegue Ubuntu 22.04.
  • Una password di root è configurata sul tuo server.
  • Un nome di dominio valido viene indirizzato all'indirizzo IP del tuo server.

Iniziare

Innanzitutto, si consiglia di aggiornare i pacchetti di sistema con l'ultima versione. Puoi farlo eseguendo il seguente comando:

apt update -y
apt upgrade -y

Dopo aver aggiornato il tuo sistema, dovrai installare alcune dipendenze richieste da Mastodon. Puoi installarli tutti con il seguente comando:

apt install git software-properties-common make apt-transport-https redis-server optipng pngquant jhead jpegoptim gifsicle imagemagick ffmpeg libpq-dev libxml2-dev libxslt1-dev file g++ libprotobuf-dev protobuf-compiler pkg-config gcc autoconf bison build-essential libssl-dev libyaml-dev libreadline6-dev zlib1g-dev libncurses5-dev libffi-dev libgdbm-dev libidn11-dev libicu-dev libjemalloc-dev -y

Una volta installate tutte le dipendenze, puoi procedere al passaggio successivo.

Installa Node.js

Mastodon richiede che Node.js sia installato sul tuo sistema. Per installare Node.js, aggiungi il repository Node.js al tuo server utilizzando il seguente comando:

curl -sL https://deb.nodesource.com/setup_16.x | bash -

Una volta aggiunto il repository, installa Node.js versione 16 con il seguente comando:

apt install nodejs -y

Successivamente, scarica e aggiungi la chiave Yarns GPG e abilita il repository con il seguente comando:

curl -sL https://dl.yarnpkg.com/debian/pubkey.gpg | gpg --dearmor | tee /usr/share/keyrings/yarnkey.gpg >/dev/null
echo "deb [signed-by=/usr/share/keyrings/yarnkey.gpg] https://dl.yarnpkg.com/debian stable main" | tee /etc/apt/sources.list.d/yarn.list

Una volta aggiunto, aggiorna il repository e installa Yarn con i seguenti comandi:

apt update -y
apt -y install yarn

Una volta terminato, puoi procedere al passaggio successivo.

Installa e configura PostgreSQL

Mastodon utilizza PostgreSQL come database back-end. Puoi installare l'ultima versione di PostgreSQL con il seguente comando:

apt install -y postgresql postgresql-contrib

Dopo aver installato PostgreSQL, avvia il servizio PostgreSQL utilizzando il seguente comando:

systemctl start postgresql

Quindi, accedi alla shell PostgreSQL:

sudo -u postgres -i psql

Quindi, crea un database per Mastodon:

CREATE DATABASE mastodon;

Quindi, crea un utente del database con il seguente comando:

CREATE USER mastodon;

Successivamente, imposta una password per l'utente Mastodon con il seguente comando:

ALTER USER mastodon WITH ENCRYPTED PASSWORD 'password';

Quindi, imposta l'autorizzazione appropriata per creare il database:

ALTER USER mastodon createdb;

Successivamente, imposta questo utente come proprietario del database Mastodon:

ALTER DATABASE mastodon OWNER TO mastodon;

Infine, esci dalla shell PostgreSQL con il seguente comando:

\q

Installa Rubino

Mastodon richiede anche l'installazione di Ruby versione 2.5+ sul tuo server. Se non è installato, puoi installarlo con il seguente comando:

apt install -y ruby ruby-dev

Dopo l'installazione, puoi verificare la versione di Ruby usando il seguente comando:

ruby -v

Otterrai la versione di Ruby nel seguente output:

ruby 3.0.2p107 (2021-07-07 revision 0db68f0233) [x86_64-linux-gnu]

Installa e configura Mastodon

Successivamente, dovrai creare un utente dedicato per eseguire Mastodon. Puoi crearlo con il seguente comando:

adduser mastodon --system --group --disabled-login

Successivamente, scarica l'ultima versione di Mastodon dal repository Git:

git clone https://github.com/tootsuite/mastodon.git

Successivamente, crea una directory /var/www/ per Mastodon se non esiste.

mkdir -p /var/www/

Successivamente, sposta la directory Mastodon nella directory /var/www:

mv mastodon/ /var/www/

Successivamente, cambia la proprietà della directory Mastodon in mastodon:

chown -R mastodon:mastodon /var/www/mastodon/

Successivamente, vai alla directory /var/www/mastodon e controlla l'ultimo ramo:

cd /var/www/mastodon/
sudo -u mastodon git checkout v4.0.2

Otterrai il seguente output:

Note: switching to 'v4.0.2'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:

  git switch -c 

Or undo this operation with:

  git switch -

Turn off this advice by setting config variable advice.detachedHead to false

HEAD is now at 03b0f3ac8 Bump version to 4.0.2 (#20725)

Successivamente, installa tutte le dipendenze richieste per Mastodon con il seguente comando:

gem install bundler
sudo -u mastodon bundle config deployment 'true'
sudo -u mastodon bundle config without 'development test'
sudo -u mastodon bundle install -j$(getconf _NPROCESSORS_ONLN)

Infine, esegui la procedura guidata di installazione di Mastodon utilizzando il seguente comando:

sudo -u mastodon RAILS_ENV=production bundle exec rake mastodon:setup

Ti verrà chiesto di fornire il tuo nome di dominio:

Type application/netcdf is already registered as a variant of application/netcdf.
Your instance is identified by its domain name. Changing it afterward will break things.
Domain name: mastodon.linuxbuz.com

Digita il tuo nome di dominio e premi il tasto Invio. Ti verrà chiesto di abilitare la modalità utente singolo:

Single user mode disables registrations and redirects the landing page to your public profile.
Do you want to enable single user mode? No

Digitare No e premere il tasto Invio. Ti verrà chiesto di utilizzare Docker per eseguire Mastodon:

Are you using Docker to run Mastodon? no

Digitare no e premere il tasto Invio. Ti verrà chiesto di fornire i dettagli del database PostgreSQL:

PostgreSQL host: /var/run/postgresql
PostgreSQL port: 5432
Name of PostgreSQL database: mastodon
Name of PostgreSQL user: mastodon
Password of PostgreSQL user: 

Fornisci i dettagli del tuo database PostgreSQL e premi il tasto Invio. Ti verrà chiesto di fornire i dettagli del server Redis:

Database configuration works! ????

Redis host: localhost
Redis port: 6379
Redis password: 

Fornisci il tuo host Redis, la porta e lascia vuoto il campo della password, quindi premi il tasto Invio. Ti verrà chiesto di archiviare i file sul cloud:

Redis configuration works! ????

Do you want to store uploaded files on the cloud? No

Digitare No e premere il tasto Invio. Ti verrà chiesto di inviare un'e-mail da localhost:

Do you want to send e-mails from localhost? yes
E-mail address to send e-mails "from": Mastodon <>
Send a test e-mail with this configuration right now? no

Fornisci tutti i dettagli richiesti e premi il tasto Invio. Ti verrà chiesto di salvare tutte le configurazioni:

This configuration will be written to .env.production
Save configuration? (Y/n) Y

Digita Y e premi il tasto Invio. Ti verrà chiesto di preparare il database:

Now that configuration is saved, the database schema must be loaded.
If the database already exists, this will erase its contents.
Prepare the database now? (Y/n) Y

Digita Y e premi il tasto Invio. Ti verrà chiesto di compilare le risorse CSS/JS.

The final step is compiling CSS/JS assets.
This may take a while and consume a lot of RAM.
Compile the assets now? (Y/n) Y

Digita Y e premi il tasto Invio. Ti verrà chiesto di impostare una password amministratore:

All done! You can now power on the Mastodon server ????

Do you want to create an admin user straight away? Yes
Username: admin
E-mail: 
You can login with the password: 9835fbd5e569ad149610862178580da7
You can change your password once you login.

Fornisci il nome utente dell'amministratore e l'e-mail e premi il tasto Invio per completare l'installazione.

Crea un file di servizio Systemd per Mastodon

Mastodon viene fornito con un file systemd preconfigurato per gestire i servizi Mastodon. Dovrai copiarli nella directory /etc/sysetmd/system/.

cp /var/www/mastodon/dist/mastodon*.service /etc/systemd/system/

Successivamente, dovrai cambiare la directory di lavoro di Mastodon da /home/mastodon/live/ a /var/www/mastodon/ nel file service. Puoi farlo con il seguente comando:

sed -i 's/home\/mastodon\/live/var\/www\/mastodon/g' /etc/systemd/system/mastodon-*.service

Dovrai anche modificare /home/mastodon/.rbenv/shims/bundle in /usr/local/bin/bundle nei file di servizio:

sed -i 's/home\/mastodon\/.rbenv\/shims/usr\/local\/bin/g' /etc/systemd/system/mastodon-*.service

Successivamente, ricaricare systemd per applicare le modifiche alla configurazione:

systemctl daemon-reload

Successivamente, avvia e abilita tutti i servizi Mastodon:

systemctl enable --now mastodon-web mastodon-sidekiq mastodon-streaming

Puoi controllare lo stato di tutti i servizi usando il seguente comando:

systemctl status mastodon-web mastodon-sidekiq mastodon-streaming

Produzione:

? mastodon-web.service - mastodon-web
     Loaded: loaded (/etc/systemd/system/mastodon-web.service; enabled; vendor preset: enabled)
     Active: active (running) since Wed 2022-11-30 03:07:13 UTC; 19s ago
   Main PID: 10201 (ruby3.0)
      Tasks: 29 (limit: 464140)
     Memory: 249.0M
     CGroup: /system.slice/mastodon-web.service
             ??10201 "puma 5.6.5 (tcp://127.0.0.1:3000) [mastodon]" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ">
             ??10228 "puma: cluster worker 0: 10201 [mastodon]" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "">
             ??10232 "puma: cluster worker 1: 10201 [mastodon]" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "">

Nov 30 03:07:15 ubuntu22042 bundle[10201]: [10201] *  Max threads: 5
Nov 30 03:07:15 ubuntu22042 bundle[10201]: [10201] *  Environment: production
Nov 30 03:07:15 ubuntu22042 bundle[10201]: [10201] *   Master PID: 10201
Nov 30 03:07:15 ubuntu22042 bundle[10201]: [10201] *      Workers: 2
Nov 30 03:07:15 ubuntu22042 bundle[10201]: [10201] *     Restarts: (?) hot (?) phased
Nov 30 03:07:15 ubuntu22042 bundle[10201]: [10201] * Preloading application
Nov 30 03:07:27 ubuntu22042 bundle[10201]: [10201] * Listening on http://127.0.0.1:3000
Nov 30 03:07:27 ubuntu22042 bundle[10201]: [10201] Use Ctrl-C to stop
Nov 30 03:07:27 ubuntu22042 bundle[10201]: [10201] - Worker 0 (PID: 10228) booted in 0.04s, phase: 0
Nov 30 03:07:27 ubuntu22042 bundle[10201]: [10201] - Worker 1 (PID: 10232) booted in 0.01s, phase: 0

? mastodon-sidekiq.service - mastodon-sidekiq
     Loaded: loaded (/etc/systemd/system/mastodon-sidekiq.service; enabled; vendor preset: enabled)
     Active: active (running) since Wed 2022-11-30 03:07:13 UTC; 19s ago
   Main PID: 10199 (ruby3.0)
      Tasks: 32 (limit: 464140)
     Memory: 247.1M
     CGroup: /system.slice/mastodon-sidekiq.service
             ??10199 "sidekiq 6.5.7 mastodon [0 of 25 busy]" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "">

Nov 30 03:07:29 ubuntu22042 bundle[10199]: 2022-11-30T03:07:29.202Z pid=10199 tid=cc7 uniquejobs=upgrade_locks INFO: Start - Converting v6 lo>
Nov 30 03:07:29 ubuntu22042 bundle[10199]: 2022-11-30T03:07:29.202Z pid=10199 tid=cc7 uniquejobs=upgrade_locks INFO: Done - Converting v6 loc>
Nov 30 03:07:29 ubuntu22042 bundle[10199]: 2022-11-30T03:07:29.202Z pid=10199 tid=cc7 uniquejobs=upgrade_locks INFO: Start - Deleting v6 keys
Nov 30 03:07:29 ubuntu22042 bundle[10199]: 2022-11-30T03:07:29.203Z pid=10199 tid=cc7 uniquejobs=upgrade_locks INFO: Done - Deleting v6 keys

Per impostazione predefinita, Mastodon è in ascolto sulla porta 3000. Puoi verificarlo con il seguente comando:

ss -lnpt | grep 3000

Produzione:

LISTEN 0      1024       127.0.0.1:3000      0.0.0.0:*    users:(("ruby3.0",pid=10232,fd=5),("ruby3.0",pid=10228,fd=5),("ruby3.0",pid=10201,fd=5))

Configura Nginx come proxy inverso

Innanzitutto, installa il pacchetto del server Web Nginx utilizzando il seguente comando:

apt install nginx -y

Successivamente, copia il file di configurazione dell'host virtuale Nginx nella directory Nginx:

cp /var/www/mastodon/dist/nginx.conf /etc/nginx/conf.d/mastodon.conf

Successivamente, modifica il file di configurazione dell'host virtuale Mastodon:

nano /etc/nginx/conf.d/mastodon.conf

Trova la seguente riga sia nel blocco del server della porta 80 che nel blocco del server della porta 443:

server_name example.com;

Sostituiti con la seguente riga:

server_name mastodon.linuxbuz.com;

Trova la seguente riga sia nel blocco del server della porta 80 che nel blocco del server della porta 443.

root /home/mastodon/live/public;

Sostituito con la seguente riga:

root /var/www/mastodon/public;

Trova le seguenti due righe.

# ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
# ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

Modificali come segue, quindi Nginx utilizzerà temporaneamente un certificato TLS autofirmato. Otterremo un certificato Let's Encrypt valido in seguito.

ssl_certificate /etc/ssl/certs/ssl-cert-snakeoil.pem;
ssl_certificate_key /etc/ssl/private/ssl-cert-snakeoil.key;

Salva e chiudi il file, quindi crea una directory cache per Nginx:

mkdir -p /var/nginx/cache/

Successivamente, verifica Nginx per eventuali errori di sintassi:

nginx -t

Otterrai il seguente output:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Successivamente, riavvia il servizio Nginx per applicare le modifiche:

systemctl restart nginx

Proteggi Mastodon con Lets Encrypt SSL

Per abilitare HTTPS sul sito Web di Mastodon, dovrai installare il pacchetto Certbot sul tuo server.

Innanzitutto, installa il gestore di pacchetti Snap con il seguente comando:

apt install snapd

Successivamente, aggiorna il pacchetto Snap all'ultima versione:

snap install core
snap refresh core

Successivamente, installa il pacchetto Certbot utilizzando il seguente comando:

snap install --classic certbot

Successivamente, crea un collegamento simbolico per il binario Certbot alla posizione di sistema:

ln -s /snap/bin/certbot /usr/bin/certbot

Successivamente, esegui il comando seguente per scaricare e installare i certificati SSL di Lets Encrypt:

certbot --nginx -d mastodon.linuxbuz.com

Ti verrà chiesto di fornire il tuo indirizzo email e di accettare i termini di servizio:

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Enter email address (used for urgent renewal and security notices)
 (Enter 'c' to cancel): 

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.3-September-21-2022.pdf. You must
agree in order to register with the ACME server. Do you agree?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: Y

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you be willing, once your first certificate is successfully issued, to
share your email address with the Electronic Frontier Foundation, a founding
partner of the Let's Encrypt project and the non-profit organization that
develops Certbot? We'd like to send you email about our work encrypting the web,
EFF news, campaigns, and ways to support digital freedom.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: Y

Digita Y e premi il tasto Invio per scaricare e installare i certificati SSL per il tuo dominio:

Account registered.
Requesting a certificate for mastodon.linuxbuz.com

Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/mastodon.linuxbuz.com/fullchain.pem
Key is saved at:         /etc/letsencrypt/live/mastodon.linuxbuz.com/privkey.pem
This certificate expires on 2023-02-28.
These files will be updated when the certificate renews.
Certbot has set up a scheduled task to automatically renew this certificate in the background.

Deploying certificate
Successfully deployed certificate for mastodon.linuxbuz.com to /etc/nginx/conf.d/mastodon.conf
Congratulations! You have successfully enabled HTTPS on https://mastodon.linuxbuz.com

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
If you like Certbot, please consider supporting our work by:
 * Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
 * Donating to EFF:                    https://eff.org/donate-le
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Accedi all'interfaccia web di Mastodon

Ora apri il tuo browser web e digita l'URL https://mastodon.linuxbuz.com. Verrai reindirizzato alla seguente pagina:

Fare clic sul pulsante Accedi. Dovresti vedere la pagina di accesso di Mastodon:

Ora, fornisci l'e-mail e la password dell'amministratore che hai creato in precedenza e fai clic sul pulsante Accedi. Dovresti vedere la dashboard di Mastodon nella pagina seguente:

Congratulazioni! hai installato e configurato correttamente Mastodon sul server Ubuntu 22.04. Ora puoi creare facilmente la tua rete di social media utilizzando Mastodon. Non esitate a chiedermi se avete domande.