OS : Ubuntu 20.04

nginx : 

 

1. 우분투 모든 패키지 최신화하기

sudo apt update
sudo apt upgrade

 

2. 우분투 20.04에 최신 Nginx 설치

2-1. 기존에 Nginx가 설치 되어있다면, 먼저 삭제 합니다.

sudo systemctl stop nginx
sudo apt-get purge nginx -y && sudo apt autoremove nginx -y

2.2 최신 Nginx PPA를 추가합니다.

sudo add-apt-repository ppa:ondrej/nginx-mainline
sudo apt update

2.3 Nginx 설치

sudo apt install nginx-core nginx-common nginx nginx-full

 

3. 저장소에 Nginx 소스코드 추가

(Modsecurity를 컴파일 하기 위해서)

3-1. apt 구성 파일을 엽니다.

sudo nano /etc/apt/sources.list.d/ondrej-ubuntu-nginx-mainline-*.list

3-2. #주석처리 되어있는 라인 해제

# deb-src http://ppa.launchpad.net/ondrej/nginx-mainline/ubuntu/ focal main

** 아래와 같이 형태로 만들면 됩니다 **
deb http://ppa.launchpad.net/ondrej/nginx-mainline/ubuntu focal main
deb-src http://ppa.launchpad.net/ondrej/nginx-mainline/ubuntu focal main

3-3. apt update

sudo apt update

 

4. Nginx 소스 다운로드

- Modsecurity를 컴파일 하려면 Nginx 소스코드를 다운로드 해야 합니다.

 

4-1. 디렉토리 생성 및 구성

sudo mkdir /usr/local/src/nginx
cd /usr/local/src/nginx

#필요한 경우, 생성한 디렉터리에 권한을 할당합니다.
sudo chown username:username /usr/local/src -R

4-2. 종속성 설치 및 소스코드 다운로드

sudo apt install dpkg-dev
sudo apt source nginx


## 아래와 비슷한 에러메시지가 나타날 수 있지만, 무시해도 됩니다 ##
dpkg-source: info: extracting nginx in nginx-1.21.1
dpkg-source: info: unpacking nginx_1.21.1.orig.tar.gz
dpkg-source: info: unpacking nginx_1.21.1-1+ubuntu20.04.1+deb.sury.org+1.debian.tar.xz
dpkg-source: info: using patch list from debian/patches/series
dpkg-source: info: applying 0001-Make-sure-signature-stays-the-same-in-all-nginx-buil.patch
dpkg-source: info: applying 0002-define_gnu_source-on-other-glibc-based-platforms.patch
W: Download is performed unsandboxed as root as file 'nginx_1.21.1.orig.tar.gz' couldn't be accessed by user '_apt'. - pkgAcquire::Run (13: Permission denied)

4.3 다운로드 받은 Nginx 버전과 현재 설치되어있는 Nginx 버전이 같은지 확인합니다.

- 같은 버전으로 다운받아야 컴파일 할 수 있습니다.

ls

## 결과 예시 ##
onsemiro@ubuntu:/usr/local/src/nginx$ ls
nginx-1.21.1
nginx_1.21.1-1+ubuntu20.04.1+deb.sury.org+1.debian.tar.xz
nginx_1.21.1-1+ubuntu20.04.1+deb.sury.org+1.dsc
nginx_1.21.1.orig.tar.gz
nginx_1.21.1.orig.tar.gz.asc


nginx -v

## 결과 예시 ##
onsemiro@ubuntu:/usr/local/src/nginx$ nginx -v
nginx version: nginx/1.21.1

 

5. ModSecurity용 libmodsecurity3 설치

5.1 GIthub에서 ModSecurity 리포 복제

sudo apt install git

git clone --depth 1 -b v3/master --single-branch https://github.com/SpiderLabs/ModSecurity /usr/local/src/ModSecurity/
cd /usr/local/src/ModSecurity/

5-2. libmodsecurity3 종속성 설치

sudo apt install gcc make build-essential autoconf automake libtool libcurl4-openssl-dev liblua5.3-dev libfuzzy-dev ssdeep gettext pkg-config libpcre3 libpcre3-dev libxml2 libxml2-dev libcurl4 libgeoip-dev libyajl-dev doxygen -y

5-3. 하위 모듈 설치 및 업데이트

git submodule init
git submodule update

 

6. ModSecurity 환경 구축

6-1. build & configure

./build.sh
./configure

## 위 2개 명령 실행 시, 아래의 오류가 표시될 수 있지만 무시해도 됩니다 ##
fatal: No names found, cannot describe anything.

6-2. 코드 컴파일

make

sudo make install

 

7. ModSecurity-nginx 커넥터 설치

7-1 ModSecurity-nginx 리포 복제

sudo git clone --depth 1 https://github.com/SpiderLabs/ModSecurity-nginx.git /usr/local/src/ModSecurity-nginx/

7-2. ModSecurity-nginx 종속성 설치

sudo apt build-dep nginx
sudo apt install uuid-dev

7-3. 컴파일

sudo ./configure --with-compat --add-dynamic-module=/usr/local/src/ModSecurity-nginx
sudo make modules

7-4. 동적모듈 복사

sudo cp objs/ngx_http_modsecurity_module.so /usr/share/nginx/modules/

 

8. Nginx conf파일 구성 (ModSeucrity-nginx 커넥터 로드 및 구성)

8-1. nginx.conf 에서 ModSecurity 활성화

sudo nano /etc/nginx/nginx.conf

## 파일 상단에 추가 ##
load_module modules/ngx_http_modsecurity_module.so;

## http {} 섹션에 추가 ##
modsecurity on;
modsecurity_rules_file /etc/nginx/modsec/modsec-config.conf;

8-2. ModSecurity용 디렉토리 및 파일 생성 및 구성

sudo mkdir /etc/nginx/modsec/
sudo cp /usr/local/src/ModSecurity/modsecurity.conf-recommended /etc/nginx/modsec/modsecurity.conf
sudo nano /etc/nginx/modsec/modsecurity.conf

## line 7
SecRuleEngine DetectionOnly -> SecRuleEngine On

## line 224
# Log everything we know about a transaction.
SecAuditLogParts ABIJDEFHZCopied! -> SecAuditLogParts ABCEFHJKZCopied!

8-3. conf 파일 생성

sudo nano /etc/nginx/modsec/modsec-config.conf

8-4. 생성한 파일에, 다음 라인 추가

Include /etc/nginx/modsec/modsecurity.conf

8-5. ModSecurity 유니코드 매핑

sudo cp /usr/local/src/ModSecurity/unicode.mapping /etc/nginx/modsec/

8-6. nginx 테스트

sudo nginx -t

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

## nginx 재시작 ##
sudo systemctl restart nginx

 

9. ModSecurity용 OWASP 핵심 규칙 세트 설치

9-1.  OWASP CRS 3.3.2 다운로드

wget https://github.com/coreruleset/coreruleset/archive/refs/tags/v3.3.2.zip

9-2. 압축 풀기

## unzip 설치 ##
sudo apt install unzip

## 압축 풀기 ##
sudo unzip v3.3.2.zip -d /etc/nginx/modsec

9-3. Core Rule Set 복사

sudo cp /etc/nginx/modsec/coreruleset-3.3.2/crs-setup.conf.example /etc/nginx/modsec/coreruleset-3.3.2/crs-setup.conf

9-4. Core Rule Set 추가

sudo nano /etc/nginx/modsec/modsec-config.conf

## 아래 명령문 추가 ##
Include /etc/nginx/modsec/coreruleset-3.3.2/crs-setup.conf
Include /etc/nginx/modsec/coreruleset-3.3.2/rules/*.conf

## nginx 테스트 ##
sudo nginx -t

## nginx 재시작 ##
sudo systemctl restart nginx

 

 

참고: https://ko.linuxcapable.com/how-to-install-modsecurity-with-nginx-on-ubuntu-20-04/

sronsemiro.tistory.com/9

 

Ubuntu 18.04.4 LTS, Nginx에 Let's Encrypt 설치 및 HTTPS적용(SSL)

1. Certbot 설치 // add repo $ sudo add-apt-repository ppa:certbot/certbot // install certbot $ sudo apt install python-certbot-nginx 2. HTTPS(SSL)을 설정할, nginx 설정 체크 // check server_name $ su..

sronsemiro.tistory.com

기존 시스템을 유지하고 있었으므로,

Ubuntu 18.04.4 / nginx 로 운영중이다.

 

예전에 작성해두었던, 위 글을 참조해서 오랜만에 SSL을 적용하려고 아래 명령어를 입력했다.

certbot --nginx -d AAA.co.kr -d www.AAA.co.kr

 

그랬더니, 생각보다 오랜 시간이 걸린 후, 아래와 같은 에러 메시지가 나왔다.

Failed authorization procedure. AAA.co.kr (http-01): urn:ietf:params:acme:error:unauthorized :: The client lacks sufficient authorization :: Invalid response from http://AAA.co.kr/.well-known/acme-challenge/96Ot7ZF9cVMwcycFhodd96TXi97sFQ9OR7Ln2sPpwN4 [183.111.125.112]: "\n<!DOCTYPE html>\n<html class=\"html\" lang=\"ko-KR\" itemscope itemtype=\"http://schema.org/WebPage\">\n<head>\n\t<meta charset=\"UTF-8\">\n", www.AAA.co.kr (http-01): urn:ietf:params:acme:error:unauthorized :: The client lacks sufficient authorization :: Invalid response from http://AAA.co.kr/.well-known/acme-challenge/YTz6-bfcu9UWltNrFoqkYxrURdasHSTSePse7oH6zyI [183.111.125.112]: "<html>\r\n<head><title>504 Gateway Time-out</title></head>\r\n<body>\r\n<center><h1>504 Gateway Time-out</h1></center>\r\n<hr><center>ng"

IMPORTANT NOTES:
 - The following errors were reported by the server:

   Domain: AAA.co.kr
   Type:   unauthorized
   Detail: Invalid response from
   http://AAA.co.kr/.well-known/acme-challenge/96Ot7ZF9cVMwcycFhodd96TXi97sFQ9OR7Ln2sPpwN4
   [183.111.125.112]: "\n<!DOCTYPE html>\n<html class=\"html\"
   lang=\"ko-KR\" itemscope
   itemtype=\"http://schema.org/WebPage\">\n<head>\n\t<meta
   charset=\"UTF-8\">\n"

   Domain: www.AAA.co.kr
   Type:   unauthorized
   Detail: Invalid response from
   http://AAA.co.kr/.well-known/acme-challenge/YTz6-bfcu9UWltNrFoqkYxrURdasHSTSePse7oH6zyI
   [183.111.125.112]: "<html>\r\n<head><title>504 Gateway
   Time-out</title></head>\r\n<body>\r\n<center><h1>504 Gateway
   Time-out</h1></center>\r\n<hr><center>ng"

   To fix these errors, please make sure that your domain name was
   entered correctly and the DNS A/AAAA record(s) for that domain
   contain(s) the right IP address.

이전이랑 똑같이 했는데,.. 왜 안되지..?

 

이것저것 찾아보니, 발생할 수 있는 이유는 여러가지가 있었다.

1. 해당 주소가 https로 리다이렉트 되고 있는 경우.

2. 해당 URL의 .well-known/acme-challenge/ <=== 이 경로로 접근이 안되는경우

3. 위 에러코드에도 나오듯이 제대로 된 IP주소가 아니거나 DNS 설정이 잘못된 경우 

등등..

 

근데 나는 아무것도 해당되지 않았다.

그래서 어떻게 해야 할까 고민하다가.

 

인증서만 발급받고 나머지 설정은 수동으로 하는 방법을 찾았고, 시도해봤다.

 

letsencrypt certonly --webroot --webroot-path=/var/www/AAA -d AAA.co.kr -d www.AAA.co.kr

certbot은 인증서를 발급받고, 설정까지 자동으로 해주는 반면

letsencrypt 명령어를 사용하면, 수동으로 설정해줄 수 있다. 

(이게 맞나? 정확히는 모르겠다..)

 

--webroot --webroot-path

여기서 --webroot는 인증서를 발급받을 홈페이지 소스가 있는 폴더의 경로를 적어주면 된다.

 

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator webroot, Installer None
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for AAA.co.kr
http-01 challenge for www.AAA.co.kr
Using the webroot path /var/www/1800_7058_co_kr for all unmatched domains.
Waiting for verification...
Cleaning up challenges

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/AAA.co.kr/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/AAA.co.kr/privkey.pem
   Your cert will expire on 2020-12-18. 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"
 - 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

 

명령어를 실행하면, 위와 같이 잘 되었다고 나온다.

그러면, /etc/letsencrypt/archive/AAA.co.kr 경로아래에 인증서들이 발급된 것을 확인할 수 있다.

 

그럼 이제, 이 인증서들을 이용해서, nginx에 설정해주면 된다.

server {
 ## 이 부분은 http로 접속하면, https로 리다이렉트 시켜주는 부분이다 ##
 listen 80;
 root /var/www/AAA;
 server_name AAA.co.kr www.AAA.co.kr;

 return       301 https://$server_name$request_uri;
}

server {
 listen 443 ssl http2;
 root /var/www/sbcompany_co_kr;
 server_name AAA.co.kr www.AAA.co.kr;
 index index.php;

 ## 아래 4줄이 아까 발급받은 인증서를 설정 하는 부분이다. ##
 ssl_certificate /etc/letsencrypt/live/AAA.co.kr/fullchain.pem;
 ssl_certificate_key /etc/letsencrypt/live/AAA.co.kr/privkey.pem;
 include /etc/letsencrypt/options-ssl-nginx.conf;
 ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

 modsecurity on;
 modsecurity_rules_file /etc/nginx/modsec/modsec_includes.conf;
 #modsecurity_rules_file /etc/nginx/modsec/modsecurity.conf;

 location ~* \.(jpg|jpeg|gif|png|css|js|ico|xml)$ {
     access_log        off;
     log_not_found     off;
     expires           360d;
 }

 access_log /var/log/nginx/sbcompany_co_kr/access.log;

 location / {
 try_files $uri $uri/ /index.php?q=uri&$args;
 }

 location ~ \.php$ {
 include snippets/fastcgi-php.conf;
 fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
 }

 location ~ /xmlrpc.php {
  deny all;
  return 444;
 }
}

nginx에 해당 사이트의 site설정에 위 4줄을 추가해주면 된다.

끝~~

 

서버환경

Ubuntu 18.04.4 LTS

Nginx 1.19.1

 

기존 운영 도메인

1. AAA.com

2. BBB.Com (SSL 적용완료)

3. a.CCC.com

4 b.CCC.com (SSL 적용완료)

..등등 약 30개 정도 사이트

 

위 환경의 서버에서, Letsencrypt를 이용해서, 추가로 x.CCC.com 사이트에 SSL를 설정하고 있었다.

그런데, 자꾸 인증과정에서 에러가 발생했고,

추가적으로, https://asdf.CCC.com 과 같이 현재 서버에는 설정되어 있지 않은, 주소로 접속을 시도하면, 

기존에 SSL이 설정되어 있던, https://BBB.com 으로 접속이 되는 현상이 있었다.

 

이게, 기존부터 이렇게 되었던 건지,

아니면, 어떤 이유로 발생 했는지는 모르겠다.

 

여기저기 찾아보다가, 

qastack.kr/server/578648/properly-setting-up-a-default-nginx-server-for-https

 

위의 링크를 찾게 되었고, 해결했다.

 

이유는 바로, default 파일이 없어서 발생했던거다.

또한, default 파일도, http로 접속했을때와, https로 접속했을때 둘다를 설정해주어야 한다.

 

나는 그래서 아래와 같이 설정했다.

server {
  listen 80 default_server;
  listen [::]:80 default_server;
  listen 443 default_server;
  listen [::]:443 default_server;

  root /var/www/html;
  index index.html;

  server_name _;

  ssl_certificate /etc/nginx/ssl/nginx.crt;
  ssl_certificate_key /etc/nginx/ssl/nginx.key;

  location / {
    try_files $uri $uri/ =404;
    }
}

 

이렇게 설정후, nginx를 재시작 또는 reload를 하면,

위의 설명했던 현상이 발생하지 않게 된다.

 

또, default를 설정하지 않았을 때,

없는 주소로, https 를 이용해서 접속하게 되면,

https://BBB.com이 접속되는데, 

아마 그 이유는 sites-available 안에 파일 중,

SSL이 설정된 가장 첫번째 파일의 주소로 접속되는 것 같다. (알파벳 순)

 

파일만 변경

find ./ -type f -exec chmod -v 권한 {} \;
예: find ./ type f -exec chmod -v 755 {} \;

 

폴더만 변경

find ./ -type d -exec chmod -v 권한 {} \;
예: find ./ type d -exec chmod -v 755 {} \;

 

해당 파일만 변경

find ./ -type f -name 파일이름 -exec chmod -v 권한 {} \;
예: find ./ type f -name text.txt -exec chmod -v 755 {} \;

 

해당 파일을 제외하고 변경

find ./ -type f \! -name 파일이름 -exec chmod -v 권한 {} \;
예: find ./ type f \! -name text.txt -exec chmod -v 755 {} \;

 

해당 폴더만 변경

find ./ -type d -name 폴더이름 -exec chmod -v 권한 {} \;
예: find ./ type d -name testFolder -exec chmod -v 755 {} \;

 

해당 폴더를 제외하고 변경

find ./ -type d \! -name 폴더이름 -exec chmod -v 권한 {} \;
예: find ./ type d \! -name testFolder -exec chmod -v 755 {} \;

 

파일과 폴더 모두 변경

chmod -R 권한 *
예: chmod -R 755 *

1. Certbot 설치

// add repo
$ sudo add-apt-repository ppa:certbot/certbot

// install certbot
$ sudo apt install python-certbot-nginx

 

2. HTTPS(SSL)을 설정할, nginx 설정 체크

// check server_name
$ sudo vi /etc/nginx/sites-available/example.com

// server_name example.com www.example.com;

 

3. nginx relaod

Certbot이 nginx 서버 블록을 확인하게 하기 위해, nginx reload

// reload nginx
$ sudo system reload nginx

 

 

4. 우분투 방화벽 (ufw)에서 https 허용

방확벽을 사용하지 않는다면, 따로 설정할 필요는 없지만,

기존에 방화벽을 사용하고 있다면, 아래 링크의 3단계를 참고해서 방화벽 설정을 진행한다.

 

참고 : https://velog.io/@pinot/Ubuntu-18.04%EC%97%90%EC%84%9C-Lets-Encrypt%EB%A5%BC-%EC%82%AC%EC%9A%A9%ED%95%98%EC%97%AC-Nginx%EC%97%90-SSL%EC%9D%84-%EC%A0%81%EC%9A%A9%ED%95%98%EB%8A%94-%EB%B0%A9%EB%B2%95

 

번역) Ubuntu 18.04에서 Let's Encrypt를 사용하여 Nginx에 SSL을 적용하는 방법

우분투 환경에서 nginx에 certbot 클라이언트를 사용하여 자동적으로 SSL을 적용하는 방법에 대하여 알아보도록 합시다.

velog.io

 

5. HTTPS (SSL) 인증서 설치

$ sudo certbot --nginx -d example.com -d www.example.com

위의 명령어를 실행하면, 몇가지 과정을 진행 후, 아래와 같은 화면이 나온다.

Output
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):

// 1번은 http, https를 따로따로 접속할 수 있게 한다.
// 2번 설정은 http접속을 강제로 https로 모두 변경하므로 https접속만 할 수 있게 된다.

나는 2번을 선택했다.

원하는 번호를 선택하고, 엔터를 누르면 Nginx가 자동으로 재시작되고, 인증서가 저장된 위치를 알려주고, certbot은 종료된다.

 

6. Let's Encrypt HTTPS(SSL) 갱신

$ sudo certbot renew --dry-run

아주 간단하게, 위 명령어만 입력하면 certbot으로 진행한, HTTPS (SSL)을 모두 갱신할 수 있다.

 

Ubuntu Cron을 이용해서, 위의 갱신 명령어를 스케쥴링으로 하는 방법은 다음 포스팅에서...

 

 

참고 : https://velog.io/@pinot/Ubuntu-18.04%EC%97%90%EC%84%9C-Lets-Encrypt%EB%A5%BC-%EC%82%AC%EC%9A%A9%ED%95%98%EC%97%AC-Nginx%EC%97%90-SSL%EC%9D%84-%EC%A0%81%EC%9A%A9%ED%95%98%EB%8A%94-%EB%B0%A9%EB%B2%95

+ Recent posts