| Next revision | Previous revision |
| computing:cockpit [2025/02/15 14:53] – created oemb1905 | computing:cockpit [2026/10/10 03:05] (current) – oemb1905 |
|---|
| ------------------------------------------- | ------------------------------------------- |
| |
| This tutorial covers how to set up an apache reverse proxy for cockpit. It is also used in conjunction with ufw, which limits connections to designated source IPs. This configuration of cockpit does not use NetworkManager.First, let's install cockpit: | === Introduction === |
| | This tutorial covers how to set up Cockpit on Debian. The approach here assumes that Cockpit will be installed in one of two different ways: |
| |
| . /etc/os-release | * Bare Metal - only ssh is open |
| echo "deb http://deb.debian.org/debian ${VERSION_CODENAME}-backports main" > \ | * Virtual Machine - ssh and 80/443 are open for the reverse proxy |
| /etc/apt/sources.list.d/backports.list | |
| apt update | |
| apt install -t ${VERSION_CODENAME}-backports cockpit | |
| apt remove network-manager | |
| apt autoremove | |
| apt install cockpit-machines | |
| |
| Now that cockpit is installed, let's make sure the firewall is setup to allow requests from the host itself (needed for Let's Encrypt requests) and from trusted source IPs of your choosing. | In both scenarios, we use the apache location package to limit url requests to approved IPs. This tutorial assumes you already have a sufficiently hardened and provisioned VPS/VM w/ a LAMP stack and some associated A/AAAA records ready to go. If not, go read [[https://wiki.haacksnetworking.org/doku.php?id=computing:apachesurvival|Apache Survival]] first and come back. If you are ready to go, then this tutorial will cover: |
| |
| ufw allow from <server's IP> to any port 80 | * Installation of Cockpit + Preferred Packages |
| ufw allow from <server's IP> to any port 443 | * Setup of Apache Reverse Proxy; including setup for "with" and "without" 80/443 exposed/listening |
| ufw allow from <trusted IP> to any port 80 | * Localhost w/ FoxyProxy |
| ufw allow from <trusted IP> to any port 443 | * Official Cockpit Flatpak |
| ufw allow from <trusted IP> to any port 9090 | |
| ufw allow from <trusted IP> to any proto udp port 1194 | === Install Cockpit & Configure Apache === |
| ufw allow from <trusted IP> to any proto tcp port 22 | Let's install cockpit and then create an apache virtual host for it. After that, we will cut the cert, then swap the vhost configs with the reverse proxy config. Let's enable TLS modules and headers. |
| | |
| The above firewall rules will differ depending on one's setup and there are certainly other ways to do this. Once this is done, let's set up apache to serve cockpit to the trusted IPs. | sudo apt install cockpit* |
| | sudo apt remove cockpit-389-ds |
| | sudo a2enmod ssl |
| | sudo a2enmod headers |
| | |
| | After that, open ''sudo nano /etc/apache2/sites-available/domain.com.conf'' and change the ''ServerName'' field, and make a placeholder directory at ''/var/www/domain.com/public_html'' and make sure the ''WebRoot'' points properly to that directory. Add the vhost with ''a2ensite domain.com.conf'' and restart the service ''systemctl restart apache2''. Once basic http is functional, build the cert: |
| | |
| | <code> |
| | sudo apt install certbot letsencrypt python3-certbot-apache |
| | sudo certbot --authenticator standalone --installer apache -d domain.com --pre-hook "systemctl stop apache2" --post-hook "systemctl start apache2" |
| | </code> |
| | |
| | The cert is now built and ''domain.com'' should now have TLS enabled. If not, check your config and debug with ''sudo apache2ctl configtest'' and resolve that issue before proceeding. Once that's done and the page resolves with TLS, enable the proxy and swap the configs. |
| |
| sudo apt install apache | |
| sudo a2enmod proxy_http | sudo a2enmod proxy_http |
| sudo a2enmod proxy | sudo a2enmod proxy |
| sudo a2enmod rewrite | sudo a2enmod rewrite |
| sudo a2enmod ssl | sudo a2enmod proxy_wstunnel |
| sudo a2enmod headers | sudo a2enmod authz_host |
| sudo nano /etc/apache2/sites-available/000-default.conf | apache2ctl configtest |
| <enter the FQDN in the virtual host> | systemctl reload apache2 |
| a2ensite 000-default.conf | |
| sudo apache2ctl configtest | |
| | |
| Once the host serves http requests without any issues, it's time to setup TLS. I prefer to use Let's Encrypt as follows: | Now, we will go ahead and swap the default virtual hosts with the reverse proxy configurations. Open the ''domain.com.conf'' vhost in ''/etc/apache2/sites-enabled'' and change it to: |
| |
| sudo apt install certbot letsencrypt python3-certbot-apache | <code> |
| sudo certbot --authenticator standalone --installer apache -d fqdn.com --pre-hook "systemctl stop apache2" --post-hook "systemctl start apache2" | <VirtualHost *:80> |
| | ServerName domain.com |
| Once the host serves https requests without any issues, it's time to replace the virtual host you set up above with a reverse proxy configuration. You will also need to delete the virtual host that Let's Encrypt setup as it will no longer be necessary. | |
| | <Location /> |
| | Require ip 96.40.208.144 |
| | Require ip 2601:8c3:8684:a3f1::b2c |
| | </Location> |
| | |
| | RewriteEngine On |
| | RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent] |
| | </VirtualHost> |
| | </code> |
| |
| cd /etc/apache2/sites-enabled | Open the ''domain.com-le-ssl.conf'' vhost in ''/etc/apache2/sites-enabled'' and change it to something like this: |
| rm 000-default-le-ssl.conf [name might differ] | |
| sudo nano 000-default.conf | |
| | |
| In the virtual host that opens up, enter something like the following: | |
| |
| <IfModule mod_ssl.c> | <code> |
| <VirtualHost *:443> | <IfModule mod_ssl.c> |
| ServerName netzion.haacksnetworking.org | <VirtualHost *:443> |
| Include /etc/letsencrypt/options-ssl-apache.conf | ServerName domain.com |
| ProxyPreserveHost On | |
| ProxyRequests Off | <Location /> |
| ProxyPass / http://127.0.0.1:9090/ upgrade=websocket | Require ip 96.40.208.144 |
| ProxyPassReverse / http://127.0.0.1:9090/ | Require ip 2601:8c3:8684:a3f1::b2c |
| SSLCertificateFile /etc/letsencrypt/live/fqdn.com/fullchain.pem | </Location> |
| SSLCertificateKeyFile /etc/letsencrypt/live/fqdn.com/privkey.pem | |
| </VirtualHost> | SSLCertificateFile /etc/letsencrypt/live/domain.com/fullchain.pem |
| </IfModule> | SSLCertificateKeyFile /etc/letsencrypt/live/domain.com/privkey.pem |
| | Include /etc/letsencrypt/options-ssl-apache.conf |
| | |
| | <IfModule mod_headers.c> |
| | Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains" |
| | </IfModule> |
| |
| In addition to setting apache to serve external requests to cockpit, you also need to configure cockpit to recognize your fqdn.com as a trusted origin: | ProxyPreserveHost On |
| | ProxyRequests Off |
| |
| sudo nano /etc/cockpit/cockpit.conf | RewriteEngine On |
| | RewriteCond %{HTTP:Upgrade} =websocket [NC] |
| | RewriteRule /(.*) ws://127.0.0.1:9090/$1 [P,L] |
| | RewriteCond %{HTTP:Upgrade} !=websocket [NC] |
| | RewriteRule /(.*) http://127.0.0.1:9090/$1 [P,L] |
| | |
| | ProxyPass / http://127.0.0.1:9090/ |
| | ProxyPassReverse / http://127.0.0.1:9090/ |
| | |
| | RequestHeader set X-Forwarded-Proto "https" |
| | </VirtualHost> |
| | </IfModule> |
| | </code> |
| | |
| | These configs were built using the examples on Cockpit's documentation for reverse proxy configurations. Make sure to edit the IPs above to IPs you actually have. Additionally, I recommend adding some more for a dedicated VPS, etc., and/or another backup server. You can always foxy proxy through ssh and SOCKS to access the host, so more IPs are safer and avoid you accidentally locking yourself out. Once they are built, make sure to configure Cockpit to trust the domain as an origin. Open ''sudo nano /etc/cockpit/cockpit.conf'' and drop in the following: |
| | |
| | <code> |
| | [WebService] |
| | Origins = https://domain.com https://domain.com:9090 |
| | #wss://domain.com is needed on some setups |
| | #ProtocolHeader = X-Forwarded-Proto can be needed in filtered setups |
| | </code> |
| | |
| | This instructs Cockpit's internal web socket system to trust the domain/origin and the web socket reverse proxy that we configured in apache (since it differs from the listener on localhost). Restart the server or minimally restart cockpit+apache. Once they are restarted and their status healthy, navigate to ''domain.com'' in a browser and it should redirect to TLS and show the Cockpit landing page. If not, trace over the steps above and fix your work before proceeding. |
| | |
| | === Discussion on Access to Cockpit === |
| | |
| | There's a lot of debate about whether production servers that run virtualized services (VMs, containers, etc.) should be publicly exposed. Now, if you fully trust your server and Cockpit's web auth, no need to continue. For most folks, however, retaining ssh access and locking everything else down is usually the go to ... meaning that 80/443 will be closed. Additionally, as already shown above, we are letting the web server (apache) further restrict url requests with the location package. At first, this might seem like we are locking down the web server that we need to access the service, but/and we are actually tunneling through ssh instead. Additionally, the addition of TLS - while not adding any security benefit to the ssh tunnel - avoids the web browser barf you get on IPs, localhost endpoints, etc., and gives you an easy to remember domain. Okay, let's install ufw and open ssh and leave everything else closed. |
| | |
| | sudo apt install ufw |
| | sudo ufw reset |
| | sudo ufw allow 22 |
| | |
| In that file, enter the following: | Now that that is done, we need to make a special certbot entry in cron that uses a pre/post hook in order to briefly open port 80 for renewal and then close it again. To do that, let's edit our ''sudo crontab -e'' entry as follows: |
| | |
| [WebService] | 30 2 * * 1 certbot renew --apache --pre-hook "ufw allow 80/tcp" --post-hook "ufw delete allow 80/tcp" >> /var/log/le-renew.log 2>&1 |
| Origins = https://fqdn.com http://127.0.0.1:9090 | |
| ProtocolHeader = X-Forwarded-Proto | This adjusted script briefly opens up 80 every 30 days to see if the cert needs renewal. After it checks, it closes the port back up. At this point, one might realize, what value is Let's Encrypt even providing here? If 80/443 are closed and you are running everything through ssh. As I mentioned above, the addition of TLS removes browser complaints and provides an easy to remember domain for access, but/and, if I make a mistake or ufw fails, etc., then Cockpit faces the public with not only TLS, but with the location restrictions stated above. This ensures that Cockpit is triple protected: ssh tunnel, ufw, and apache locations. To access the Cockpit web interface, we first ssh into the host with the ''-D'' flag: |
| AllowUnencrypted = true | |
| | ssh -D 8090 root@domain.com |
| Now that your virtual host is setup as a reverse proxy and your origin is trusted by cockpit, you should restart apache with ''systemctl restart apache2'' and navigate to your cockpit instance ''https://fqdn.com''. If you did everything correctly, cockpit will render and you will not need to append ''9090'' to the fqdn.com. Additionally, since you specified the host itself in the firewall rules above, it will be able to renew your certificate files every 3 months. | |
| | Once you are logged in, configure your Firefox's network settings to use SOCKS5 proxy with localhost and port 8090. //Using 8090 or something non-standard is helpful as containers and/or other services might already be bound to 8080//. You can now enter domain.com into your browser and your Cockpit instance will just work. If you check from another location, it won't work because 80/443 are closed (or your IPs are not approved). It's using the reverse proxy but accessing it locally (via the ssh tunnel), which is automatically trusted by apache. If changing Firefox's network settings proves inconvenient or cumbersome, you can setup a proxy with the foxyproxy addon for Firefox: |
| | |
| | {{ :computing:screenshot_20261009_205620.png?direct&800 |}} |
| | |
| | Of course, you could have skipped the apache configuration entirely and just run the ssh proxy and accessed Cockpit via localhost:9090 and/or 127.0.0.1:9090 but then you have to edit Firefox's developer settings to allow "localhost hijacking" or Firefox will get confused over which 'local' host is intended? Especially with reverse proxies, some internal web service assets will be originating from localhost (since they are behind the reverse proxy lol) and thus Firefox gets confused. There are also associated security concerns. However, if you want to avoid the apache part and just ssh with SOCKS, set the following to true: |
| | |
| | {{ :computing:screenshot_from_2026-03-29_13-28-00.png?direct |}} |
| | |
| | Set this value to true and you can then access Cockpit via localhost:9090 without any issues. Before doing this, however, be aware of the risks/dangers of this approach. Just search //localhost hijacking// if you need to read up on it. I personally use the reverse proxy with apache, which although overkill, also results in avoiding this flag / dev mode hack. Other solutions involve binding Cockpit to a local IP and then proxying into the host and accessing via that IP:9090, or simply using the Cockpit flatpak, which connects via ssh and runs the Cockpit web socketing system internally, and even allows folks to connect to instances without Cockpit installed and get that familiar form factor: |
| | |
| | * [[https://cockpit-project.org/faq.html|Using Cockpit ]] |
| | |
| | In conclusion, this tutorial allows for secure web browser access with no browser barf or work arounds. The cert renews on schedule and briefly opens port 80 during that process. If something goes wrong, TLS is ready and secure if ufw or location packages fail. Additionally, since ssh is open by default, one can also simply use the official Cockpit flatpak. However, it's nice to not have to rely on the flatpak and this tutorial solves that. |
| |
| --- //[[alerts@haacksnetworking.org|oemb1905]] 2025/02/15 14:32// | --- //[[alerts@haacksnetworking.org|oemb1905]] 2026/10/10 02:57// |