installation.md 8.34 KB
Newer Older
Eugen Rochko's avatar
Eugen Rochko committed
1 2
---
title: Installation
3
description: How to install Mastodon on an Ubuntu 18.04 server
Eugen Rochko's avatar
Eugen Rochko committed
4 5 6 7 8
menu:
  docs:
    parent: administration
    weight: 1
---
Eugen Rochko's avatar
Eugen Rochko committed
9

10
<img src="/setup.png" alt="" style="margin: 0; box-shadow: none">
Eugen Rochko's avatar
Eugen Rochko committed
11

Eugen Rochko's avatar
Eugen Rochko committed
12
## Basic server setup (optional)
Eugen Rochko's avatar
Eugen Rochko committed
13

14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
If you are setting up a fresh machine, it is recommended that you secure it first. Assuming that you are running **Ubuntu 18.04**:

### Do not allow password-based SSH login (keys only)

First make sure you are actually logging in to the server using keys and not via a password, otherwise this will lock you out. Many hosting providers support uploading a public key and automatically set up key-based root login on new machines for you.

Edit `/etc/ssh/sshd_config` and find `PasswordAuthentication`. Make sure it's uncommented and set to `no`. If you made any changes, restart sshd:

```sh
systemctl restart ssh
```

### Update system packages

```sh
apt update && apt upgrade -y
```

### Install fail2ban so it blocks repeated login attempts
Eugen Rochko's avatar
Eugen Rochko committed
33

34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
```sh
apt install fail2ban
```

Edit `/etc/fail2ban/jail.local` and put this inside:

```ini
[DEFAULT]
destemail = your@email.here
sendername = Fail2Ban

[sshd]
enabled = true
port = 22

[sshd-ddos]
enabled = true
port = 22
```

Finally restart fail2ban:

```sh
systemctl restart fail2ban
```

### Install a firewall and only whitelist SSH, HTTP and HTTPS ports

First, install iptables-persistent. During installation it will ask you if you want to keep current rules--decline.

```sh
apt install -y iptables-persistent
```

Edit `/etc/iptables/rules.v4` and put this inside:

```
*filter

#  Allow all loopback (lo0) traffic and drop all traffic to 127/8 that doesn't use lo0
-A INPUT -i lo -j ACCEPT
-A INPUT ! -i lo -d 127.0.0.0/8 -j REJECT

#  Accept all established inbound connections
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

#  Allow all outbound traffic - you can modify this to only allow certain traffic
-A OUTPUT -j ACCEPT

#  Allow HTTP and HTTPS connections from anywhere (the normal ports for websites and SSL).
-A INPUT -p tcp --dport 80 -j ACCEPT
-A INPUT -p tcp --dport 443 -j ACCEPT

#  Allow SSH connections
#  The -dport number should be the same port number you set in sshd_config
-A INPUT -p tcp -m state --state NEW --dport 22 -j ACCEPT

#  Allow ping
-A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT

#  Log iptables denied calls
-A INPUT -m limit --limit 5/min -j LOG --log-prefix "iptables denied: " --log-level 7

#  Reject all other inbound - default deny unless explicitly allowed policy
-A INPUT -j REJECT
-A FORWARD -j REJECT

COMMIT
```

With iptables-persistent, that configuration will be loaded at boot time. But since we are not rebooting right now, we need to load it manually for the first time:

```sh
iptables-restore < /etc/iptables/rules.v4
```
Eugen Rochko's avatar
Eugen Rochko committed
109

Eugen Rochko's avatar
Eugen Rochko committed
110
## Pre-requisites
Eugen Rochko's avatar
Eugen Rochko committed
111

112 113 114
- A machine running **Ubuntu 18.04** that you have root access to
- A **domain name** (or a subdomain) for the Mastodon server, e.g. `example.com`
- An e-mail delivery service or other **SMTP server**
Eugen Rochko's avatar
Eugen Rochko committed
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142

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
```

Eugen Rochko's avatar
Eugen Rochko committed
143
### System packages
Eugen Rochko's avatar
Eugen Rochko committed
144 145 146 147 148 149 150 151 152

```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 \
153
  certbot python-certbot-nginx yarn libidn11-dev libicu-dev libjemalloc-dev
Eugen Rochko's avatar
Eugen Rochko committed
154 155
```

Eugen Rochko's avatar
Eugen Rochko committed
156
### Installing Ruby
Eugen Rochko's avatar
Eugen Rochko committed
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183

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
184 185
RUBY_CONFIGURE_OPTS=--with-jemalloc rbenv install 2.6.1
rbenv global 2.6.1
Eugen Rochko's avatar
Eugen Rochko committed
186 187
```

188
Default gem version shipped with ruby_2.6.0 is incompatible with latest bundler, so we need to update gem:
ikuradon's avatar
ikuradon committed
189 190 191 192 193

```
gem update --system
```

Eugen Rochko's avatar
Eugen Rochko committed
194 195 196
We'll also need to install bundler:

```sh
ikuradon's avatar
ikuradon committed
197
gem install bundler --no-document
Eugen Rochko's avatar
Eugen Rochko committed
198 199 200 201 202 203 204 205
```

Return to the root user:

```sh
exit
```

Eugen Rochko's avatar
Eugen Rochko committed
206 207
## Setup
### Setting up PostgreSQL
Eugen Rochko's avatar
Eugen Rochko committed
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
#### 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!

Eugen Rochko's avatar
Eugen Rochko committed
231
### Setting up Mastodon
Eugen Rochko's avatar
Eugen Rochko committed
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280

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
```

Eugen Rochko's avatar
Eugen Rochko committed
281
### Setting up nginx
Eugen Rochko's avatar
Eugen Rochko committed
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297

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
```

Eugen Rochko's avatar
Eugen Rochko committed
298
### Acquiring a SSL certificate
Eugen Rochko's avatar
Eugen Rochko committed
299 300 301 302

We'll use Let's Encrypt to get a free SSL certificate:

```sh
303
certbot --nginx -d example.com
Eugen Rochko's avatar
Eugen Rochko committed
304 305
```

306
This will obtain the certificate, automatically update `/etc/nginx/sites-available/mastodon` to use the new certificate, and reload nginx for the changes to take effect.
Eugen Rochko's avatar
Eugen Rochko committed
307 308 309

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.

Eugen Rochko's avatar
Eugen Rochko committed
310
### Setting up systemd services
Eugen Rochko's avatar
Eugen Rochko committed
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331

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.
Eugen Rochko's avatar
Eugen Rochko committed
332 333

**Hurray! This is it. You can visit your domain in the browser now!**