Installeren van Nginx gratis HTTPS-certificaat op Ubuntu Linux

Deze tutorial zal je alle stappen tonen die nodig zijn om Nginx Server Blocks op Ubuntu Linux te configureren.

Nginx Server Blocks is vrijwel hetzelfde als de Apache Virtual Host-functie.

Deze tutorial is getest op Ubuntu 18.04.

1. Installeer Nginx op Ubuntu Linux

Gebruik de opdracht Ubuntu APT om de Nginx-server te installeren.

# apt-get update
# apt-get install nginx

Start de Nginx-webserver handmatig opnieuw.

# service nginx restart
# service nginx status

Controleer de Nginx-servicestatus.

● 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

Je hebt de installatie van de Nginx-webserver op Ubuntu Linux voltooid.

2. Voeg PHP-ondersteuning toe aan Nginx

Nginx heeft een extern programma nodig om PHP-ondersteuning toe te voegen.

Gebruik de opdracht Ubuntu APT om de vereiste PHP-pakketten te installeren.

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

Optioneel. Gebruik de volgende opdracht om de meest gebruikte PHP-modules te installeren.

# 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

Zoek de locatie van het PHP-configuratiebestand op uw systeem.

Bewerk het php.ini-configuratiebestand.

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

Uw PHP-versie is misschien niet dezelfde als die van ons.

Uw PHP-configuratiebestandslocatie is mogelijk niet dezelfde als die van ons.

Hier is het bestand met onze configuratie.

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

Nginx standaard websiteconfiguratiebestand.

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

Hier is het originele bestand, vóór onze configuratie.

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;
}
}

Hier is het nieuwe bestand met onze configuratie.

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;
}
}

Controleer of uw Nginx-configuratiebestand geen fouten bevat.

# nginx -t

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

Start de PHP-service opnieuw.
Start de Nginx-service opnieuw.

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

Je hebt de Nginx-integratie met PHP op Ubuntu Linux voltooid.

3. Configureer Nginx Server Blocks

Serverblokken is een functie waarmee één Nginx-server meerdere websites kan aanbieden met hetzelfde IP-adres.

Hiermee wordt de benodigde infrastructuur gemaakt om de Nginx Server-blokkeringsfunctie te gebruiken.

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

Onze website zal mijnpool.ninja heten.

De mining-pool.ninja-websitebestanden moeten zich in de directory / websites / mining-pool / www bevinden.

Alert!

Mining-pool.ninja was used as an example!

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

Maak een Nginx Virtualhost-configuratiebestand aan op uw website.

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

Hier is het bestand met onze configuratie.

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;
}
}

Maak een symbolische koppeling om de virtuele hostconfiguratie van Nginx in te schakelen.

Start de Nginx-service opnieuw.

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

U hebt de blokconfiguratie van Nginx Server voltooid.

4. Configureer het DNS-domein

Toegang krijgen tot GODADDY en koop een DNS-domein.

In ons voorbeeld kopen we het domein met de naam MININGPOOL.NINJA.

U kunt elke website gebruiken om een DNS-domein aan te schaffen, GoDaddy is gewoon mijn persoonlijke keuze.

Maak een DNS-invoer die uw website naar de computer met Nginx wijst.

In ons voorbeeld hebben we een DNS-invoer gemaakt die het domein MININGPOOL.NINJA naar het IP-adres 35.163.100.49 leidt.

Godaddy Apache DNS Configuration

Gebruik de opdracht NSLOOKUP om uw DNS-configuratie te testen

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

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

U hebt de DNS-domeinconfiguratie voltooid.

Om onze configuratie te testen, maken we een eenvoudige PHP-testpagina.

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

Hier is de inhoud van het index.php-bestand.

<?php phpinfo(); ?>

Open uw browser en probeer toegang te krijgen tot de HTTP-versie van uw website.

In ons voorbeeld is de volgende URL in de browser ingevoerd:

• http://mining-pool.ninja

De PHP-informatiepagina moet worden gepresenteerd.

PHPInfo Nginx

5. Configureer het gratis HTTPS-certificaat op Nginx

Installeer de vereiste pakketten om de LET'S ENCRYPT gratis SSL / TLS-certificaten op Ubuntu Linux te gebruiken

# 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

Vraag en installeer het gratis HTTPS-certificaat van Nginx.

certbot --nginx -d mining-pool.ninja

• Druk op (A) om akkoord te gaan met de servicevoorwaarden.
• Druk op (Y) om uw e-mail te delen en nieuwsbrieven te ontvangen.
• Druk op (2) om uw HTTP-website automatisch om te leiden naar de HTTPS-versies.

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

Het systeem vraagt automatisch het gratis certificaat aan.

Het zal ook uw Nginx-webserver configureren om alle HTTP-toegang naar de HTTPS-versie van uw website om te leiden.

In ons voorbeeld heeft het systeem het configuratiebestand Virtualhost met de naam mining-pool.conf aangepast.

Hier is de inhoud van het gewijzigde bestand 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
}

Het KEY-bestand bevat uw privésleutel voor certificaten en moet altijd op een veilige plaats worden bewaard.

Het sleutelbestand voor mining-pool.ninja is opgeslagen op /etc/letsencrypt/live/mining-pool.ninja/privkey.pem.

6. Test het gratis HTTPS-certificaat op Nginx

Alle benodigde configuratie is voltooid.

Het is tijd om uw configuratie te testen.

Open uw browser en probeer toegang te krijgen tot de HTTP-versie van uw website.

In ons voorbeeld is de volgende URL in de browser ingevoerd:

• http://mining-pool.ninja

Nginx zal het HTTP-verzoek automatisch doorsturen naar de HTTPS-versie van uw website.

Lets Encrypt Nginx HTTPS

U hebt de Nginx HTTPS-vrije certificaatconfiguratie voltooid.

7. Hoe het gratis HTTPS-certificaat te verlengen

De LET'S ENCRYPT Free SSL / TLS-certificaten zijn slechts 90 dagen geldig.

Het systeem maakt een geplande taak om elk certificaat automatisch te vernieuwen binnen dertig dagen na afloop.

De geplande taaknaam is certbot en deze bevindt zich in de directory /etc/cron.d.

Hier is de inhoud van het bestand /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

Gebruik de volgende opdracht om het proces van certificaatvernieuwing te simuleren.

# certbot renew --dry-run

Je zou de volgende berichten moeten zien:

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.)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -