Aug 10, 2024 3 min read

Cockpit: Simplifying Self-Hosting

Cockpit: Simplifying Self-Hosting
Table of Contents

Cockpit is a lightweight, web-based server management tool designed to simplify system administration tasks. It provides a user-friendly dashboard to monitor system performance, manage services, configure network settings, and more, all from a browser. As a self-hosted solution, Cockpit offers complete control over your infrastructure, making it ideal for developers and administrators who value customization and data sovereignty. This guide will walk you through installing, configuring, securing, and managing Cockpit to fully leverage its powerful features.

Installing Cockpit

πŸ“¦ Docker/Docker Compose Setup

To run Cockpit using Docker, create a docker-compose.yml file to define the container’s configuration. Use the following steps:

  1. Create a directory for Cockpit and navigate into it:

mkdir cockpit && cd cockpit

  1. Create the docker-compose.yml file with the following content:

version: '3.8'

services:

cockpit:

image: cockpit-project/cockpit:latest

container_name: cockpit

ports:

- "9090:9090" # Expose the Cockpit web interface

volumes:

- ./data:/var/lib/cockpit-data # Persistent data storage

restart: always

  1. Start the container with:

docker-compose up -d

This will launch Cockpit on port 9090. Visit http://<your-server-ip>:9090 in your browser to access the web interface.

πŸš€ Manual Installation

For those who prefer a manual setup, follow these steps to install Cockpit on a Linux server (e.g., Ubuntu):

  1. Update your system and install Cockpit:

sudo apt update

sudo apt install cockpit -y

  1. Enable and start the Cockpit service:

sudo systemctl enable --now cockpit

  1. Open port 9090 on the firewall to access Cockpit:

sudo ufw allow 9090/tcp

You can now navigate to http://<your-server-ip>:9090 to log in with your server credentials.

Configuring Nginx as a Reverse Proxy

🌐 Nginx Configuration

To route traffic to Cockpit via Nginx, create an Nginx server block:

  1. Install Nginx:

sudo apt install nginx -y

  1. Create a configuration file for Cockpit:

sudo nano /etc/nginx/sites-available/cockpit

  1. Add the following content:

server {

listen 80;

server_name your-domain.com;

location / {

proxy_pass http://localhost:9090;

proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

}

}

  1. Enable the configuration:

sudo ln -s /etc/nginx/sites-available/cockpit /etc/nginx/sites-enabled/

sudo nginx -t

sudo systemctl reload nginx

πŸ”’ SSL/TLS Setup

Secure Cockpit with Let's Encrypt:

  1. Install Certbot and get an SSL certificate:

sudo apt install certbot python3-certbot-nginx -y

sudo certbot --nginx -d your-domain.com

  1. Automate certificate renewal:

sudo crontab -e

0 3 * * * certbot renew --quiet

πŸ› οΈ Testing and Reloading Nginx

Test the Nginx configuration and reload it:


sudo nginx -t

sudo systemctl reload nginx

Logging and Debugging Cockpit

πŸ—ƒοΈ Enabling Debug Logs

To enable Cockpit debug logs, add a specific environment variable:

  1. Edit the Cockpit systemd service file:

sudo systemctl edit cockpit.service

  1. Add the following:

[Service]

Environment="COCKPIT_DEBUG=true"

  1. Reload the service:

sudo systemctl daemon-reload

sudo systemctl restart cockpit

πŸ“„ Viewing Logs

View Cockpit logs in real-time using journalctl:


sudo journalctl -u cockpit -f

πŸ› οΈ Troubleshooting Common Issues

Check for common issues such as port conflicts or permissions by inspecting logs:


sudo journalctl -u cockpit | grep ERROR

πŸ“€ Exporting Logs

Forward Cockpit logs to an external system like an ELK stack. For example, configure rsyslog to send logs to a remote server:


sudo nano /etc/rsyslog.d/50-default.conf

## Add:

*.info @elk-server-ip:514

sudo systemctl restart rsyslog

Backup and Restore

πŸ—‚οΈ File-Based Backups

Back up Cockpit config files and data:


tar -czvf cockpit-backup.tar.gz /var/lib/cockpit-data

πŸ”„ Database Backups

If Cockpit uses a database, back it up:


mysqldump -u root -p cockpit_db > cockpit_db_backup.sql

πŸ“… Automated Backup Scripts

Automate backups by adding a cron job:


crontab -e

## Add:

0 2 * * * tar -czvf /backups/cockpit-$(date +\%F).tar.gz /var/lib/cockpit-data

Updating and Upgrading Cockpit

⬆️ Updating Docker Images

Update to the latest Cockpit Docker image:


docker-compose pull

docker-compose down

docker-compose up -d

πŸ› οΈ Manual Updates

Update Cockpit on a Linux server:


sudo apt update

sudo apt upgrade cockpit -y

πŸ” Checking for Updates

Verify the Cockpit version:


cockpit --version

Leveraging Cockpit’s Unique Features

πŸ”§ Enabling APIs

Cockpit provides APIs to interact programmatically. Enable API access by configuring the system and testing an example:

  1. Install curl:

sudo apt install curl -y

  1. Test API authentication:

curl -u username:password http://localhost:9090/api/

🌟 Advanced Configurations

Customize Cockpit’s behavior by editing its settings. For example, enable additional modules using:


sudo dnf install cockpit-packagekit cockpit-networkmanager

sudo systemctl restart cockpit

Wrapping Up

This guide covered everything you need to deploy, configure, and manage Cockpit, from installation to advanced usage. Self-hosting Cockpit provides unparalleled flexibility for server management, and the steps outlined here will help you unlock its full potential. Start implementing these tips today to streamline and secure your server administration workflow.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to Selfhosted Ninja.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.