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