Installazione del Certificato HTTPS Gratuito Nginx su Ubuntu Linux

This tutorial will show you all the steps required to configure Nginx Server Blocks on Ubuntu Linux.

Nginx Server Blocks is very much the same as Apache Virtual host feature.

This tutorial was tested on Ubuntu 18.04.

1. Installa Nginx su Ubuntu Linux

Utilizzare il comando APT di Ubuntu per installare il server Nginx.

# apt-get update
# apt-get install nginx

Riavviare manualmente il server Web Nginx.

# service nginx restart
# service nginx status

Verifica lo stato del servizio Nginx.

● nginx.service - A high performance web server and a reverse proxy server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
Active: active (running) since Sat 2018-12-29 04:29:22 UTC; 1h 17min ago
Docs: man:nginx(8)
Process: 2233 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status
Process: 2221 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exite
Main PID: 2238 (nginx)
Tasks: 2 (limit: 1152)
CGroup: /system.slice/nginx.service
├─2238 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
└─2239 nginx: worker process

Hai finito l'installazione del server Web Nginx su Ubuntu Linux.

2. Aggiungi il Supporto PHP a Nginx

Nginx ha bisogno di un programma esterno per aggiungere il supporto PHP.

Utilizzare il comando APT di Ubuntu per installare i pacchetti richiesti PHP.

# apt-get update
# apt-get install php7.2-fpm

Opzionale. Utilizzare il seguente comando per installare i moduli PHP più utilizzati.

# apt-get install php7.2-xml php7.2-curl php7.2-gd php7.2-mbstring php7.2-mysql
# apt-get install php7.2-bz2 php7.2-zip php7.2-json php7.2-readline

Trova la posizione del file di configurazione PHP sul tuo sistema.

Modifica il file di configurazione php.ini.

# updatedb
# locate php.ini
# vi /etc/php/7.2/fpm/php.ini

La tua versione di PHP potrebbe non essere uguale alla nostra.

La posizione del file di configurazione PHP potrebbe non essere uguale alla nostra.

Ecco il file con la nostra configurazione.

file_uploads = On
max_execution_time = 300
memory_limit = 256M
post_max_size = 32M
max_input_time = 60
max_input_vars = 4440
upload_max_filesize = 32M

Modifica il file di configurazione del sito Web predefinito di Nginx.

# vi /etc/nginx/sites-available/default

Ecco il file originale, prima della nostra configurazione.

server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
try_files $uri $uri/ =404;
}
}

Ecco il nuovo file con la nostra configurazione.

server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.php index.html index.htm;
server_name _;
location / {
try_files $uri $uri/ =404;
}
location ~ .php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}
}

Verifica se il tuo file di configurazione Nginx non ha errori.

# nginx -t

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Riavvia il servizio PHP.
Riavvia il servizio Nginx.

# service php7.2-fpm restart
# service nginx restart

Hai completato l'integrazione di Nginx con PHP su Ubuntu Linux.

3. Configurare i Blocchi del Server Nginx

I blocchi server sono una funzionalità che consente a un server Nginx di offrire più siti Web utilizzando lo stesso indirizzo IP.

Consente di creare l'infrastruttura necessaria per utilizzare la funzionalità dei blocchi Nginx Server.

# mkdir /websites/mining-pool -p
# cd /websites/mining-pool
# mkdir www
# chown www-data.www-data /websites -R

Il nostro sito Web sarà denominato mining-pool.ninja.

I file del sito Web mining-pool.ninja devono trovarsi all'interno della directory / websites / mining-pool / www.

Alert!

Mining-pool.ninja was used as an example!

You need to change your configuration files to reflect your website name.

Crea un file di configurazione Nginx Virtualhost sul tuo sito web.

# vi /etc/nginx/sites-available/mining-pool.conf

Ecco il file con la nostra configurazione.

server {
listen 80;
listen [::]:80;
root /websites/mining-pool/www;
index index.php index.html index.htm;
server_name mining-pool.ninja;
location / {
try_files $uri $uri/ =404;
}
location ~ .php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}
}

Creare un collegamento simbolico per abilitare la configurazione dell'host virtuale Nginx.

Riavvia il servizio Nginx.

# ln -s /etc/nginx/sites-available/mining-pool.conf /etc/nginx/sites-enabled/
# service nginx restart

Hai finito la configurazione del blocco Nginx Server.

4. Configura il Dominio DNS

Accedi a GODADDY e acquistare un dominio DNS.

Nel nostro esempio, acquistiamo il dominio denominato MINING-POOL.NINJA.

Puoi utilizzare qualsiasi sito web per acquistare un dominio DNS, GoDaddy è solo una mia scelta personale.

Crea una voce DNS che punta il tuo sito web al computer su cui gira Nginx.

Nel nostro esempio, abbiamo creato una voce DNS che indirizza il dominio MINING-POOL.NINJA all'indirizzo IP 35.163.100.49.

Godaddy Apache DNS Configuration

Utilizzare il comando NSLOOKUP per verificare la configurazione DNS

# apt-get update
# apt-get install dnsutils
# nslookup mining-pool.ninja

Non-authoritative answer:
Name: mining-pool.ninja
Address: 35.163.100.49

Hai finito la configurazione del dominio DNS.

Per testare la nostra configurazione, creiamo una pagina di test PHP di base.

# vi /websites/mining-pool/www/index.php

Ecco il contenuto del file index.php.

<?php phpinfo(); ?>

Apri il browser e prova ad accedere alla versione HTTP del tuo sito web.

Nel nostro esempio, il seguente URL è stato inserito nel browser:

• http://mining-pool.ninja

Dovrebbe essere presentata la pagina di informazioni PHP.

PHPInfo Nginx

5. Configura il Certificato HTTPS Gratuito su Nginx

Installa i pacchetti richiesti per utilizzare i certificati SSL / TLS gratuiti di LET'S ENCRYPT su Ubuntu Linux

# apt-get install software-properties-common
# add-apt-repository universe
# add-apt-repository ppa:certbot/certbot
# apt-get update
# apt-get install python-certbot-nginx

Richiedi e installa il certificato HTTPS gratuito Nginx.

certbot --nginx -d mining-pool.ninja

• Premere (A) per accettare i Termini di servizio.
• Premere (Y) per condividere l'e-mail e ricevere le newsletter.
• Premere (2) per reindirizzare automaticamente il sito Web HTTP alle versioni HTTPS.

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator nginx, Installer nginx
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel): techexpert.tips@gmail.com

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must
agree in order to register with the ACME server at
https://acme-v02.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 our work
encrypting the web, EFF news, campaigns, and ways to support digital freedom.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: Y
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for mining-pool.ninja
Waiting for verification...
Cleaning up challenges
Deploying Certificate to VirtualHost /etc/nginx/sites-enabled/mining-pool.conf

Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: No redirect - Make no further changes to the webserver configuration.
2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for
new sites, or if you're confident your site works on HTTPS. You can undo this
change by editing your web server's configuration.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 2
Redirecting all traffic on port 80 to ssl in /etc/nginx/sites-enabled/mining-pool.conf

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Congratulations! You have successfully enabled https://mining-pool.ninja

You should test your configuration at:
https://www.ssllabs.com/ssltest/analyze.html?d=mining-pool.ninja
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at:
/etc/letsencrypt/live/mining-pool.ninja/fullchain.pem
Your key file has been saved at:
/etc/letsencrypt/live/mining-pool.ninja/privkey.pem
Your cert will expire on 2019-03-31. To obtain a new or tweaked
version of this certificate in the future, simply run certbot again
with the "certonly" option. To non-interactively renew *all* of
your certificates, run "certbot renew"
- Your account credentials have been saved in your Certbot
configuration directory at /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

Il sistema richiederà automaticamente il certificato gratuito.

Configura inoltre il tuo server Web Nginx per reindirizzare tutto l'accesso HTTP alla versione HTTPS del tuo sito web.

Nel nostro esempio, il sistema ha modificato il file di configurazione di Virtualhost denominato mining-pool.conf.

Ecco il contenuto del file modificato mining-pool.conf.

server {
root /websites/mining-pool/www;
index index.php index.html index.htm;
server_name mining-pool.ninja;
location / {
try_files $uri $uri/ =404;
}
location ~ .php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}
listen [::]:443 ssl ipv6only=on; # managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/mining-pool.ninja/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/mining-pool.ninja/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}

server {
if ($host = mining-pool.ninja) {
return 301 https://$host$request_uri;
} # managed by Certbot

listen 80;
listen [::]:80;
server_name mining-pool.ninja;
return 404; # managed by Certbot
}

Il file KEY contiene la chiave privata del certificato e deve essere conservato in un luogo sicuro per tutto il tempo.

Il file chiave di mining-pool.ninja è stato memorizzato in /etc/letsencrypt/live/mining-pool.ninja/privkey.pem.

6. Prova il Certificato HTTPS Gratuito su Nginx

Tutta la configurazione richiesta è fatta

È tempo di testare la tua configurazione.

Apri il browser e prova ad accedere alla versione HTTP del tuo sito web.

Nel nostro esempio, il seguente URL è stato inserito nel browser:

• http://mining-pool.ninja

Nginx reindirizzerà automaticamente la richiesta HTTP alla versione HTTPS del tuo sito web.

Lets Encrypt Nginx HTTPS

Hai terminato la configurazione del certificato gratuito di Nginx HTTPS.

7. Come Rinnovare il Certificato HTTPS Gratuito

I certificati SSL / TLS gratuiti di LET'S ENCRYPT sono validi solo per 90 giorni.

Il sistema crea un'attività programmata per rinnovare automaticamente qualsiasi certificato entro trenta giorni dalla scadenza.

Il nome dell'attività pianificata è certbot e si trova all'interno della directory /etc/cron.d.

Ecco il contenuto del file /etc/cron.d/certbot:

# /etc/cron.d/certbot: crontab entries for the certbot package
#
# Upstream recommends attempting renewal twice a day
#
# Eventually, this will be an opportunity to validate certificates
# haven't been revoked, etc. Renewal will only occur if expiration
# is within 30 days.
#
# Important Note! This cronjob will NOT be executed if you are
# running systemd as your init system. If you are running systemd,
# the cronjob.timer function takes precedence over this cronjob. For
# more details, see the systemd.timer manpage, or use systemctl show
# certbot.timer.
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

0 */12 * * * root test -x /usr/bin/certbot -a ! -d /run/systemd/system && perl -e 'sleep int(rand(43200))' && certbot -q renew

Utilizzare il seguente comando per simulare il processo di rinnovo del certificato.

# certbot renew --dry-run

Dovresti vedere i seguenti messaggi:

Saving debug log to /var/log/letsencrypt/letsencrypt.log

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Processing /etc/letsencrypt/renewal/mining-pool.ninja.conf
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Cert not due for renewal, but simulating renewal for dry run
Plugins selected: Authenticator nginx, Installer nginx
Renewing an existing certificate
Performing the following challenges:
http-01 challenge for mining-pool.ninja
Waiting for verification...
Cleaning up challenges

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
new certificate deployed with reload of nginx server; fullchain is
/etc/letsencrypt/live/mining-pool.ninja/fullchain.pem
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
** DRY RUN: simulating 'certbot renew' close to cert expiry
** (The test certificates below have not been saved.)

Congratulations, all renewals succeeded. The following certs have been renewed:
/etc/letsencrypt/live/mining-pool.ninja/fullchain.pem (success)
** DRY RUN: simulating 'certbot renew' close to cert expiry
** (The test certificates above have not been saved.)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -