Előfeltételek
Függőségek telepítése
A PostgreSQL telepítése és konfigurálása
Telepítsd a Rubyt
Telepítse az Nginxet az SSL titkosítással
A Systemd szolgáltatás beállítása
Következtetés
A Mastodon egy nyílt forráskódú saját üzemeltetésű mikroblog- és közösségi hálózati szerver. Twitterhez hasonló funkciókat biztosít; de ahelyett, hogy egyetlen vállalat irányítaná, decentralizált kiszolgálók szövetsége, amelyek összekapcsolódnak, és létrehozzák a közösségi hálózatok egész rendszerét. Egy kis csapat elindíthatja a Mastodon szerverpéldányt, hogy kommunikáljon egymással, valamint az egyesített közösség többi felhasználójával. Lehetővé teszi továbbá saját adatvédelmi és moderálási szabályzatának kiválasztását, valamint annak meghatározását, hogy hogyan kommunikáljon a különböző szerverek más felhasználóival. Az egyéni felhasználó által létrehozott és előfizetett tartalom azon a szerveren található, ahol a fiókja létrejön. A felhasználók akkor is követhetik egymást, ha nem ugyanazon a szerveren vannak. A Mastodon hálózat minden felhasználójának egyedi neve vagy azonosítója van, amely a@username@domain-name.comformátum. A Mastodon kliens alkalmazások szinte minden platformon elérhetők.
Előfeltételek
Megjegyzés: A social.example.comMastodon példányhoz használt tartománynévként fogjuk használni . Cserélje le az összes előfordulását social.example.coma tényleges domain nevére, amelyet a Mastodon alkalmazáshoz használni szeretne .
Frissítse alaprendszerét a CentOS 7 frissítése útmutató segítségével , ha szükséges. A rendszer frissítése után folytassa a szükséges függőségek telepítésével.
Függőségek telepítése
A Mastodon működéséhez számos függőség szükséges. Telepítse a legújabb verzióját Node.js, amely a JavaScript-fájlok fordításához lesz használva.
curl --silent --location https://rpm.nodesource.com/setup_8.x | sudo bash -
sudo yum -y install nodejs
Telepítse Yarn, amely egy gyors, megbízható és biztonságos függőségkezelő alkalmazás. A Node.js függőségek telepítéséhez a Yarn programot fogjuk használni.
sudo wget https://dl.yarnpkg.com/rpm/yarn.repo -O /etc/yum.repos.d/yarn.repo
sudo yum -y install yarn
Telepítse Redis. A Redis egy memórián belüli adatstruktúra tároló és gyorsítótár alkalmazás.
sudo yum -y install redis
Indítsa el a Redis-t, és engedélyezze az automatikus indulást a rendszerindításkor.
sudo systemctl start redis
sudo systemctl enable redis
Telepítsen néhány további függőséget, amelyek a Rubytelepítés és egyéb függőségek létrehozásához szükségesek.
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
A fejlesztőeszközöket is telepítenie kell.
sudo yum -y groupinstall 'Development Tools'
A PostgreSQL telepítése és konfigurálása
A PostgreSQL egy objektumrelációs adatbázisrendszer. Hozzá kell adnia a PostgreSQL-tárat a rendszeréhez, mivel az alapértelmezett yumlerakat az alkalmazás régebbi verzióját tartalmazza.
sudo rpm -Uvh https://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-7-x86_64/pgdg-centos96-9.6-3.noarch.rpm
Telepítse a PostgreSQL adatbázis-kiszolgálót.
sudo yum -y install postgresql96-server postgresql96-contrib postgresql96-devel
Inicializálja az adatbázist.
sudo /usr/pgsql-9.6/bin/postgresql96-setup initdb
Szerkessze a pg_hba.conffájlt az MD5alapú hitelesítés engedélyezéséhez .
sudo nano /var/lib/pgsql/9.6/data/pg_hba.conf
Keresse meg a következő sorokat, és váltson peererre trustés ident-ra 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
Frissítés után a konfigurációnak így kell kinéznie.
# 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
Indítsa el a PostgreSQL-kiszolgálót, és engedélyezze az automatikus indulást rendszerindításkor.
sudo systemctl start postgresql-9.6
sudo systemctl enable postgresql-9.6
Módosítsa az alapértelmezett PostgreSQL-felhasználó jelszavát.
sudo passwd postgres
Felhasználóként jelentkezzen be a shellbe PostgreSQL.
sudo su - postgres
Hozzon létre egy új PostgreSQLfelhasználót a Mastodon számára.
createuser mastodon
A PostgreSQL biztosítja a psqlparancsértelmezőt a lekérdezések futtatásához az adatbázisban. Futtatással váltson a PostgreSQL shellre.
psql
Állítson be jelszót az újonnan létrehozott felhasználó számára a Mastodon adatbázishoz, és adja meg az engedélyt új adatbázisok hozzáadásához.
ALTER USER mastodon WITH ENCRYPTED password 'DBPassword' CREATEDB;
Cserélje DBPasswordki erős jelszóra. Kilépés a psqlhéjból:
\q
Váltás a sudofelhasználóra.
exit
Telepítsen még néhány szükséges PostgreSQL-függőséget.
sudo yum -y install libpqxx-devel protobuf-devel
Telepítsd a Rubyt
Hozzon létre egy új felhasználót a Mastodon számára, és váltson az újonnan létrehozott felhasználóra.
sudo adduser mastodon -d /opt/mastodon
sudo su - mastodon
A Ruby legújabb verzióját a Ruby Version Manager vagy RVM segítségével telepítjük. A Ruby több verziójának telepítésére és kezelésére szolgál.
Adja hozzá az GPGRVM kulcsát a szerverhez.
gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
Telepítse az RVM-et.
curl -sSL https://get.rvm.io | bash -s stable
source /opt/mastodon/.rvm/scripts/rvm
Most töltse le a Ruby elérhető verzióinak listáját.
rvm list known
A következő kimenetet fogja látni.
[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
...
Most telepítse a Ruby legújabb verzióját a listából.
rvm install 2.4
Használja a Ruby telepített verzióját.
rvm use 2.4
Ellenőriznie kell a verzióját.
ruby -v
Hasonló kimenetet kell látnod.
[mastodon@vultr ~]$ ruby -v
ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-linux]
Telepítse bundler, amely a Ruby alkalmazás függőségi kezelője.
gem install bundler
Most váltson át a Mastodon felhasználó kezdőkönyvtárára, és klónozza az alkalmazástár fájljait a Githubból.
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.
Let's Encrypt certificates expire in 90 days, so it is recommended to set up auto renewal for the certificates using Cron jobs. Cron is a system service which is used to run periodic tasks.
Open the cron job file.
sudo crontab -e
Add the following line.
0 0 * * * /usr/bin/certbot renew --quiet
The above cron job will run daily at midnight. If the certificate is due for expiry, it will automatically renew the certificates.
Now create a new Nginx server block for the Mastodon site.
sudo nano /etc/nginx/conf.d/mastodon.conf
Populate the file with this.
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;
}
Be sure to change the domain name and the path to the SSL certificates.
Now you can start the Nginx web server and enable it to start at boot automatically.
sudo systemctl start nginx
sudo systemctl enable nginx
You will also be required to allow port 4000 through the firewall as the port will be providing the streaming API service.
sudo firewall-cmd --permanent --zone=public --add-port=4000/tcp
sudo firewall-cmd --reload
Provide the execution permissions for the Mastodon directory.
sudo chmod +x /opt/mastodon
Setting Up Systemd Service
A Mastodon szerver sikeres elindításához három különböző folyamatot kell elindítania. A Systemd szolgáltatás használata biztosítja, hogy a Mastodon szerver automatikusan elinduljon rendszerindításkor, és feldolgozza a hibákat.
Hozzon létre egy új rendszerszolgáltatást a Mastodon webszolgáltatás futtatásához. Ez a szolgáltatás elindítja a beépített webszervert a porton 3000.
sudo nano /etc/systemd/system/mastodon-web.service
Töltse fel a fájlt a következő tartalommal.
[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
Hozza létre a második Systemd szolgáltatást a Mastodon háttérsorszolgáltatás futtatásához. A Mastodon queue szolgáltatás minden háttérfeladatot ellát.
sudo nano /etc/systemd/system/mastodon-queue.service
Töltse fel a fájlt a következő tartalommal.
[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
Végül hozza létre a Mastodon streaming API szolgáltatás utolsó szolgáltatásfájlját.
sudo nano /etc/systemd/system/mastodon-api.service
Töltse fel a fájlt a következő tartalommal.
[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
Engedélyezze a Mastodon szolgáltatások automatikus indulását a rendszerindításkor.
sudo systemctl enable mastodon-web mastodon-queue mastodon-api
A Mastodon szolgáltatások elindításához futtassa ezt.
sudo systemctl start mastodon-web mastodon-queue mastodon-api
A szolgáltatások állapotának ellenőrzéséhez futtassa ezt.
sudo systemctl status mastodon-web mastodon-queue mastodon-api
Következtetés
Mostantól elérheti a Mastodon alkalmazást a címen https://social.example.com. Hozzon létre egy új felhasználói fiókot a szerverén, és ellenőrizze a felhasználói fiókot az e-mailben küldött link megnyitásával. Ha rendszergazdai jogosultságokat szeretne adni az imént létrehozott fiókhoz, futtassa ezt.
sudo su - mastodon && cd ~/app
RAILS_ENV=production bundle exec rails mastodon:make_admin USERNAME=<user_name>
Cserélje <user_name>le a tényleges felhasználónévvel, amelyet a fiók létrehozásához használt a Mastodon felületen.
A Mastodon-példány most telepítve van, és futásra konfigurálva van. Kezdje azzal, hogy meghívja barátait, hogy csatlakozzanak a példányához, és fedezze fel a Mastodon által biztosított funkciókat. Használhatja közösségi oldalát mobilon, ha letölti a különböző platformokon elérhető alkalmazások bármelyikét .