Skip to content

Instantly share code, notes, and snippets.

@ONEKENO
Forked from justinatack/letsencrypt.md
Last active June 1, 2019 14:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ONEKENO/bb1b9f1a7b58563f81481b82b78d925e to your computer and use it in GitHub Desktop.
Save ONEKENO/bb1b9f1a7b58563f81481b82b78d925e to your computer and use it in GitHub Desktop.
Installing Let's Encrypt with Cerbot on Vultr & ServerPilot

Let's Encrypt

1.SSH as root into the server

ssh root@SERVER_IP_ADDRESS

2.Install Certbot (ถ้าเคย install แล้วข้ามขั้นนี้)

$ sudo apt-get update
$ sudo apt-get install software-properties-common
$ sudo add-apt-repository ppa:certbot/certbot
$ sudo apt-get update
$ sudo apt-get install python-certbot-nginx
chmod a+x certbot-auto
mv certbot-auto /usr/bin/certbot

3.Stop NGINX

service nginx-sp stop

4.Run the certbot-auto Script

อันเก่าไม่ใช้แล้ว ใช้อันล่างแทน

certbot certonly --standalone -d DOMAIN_NAME.com -d www.DOMAIN_NAME.com

ใช้อันนี้แทน

certbot certonly --webroot -w /srv/users/serverpilot/apps/APP_NAME/public -d www.DOMAIN_NAME.com -d DOMAIN_NAME.com -d sub.domain.com -d sub2.domain.com

(ใช้โค๊ดนี้แทนที่ fork มา เพราะ cloudflare ทำให้ auto renew ไม่ได้) ref: https://certbot.eff.org/docs/using.html#webroot

The first time you run this command the process will take a few minutes, after it's finished installing, follow on screen instructions and you should get a message similar to:

Congratulations! Your certificate and chain have been saved at /etc/letsencrypt/live/DOMAIN_NAME.com/fullchain.pem.

5.Add SSL config file

nano /etc/nginx-sp/vhosts.d/APP_NAME.ssl.conf

APP_NAME = ServerPilot App Name (eg serverpilot/apps/myapp)

DOMAIN_NAME = Domain name (eg myawesomesite.com)

NOTE: The filename should match the serverpilot app name. Another way to know is to see what ever letsencrypt named what the files here /etc/nginx-sp/vhosts.d/APP_NAME.conf

Ken Update 01/02/2019: แก้ nginx[3429]: nginx: [warn] the "ssl" directive is deprecated, use the "listen ... ssl" โดย comment ที่ # ssl on;

server {
  listen 443 ssl http2;
  listen [::]:443 ssl http2;
  server_name DOMAIN_NAME.com www.DOMAIN_NAME.com;

  # ssl on;

  # LetsEncrypt Certs
  ssl_certificate /etc/letsencrypt/live/DOMAIN_NAME.com/fullchain.pem;
  ssl_certificate_key /etc/letsencrypt/live/DOMAIN_NAME.com/privkey.pem;

  # Verify Chain of Trust of OCSP Repsonse
  # http://nginx.org/en/docs/http/ngx_http_ssl_module.html
  ssl_trusted_certificate /etc/letsencrypt/live/DOMAIN_NAME.com/chain.pem;

  # Secure SSL protocols and ciphers
  ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  ssl_prefer_server_ciphers on;
  ssl_ciphers 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:AES:CAMELLIA:DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:!EDH-DSS-DES-CBC3-SHA:!EDH-RSA-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA';
  ssl_session_timeout 1d;
  ssl_session_cache shared:SSL:50m;

  # OCSP Stapling
  ssl_stapling on;
  ssl_stapling_verify on;
  
  # Strict Transport Security
  # https://www.chromium.org/hsts
  add_header Strict-Transport-Security max-age=15768000;

  root   /srv/users/serverpilot/apps/APP_NAME/public;

  access_log  /srv/users/serverpilot/log/APP_NAME/APP_NAME_nginx.access.log  main;
  error_log  /srv/users/serverpilot/log/APP_NAME/APP_NAME_nginx.error.log;

   # Proxy Set
  proxy_set_header Host $host;
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_set_header X-Forwarded-SSL on;
  proxy_set_header X-Forwarded-Proto $scheme;
  
   # Includes
  include /etc/nginx-sp/vhosts.d/APP_NAME.d/*.nonssl_conf;
  include /etc/nginx-sp/vhosts.d/APP_NAME.d/*.conf;
}

6.Start NGINX

service nginx-sp start

Auto Renewing

7.Add crontab (ถ้าเคยทำแล้วข้ามขั้นนี้)

crontab -e
# letsencrypt-auto renew command every Monday at 2:30 am
30 2 * * 1 certbot renew --pre-hook "service nginx-sp stop" --post-hook "service nginx-sp start" >> /var/log/letsencrypt-renew.log

8.Checking if a cert needs to be renewed

certbot renew

9.Adding a new subdomain to certs (ถ้าไม่ add เพิ่มข้ามขั้นนี้)

certbot certonly --webroot -w /srv/users/serverpilot/apps/APP_NAME/public -d www.DOMAIN_NAME.com -d DOMAIN_NAME.com -d sub.domain.com -d sub2.domain.com

(ใช้โค๊ดนี้แทนที่ fork มา เพราะ cloudflare ทำให้ auto renew ไม่ได้) ref: https://certbot.eff.org/docs/using.html#webroot

ETC.

ถ้าเจอ Problem binding to port 80: Could not bind to IPv4 or IPv6.

ลองรัน

fuser 80/tcp

ถ้ารัน service nginx-sp start แล้วเจอ

Job for nginx-sp.service failed because the control process exited with error code. See "systemctl status nginx-sp.service" and "journalctl -xe" for details. ให้เช็คชื่อที่เปลี่ยนต่างๆด้านบนน่าจะผิด แก้ให้ถูกก็ใช้งานได้

Redirect a WordPress site from WWW to non-WWW

Go to serverpilot/apps/myapp/public/.htaccess and add this below

# BEGIN Redirect www to non-www
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.DOMAIN_NAME\.com [NC]
RewriteRule ^(.*)$ https://DOMAIN_NAME.com/$1 [L,R=301]
# END Redirect www to non-www
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment