Preduvjeti
Instaliranje ovisnosti
Instalacija i konfiguracija PostgreSQL-a
Instalirajte Ruby
Instalirajte Nginx s Let's Encrypt SSL
Postavljanje usluge Systemd
Zaključak
Mastodon je open source poslužitelj za mikroblogiranje i društveno umrežavanje s vlastitim hostingom. Pruža značajke poput Twittera; ali umjesto da ga kontrolira jedna tvrtka, to je federacija decentraliziranih poslužitelja koji se međusobno povezuju kako bi napravili cijeli sustav društvenih mreža. Mali tim može pokrenuti svoju instancu poslužitelja Mastodon kako bi komunicirao među sobom, kao i s drugim korisnicima savezne zajednice. Također vam omogućuje odabir vlastitih pravila privatnosti i moderiranja te definiranje načina na koji komunicirate s drugim korisnicima različitih poslužitelja. Sadržaj koji pojedini korisnik kreira i na koji se pretplati nalazi se na poslužitelju na kojem je kreiran njegov/njezin račun. Korisnici mogu pratiti jedni druge čak i ako nisu hostirani na istom poslužitelju. Svaki korisnik u mreži Mastodon ima svoje jedinstveno ime ili ID koji se nalazi u@username@domain-name.comformat. Klijentske aplikacije za Mastodon dostupne su za gotovo sve platforme.
Preduvjeti
- Instanca poslužitelja Vultr CentOS 7.
- Korisnik sudoa .
Napomena: Koristit ćemo se social.example.comkao naziv domene koji se koristi za instancu Mastodon. Zamijenite sva pojavljivanja social.example.comsa svojim stvarnim imenom domene koju želite koristiti za svoju Mastodon aplikaciju .
Ažurirajte svoj osnovni sustav pomoću vodiča Kako ažurirati CentOS 7 , ako je potrebno. Nakon što se vaš sustav ažurira, nastavite s instaliranjem potrebnih ovisnosti.
Instaliranje ovisnosti
Mastodont zahtijeva nekoliko ovisnosti za rad. Instalirajte najnoviju verziju Node.js, koja će se koristiti za sastavljanje JavaScript datoteka.
curl --silent --location https://rpm.nodesource.com/setup_8.x | sudo bash -
sudo yum -y install nodejs
Install Yarn, što je brza, pouzdana i sigurna aplikacija upravitelja ovisnosti. Koristit ćemo Yarn za instalaciju ovisnosti Node.js.
sudo wget https://dl.yarnpkg.com/rpm/yarn.repo -O /etc/yum.repos.d/yarn.repo
sudo yum -y install yarn
Instaliraj Redis. Redis je aplikacija za pohranu strukture podataka u memoriji i predmemoriju.
sudo yum -y install redis
Pokrenite Redis i omogućite mu automatsko pokretanje pri pokretanju.
sudo systemctl start redis
sudo systemctl enable redis
Instalirajte još nekoliko ovisnosti koje su potrebne za izgradnju Rubyinstalacije i drugih ovisnosti.
sudo yum -y install ImageMagick git libxml2-devel libxslt-devel gcc bzip2 openssl-devel zlib-devel gdbm-devel ncurses-devel autoconf automake bison gcc-c++ libffi-devel libtool patch readline-devel sqlite-devel glibc-headers glibc-devel libyaml-devel libicu-devel libidn-devel
Također ćete morati instalirati razvojne alate.
sudo yum -y groupinstall 'Development Tools'
Instalacija i konfiguracija PostgreSQL-a
PostgreSQL je objektni relacijski sustav baze podataka. Morat ćete dodati PostgreSQL repozitorij u svoj sustav jer zadano yumspremište sadrži stariju verziju aplikacije.
sudo rpm -Uvh https://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-7-x86_64/pgdg-centos96-9.6-3.noarch.rpm
Instalirajte PostgreSQL poslužitelj baze podataka.
sudo yum -y install postgresql96-server postgresql96-contrib postgresql96-devel
Inicijalizirajte bazu podataka.
sudo /usr/pgsql-9.6/bin/postgresql96-setup initdb
Uredite pg_hba.confdatoteku kako biste omogućili MD5provjeru autentičnosti.
sudo nano /var/lib/pgsql/9.6/data/pg_hba.conf
Pronađite sljedeće retke i promijenite peeru trusti identu md5.
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all peer
# IPv4 local connections:
host all all 127.0.0.1/32 ident
# IPv6 local connections:
host all all ::1/128 ident
Nakon ažuriranja, konfiguracija bi trebala izgledati ovako.
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
host all all 127.0.0.1/32 md5
# IPv6 local connections:
host all all ::1/128 md5
Pokrenite PostgreSQL poslužitelj i omogućite mu automatsko pokretanje pri pokretanju.
sudo systemctl start postgresql-9.6
sudo systemctl enable postgresql-9.6
Promijenite lozinku za zadanog korisnika PostgreSQL.
sudo passwd postgres
Prijavite se u ljusku kao PostgreSQLkorisnik.
sudo su - postgres
Stvorite novog PostgreSQLkorisnika za Mastodon.
createuser mastodon
PostgreSQL pruža psqlljusku za pokretanje upita u bazi podataka. Prebacite se na ljusku PostgreSQL pokretanjem.
psql
Postavite lozinku za novostvorenog korisnika za bazu podataka Mastodon i dajte dopuštenje za dodavanje novih baza podataka.
ALTER USER mastodon WITH ENCRYPTED password 'DBPassword' CREATEDB;
Zamijenite DBPasswordjakom lozinkom. Izlaz iz psqlljuske:
\q
Prebacite se na sudokorisnika.
exit
Instalirajte još nekoliko potrebnih PostgreSQL ovisnosti.
sudo yum -y install libpqxx-devel protobuf-devel
Instalirajte Ruby
Stvorite novog korisnika za Mastodon i prijeđite na novostvorenog korisnika.
sudo adduser mastodon -d /opt/mastodon
sudo su - mastodon
Instalirat ćemo najnoviju verziju Rubyja koristeći Ruby Version Manager ili RVM. Koristi se za instalaciju i upravljanje više verzija Rubyja.
Dodajte GPGključ RVM-a na svoj poslužitelj.
gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
Instalirajte RVM.
curl -sSL https://get.rvm.io | bash -s stable
source /opt/mastodon/.rvm/scripts/rvm
Sada dohvatite popis dostupnih verzija Rubyja.
rvm list known
Vidjet ćete sljedeći izlaz.
[mastodon@vultr ~]$ rvm list known
# MRI Rubies
[ruby-]1.8.6[-p420]
[ruby-]1.8.7[-head] # security released on head
[ruby-]1.9.1[-p431]
[ruby-]1.9.2[-p330]
[ruby-]1.9.3[-p551]
[ruby-]2.0.0[-p648]
[ruby-]2.1[.10]
[ruby-]2.2[.7]
[ruby-]2.3[.4]
[ruby-]2.4[.1]
ruby-head
...
Sada instalirajte najnoviju verziju Rubyja s popisa.
rvm install 2.4
Koristite instaliranu verziju Rubyja.
rvm use 2.4
Trebali biste moći provjeriti njegovu verziju.
ruby -v
Trebali biste vidjeti sličan izlaz.
[mastodon@vultr ~]$ ruby -v
ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-linux]
Install bundler, koji je upravitelj ovisnosti za aplikaciju Ruby.
gem install bundler
Sada se prebacite na početni direktorij korisnika Mastodon i klonirajte datoteke repozitorija aplikacije s Githuba.
cd ~
git clone https://github.com/tootsuite/mastodon.git app
Move the application directory and checkout the latest releases of the application.
cd ~/app
git checkout $(git describe --tags `git rev-list --tags --max-count=1`)
Configure the bundle to use a custom path for the PostgreSQL configuration. Also, install the Ruby dependencies.
bundle config build.pg --with-pg-config=/usr/pgsql-9.6/bin/pg_config
bundle install --deployment --without development test
Install the Node.js dependencies using Yarn.
yarn install --pure-lockfile
You will need to create a configuration file for the Mastodon application. Copy the sample configuration file.
cd ~/app
cp .env.production.sample .env.production
Before editing the file, generate three different secrets by running the following command three times. You will need to set these secrets in the configuration file.
RAILS_ENV=production bundle exec rake secret
You should get a similar output.
[mastodon@vultr app]$ RAILS_ENV=production bundle exec rake secret
0f17dab1cf4a07f6fac671ecd5815adcb59d012b338dae9350c66402250c6c729dccd6182b1a8f75c4fde55453ce283ea66e07ed4466cdc6d4d6974c98512967
Edit the configuration file using the nano editor.
nano .env.production
Find the following lines and change the values accordingly.
REDIS_HOST=127.0.0.1
REDIS_PORT=6379
Provide the database credentials, according to database user you have created during installation of PostgreSQL. Provide any name for database name as Mastodon will automatically create the database with the name you provide.
DB_HOST=127.0.0.1
DB_USER=mastodon
DB_NAME=mastodon
DB_PASS=DBPassword
DB_PORT=5432
Set the domain name of the application.
LOCAL_DOMAIN=social.example.com
Find the following lines and set its value to the secrets you have generated.
PAPERCLIP_SECRET=
SECRET_KEY_BASE=
OTP_SECRET=
Save the file and exit from the editor.
Now, run the following command to generate Web Push VAPID keys. You will get an error while generating the Web Push VAPID keys if you have not set the secrets in the configuration file mentioned above.
RAILS_ENV=production bundle exec rake mastodon:webpush:generate_vapid_key
You should see the following output.
[mastodon@vultr app]$ RAILS_ENV=production bundle exec rake mastodon:webpush:generate_vapid_key
VAPID_PRIVATE_KEY=DCMQdSPkdm-mepsNh4F3suc-UIvsABCvwFd03jSVemM=
VAPID_PUBLIC_KEY=BMMVcVXmqnV0C8S_ybZ7eQH-MXBEX2exqfdPSYQiMMUF2rRxfhoEtVF931i26ebMgmslHB_nvVadOdcBPhaFEjw=
Open the configuration file again and set the generated keys in their respective lines.
VAPID_PRIVATE_KEY=
VAPID_PUBLIC_KEY=
Finally, provide the information of your SMTP mail server so that the application can send emails to users. It is important that you use a working SMTP server as, upon new registration, users will be sent an email with an activation link. New users will have to click on the activation link to activate their account.
SMTP_SERVER=mail.example.com
SMTP_PORT=456
SMTP_LOGIN=mail@example.com
SMTP_PASSWORD=MailPassword
SMTP_FROM_ADDRESS=notifications@example.com
SMTP_TLS=true
Once done, save the file and exit from the editor. You have done the basic configuration of Mastodon instance.
Setup the PostgreSQL database for the first time.
RAILS_ENV=production bundle exec rails db:setup
First, the above command will create a new database with the name mentioned in the configuration. Then, it will write the database according to the configuration.
Precompile all CSS and JavaScript files to create the cache.
RAILS_ENV=production bundle exec rails assets:precompile
Mastodon is now installed on your server. Now you will need to setup an Nginx web server to serve the application and setup Systemd services to run the Mastodon services.
From now on, we will need to execute the commands using the sudo user.
Install Nginx with Let's Encrypt SSL
We will use the Nginx web server as a reverse proxy to serve the application to the users. We will also obtain and install SSL certificates from Let's Encrypt.
Install Nginx and Certbot. Certbot is the official certificates issuing client for Let's Encrypt CA.
sudo yum -y install nginx certbot
Adjust your firewall setting to allow the standard HTTP and HTTPS ports through the firewall as Certbot needs to make an HTTP connection for verifying the domain authority.
sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload
Note: To obtain certificates from Let's Encrypt CA, you must ensure that the domain for which you wish to generate the certificates is pointed towards the server. If not, then make the necessary changes to the DNS records of your domain and wait for the DNS to propagate before making the certificate request again. Certbot checks the domain authority before providing the certificates.
Now use the builtin web server in Certbot to generate the certificates for your domain.
sudo certbot certonly --standalone -d social.example.com
The generated certificates are likely to be stored in the /etc/letsencrypt/live/social.example.com/ directory. The SSL certificate will be retained as fullchain.pem and the private key will be saved as privkey.pem.
Certifikati Let's Encrypt istječu za 90 dana, stoga je preporučljivo postaviti automatsku obnovu za certifikate pomoću Cron poslova. Cron je sistemska usluga koja se koristi za izvršavanje periodičnih zadataka.
Otvorite cron datoteku posla.
sudo crontab -e
Dodajte sljedeći redak.
0 0 * * * /usr/bin/certbot renew --quiet
Gornji cron posao će se izvoditi svakodnevno u ponoć. Ako potvrda istekne, automatski će se obnoviti certifikati.
Sada kreirajte novi blok poslužitelja Nginx za mjesto Mastodon.
sudo nano /etc/nginx/conf.d/mastodon.conf
Popunite datoteku ovim.
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
server {
listen 80;
listen [::]:80;
server_name social.example.com;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name social.example.com;
ssl_protocols TLSv1.2;
ssl_ciphers HIGH:!MEDIUM:!LOW:!aNULL:!NULL:!SHA;
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;
ssl_certificate /etc/letsencrypt/live/social.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/social.example.com/privkey.pem;
keepalive_timeout 70;
sendfile on;
client_max_body_size 0;
root /opt/mastodon/app/public;
gzip on;
gzip_disable "msie6";
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
add_header Strict-Transport-Security "max-age=31536000";
location / {
try_files $uri @proxy;
}
location ~ ^/(emoji|packs|system/accounts/avatars|system/media_attachments/files) {
add_header Cache-Control "public, max-age=31536000, immutable";
try_files $uri @proxy;
}
location @proxy {
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 https;
proxy_set_header Proxy "";
proxy_pass_header Server;
proxy_pass http://127.0.0.1:3000;
proxy_buffering off;
proxy_redirect off;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
tcp_nodelay on;
}
location /api/v1/streaming {
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 https;
proxy_set_header Proxy "";
proxy_pass http://127.0.0.1:4000;
proxy_buffering off;
proxy_redirect off;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
tcp_nodelay on;
}
error_page 500 501 502 503 504 /500.html;
}
Svakako promijenite naziv domene i put do SSL certifikata.
Sada možete pokrenuti Nginx web poslužitelj i omogućiti mu da se automatski pokrene pri pokretanju.
sudo systemctl start nginx
sudo systemctl enable nginx
Također ćete morati dopustiti port 4000kroz vatrozid jer će port pružati uslugu API za streaming.
sudo firewall-cmd --permanent --zone=public --add-port=4000/tcp
sudo firewall-cmd --reload
Navedite dopuštenja za izvršavanje za direktorij Mastodon.
sudo chmod +x /opt/mastodon
Postavljanje usluge Systemd
Morat ćete pokrenuti tri različita procesa da biste uspješno pokrenuli poslužitelj Mastodon. Korištenje usluge Systemd osigurat će da se Mastodon poslužitelj automatski pokrene pri pokretanju i procesima neuspjeh.
Izradite novu uslugu systemd za pokretanje web usluge Mastodon. Ova usluga će pokrenuti ugrađeni web poslužitelj na portu 3000.
sudo nano /etc/systemd/system/mastodon-web.service
Popunite datoteku sljedećim sadržajem.
[Unit]
Description=Mastodon Web Service
After=network.target
[Service]
Type=simple
User=mastodon
Group=mastodon
WorkingDirectory=/opt/mastodon/app
Environment="RAILS_ENV=production"
Environment="PORT=3000"
ExecStart=/bin/bash -lc 'bundle exec puma -C config/puma.rb'
TimeoutSec=15
Restart=always
[Install]
WantedBy=multi-user.target
Stvorite drugu uslugu Systemd za pokretanje pozadinske usluge čekanja Mastodon. Usluga čekanja čekanja Mastodon brine se za sve pozadinske zadatke.
sudo nano /etc/systemd/system/mastodon-queue.service
Popunite datoteku sljedećim sadržajem.
[Unit]
Description=Mastodon Queue Service
After=network.target
[Service]
Type=simple
User=mastodon
WorkingDirectory=/opt/mastodon/app
Environment="RAILS_ENV=production"
Environment="DB_POOL=5"
ExecStart=/bin/bash -lc 'bundle exec sidekiq -c 5 -q default -q mailers -q pull -q push'
TimeoutSec=15
Restart=always
[Install]
WantedBy=multi-user.target
Konačno, stvorite posljednju datoteku usluge za Mastodon streaming API uslugu.
sudo nano /etc/systemd/system/mastodon-api.service
Popunite datoteku sljedećim sadržajem.
[Unit]
Description=Mastodon Streaming
After=network.target
[Service]
Type=simple
User=mastodon
WorkingDirectory=/opt/mastodon/app
Environment="NODE_ENV=production"
Environment="PORT=4000"
ExecStart=/bin/npm run start
TimeoutSec=15
Restart=always
[Install]
WantedBy=multi-user.target
Omogućite da se usluge Mastodon automatski pokreću u trenutku pokretanja.
sudo systemctl enable mastodon-web mastodon-queue mastodon-api
Da biste pokrenuli usluge Mastodon, možete pokrenuti ovo.
sudo systemctl start mastodon-web mastodon-queue mastodon-api
Da biste provjerili status usluga, možete pokrenuti ovo.
sudo systemctl status mastodon-web mastodon-queue mastodon-api
Zaključak
Sada možete pristupiti aplikaciji Mastodon tako da odete na https://social.example.com. Napravite novi korisnički račun na svom poslužitelju i potvrdite korisnički račun otvaranjem veze poslane u e-poruci. Da biste dali administratorske privilegije za račun koji ste upravo stvorili, pokrenite ovo.
sudo su - mastodon && cd ~/app
RAILS_ENV=production bundle exec rails mastodon:make_admin USERNAME=<user_name>
Zamijenite <user_name>stvarnim korisničkim imenom koje ste koristili za kreiranje računa u sučelju Mastodon.
Vaša Mastodon instanca je sada instalirana i konfigurirana za pokretanje. Započnite tako što ćete pozvati svoje prijatelje da se pridruže vašoj instanci i istražite značajke koje nudi Mastodon. Svoju društvenu mrežu možete koristiti na mobilnom uređaju preuzimanjem bilo koje aplikacije dostupne za različite platforme.