개발 일지/개인 회고

[Main Project] NginX 를 이용하여 Https 적용하는 방법

개발하는 동그리 2022. 10. 2. 14:41

NgineX 설치 및 실행

# Nginx 설치
$ sudo apt install nginX

# Nginx 실행
$ sudo service nginx start

 

https 적용을 위해 certbot 설치

1. snapd를 이용하여 cerbot 설치 명령으로 설치 ( ubuntu 최신 버전(v20) 에서 동작하지 않을 수 있다. 

$ wget https://dl.eff.org/certbot-auto

 

따라서 snapd를 이용하여 설치 

# snap을 이용하여 core 설치 -> snap을 최신 버전으로 유지하기 위해 설치
$ sudo snap install core

# core를 refresh 해준다.
$ sudo snap refresh core

# 기존에 잘못된 certbot이 설치되어있을 수도 있으니 삭제 해준다.
$ sudo apt remove certbot

# certbot 설치
$ sudo snap install --classic certbot

# certbot 명령을 로컬에서 실행할 수 있도록 
# snap의 certbot 파일을 로컬의 cerbot과 링크(연결) 시켜준다. -s 옵션은 심볼릭링크를 하겠다는 것.
$ ln -s /snap/bin/certbot /usr/bin/certbot

 

nginx.conf
event {
	worker_connections 1024;
}

http {
	include mime.types
    
    // redirect all traffic to Https http 요청을 https 로 redirect
    server {
	    listen 80;
	    server_name [server_ip];
	    return 301 https://$host$reqeust_uri;
    }
    
    server {
    		
            listen 443 ssl http2;
            server_name [server_ip];
            
            root /sites/demo;
            index index.html
            
            ssl_certificate /etc/nginx/ssl/self.crt
            ssl_certificate_key /etc/nginx/ssl/self.key;
            
            location / {
            		try_files $uri $uri/ = 404;
            }
            
            location ~\.php$ {
            		# Pass php requests to the php-fpm service (fastcgi)
                    include fastcgi/conf
                    fastcgi_pass unix:/run/php/php7.10fpm.sock;
    }