documentation/content/en/administration/installation.md

239 lines
5.8 KiB
Markdown
Raw Normal View History

2018-09-23 01:00:04 +02:00
---
title: Installation
menu:
docs:
parent: administration
weight: 1
---
## Basic server setup (optional)
2018-09-24 02:38:41 +02:00
If you are setting up a fresh machine, it is recommended that you secure it first:
- Do not allow password-based SSH login (keys only)
- Update system packages
- Install fail2ban so it blocks repeated login attempts
- Install a firewall and only whitelist SSH, HTTP and HTTPS ports
2018-09-23 01:00:04 +02:00
## Pre-requisites
2018-09-24 02:38:41 +02:00
- A machine running Ubuntu 18.04 that you have root access to
- A domain name (or a subdomain) for the Mastodon server
You will be running the commands as root. If you aren't already root, switch to root:
```sh
sudo -i
```
### System repositories
Make sure curl is installed first:
```sh
apt install -y curl
```
#### Node.js
```sh
curl -sL https://deb.nodesource.com/setup_8.x | bash -
```
#### Yarn
```sh
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list
```
2018-09-23 01:00:04 +02:00
### System packages
2018-09-24 02:38:41 +02:00
```sh
apt update
apt install -y \
imagemagick ffmpeg libpq-dev libxml2-dev libxslt1-dev file git-core \
g++ libprotobuf-dev protobuf-compiler pkg-config nodejs gcc autoconf \
bison build-essential libssl-dev libyaml-dev libreadline6-dev \
zlib1g-dev libncurses5-dev libffi-dev libgdbm5 libgdbm-dev \
nginx redis-server redis-tools postgresql postgresql-contrib \
certbot yarn libidn11-dev libicu-dev libjemalloc-dev
```
2018-09-23 01:00:04 +02:00
### Installing Ruby
2018-09-24 02:38:41 +02:00
We will be using rbenv to manage Ruby versions, because it's easier to get the right versions and to update once a newer release comes out. rbenv must be installed for a single Linux user, therefore, first we must create the user Mastodon will be running as:
```sh
adduser --disabled-login mastodon
```
We can then switch to the user:
```sh
su - mastodon
```
And proceed to install rbenv and rbenv-build:
```sh
git clone https://github.com/rbenv/rbenv.git ~/.rbenv
cd ~/.rbenv && src/configure && make -C src
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(rbenv init -)"' >> ~/.bashrc
exec bash
git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
```
Once this is done, we can install the correct Ruby version:
```sh
RUBY_CONFIGURE_OPTS=--with-jemalloc rbenv install 2.5.1
rbenv global 2.5.1
```
We'll also need to install bundler:
```sh
gem install bundler --no-ri --no-rdoc
```
Return to the root user:
```sh
exit
```
2018-09-23 01:00:04 +02:00
## Setup
### Setting up PostgreSQL
2018-09-24 02:38:41 +02:00
#### Performance configuration (optional)
For optimal performance, you may use [pgTune](https://pgtune.leopard.in.ua/#/) to generate an appropriate configuration and edit values in `/etc/postgresql/9.6/main/postgresql.conf` before restarting PostgreSQL with `systemctl restart postgresql`
#### Creating a user
You will need to create a PostgreSQL user that Mastodon could use. It is easiest to go with "ident" authentication in a simple setup, i.e. the PostgreSQL user does not have a separate password and can be used by the Linux user with the same username.
Open the prompt:
```sh
sudo -u postgres psql
```
In the prompt, execute:
```
CREATE USER mastodon CREATEDB;
\q
```
Done!
2018-09-23 01:00:04 +02:00
### Setting up Mastodon
2018-09-24 02:38:41 +02:00
It is time to download the Mastodon code. Switch to the mastodon user:
```sh
su - mastodon
```
#### Checking out the code
Use git to download the latest stable release of Mastodon:
```sh
git clone https://github.com/tootsuite/mastodon.git live && cd live
git checkout $(git tag -l | grep -v 'rc[0-9]*$' | sort -V | tail -n 1)
```
#### Installing the last dependencies
Now to install Ruby and JavaScript dependencies:
```sh
bundle install \
-j$(getconf _NPROCESSORS_ONLN) \
--deployment --without development test
yarn install --pure-lockfile
```
#### Generating a configuration
Run the interactive setup wizard:
```sh
RAILS_ENV=production bundle exec rake mastodon:setup
```
This will:
- Create a configuration file
- Run asset precompilation
- Create the database schema
The configuration file is saved as `.env.production`. You can review and edit it to your liking. Refer to the [documentation on configuration]({{< relref "configuration.md" >}}).
You're done with the mastodon user for now, so switch back to root:
```sh
exit
```
2018-09-23 01:00:04 +02:00
### Setting up nginx
2018-09-24 02:38:41 +02:00
Copy the configuration template for nginx from the Mastodon directory:
```sh
cp /home/mastodon/live/dist/nginx.conf /etc/nginx/sites-available/mastodon
ln -s /etc/nginx/sites-available/mastodon /etc/nginx/sites-enabled/mastodon
```
Then edit `/etc/nginx/sites-available/mastodon` to replace `example.com` with your own domain name, and make any other adjustments you might need.
Reload nginx for the changes to take effect:
```sh
systemctl reload nginx
```
2018-09-23 01:00:04 +02:00
### Acquiring a SSL certificate
2018-09-24 02:38:41 +02:00
We'll use Let's Encrypt to get a free SSL certificate:
```sh
certbot certonly --webroot -d example.com -w /home/mastodon/live/public/
```
You can now edit `/etc/nginx/sites-available/mastodon` to uncomment and adjust the `ssl_certificate` and `ssl_certificate_key` lines.
Then, reload nginx for the changes to take effect:
```sh
systemctl reload nginx
```
At this point you should be able to visit your domain in the browser and see the elephant hitting the computer screen error page. This is because we haven't started the Mastodon process yet.
2018-09-23 01:00:04 +02:00
### Setting up systemd services
2018-09-24 02:38:41 +02:00
Copy the systemd service templates from the Mastodon directory:
```sh
cp /home/mastodon/live/dist/mastodon-*.service /etc/systemd/system/
```
Then edit the files to make sure the username and paths are correct:
- `/etc/systemd/system/mastodon-web.service`
- `/etc/systemd/system/mastodon-sidekiq.service`
- `/etc/systemd/system/mastodon-streaming.service`
Finally, start and enable the new systemd services:
```sh
systemctl start mastodon-web mastodon-sidekiq mastodon-streaming
systemctl enable mastodon-*
```
They will now automatically start at boot time.