My personal website https://leviolson.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

142 lines
4.2 KiB

  1. # Let's Encrypt
  2. ## Working with NGINX configs locally
  3. Sync your local file to the server (run after every update)
  4. ```bash
  5. rsync -rtvpl path/to/local/leviolson.com.conf base-droplet:/etc/nginx/sites-available/
  6. ```
  7. Create a symlink in nginx `sites-enabled`
  8. ```bash
  9. ssh base-droplet ln -s /etc/nginx/sites-available/leviolson.com.conf /etc/nginx/sites-enabled/
  10. ```
  11. Restart Nginx after each `rsync` command
  12. ```bash
  13. ssh base-droplet sudo service nginx restart
  14. ```
  15. ## Setup NGINX for cert creation
  16. Nginx must be minimally configured at this point to allow port 80 to work and not fail trying to access port 443 or redirecting you. Our main nginx config (below) tries to redirct to HTTPS which would cause Let's Encrypt to fail.
  17. ```nginx
  18. server {
  19. listen 80;
  20. server_name leviolson.com www.leviolson.com;
  21. location / {
  22. proxy_pass http://localhost:3000; # port must match the same as the node app
  23. proxy_http_version 1.1;
  24. proxy_set_header Upgrade $http_upgrade;
  25. proxy_set_header Connection 'upgrade';
  26. proxy_set_header Host $host;
  27. proxy_cache_bypass $http_upgrade;
  28. }
  29. }
  30. ```
  31. Sync this file:
  32. ```bash
  33. rsync -rtvpl ./leviolson.com.conf base-droplet:/etc/nginx/sites-available/
  34. ```
  35. Restart Nginx:
  36. ```bash
  37. ssh base-droplet sudo service nginx restart
  38. ```
  39. ## Create the cert
  40. ```bash
  41. sudo certbot certonly --nginx --manual -d leviolson.com -d www.leviolson.com
  42. ```
  43. This will start the process of generating a cert for the two domain names provided.
  44. As part of that process it will look for `.well-known/acme-challenge/{file}` where `{file}` is provided by the command output. The contents of that file are provided as well.
  45. Something like the following will create this file for you:
  46. ```bash
  47. echo "long string provided" > {file}
  48. ```
  49. After creating the file(s), you can complete the cert process and at that point you need to change the nginx conf file.
  50. ## NGINX final state
  51. ```nginx
  52. server {
  53. if ($host = www.leviolson.com) {
  54. return 301 https://leviolson.com$request_uri;
  55. }
  56. server_name leviolson.com www.leviolson.com;
  57. location / {
  58. proxy_pass http://localhost:3000; # port must match the same as the node app
  59. proxy_http_version 1.1;
  60. proxy_set_header Upgrade $http_upgrade;
  61. proxy_set_header Connection 'upgrade';
  62. proxy_set_header Host $host;
  63. proxy_cache_bypass $http_upgrade;
  64. }
  65. listen 443 ssl; # managed by Certbot
  66. ssl_certificate /etc/letsencrypt/live/leviolson.com-0001/fullchain.pem; # managed by Certbot
  67. ssl_certificate_key /etc/letsencrypt/live/leviolson.com-0001/privkey.pem; # managed by Certbot
  68. include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
  69. ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
  70. }
  71. server {
  72. if ($host = leviolson.com) {
  73. return 301 https://$host$request_uri;
  74. }
  75. if ($host = www.leviolson.com) {
  76. return 301 https://leviolson.com$request_uri;
  77. }
  78. server_name leviolson.com www.leviolson.com;
  79. listen 80;
  80. return 404;
  81. }
  82. ```
  83. You'll want to verify the location of the `*.pem` files linked in the nginx config above.
  84. ```bash
  85. ssh base-droplet ls -al /etc/letsencrypt/live/leviolson.com-0001/
  86. ```
  87. This should return something like the following:
  88. ```
  89. total 12
  90. drwxr-xr-x 2 root root 4096 Jul 20 18:14 .
  91. drwxr-xr-x 7 root root 4096 Jul 20 19:21 ..
  92. lrwxrwxrwx 1 root root 42 Jul 20 18:14 cert.pem -> ../../archive/leviolson.com-0001/cert1.pem
  93. lrwxrwxrwx 1 root root 43 Jul 20 18:14 chain.pem -> ../../archive/leviolson.com-0001/chain1.pem
  94. lrwxrwxrwx 1 root root 47 Jul 20 18:14 fullchain.pem -> ../../archive/leviolson.com-0001/fullchain1.pem
  95. lrwxrwxrwx 1 root root 45 Jul 20 18:14 privkey.pem -> ../../archive/leviolson.com-0001/privkey1.pem
  96. -rw-r--r-- 1 root root 692 Jul 20 18:14 README
  97. ```
  98. Sync this file:
  99. ```bash
  100. rsync -rtvpl ./leviolson.com.conf base-droplet:/etc/nginx/sites-available/
  101. ```
  102. Restart Nginx:
  103. ```bash
  104. ssh base-droplet sudo service nginx restart
  105. ```
  106. ## Some helpful debugging commands
  107. ```bash
  108. pm2 status
  109. ```
  110. ```bash
  111. journalctl -xe
  112. ```
  113. ```bash
  114. sudo ufw status
  115. ```
  116. This should include the port you are serving if you wish to have that port accessible to the public as well.