Introduction
Mastodon is a popular open-source microblogging platform that allows users to create their own social networks. Self-hosting Mastodon gives you full control over your data and the ability to customize your instance. In this guide, we will walk you through the steps to install Mastodon on a VPS, covering everything from server selection to configuration.
Prerequisites
Before we start, ensure you have the following:
- A VPS running Ubuntu 20.04 or later (see our full VPS comparison for options).
- Domain name for your Mastodon instance.
- Basic understanding of command-line tools.
Choosing a VPS Provider
When selecting a VPS provider, consider the following:
| Provider | Starting Price | Specs |
|---|---|---|
| Contabo VPS | 5.99 EUR/mo | 4 vCPU, 8 GB RAM, 300 GB SSD |
| Hetzner Cloud | 4.15 EUR/mo | 2 vCPU, 2 GB RAM, 20 GB SSD |
| DigitalOcean | 6 USD/mo | 1 vCPU, 1 GB RAM, 25 GB SSD |
| Vultr | 6 USD/mo | 1 vCPU, 1 GB RAM, 25 GB SSD |
| Linode | 5 USD/mo | 1 vCPU, 1 GB RAM, 25 GB SSD |
For this guide, we recommend either Contabo or Hetzner Cloud for their balance of price and performance.
Step 1: Initial Setup
-
Create a new VPS instance: Choose your desired provider and set up a new instance with a minimal installation of Ubuntu.
-
Update your system: Connect to your VPS using SSH and run the following commands:
sudo apt update sudo apt upgrade -y -
Install required packages:
sudo apt install -y git curl wget build-essential
Step 2: Install Docker and Docker Compose
Mastodon runs within Docker containers, which makes installation and management easier.
-
Install Docker:
curl -fsSL https://get.docker.com -o get-docker.sh sh get-docker.sh -
Install Docker Compose:
sudo apt install -y python3-pip sudo pip3 install docker-compose
Step 3: Clone Mastodon Repository
Next, clone the Mastodon repository and navigate into it.
git clone https://github.com/mastodon/mastodon.git ~/.mastodon
cd ~/.mastodon
Step 4: Configure Environment Variables
Create a new configuration file and populate it with your details:
cp .env.production.sample .env.production
nano .env.production
Make sure to set your domain, email, and other relevant configurations.
Step 5: Install Dependencies
In order to build the Mastodon app, you need to install Node.js, Yarn, and other dependencies.
-
Install Node.js:
curl -sL https://deb.nodesource.com/setup_16.x | sudo -E bash - sudo apt install -y nodejs -
Install Yarn:
npm install --global yarn -
Install Ruby dependencies:
sudo apt install -y gcc libpq-dev libjpeg-dev libxml2-dev libxslt1-dev -
Install Bundler:
sudo gem install bundler -
Install gems:
bundle install
Step 6: Database Setup
Mastodon uses PostgreSQL. Create a new database for your instance.
-
Install PostgreSQL:
sudo apt install -y postgresql postgresql-contrib -
Create a database user and database:
sudo -u postgres createuser mastodon
sudo -u postgres createdb mastodon_production --owner mastodon
-
Assign a password:
Open the PostgreSQL prompt:
sudo -u postgres psqlThen run the following commands, substituting
<yourpassword>:ALTER USER mastodon PASSWORD '<yourpassword>';
Step 7: Start Mastodon Using Docker
Now you can start the Mastodon application using Docker Compose.
docker-compose build
docker-compose up -d
Step 8: Configure Nginx
We’ll set up Nginx as a reverse proxy to forward requests to your Mastodon instance.
-
Install Nginx:
sudo apt install -y nginx -
Configure Nginx: Create a new configuration file for your Mastodon instance.
sudo nano /etc/nginx/sites-available/mastodonPopulate it with the following:
server { listen 80; server_name yourdomain.com; location / { proxy_pass http://localhost:3000; 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 $scheme; } } -
Enable the configuration:
sudo ln -s /etc/nginx/sites-available/mastodon /etc/nginx/sites-enabled/ -
Restart Nginx:
sudo systemctl restart nginx
Step 9: Complete Installation
Ensure everything is running properly:
docker-compose logs
Navigate to your Mastodon instance in a web browser to complete the setup.
Frequently Asked Questions
1. How much does it cost to self-host Mastodon on a VPS?
Costs depend on the provider and specific plan you choose. For example, Contabo VPS starts at 5.99 EUR/mo, while Hetzner Cloud begins at 4.15 EUR/mo. Choose a plan that matches your resource needs, such as CPU and RAM, especially if you anticipate high usage.
2. Can I host multiple Mastodon instances on one VPS?
Yes, it is possible to host multiple Mastodon instances on one VPS, but this requires careful resource management. Each instance will need its own database and may require significant resources to function efficiently. Ensure your VPS has enough CPU power and RAM to support them.
3. Can I customize my Mastodon instance after installation?
Absolutely! Mastodon is highly customizable. You can change themes, add new features, and manage user accounts through the admin dashboard. Many users prefer to tweak their instances to fit specific community needs, such as creating bespoke content moderation tools or installing additional plugins.
By following this guide, you are now equipped to self-host Mastodon on your VPS, allowing for greater control and customization of your social networking experience.