Skip to content

Instantly share code, notes, and snippets.

@jnsprnw
Last active March 2, 2024 13:28
Show Gist options
  • Save jnsprnw/be73def30e93ed79a3ecfd7d06b29da7 to your computer and use it in GitHub Desktop.
Save jnsprnw/be73def30e93ed79a3ecfd7d06b29da7 to your computer and use it in GitHub Desktop.
Tutorial on how to install Strapi on Uberspace

Strapi on Uberspace

Some people had problems building Strapi on Uberspace since it will kill your procress when it uses more than 1.5GB of RAM. I had the problem with some versions of Strapi, but the current (4.5) run through without any problems.

⚠️ Notice that you need to run Node version 14 on your Uberspace to run Strapi (uberspace tools version use node 14).

1. Database (Postgres)

I run into problems when running Postgres on Uberspace and using it for Strapi. (Something with pooling) And since I’m no expert in Postgres I decided to switch to Supabase’s free plan. This also saves Uberspace some ressources. If you want to run Postgres on Uberspace, follow this tutorial.

So for Supabase, first create an account and a new database/project. Next, go to Project Settings/Database. You find all the credetials there. Save them for later.

⚠️ I had to wait for some time until the database was reachable from outside.

If you are coming from Heroku and have a existing database, you can use this tutorial on how to migrate.

2. Install Strapi

I would recommend to install it in the following way (similar to using it on Heroku):

  1. Install Stapi on your local machine
  2. Push you a Github repo
  3. Clone the repo from Github to Uberspace

This way, you make changes like altering the data structure locally, push them to Github and pull changes to Uberspace.

Installing Strapi on your local machine

Follow the instructions from the offical documentation

You might want to use Cloudinary to not use any space from Uberspace for your media files. Follow the instructions in their blog.

Next, tell Strapi to use Supabase in production mode. You need to edit two files. (We will take care of the environment variables later)

⚠️ Notice that the layout of these two files changed in Strapi version 4. Find the latest version in their official documentation.

config/env/production/database.js

module.exports = ({ env }) => ({
  connection: {
    client: 'postgres',
    connection: {
      host: env('DATABASE_HOST'),
      port: env('DATABASE_PORT'),
      database: env('DATABASE_DB'),
      user: env('DATABASE_USER'),
      password: env('DATABASE_PW'),
      ssl: false // I haven’t tried it with ssl enabled
    },
    debug: false,
  },
});

config/env/production/server.js

module.exports = ({ env }) => ({
  proxy: true,
  url: env('URL'),
  port: env('PORT'), // This custom port will be important to point a domain to the service
  host: env('HOST'),
  app: { 
    keys: env.array('APP_KEYS')
  },
})

⚠️ If you make any changes here, remember to rebuild the admin UI with npm run build on Strapi.

Push everything to Github.

Install Strapi on Uberspace

To clone the repo from Github, ssh into your Uberspace account.

⚠️ Notice that you need to run Node version 14 on your Uberspace to run Strapi (uberspace tools version use node 14).

Navigate to a location on your Uberspace. I use /home/<user>/strapi/. The run git clone <repo-url> and then npm install. With every change, you need to rebuild the admin UI with npm run build.

Creating a service

Most other tutorials recomment pm2, but I’ll just go with supervisord as recommended by Uberspace. The next steps are mostly taken from the tutorial by Uberspace. Check their website for the latest version.

For the next step, you need the credentials you got from Supabase (or the Postgres database running on Uberspace)

Create the following file at etc/services.d/<project-name>.ini

[program:<service-name>] # Choose any dash-cased name for the service
directory=/home/mmmd/strapi/<project-name> # The path where your Strapi is located on Uberspace
command=npm run develop # This will run Strapi in development mode, but you can also change it to production mode accordingly
autostart=true
autorestart=true
environment=
	NODE_ENV="production", # This tells Strapi to use the production settings the database and the server
	DATABASE_USER="<user>",
	DATABASE_PW="<pw>",
	DATABASE_HOST="<host-url>",
	DATABASE_PORT="<port>",
	DATABASE_DB="<db>",
	ADMIN_JWT_SECRET="<uniq>", # Generate this with `openssl rand -base64 32` on a Mac/Linux
	API_TOKEN_SALT="<uniq>", # Generate this with `openssl rand -base64 32` on a Mac/Linux
	APP_KEYS="<uniq>", # Generate this with `openssl rand -base64 32` on a Mac/Linux
	JWT_SECRET="<uniq>", # Generate this with `openssl rand -base64 32` on a Mac/Linux
	CLOUDINARY_KEY="<key>", # If you are using Cloudinary
	CLOUDINARY_NAME="<name>", # If you are using Cloudinary
	CLOUDINARY_SECRET="<secret>", # If you are using Cloudinary
	URL="<url>", # The url from which you will access Strapi later on. We will define this in the next step. Probably something like https://strapi.<user>.uber.space/
	PORT="1031", # This port must not be used by any other app an between 1024 and 65535. 
	HOST="0.0.0.0"

⚠️ Notice the tabs for the environment variables.

Next, run the following commands:

supervisorctl reread
supervisorctl update
supervisorctl start <service-name>

⚠️ Remember to always run these three commands when making changes to the file!

You can check the logfile at logs/supervisored.log.

Pointing a url to your service

The next step is again from Uberspace’s documentation about subdomains. Run the following command to create subdomain:

uberspace web domain add strapi.<user>.uber.space

The domain must be the same as the one defined in your service file. Next, link your service to the domain:

uberspace web backend set strapi.<user>.uber.space --http --port <port>

Again, use your domain and the port that you defined in the service file.

The Strapi admin UI should now be available under the url.

Problems

The url is available but not the admin path.

This is probably because you changed the host in the config or never build the interface. Do that with npm run build.

Other tutorials

Find other tutorials here and an issue for an official guide here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment