Lemmy, a Reddit-like content aggregator, has been making waves in the world of open-source software. It’s a microblogging platform that enables the creation and moderation of communities and facilitates communication with other ActivityPub services such as Mastodon, Pleroma, and Peertube. This guide will walk you through the process of installing and configuring Lemmy on Ubuntu 22.04.
Prerequisites
Before we begin, there are a few prerequisites:
- A server running Ubuntu 22.04
- A non-root sudo user
- A fully qualified domain name (FQDN) like example.com
- Ensure that your system is up to date and has the required packages installed
You can use the following commands to update your system and install the necessary packages:
$ sudo apt update
$ sudo apt upgrade
$ sudo apt install wget curl nano ufw software-properties-common dirmngr apt-transport-https gnupg2 ca-certificates lsb-release ubuntu-keyring unzip -y
Step 1: Configuring the Firewall
Ubuntu comes with a built-in firewall known as Uncomplicated Firewall (ufw). It’s crucial to configure the firewall to ensure the security of your Lemmy installation. Use the following commands to configure your firewall:$ sudo ufw status
$ sudo ufw allow OpenSSH
$ sudo ufw allow http
$ sudo ufw allow https
$ sudo ufw enable
$ sudo ufw status
These commands will enable the firewall and allow SSH, HTTP, and HTTPS connections.
Step 2: Installing Docker and Docker Compose
Docker is an open-source platform that allows you to automate the deployment, scaling, and management of applications. Docker Compose is a tool for defining and managing multi-container Docker applications.
Ubuntu 22.04 comes with an older version of Docker. To install the latest version, use the following commands:
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker.gpg
$ echo "deb [arch="$(dpkg --print-architecture)" signed-by=/usr/share/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu "$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
$ sudo apt update
$ sudo apt install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
To verify the installation of Docker and Docker Compose, use the following command:$ sudo systemctl status docker
Step 3: Creating and Downloading Lemmy Files
Now that Docker and Docker Compose are installed, the next step is to create and download the necessary Lemmy files. Use the following commands:
$ mkdir ~/lemmy
$ cd ~/lemmy
$ wget https://raw.githubusercontent.com/LemmyNet/lemmy-ansible/main/templates/nginx_internal.conf
$ wget https://raw.githubusercontent.com/LemmyNet/lemmy-ansible/main/templates/docker-compose.yml
$ mkdir -p volumes/pict-rs
$ sudo chown -R 991:991 volumes/pict-rs
Step 4: Configuring Lemmy
Before proceeding with the installation, we need to edit the files we downloaded. Open the Docker compose file for editing and make the necessary changes as per your requirements. Generate an API key for pict-rs using the following command:
Replace the necessary variables in the file with your domain name, API key, and other details. Save the changes once you are done.
Step 5: Installing Lemmy
With all the files configured, it is now time to run Lemmy. Use the following commands to launch the Docker containers and check their status:
$ docker compose up -d
$ docker ps
You can test the installation by opening the URL http://<server_IP>:8536/
in your browser. If you see Lemmy’s homepage, then everything is set up correctly.
Step 6: Installing Nginx
Nginx is a popular open-source web server that can also be used as a reverse proxy, load balancer, and HTTP cache. To install the latest version of Nginx on Ubuntu 22.04, use the following commands:
$ curl https://nginx.org/keys/nginx_signing.key | gpg --dearmor | sudo tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null
$ echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg arch=amd64] http://nginx.org/packages/ubuntu `lsb_release -cs` nginx" | sudo tee /etc/apt/sources.list.d/nginx.list
$ sudo apt update
$ sudo apt install nginx
You can verify the installation and start Nginx using the following commands:$ nginx -v
$ sudo systemctl start nginx
$ sudo systemctl status nginx
Step 7: Installing SSL
To secure your Lemmy installation, it’s important to install an SSL certificate. You can use Certbot, a free, open-source tool to automate the process of obtaining and installing Let’s Encrypt SSL certificates. Use the following commands to install Certbot and generate an SSL certificate:
$ sudo snap install core && sudo snap refresh core
$ sudo snap install --classic certbot
$ sudo ln -s /snap/bin/certbot /usr/bin/certbot
$ sudo certbot certonly --nginx --agree-tos --no-eff-email --staple-ocsp --preferred-challenges http -m [email protected] -d example.com
$ sudo openssl dhparam -dsaparam -out /etc/ssl/certs/dhparam.pem 4096
You can verify the SSL renewal process using the following command:$ sudo certbot renew –dry-run
Step 8: Configuring Nginx
Create and open the file /etc/nginx/conf.d/lemmy.conf
for editing and paste the required code in it. Save the file once you are done. Then, open the file /etc/nginx/nginx.conf
for editing and add the required line before the line include /etc/nginx/conf.d/*.conf;
. Again, save the file once you are done.
Verify the Nginx configuration file syntax using the following command:$ sudo nginx -t
Restart the Nginx service using the following command:$ sudo systemctl restart nginx
Step 9: Accessing Lemmy
Open the URL https://example.com
in your browser. You will see the Lemmy homepage. Click on the Login link at the top to bring up the login page. Enter your credentials and click the Login button to proceed. You can start using Lemmy from here.
Step 10: Upgrading Lemmy
To upgrade Lemmy, open the docker-compose.yml
file in the ~/lemmy
folder and change the version numbers of the apps, Lemmy, Lemmy-UI, and Pictrs in the file. Save the file once you are done. Use the following commands to power down the Lemmy containers and start them back up using the new configuration:
$ docker compose down --remove-orphan
$ docker compose up -d
Step 11: Backing Up and Restoring Lemmy
To back up Lemmy, you need to take a dump of both the database and the volumes folder. Use the following commands to back up the database and the volumes folder:
$ cd ~/lemmy
$ docker compose exec postgres pg_dumpall -c -U lemmy | gzip > lemmy_dump_`date +%Y-%m-%d"_"%H_%M_%S`.sql.gz
$ sudo rsync -avP volumes ~/backup/lemmy
To restore the database on a new Lemmy installation, use the following set of commands:$ docker compose up -d postgres
$ gunzip < db_dump.sql | docker-compose exec -T postgres psql -U lemmy
$ chown -R $USER volumes
$ docker compose restart postgres
$ docker compose up -d
Conclusion
Following the steps outlined in this guide, you should now have a fully functional Lemmy instance running on Ubuntu 22.04. Whether you’re a hobbyist looking to start your own Reddit-like community or a business in need of a robust content aggregation platform, Lemmy is a versatile and powerful tool that can serve your needs.
For those who don’t want to go through the hassle of setting up their own server, you can always rely on Shape.host services. They provide ready-to-use Cloud Linux VPS with excellent customer support to keep your applications running smoothly.