Tag Archives: mod_ssl

macOS Mojave 에서 SSL 지원하는 Apache 웹서버 설치하기

이번에 새롭게 출시된 macOS 모하비에서 Apache를 설치하는 방법을 정리 해 보겠습니다. 또한 추가로 HTTPS 프로토콜을 지원하는 설정도 함께 적어보도록 하겠습니다. 이글은 macOS에서 Homebrew를 사용중인것을 가정하고 작성되었습니다.

과거 시에라등의 버전에서는 Apache 웹서버가 기본적으로 설치가 되어있습니다. 계속해서 업데이트를 해왔다면 해당 아파치 웹서버가 그대로 살아있을수 있으니 다음과 같은 방법으로 제거를 해줍시다.

$ ps aux | grep httpd
_www               232   0.0  0.0  4329504    936   ??  S    12:12AM   0:00.00 /usr/local/opt/httpd/bin/httpd -D FOREGROUND
_www               231   0.0  0.0  4321312    908   ??  S    12:12AM   0:00.00 /usr/local/opt/httpd/bin/httpd -D FOREGROUND
_www               230   0.0  0.0  4329504    888   ??  S    12:12AM   0:00.03 /usr/local/opt/httpd/bin/httpd -D FOREGROUND
_www               229   0.0  0.0  4321312    932   ??  S    12:12AM   0:00.00 /usr/local/opt/httpd/bin/httpd -D FOREGROUND
_www               228   0.0  0.0  4329504    924   ??  S    12:12AM   0:00.00 /usr/local/opt/httpd/bin/httpd -D FOREGROUND

위와 같이 돌아가고 있는 httpd 프로세스가 있는것을 확인하였다면 다음의 명령을 사용하여 종료하고 기본 구동 서비스에서도 삭제 해 줍니다.

$ sudo apachectl stop
$ sudo launchctl unload -w /System/Library/LaunchDaemons/org.apache.httpd.plist

위와 같은 명령을 사용하고 기본적으로 설치되어있는 아파치 웹서버를 더이상 실행되지 않도록 변경할 수 있습니다. 이번에는 새로운 아파치 2.4 버전을 Homebrew를 사용하여 설치한 뒤 서비스로 구동하는 방법을 알아보겠습니다.

$ brew install httpd24 --with-privileged-ports --with-http2

위와 같은 명령을 사용하여 매우 간단하게 아파치 웹서버 2.4 버전을 설치할 수 있었습니다. –with-privileged-ports 옵션을 1024 이하의 낮은 숫자의 시스템 포트를 사용하기 위해서 사용하는 옵션입니다. 이 옵션을 사용하여 80 포트를 사용할 수 있게 되며 8080과 같은 포트를 사용하려면 굳이 이 옵션을 사용하실 필요가 없습니다.

이제 아파치의 설정 파일을 수정하여 80 포트로 구동되도록 수정하겠습니다.

$ vi /usr/local/etc/httpd/httpd.conf

다음의 내용을 찾아서

Listen 8080

다음과 같이 수정합니다.

Listen 80

이제 다음의 명령을 사용하여 웹서버를 구동할 수 있습니다.

$ sudo brew services start httpd24

위에서 설명했던것과 같이 1024 이하의 시스템 포트를 사용하려면 sudo를 사용하여 root 권한으로 구동해야 합니다. 이후의 포트를 사용하려면 굳이 sudo를 사용하지 않아도 사용이 가능합니다.

정상적으로 설치가 되었고 구동이 되었다면 웹브라우저를 통해서 localhost에 접속해 보면 “It works!”라는 문자열 출력을 확인하실 수 있습니다.

이번에는 SSL 설정을 해보도록 하겠습니다. 다시한번 아파치 웹서버 설정 파일을 수정 해 보겠습니다.

$ vi /usr/local/etc/httpd/httpd.conf

다음의 4가지 항목을 찾아서 모두 주석을 해제 해 줍니다.

#LoadModule socache_shmcb_module lib/httpd/modules/mod_socache_shmcb.so
#LoadModule ssl_module lib/httpd/modules/mod_ssl.so
#Include /usr/local/etc/httpd/extra/httpd-vhosts.conf
#Include /usr/local/etc/httpd/extra/httpd-ssl.conf

또한 ServerName 항목을 찾아서 주석을 해제하고 다음과 같이 내용을 수정 해 줍니다.

ServerName localhost:80

httpd.conf 파일의 수정은 완료되었고 이번에는 httpd-ssl.conf 파일을 수정 해 보겠습니다.

$ vi /usr/local/etc/httpd/extra/httpd-ssl.conf

다음의 내용을 찾아서

Listen 8443

다음과 같이 수정 해 줍니다.

Listen 443

추가로 좀 더 밑으로 가서 다음의 내용을 찾아서

<VirtualHost _default_:8443>

# General setup for the virtual host
DocumentRoot "/usr/local/var/www"
ServerName www.example.com:8443
ServerAdmin you@example.com

다음과 같이 수정 해 줍니다.

<VirtualHost _default_:443>

# General setup for the virtual host
#DocumentRoot "/usr/local/var/www"
#ServerName www.example.com:443
#ServerAdmin you@example.com

이번에는 httpd-vhosts.conf 파일을 수정하여 가상호스트 설정을 해 보겠습니다.

$ vi /usr/local/etc/httpd/extra/httpd-vhosts.conf

이미 몇개의 가상호스트 설정이 있습니다만, 모두 주석 처리 해 주고 다음의 두가지 설정을 추가 해 줍니다.

<VirtualHost *:80>
    DocumentRoot /usr/local/var/www
    ServerName localhost
</VirtualHost>

<VirtualHost *:443>
    DocumentRoot /usr/local/var/www
    ServerName localhost
    SSLEngine on
    SSLCertificateFile "/usr/local/etc/httpd/server.crt"
    SSLCertificateKeyFile "/usr/local/etc/httpd/server.key"
</VirtualHost>

여기서 눈여겨 봐야 하는 부분은 DocumentRoot 설정을 통해서 /usr/local/var/www를 웹서버의 기본 리소스 디렉토리로 설정했다는것과 443포트 연결에 대해서 SSLEngine이 on되었고 추가로 SSLCertificateFile와 SSLCertificateKeyFile 세팅이 되었다는 점 입니다.

DocumentRoot의 설정은 본인의 개발 환경에 맞게 설정 해 주시면 됩니다.

이번에는 로컬에서만 사용될 Self-Signed 인증서를 만들어보도록 하겠습니다. 여기서 만들 인증서 파일을 사용하겠다는 설정은 이미 위에서 해두었습니다. 다음의 명령어를 사용하여 인증서를 만드시면 됩니다.

$ cd /usr/local/etc/httpd
$ openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout server.key -out server.crt
Generating a 2048 bit RSA private key
.............+++
.......................................................................................+++
writing new private key to 'server.key'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) []:KR
State or Province Name (full name) []:Gyeonggi-do
Locality Name (eg, city) []:Seongnam-si
Organization Name (eg, company) []:MyCompany
Organizational Unit Name (eg, section) []:
Common Name (eg, fully qualified host name) []:
Email Address []:

이제 다 된것 같습니다. 다음의 명령을 사용하여 설정이 정상적으로 되었는지 확인이 가능합니다.

$ sudo apachectl configtest
Syntax OK

이제 웹브라우저를 재시행하여 http://localhost https://localhost 둘 모두 접속이 잘 되는것을 확인 해 봅시다.

$ sudo brew services restart httpd24

Self-Signed SSL로 Apache HTTPS 구현하기

apache-logo

기존의 HTTP(HyperText Transfer Protocol)과 다르게 HTTPS(Hypertext Transfer Protocol over Secure Socket Layer)는 SSL위에서 돌아가는 HTTP의 평문 전송 대신에 암호화된 통신을 하는 프로토콜입니다.

이런 HTTPS를 통신을 서버에서 구현하기 위해서는 신뢰할 수 있는 상위 기업이 발급한 인증서가 필요로 한데 이런 발급 기관을 CA(Certificate authority)라고 합니다. 하지만 이러한 기업의 인증서를 발급받는것은 무료가 아니며 단순히 모바일앱과의 통신이라던가 테스트 목적에서 발급을 받기에는 부담스러운 부분이 있을 수 있습니다.

이런 경우 자체적으로 인증서를 발급하여 사용하는 방법을 고려해 볼 수 있습니다. 이 경우 브라우저 접속시에 보안 경고가 발생하므로 주의하시기 바랍니다. 이 글에서는 CentOS와 Apache 웹서버 구동 환경에서의 HTTPS 구축하는 방법을 정리해 보겠습니다.

# yum install openssl mod_ssl

우선 위와 같이 openssl과 mod_ssl을 설치해줍니다. 만약 설치가 되어있는 상태라면 무시해도 됩니다. 이번엔 다음과 같은 명령을 사용하여 인증서를 생성해 줍니다.

(선택 1) 한번에 원하는 인증서를 발급하기

따로 원하는 CA를 둘것없이 간편하게 서버에서 사용할 인증서를 발급하는 방법은 다음과 같습니다.

# openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/ssl/certs/mycert.key -out /etc/ssl/certs/mycert.crt
Generating a 2048 bit RSA private key
........................+++
writing new private key to '/etc/ssl/certs/mycert.key'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:KR
State or Province Name (full name) []:Gyeonggi-do
Locality Name (eg, city) [Default City]:Seongnam-si
Organization Name (eg, company) [Default Company Ltd]:TheEye Company
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server's hostname) []:
Email Address []:

(선택 2) 자체 CA 인증서를 생성하고 이를 통해 인증서를 발급하기

자체적으로 CA를 구축하는 방법도 있습니다. 세상 누구도 알아주지 않겠지만 다양한 서비스를 동시에 운영중이라면 해볼만한 시도라고 생각이 됩니다. 우선 CA 인증서부터 발급합니다.

# openssl genrsa -out /etc/ssl/certs/rootCA.key 2048
Generating RSA private key, 2048 bit long modulus
......................................................+++
..........+++
e is 65537 (0x10001)

# openssl req -x509 -new -nodes -key /etc/ssl/certs/rootCA.key -days 365 -out /etc/ssl/certs/rootCA.crt
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:KR
State or Province Name (full name) []:Gyeonggi-do
Locality Name (eg, city) [Default City]:Seongnam-si
Organization Name (eg, company) [Default Company Ltd]:Theeye Company
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server's hostname) []:
Email Address []:

이번엔 CA에 인증서 발급을 요청하기 위한 CSR(Certificate Signing Request)를 생성합니다.

# openssl genrsa -out /etc/ssl/certs/mycert.key 2048    
Generating RSA private key, 2048 bit long modulus
........+++
..........................+++
e is 65537 (0x10001)

# openssl req -new -key /etc/ssl/certs/mycert.key -out /etc/ssl/certs/mycert.csr
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:KR
State or Province Name (full name) []:Gyeonggi-do
Locality Name (eg, city) [Default City]:Seongnam-si
Organization Name (eg, company) [Default Company Ltd]:TheEye Company
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server's hostname) []:
Email Address []:

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:

이번엔 마지막으로 CA 인증서와 CSR을 이용하여 서비스에 사용할 인증서를 발급해 보도록 하겠습니다.

# openssl x509 -req -in /etc/ssl/certs/mycert.csr -CA /etc/ssl/certs/rootCA.crt -CAkey /etc/ssl/certs/rootCA.key -CAcreateserial -out /etc/ssl/certs/mycert.crt -days 365
Signature ok
subject=/C=KR/ST=Gyeonggi-do/L=Seongnam-si/O=TheEye Company
Getting CA Private Key

아파치 웹서버 설정에 적용

이제 /etc/ssl/certs 디렉토리에 mycert.key 파일과 mycert.crt 파일이 생성되었습니다. 이제 Apache 설정에 다음과 같은 형태로 이 키를 지정해 줍니다.

LoadModule ssl_module modules/mod_ssl.so

...

<VirtualHost 124.217.198.56:443>
  DocumentRoot /home/theeye/public_html
  ServerName theeye.pe.kr
  SSLEngine on
  SSLCertificateFile /etc/ssl/certs/mycert.crt
  SSLCertificateKeyFile /etc/ssl/certs/mycert.key
</VirtualHost>

이제 Apache 데몬을 재시작 한 뒤 해당 도메인에 https 로 접속하면 다음과 같은 보안 경고가 뜨게 됩니다.

not_secure_on_apache_https

하지만 아래의 안전하지 않음으로 이동을 통해 사이트에 정상적으로 접속하는 것이 가능합니다. 만약 iOS/Android에서 구동되는 어플리케이션의 경우 별도의 처리가 필요할 것입니다.