cmc/cleberg.net

My personal web garden & blog.

clone: git clone https://gitbay.org/cmc/cleberg.net.git

main: content/blog/2025-12-06-self-hosting-tor.org · raw

  1#+date:        [2025-12-06 Sat 14:13:00]
  2#+title:       Self-Hosting Guide: Tor Websites
  3#+description: How to host a website on Tor with an onion address.
  4#+slug: self-hosting-tor
  5#+filetags:    :linux:self-hosting:
  6
  7* Installation
  8
  9First, install the Tor package. I'm using Ubuntu, so I'll be using =apt= to
 10install Tor.
 11
 12#+begin_src sh
 13sudo apt install tor
 14#+end_src
 15
 16* Configuration
 17
 18** Tor
 19Next, let's configure the service we'll be serving via Tor. For this, edit the
 20file below and add the lines below to the end of the file.
 21
 22#+begin_src sh
 23sudo nano /etc/tor/torrc
 24#+end_src
 25
 26The following lines you add to the file will define the directory to use and the
 27internet protocol (IP) address and port we will be using.
 28
 29#+begin_src text
 30HiddenServiceDir /var/lib/tor/hidden_service/
 31HiddenServicePort 80 127.0.0.1:5000
 32#+end_src
 33
 34Next, you will need to =cat= the contents of the file below to find the onion
 35address provided to you.
 36
 37#+begin_src sh
 38sudo cat /var/lib/tor/hidden_service/hostname
 39#+end_src
 40
 41** Nginx
 42
 43With this onion address handy, create a file in your Nginx configuration
 44directory. Mine is =conf.d=, but yours may be =sites-available=. If that's the
 45case, don't forget to create a symbolic link from the file you created in
 46=sites-available= to =sites-enabled=.
 47
 48#+begin_src sh
 49sudo nano /etc/nginx/conf.d/tor-website.conf
 50#+end_src
 51
 52Within this Nginx configuration file, I will be using the file below. I have
 53left comments to explain each part.
 54
 55#+begin_src conf
 56server {
 57	# The IP address and port Nginx will listen on
 58	listen 127.0.0.1:80;
 59	# The onion address to listen for
 60	server_name sv3g2dlyvwyk2nvi3eeh55fcrpdvjlclhi6wwsx57cste6lwdjanzyyd.onion;
 61	# The directory where your website files are
 62	root /var/www/tor-website/;
 63
 64	# By default, try to serve the files. Return 404 if a file is not found.
 65	location / {
 66		try_files $uri $uri/ =404;
 67	}
 68
 69	# Put any other configurations here at the end, such as redirects
 70	# ...
 71}
 72#+end_src
 73
 74Finally, we can restart the services and check to see if it's working.
 75
 76#+begin_src sh
 77sudo systemctl restart nginx.service
 78sudo systemctl restart tor.service
 79#+end_src
 80
 81At this point, you should be able to view your Onion address in Tor.
 82
 83[[https://img.cleberg.net/blog/20251206-self-hosting-tor/onion.webp]]
 84#+caption: Onion Address in Tor
 85
 86** Onion-Location
 87
 88Lastly, let's advertise the =Onion-Location= on our clearnet website so that
 89users on Tor can automatically switch to the Onion address if they want to.
 90
 91#+begin_src sh
 92sudo nano /etc/nginx/conf.d/regular_website.conf
 93#+end_src
 94
 95#+begin_src conf
 96server {
 97	server_name example.com;
 98	add_header Onion-Location http://YourOnionAddress.onion$request_uri;
 99}
100#+end_src
101
102Once complete, restart Nginx.
103
104#+begin_src sh
105sudo systemctl restart nginx.service
106#+end_src
107
108You should now see a big purple button in Tor titled =.onion available= when
109visiting the clearnet web page.
110
111[[https://img.cleberg.net/blog/20251206-self-hosting-tor/clearnet.webp]]
112#+caption: Onion-Location Button
113
114If everything worked, you're all done! You now have a website served via an
115onion address on Tor and an Onion-Location header advertising your new onion
116address.