Skip to content

Instantly share code, notes, and snippets.

@0x263b
Last active November 13, 2018 18:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 0x263b/84b16cabf0a4c60ded5ec718c22b15ef to your computer and use it in GitHub Desktop.
Save 0x263b/84b16cabf0a4c60ded5ec718c22b15ef to your computer and use it in GitHub Desktop.
Nginx + Let's Encrypt

First, set up your cert directory

$ mkdir /var/www/dehydrated
$ cd /var/www/dehydrated

Download dehydrated, and make it runnable.

$ wget https://raw.githubusercontent.com/lukas2511/dehydrated/master/dehydrated
$ chmod u+x dehydrated

Add a location block to your example.com nginx config pointing to the dehydrated directory (goes inside the server{} block)

location ^~ /.well-known/acme-challenge {
	alias /var/www/dehydrated;
}

Restart nginx

$ service nginx restart

Now run the script with your domain

$ ./dehydrated -c -d example.com

Congratulations, you have some certs.

Go back to your example.com nginx config, add the certs and 301 redirect http requests.

# Redirect all http traffic
server {
	listen 80;
	sever_name example.com;
	return 301 https://$host$request_uri;
}

server {
	listen 443 ssl;
	
	root /var/www/example.com;
	index index.html index.htm;
	
	server_name example.com;
	add_header Strict-Transport-Security "max-age=31536000; includeSubdomains;";

	ssl_certificate /var/www/dehydrated/certs/example.com/fullchain.pem;
	ssl_certificate_key /var/www/dehydrated/certs/example.com/privkey.pem;

	location ^~ /.well-known/acme-challenge {
		alias /var/www/dehydrated;
	}
}

Restart nginx

$ service nginx restart

You're done!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment