Posted
Filed under 개발/Linux
Let's encrypt는 사용자에게 무료로 TLS 인증서를 발급해주는 비영리기관이다. 루트 도메인, 서브 도메인, 와일드카드 서브 도메인 등에 대한 인증서 발급이 가능하다.

발급된 인증서의 유효기간은 90일이며 만료 30일 전부터 갱신할 수 있으며 갱신가능 횟수는 무제한이다.

Let's encrypt가 인증서를 발급하는 방식에는 standalone, webroot, dns 세가지가 있는데 여기서는 webroot 명령어를 이용하여 발급한다.

webroot 명령어를 이용하면 자신의 웹서버에서 작동중인 http 프로토콜의 웹사이트에 certbot이 접속하여 서버가 유효한지 확인하고 인증서를 발급한다.

현재 Nginx에서 http://www.test.com이라는 웹사이트를 운영중이고 Nginx 설정은 다음과 같다고 하자.

[code]
server {
    listen 80;
    listen [::]:80;

    server_name www.test.com;

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

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

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


* certbot 패키지를 설치하고 인증서를 발급한다.
$ sudo apt-get install certbot
$ sudo certbot certonly --webroot --webroot-path=/var/www/html
-d www.test.com


* 발급받은 인증서는 /etc/letsencrypt/live/[인증서이름] 에 저장된다.
cert.pem - 인증서 파일
chain.pem - 인증서 발급자 파일
fullchain.pem - cert.pem 과 chain.pen 을 하나로 합쳐놓은 파일
privkey.pem - 인증암호를 해독하는 개인키

Apache2 서버에서는 cert.pem, chain.pem, privkey.pem 을,
Nginx 서버에서는 fullchain.pem, privkey.pem 을 사용한다.


* Nginx의 서버설정 변경
[code]
server {
    listen 80;
    listen [::]:80;

    server_name www.test.com;
   
    # 모든 http 요청을 https로 301 리다이렉트한다
    return 301 https://$server_name$request_uri;

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

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

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


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

    server_name www.test.com;
    root /var/www/html;
    index index.html index.htm index.php;

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

    ssl                  on;
    ssl_certificate      /etc/letsencrypt/live/www.test.com/fullchain.pem;
    ssl_certificate_key  /etc/letsencrypt/live/www.test.com/privkey.pem;
    ssl_ciphers  HIGH:!aNULL:!MD5;

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


* Nginx 서버 재시작
$ sudo systemctl restart nginx


* 발급받은 인증서 확인
$ sudo certbot certificates


* 인증서 갱신
$ sudo certbot renew


* 인증서 취소 및 삭제
$ certbot revoke --cert-path /etc/letsencrypt/live/www.test.com/cert.pem
$ sudo certbot delete --cert-name www.test.com
2019/04/12 13:43 2019/04/12 13:43
Posted
Filed under 개발/Linux
LEMP는 Linux + Nginx + MariaDB + PHP7 의 조합을 의미한다. Nginx의 N 대신 발음하기 좋게 EngineX의 E를 쓴다

1. Nginx
Nginx는 Apache와 다르게 비동기서버라서 퍼포먼스가 더 좋고 메모리 소비량도 적다.

* nginx 설치
$ sudo apt install nginx
이 시점에서 서버의 80번 포트로 접속할수 있게 된다.


2. PHP
* Nginx는 PHP를 네이티브로 지원하지 않으므로 Fastcgi Process Manager(FPM)로 구현된 PHP를 설치한다
$ sudo apt install php-fpm
$ sudo nano /etc/nginx/sites-available/default
에서
index index.html index.htm index.nginx-debian.html;
→ index index.html index.htm index.php;
로 수정.

[code]
#location ~ \.php$ {
# include snippets/fastcgi-php.conf;
#
# # With php5-cgi alone:
# fastcgi_pass 127.0.0.1:9000;
# # With php5-fpm:
# fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
#}
[/code]
에서

[code]
location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
}
[/code]
로 수정.

* 서비스 재시작
$ sudo service nginx reload


3. MariaDB 설치
$ sudo apt install mysql-server php-mysql

* 초기셋업
$ sudo mysql_secure_installation

Enter current password for root (enter for none): 엔터
Set root password? [Y/n] y
New password: 패스워드 입력
Remove anonymous users? [Y/n] y
Disallow root login remotely? [Y/n] y
Remove test database and access to it? [Y/n] y
Reload privilege tables now? [Y/n] y

* 로그인
$ sudo mysql -uroot -p

* 워드프레스용 DB 생성
create database wordpress;

* 워드프레스 DB에 접근할 수 있는 유저 생성
create user 사용자이름 identified by '패스워드';

* 생성한 유저에 워드프레스 DB에 대한 모든 권한을 부여
grant all privileges on wordpress.* to '사용자이름'@'localhost' identified by '패스워드';

* 권한변경을 적용
flush privileges;


4. 워드프레스 설치

* html 폴더 안의 모든 파일 삭제
$ cd /var/www/html/
$ sudo rm *

* 워드프레스 다운로드
$ sudo wget http://wordpress.org/latest.tar.gz

* 다운로드한 파일의 압축해제
$ sudo tar xzf latest.tar.gz

* 워드프레스 폴더안의 모든파일을 현재 폴더로 이동
$ sudo mv wordpress/* .

* 압축파일과 빈 워드프레스 폴더를 삭제
$ sudo rm -rf wordpress latest.tar.gz

* 워드프레스 셋업
http://localhost 로 접속하여 웹브라우저 상에서 셋업


5. SSL 설정

* Let's encrypt 설치
$ sudo apt install letsencrypt

* HTTP-based DCV 방식으로 서버 인증
$ sudo letsencrypt certonly --webroot --webroot-path=/var/www/html -d test.com -d www.test.com

* 인증성공시 인증서 발급
/etc/letsencrypt/live/test.com/ 폴더에 인증서가 생성된다.
cert.pem(인증서 파일), chain.pem(인증서 발급자 파일),
fullchain.pem(cert.pem 과 chain.pen 을 하나로 합쳐놓은 파일),
privkey.pem(인증암호를 해독하는 개인키)

Apache2는 cert.pem, chain.pem, privkey.pem 을 사용.
Nginx는 fullchain.pem, privkey.pem 을 사용.

* Nginx 설정
/etc/nginx/sites-available/default를 다음과 같이 수정한다.

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

    server_name www.test.com test.com;
    return 301 https://$server_name$request_uri;
}

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

    root /var/www/html;
    index index.html index.htm index.php;
    server_name _;

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

    ssl                  on;
    ssl_certificate      /etc/letsencrypt/live/test.com/fullchain.pem;
    ssl_certificate_key  /etc/letsencrypt/live/test.com/privkey.pem;
    ssl_ciphers  HIGH:!aNULL:!MD5;

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

* 인증서 갱신
$ sudo letsencrypt renew

* 인증서 삭제
$ certbot revoke --cert-path /etc/letsencrypt/live/test.com/cert.pem
$ certbot delete --cert-name test.com
2019/04/09 08:55 2019/04/09 08:55