To set up a self-hosted instance of Swetrix, follow these steps. This involves installing the software on your server, configuring it, and integrating it with your website.
You’ll need to install some dependencies before installing Swetrix.
Update your system:
sudo apt update && sudo apt upgrade -y
Install Node.js and npm:
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt install -y nodejs
Install MongoDB (or another database of your choice):
sudo apt install -y mongodb
Clone the Swetrix repository:
git clone https://github.com/swetrix/swetrix.git
Navigate to the project directory:
cd swetrix
Install project dependencies:
npm install
Build the project:
npm run build
Create a .env file in the root directory and configure the following variables:
PORT=3000
MONGO_URI=mongodb://localhost:27017/swetrix
Adjust the PORT and MONGO_URI as needed.
Start the server:
npm start
You can also use a process manager like PM2 to keep the application running:
npm install -g pm2
pm2 start npm -- start
pm2 save
http://your-server-ip:3000).</head> tag in your HTML.If you want to serve Swetrix over a standard HTTP/HTTPS port, consider setting up Nginx as a reverse proxy:
Install Nginx:
sudo apt install nginx
Configure Nginx:
Create a configuration file for your site:
sudo nano /etc/nginx/sites-available/swetrix
Add the following configuration:
server {
listen 80;
server_name your-domain.com; # Replace with your domain or server IP
location / {
proxy_pass http://localhost:3000; # Swetrix runs on this port
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
Enable the configuration:
sudo ln -s /etc/nginx/sites-available/swetrix /etc/nginx/sites-enabled/
Test and restart Nginx:
sudo nginx -t
sudo systemctl restart nginx