在Ubuntu Linux上安装Nginx免费HTTPS证书

本教程将向您展示在Ubuntu Linux上配置Nginx服务器块所需的所有步骤。

Nginx服务器块与Apache虚拟主机功能非常相似。

本教程在Ubuntu 18.04上进行了测试。

1.在Ubuntu Linux上安装Nginx

使用Ubuntu APT命令安装Nginx服务器。

# apt-get update
# apt-get install nginx

手动重新启动Nginx Web服务器。

# service nginx restart
# service nginx status

验证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

您在Ubuntu Linux上完成了Nginx Web服务器安装。

2.向Nginx添加PHP支持

Nginx需要一个外部程序来添加PHP支持。

使用Ubuntu APT命令安装PHP所需的包。

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

可选的。 使用以下命令安装最常用的PHP模块。

# 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

在系统上查找PHP配置文件的位置。

编辑php.ini配置文件。

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

您的PHP版本可能与我们的版本不同。

您的PHP配置文件位置可能与我们的不同。

这是我们配置的文件。

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默认网站配置文件。

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

在配置之前,这是原始文件。

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

这是我们配置的新文件。

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

验证您的Nginx配置文件是否没有错误。

# nginx -t

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

重启PHP服务。
重启Nginx服务。

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

你在Ubuntu Linux上完成了与Nginx的PHP集成。

3.配置Nginx服务器块

服务器块是一项功能,允许一个Nginx服务器使用相同的IP地址提供多个网站。

让我们创建必要的基础设施来使用Nginx Server块功能。

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

我们的网站名为mining-pool.ninja。

mining-pool.ninja网站文件应位于/ 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.

为您的网站创建Nginx Virtualhost配置文件。

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

这是我们配置的文件。

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

创建符号链接以启用Nginx虚拟主机配置。

重启Nginx服务。

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

您完成了Nginx Server块配置。

4.配置DNS域

访问 GODADDY 并购买DNS域名。

在我们的示例中,我们购买名为MINING-POOL.NINJA的域。

您可以使用任何网站购买DNS域名,GoDaddy只是我个人的选择。

创建一个DNS条目,将您的网站指向运行Nginx的计算机。

在我们的示例中,我们创建了一个DNS域,将域MINING-POOL.NINJA指向IP地址35.163.100.49。

Godaddy Apache DNS Configuration

使用NSLOOKUP命令测试DNS配置

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

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

您完成了DNS域配置。

要测试我们的配置,让我们创建一个基本的PHP测试页面。

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

这是index.php文件的内容。

<?php phpinfo(); ?>

打开浏览器并尝试访问您网站的HTTP版本。

在我们的示例中,在浏览器中输入了以下URL:

•http://mining-pool.ninja

应该显示PHP信息页面。

PHPInfo Nginx

5.在Nginx上配置免费HTTPS证书

安装所需的软件包以在Ubuntu Linux上使用LET'S ENCRYPT免费SSL / TLS证书

# 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

请求并安装Nginx免费HTTPS证书。

certbot --nginx -d mining-pool.ninja

•按(A)同意服务条款。
•按(Y)分享您的电子邮件并接收简报。
•按(2)自动将HTTP网站重定向到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

系统将自动请求免费证书。

它还将配置您的Nginx Web服务器,以将所有HTTP访问重定向到您的网站的HTTPS版本。

在我们的示例中,系统修改了名为mining-pool.conf的Virtualhost配置文件。

以下是修改后的文件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
}

KEY文件包含您的证书私钥,必须始终保存在安全的地方。

mining-pool.ninja的密钥文件存储在/etc/letsencrypt/live/mining-pool.ninja/privkey.pem中。

6.在Nginx上测试免费HTTPS证书

完成所有配置。

是时候测试您的配置了。

打开浏览器并尝试访问您网站的HTTP版本。

在我们的示例中,在浏览器中输入了以下URL:

•http://mining-pool.ninja

Nginx会自动将HTTP请求重定向到您网站的HTTPS版本。

Lets Encrypt Nginx HTTPS

您完成了Nginx HTTPS免费证书配置。

7.如何续订免费HTTPS证书

LET'S ENCRYPT免费SSL / TLS证书仅在90天内有效。

系统创建计划任务,以在到期后的30天内自动续订任何证书。

计划的任务名称是certbot,它位于目录/etc/cron.d中。

这是/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

使用以下命令模拟证书续订过程。

# certbot renew --dry-run

您应该看到以下消息:

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