우분투 리눅스에 Nginx 무료 HTTPS 인증서 설치하기

이 튜토리얼은 Ubuntu Linux에서 Nginx Server Block을 설정하는 데 필요한 모든 단계를 보여줍니다.

Nginx Server Blocks는 Apache 가상 호스트 기능과 거의 같습니다.

이 튜토리얼은 Ubuntu 18.04에서 테스트되었습니다.

1. Ubuntu Linux에 Nginx를 설치하십시오.

Ubuntu APT 명령을 사용하여 Nginx 서버를 설치하십시오.

# apt-get update
# apt-get install nginx

Nginx 웹 서버를 수동으로 다시 시작하십시오.

# 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

우분투 리눅스에서 Nginx 웹 서버 설치를 마쳤습니다.

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

우분투 리눅스에서 PHP와의 Nginx 통합을 마쳤습니다.

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는 제 개인적인 선택입니다.

Nginx를 실행하는 컴퓨터에 웹 사이트를 가리키는 DNS 항목을 만듭니다.

이 예에서는 도메인 MINING-POOL.NINJA를 IP 주소 35.163.100.49로 가리키는 DNS 항목을 만들었습니다.

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

시스템에서 자동으로 무료 인증서를 요청합니다.

또한 모든 HTTP 액세스를 웹 사이트의 HTTPS 버전으로 리디렉션하도록 Nginx 웹 서버를 구성합니다.

이 예에서 시스템은 mining-pool.conf라는 Virtualhost 구성 파일을 수정했습니다.

다음은 수정 된 파일 마이닝 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.)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -