Self Host Your Website without Opening Ports
This post is a follow along to a Youtube video walk-through that I recorded. We will be setting up a home server to host a web application without opening any ports on my home network. To accomplish this I’ll be using a Cloudflare tunnel.
[!note] The
$denotes a terminal command. Anything before$denotes the current working directory.
0. Prerequisites
- Remote Server
- Cloudflare Account
- Reliable Internet Connection
[!warning] A little disclaimer: I am not a professional; just a student. Do your own research but this should help you get up and running. For the most part I am just following Cloudflare and other documentation. I highly recommend you read through official documentation as needed.
1. Configure Server
- Make sure you have root permissions
sudo -lshould output (ALL : ALL) ALL on the current user.
- Add current user to sudo as root
usermod -aG sudo mauzy
- Install OpenSSH
ufw allow OpenSSHufw enableufw status
- Install nginx
sudo ufw allow 'Nginx HTTP'
2. Add your site to Cloudflare
- Register using Cloudflare
- If you have an existing domain, click add site
- Copy the name servers from Cloudflare to your registrar
- Ensure DNSSEC is disabled before doing this
[!note] DNS Records: This is where you can show everyone on Discord how cool you are by adding your website as a connection.
3. Additional Cloudflare settings
Configure DNSSEC
-
Enable in Cloudflare
- DNS > Settings
- Enable DNSSEC
-
Find walkthrough for your registrar
-
NameCheap
- Manage > Advanced DNS
- Enable DNSSEC
- Copy values from Cloudflare
- Save it and wait an hour
-
Set SSl/TLS encryption mode to Full(strict)
- This ensures a secure connection between your origin server and Cloudflare.
- Cloudflare will validate the SSL certificate on your server.
- This reduces the chances of man-in-the-middle attacks
- Find it in SSL/TLS > Overview
- Make sure the SSL/TLS recommender is on
-
Enable Always use Https
- SSL/TLS > Edge Certificates
-
Set Authenticated Origin Pulls
- This ensures only requests from Cloudflare with a valid certificate are accepted by your origin server. It helps to protect against unauthorizes access to your server.
- SSL/TLS > Origin Server
3. Create a tunnel and download the daemon for your Operating System
- Go to your newly added site in Cloudflare
- In the access tab, Launch the Zero Trust Platform. They move the location of this all of the time for no reason.
- Copy and paste into your server shell
4. Setup Firewall
- Download the firewall setup script from my Github page
~/$ curl https://raw.githubusercontent.com/Mauzy0x/Scripts/main/Cloudflare%20IPtable%20setup.sh >> ipScript.sh
- Make the script executable
~/$ chmod +x ipScript.sh
- Run the script
~/$ ./ipScript.sh
5. Nginx
- Set up server block
- Create a directory for your domain with the
pflag to create any necessary parent directories
sudo mkdir -p /var/www/your_domain/html- Now assign ownership of the directory to the current user
sudo chown -R $USER:$USER /var/www/your_domain/html- Ensure permissions are correct with chmod
sudo chmod -R 755 /var/www/your_domain- This uses octal notation. This recursively ensures the owner has full permissions
- Create a directory for your domain with the
- Configure Nginx server block
sudo rm /etc/nginx/sites-enabled/defaultsudo nano /etc/nginx/sites-available/your_domain
- Configure nginx configuration file
server {
listen 5552;
listen [::]:5552;
server_name localhost;
root /var/www/Mauzy-Site/;
location / {
try_files $uri /html/$uri; # Serve from /html/ if not found
}
location ~* \.(?:gif|css|js|)$ {
try_files $uri =404;
}
location /html/ {
index index.html index.htm;
try_files $uri $uri/index.html $uri.html =404;
}
error_page 404 = /404.html;
}
- Allow HTTPS traffic with Nginx Full which allows https and http
sudo ufw allow 'Nginx Full'sudo ufw reload
- Enable the site with a symbolic link
sudo ln -s /etc/nginx/sites-available/example.com.conf /etc/nginx/sites-enabled/example.com.conf
- Test config
sudo nginx -tsudo systemctl restart nginx- In most cases you can just reload nginx to make changes so that applications don’t go down. But in this case, let us restart nginx.
6. Trouble Shooting Connection
- If for whatever reason something is not working, lets troubleshoot.
- Let us first make sure we are properly hosting our webpage
curl localhost:portNo- If this command does not return your HTML then there is an issue with your NGINX configuration
- If you get an error where it is ‘argo tunnel error’ there is an issue where Cloudflare cannot see your service.
- Is your tunnel config pointing to http://localhost:portNo ? It needs to be http and it needs to be the same port number specified in your Nginx config.
- Let us first make sure we are properly hosting our webpage