Giunsa ang Pag-secure sa Nginx sa SSL ug Atong I-encrypt sa FreeBSD


Niini nga giya atong hisgutan kung giunsa ang pag-secure sa Nginx web server sa FreeBSD nga adunay mga sertipiko sa TLS/SSL nga gitanyag sa Let's Encrypt Certificate Authority. Ipakita usab namo kanimo kung giunsa ang awtomatikong pag-renew sa Lets' Encrypt nga mga sertipiko sa dili pa ang expiring date.

Ang TLS, usa ka acronym sa Transport Layer Security, usa ka protocol nga gipadagan ubos sa HTTP protocol ug naggamit sa mga sertipiko ug mga yawe aron ma-encapsulate ang mga packet ug ma-encrypt ang datos nga gibaylo tali sa usa ka server ug usa ka kliyente, o sa kini nga kaso tali sa Nginx web server ug sa kliyente. browser, aron masiguro ang koneksyon, aron ang ikatulo nga partido, nga mahimong makapugong sa trapiko, dili maka-decrypt sa transmission.

Ang proseso sa pagkuha sa usa ka libre nga Let's Encrypt nga sertipiko sa FreeBSD mahimong mapasayon pag-ayo pinaagi sa pag-install sa certboot client utility, nga mao ang opisyal nga Let's Encrypt nga kliyente nga gigamit alang sa paghimo ug pag-download sa mga sertipiko.

  1. I-install ang FBEMP (Nginx, MariaDB ug PHP) stack sa FreeBSD

Lakang 1: I-configure ang Nginx TLS/SSL

1. Sa kasagaran, ang TLS/SSL server configuration wala ma-enable sa FreeBSD tungod kay ang TLS server block nga mga pahayag gikomento sa Nginx default configuration file.

Aron ma-activate ang TLS server sa Nginx, ablihi ang nginx.conf configuration file, pangitaa ang linya nga naghubit sa pagsugod sa SSL server ug i-update ang tibuok block aron mahisama sa ubos nga sample.

# nano /usr/local/etc/nginx/nginx.conf

Nginx HTTPS block kinutlo:

server {
       listen 443 ssl  default_server;
       server_name  www.yourdomain.com;
	
	access_log /var/log/nginx/access.log;
	error_log /var/log/nginx/error.log;
	error_page   500 502 503 504  /50x.html;
        
	location = /50x.html {
            root   /usr/local/www/nginx-dist;
        }
	
	location / {
	    root   /usr/local/www/nginx;
       	    index  index.html index.htm;
	    try_files $uri $uri/ /index.php?$args;
				}

	ssl_certificate "/usr/local/etc/letsencrypt/live/www.yourdomain.com/cert.pem";
	ssl_certificate_key "/usr/local/etc/letsencrypt/live/www.yourdomain.com/privkey.pem";
	ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
	ssl_dhparam /usr/local/etc/nginx/dhparam.pem;
	ssl_session_cache shared:SSL:1m;
	ssl_session_timeout 10m;
	ssl_ciphers HIGH:!aNULL:!MD5;
	ssl_prefer_server_ciphers  on;

	# Use gzip compression
gzip on;
gzip_disable "msie6";
gzip_vary on;
gzip_proxied any;
gzip_comp_level 5;
gzip_buffers 16 8k;
gzip_http_version 1.0;

	# Set a variable to work around the lack of nested conditionals
	
	set $cache_uri $request_uri;
	
	location ~ /.well-known {
	allow all;
		}
    


        location ~ \.php$ {
        root	/usr/local/www/nginx;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        #fastcgi_param  SCRIPT_FILENAME /scripts$fastcgi_script_name;
        fastcgi_param SCRIPT_FILENAME $request_filename;    
		include        fastcgi_params;
        }	
    }

Ang naa sa ibabaw nga block, dugang sa SSL block, naglakip usab sa pipila ka mga pahayag alang sa pagpagana sa gzip compression ug FastCGI Process Manager, nga gigamit sa pagpasa sa PHP code ngadto sa PHP-FPM gateway aron sa pagpadagan sa dinamikong mga aplikasyon sa web.

Human nimo idugang ang code sa ibabaw sa Nginx main configuration file, ayaw i-restart ang daemon o i-apply ang mga setting sa dili pa i-install ug makuha ang Let's Encrypt certificate para sa imong domain.

Lakang 2: I-install ang Certbot Client sa FreeBSD

2. Ang proseso sa pag-instalar sa Let's Encrypt certbot client utility sa FreeBSD naglakip sa pag-download sa source code para sa py-certbot ug locally compile niini, pinaagi sa pag-isyu sa ubos nga mga command.

# cd /usr/ports/security/py-certbot
# make install clean

3. Ang pag-compile sa py-certbot utility nagkinahanglan og daghang panahon itandi sa pag-instalar sa usa ka regular nga binary nga pakete. Niining panahona, usa ka serye sa mga dependency ang gikinahanglan nga ma-download sa gitipon nga lokal sa FreeBSD.

Usab, usa ka serye sa mga pag-aghat ang makita sa imong screen, nga naghangyo kanimo sa pagpili kung unsang mga pakete ang gamiton sa oras sa pag-compile alang sa matag dependency. Sa unang screen, pilia ang mosunod nga mga himan, pinaagi sa pagpindot sa [space] key, para sa pag-compile sa python27 dependency, sama sa gihulagway sa ubos nga hulagway.

  • IPV6
  • LIBFFI
  • NLS
  • PYMALLOC
  • MGA THREADS
  • UCS4 para sa suporta sa Unicode

4. Sunod, pilia ang DOCS ug THREADS para sa dependency sa gettext-tools ug pindota ang OK aron magpadayon sama sa gipakita sa ubos nga hulagway.

5. Sa sunod nga screen ibilin ang TESTS option nga disabled para sa libffi-3.2.1 ug pindota ang OK aron mopadayon pa.

6. Sunod, i-hit ang space aron mapili ang DOCS alang sa py27-enum34 dependency, nga mag-install sa dokumentasyon alang niini nga himan, ug pindota ang OK aron magpadayon, ingon sa gihulagway sa screenshot sa ubos.

7. Sa katapusan, pilia ang pag-instalar sa mga sample sample para sa py27-openssl dependency pinaagi sa pagpindot sa [space] key ug pag-hit OK aron mahuman ang proseso sa pag-compile ug pag-install para sa py-certbot client.

8. Human mahuman ang proseso sa pag-compile ug pag-instalar sa py-certbot utility, padagana ang ubos nga sugo aron ma-upgrade ang himan sa pinakabag-o nga bersyon sa package sama sa gihulagway sa ubos nga mga screenshot.

# pkg install py27-certbot

9. Aron malikayan ang pipila ka mga isyu nga mahimong mahitabo samtang nagkuha sa usa ka libre nga Let's Encrypt nga sertipiko, ang labing kasagaran nga sayup mao ang \pkg_resources.DistributionNotFound, siguroha nga ang mosunod nga duha ka dependency anaa usab sa imong sistema: py27-salt ug py27- acme.

# pkg install py27-salt
# pkg install py27-acme

Lakang 3: I-install ang Let's Encrypt Certificate para sa Nginx sa FreeBSD

10. Aron makakuha og Let's Encrypt standalone nga sertipiko para sa imong domain, padagana ang mosunod nga command ug ihatag kanimo ang domain name ug tanang subdomain nga gusto nimong makuhaan og mga sertipiko pinaagi sa pagpasabot sa -d flag.

# certbot certonly --standalone -d yourdomain.com -d www.yourdomain.com

11. Samtang nagmugna sa sertipiko hangyoon ka sa pagsulod sa imong email address ug pag-uyon sa Let's Encrypt nga mga termino sa serbisyo. I-type ang a gikan sa keyboard aron mouyon ug magpadayon ug pangutan-on ka usab kung gusto nimo ipaambit ang imong email address sa mga kauban sa Let's Encrypt.

Kung dili nimo gusto nga ipaambit ang imong email address, i-type lang ang no nga pulong sa prompt ug pindota ang [enter] key aron magpadayon. Human nga malampuson nga nakuha ang mga sertipiko alang sa imong domain, makadawat ka ug pipila ka importanteng mga nota nga magpahibalo kanimo kung asa gitipigan ang mga sertipiko sa imong sistema ug kung kanus-a kini matapos.

12. Kung gusto nimo makakuha og Let's Encrypt certificate gamit ang webroot plugin pinaagi sa pagdugang sa webroot directory sa Nginx server para sa imong domain, i-issue ang mosunod nga command gamit ang --webroot ug -w mga bandera. Sa kasagaran, kung wala nimo giusab ang Nginx webroot nga agianan, kini kinahanglan nga nahimutang sa /usr/local/www/nginx/ system path.

# certbot certonly --webroot -w /usr/local/www/nginx/ -d yourdomain.com -d www.yourdomain.com

Sama sa --strandalone nga pamaagi alang sa pagkuha og sertipiko, ang --webroot nga pamaagi mohangyo usab kanimo sa paghatag og email address alang sa pag-renew sa sertipiko ug mga pahibalo sa seguridad, aron ipadayon ang a sa pag-uyon sa Let's Encrypt terms and conditions ug no o yes sa o dili pagpaambit sa email address nga Let's Encrypt partners sama sa gihulagway sa ubos nga sample.

Hibal-i nga ang kliyente sa certbot makamatikod sa usa ka peke nga email address ug dili nimo tugutan nga magpadayon sa paghimo og usa ka sertipiko hangtod nga maghatag ka usa ka tinuud nga email address.

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel):[email   #A fake email address will be detected
There seem to be problems with that address. Enter email address (used for
urgent renewal and security notices)  If you really want to skip this, you can
run the client with --register-unsafely-without-email but make sure you then
backup your account key from /etc/letsencrypt/accounts   (Enter 'c' to cancel):[email 

-------------------------------------------------------------------------------
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.1.1-August-1-2016.pdf. You must agree
in order to register with the ACME server at
https://acme-v01.api.letsencrypt.org/directory
-------------------------------------------------------------------------------
(A)gree/(C)ancel: a ------------------------------------------------------------------------------- Would you be willing to share your email address with the Electronic Frontier Foundation, a founding partner of the Let's Encrypt project and the non-profit organization that develops Certbot? We'd like to send you email about EFF and our work to encrypt the web, protect its users and defend digital rights. ------------------------------------------------------------------------------- (Y)es/(N)o: n Obtaining a new certificate Performing the following challenges: http-01 challenge for www.domain.com Using the webroot path /usr/local/www/nginx/ for all unmatched domains. Waiting for verification... Cleaning up challenges IMPORTANT NOTES: - Congratulations! Your certificate and chain have been saved at /usr/local/etc/letsencrypt/live/www.yourdomain.com/fullchain.pem. Your cert will expire on 2017-12-28. To obtain a new or tweaked version of this certificate in the future, simply run certbot again. To non-interactively renew *all* of your certificates, run "certbot renew" - Your account credentials have been saved in your Certbot configuration directory at /usr/local/etc/letsencrypt. You should make a secure backup of this folder now. This configuration directory will also contain certificates and private keys obtained by Certbot so making regular backups of this folder is ideal. - If you like Certbot, please consider supporting our work by: Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate Donating to EFF: https://eff.org/donate-le

Lakang 4: I-update ang Nginx TLS Certificates

13. Ang lokasyon sa nakuha nga Let's Encrypt nga mga sertipiko ug mga yawe sa FreeBSD mao ang /usr/local/etc/letsencrypt/live/www.yourdomain.com/ system path. I-isyu ang ls nga sugo aron mapakita ang mga sangkap sa imong Let's Encrypt nga sertipiko: ang chain file, ang fullchain file, ang pribadong yawe ug ang certificate file, sama sa gihulagway sa mosunod nga pananglitan.

# ls /usr/local/etc/letsencrypt/live/www.yourdomain.com/

14. Aron ma-install ang Let's Encrypt nga mga sertipiko para sa imong domain sa Nginx web server, ablihi ang Nginx main configuration file o ang configuration file alang sa Nginx TLS server, kung kini lahi nga file, ug usba ang mga linya sa ubos aron ipakita ang agianan sa Let's Encrypt naghatag og mga sertipiko sama sa gihulagway sa ubos.

# nano /usr/local/etc/nginx/nginx.conf

I-update ang mga musunod nga linya aron makita kini nga sample:

ssl_certificate "/usr/local/etc/letsencrypt/live/www.yourdomain.com/cert.pem";
	ssl_certificate_key "/usr/local/etc/letsencrypt/live/www.yourdomain.com/privkey.pem";

15. Usab, kon ang linya nga ssl_dhparam anaa sa Nginx SSL configuration, kinahanglan ka nga makamugna og bag-ong 2048 bit Diffie–Hellman key uban sa mosunod nga sugo:

# openssl dhparam –out /usr/local/etc/nginx/dhparam.pem 2048 

16. Sa katapusan, aron ma-activate ang Nginx TLS configuration, susiha una ang Nginx global configuration alang sa posible nga syntax errors ug, unya, i-restart ang Nginx service aron magamit ang SSL configuration pinaagi sa pag-isyu sa mosunod nga mga command.

# nginx -t
# service nginx restart

17. Kumpirma kung ang Nginx daemon nagbugkos sa 443 port pinaagi sa pag-isyu sa mosunod nga mga sugo nga makalista sa tanang giablihan nga network sockets sa sistema sa listening state.

# netstat -an -p tcp| grep LISTEN
# sockstat -4 

18. Mahimo usab nimo nga bisitahan ang imong adres sa domain pinaagi sa HTTPS protocol pinaagi sa pag-abli sa usa ka browser ug i-type ang mosunod nga adres aron makumpirma nga ang Let's Encrypt nga mga sertipiko nagtrabaho sama sa gipaabut. Tungod kay naggamit ka og mga sertipiko nga gihimo sa usa ka balido nga Awtoridad sa Sertipiko, walay sayup nga kinahanglan ipakita sa browser.

https://www.yourdomain.com

19. Ang Openssl utility makatabang usab kanimo sa pagpangita og impormasyon mahitungod sa usa ka sertipiko nga nakuha gikan sa Let's Encrypt CA, pinaagi sa pagpadagan sa command sa mosunod nga mga opsyon.

# openssl s_client -connect www.yourdomain.com:443

Kung gusto nimo pugson ang Nginx nga idirekta ang tanan nga http sa https nga mga hangyo nga nadawat para sa imong domain sa port 80 sa HTTPS, ablihi ang Nginx configuration file, pangitaa ang direktiba sa server alang sa port 80 ug idugang ang ubos nga linya human sa server_name nga pahayag sama sa gihulagway sa ubos nga pananglitan .

rewrite ^(.*) https://www.yourdomain.com$1 permanent;

20. Ang pag-set up sa auto renewal alang sa sertipiko nga gi-isyu sa Let's Encrypt nga awtoridad sa dili pa kini ma-expire mahimo pinaagi sa pag-iskedyul sa usa ka cron job nga modagan kausa sa usa ka adlaw pinaagi sa pag-isyu sa mosunod nga sugo.

# crontab -e

Cron nga buluhaton sa pag-renew sa sertipiko.

0 0 * * * certbot renew >> /var/log/letsencrypt.log

Kana lang! Ang Nginx mahimo na nga mag-alagad sa mga secured nga web application sa imong mga bisita gamit ang Let's Encrypt nga libre nga mga sertipiko.