Linux WEB

apache1.3 + mod_ssl - 보안서버 구축하기

2008.05.15 00:43

sylee 조회 수:20080

보안 서버(SSL) 구축하기- apache 1.3

쇼핑몰등의 사이트에서는 반드시 구축하도록 법제화가 되었다고 한다.
회원가입이나, 결재정보 입력등 개인 정보를 입력할때 암호화 되어 전송되도록 "https://도메인" 으로 접속할 수 있게 구축된 웹서버를 보안 서버라고 한다.

mysql, gd, imap등의 패키지는 이미 깔려 있는 상태이며, 모든 패키지는 /usr/local/src/ 디렉토리 안에 다운로드 받은 상태로 설치를 시작한다.


1. 소스를 다운 받은 디렉토리로 이동

[root@hostway.co.kr root]# cd /usr/local/src


2. 압축 풀기

[root@hostway.co.kr src]# tar xvzf apache_1.3.33.tar.gz
[root@hostway.co.kr src]# tar xvzf mod_ssl-2.8.22-1.3.33.tar.gz
[root@hostway.co.kr src]# tar xvzf php-4.3.10.tar.gz
[root@hostway.co.kr src]# tar xvzf ZendOptimizer-2.6.2-linux-glibc21-i386.tar.gz


3. mod_ssl configure

[root@hostway.co.kr src]# cd mod_ssl-2.8.22-1.3.33
[root@hostway.co.kr mod_ssl-2.8.22-1.3.33]# ./configure --with-apache=../apache_1.3.33


4. apache 설치

- apache를 설치하기전에, source 디렉토리내의 src/include/httpd.h 파일에서 #define HARD_SERVER_LIMIT 부분을 1024로 수정 한다.  디폴트값 256으로 컴파일 하게 되면, 아파치 데몬이 최대 256개 까지 밖에 뜰수 없게 된다, 이런 경우는 서버의 접속량이 많을경우 서버의 자원이 남아도 더 이상의 데몬을 띄우지 못해서 서버가 느려지게 된다.

[root@hostway.co.kr mod_ssl-2.8.22-1.3.33]# cd /usr/local/src/apache_1.3.33
[root@hostway.co.kr apache_1.3.33]# SSL_BASE=/path-to-openssl (ex : SSL_BASE=/usr)
[root@hostway.co.kr apache_1.3.33]# ./configure --prefix=/usr/local/apache --enable-module=so --enable-shared=max --enable-module=ssl
[root@hostway.co.kr apache_1.3.33]# make
[root@hostway.co.kr apache_1.3.33]# make certificate

- make certificate 실행시 여러가지 정보를 물어보게 된다. 회사, 주소, 부서 이메일등 ssl 인증키 발급에 필요한 정보가 입력된 csr 코드를 생성하는데 필요한 정보들이기 때문에 정확한 정보를 입력한다. 마지막에 패스워드 설정 부분은 n 을 두번 입력.(y를 선택할경우 apachectl startssl 실행시 패스워드를 물어본다)

- make certificate 실행시 아무 정보를 입력하지 않고 엔터키만 입력한후 마지막의 패스워드 설정부분만 n 을 선택해서 설치를 진행해도 됨. 이 경우는 apache+mod_ssl+php 의 설치가 완료된 후 openssl 명령으로 csr 코드를 재생성 할 수 있다.

[root@hostway.co.kr apache_1.3.33]# make install

+--------------------------------------------------------+
| You now have successfully built and installed the      |
| Apache 1.3 HTTP server. To verify that Apache actually |
| works correctly you now should first check the         |
| (initially created or preserved) configuration files   |
|                                                        |
|   /usr/local/apache/conf/httpd.conf
|                                                        |
| and then you should be able to immediately fire up     |
| Apache the first time by running:                      |
|                                                        |
|   /usr/local/apache/bin/apachectl start
|                                                        |
| Or when you want to run it with SSL enabled use:       |
|                                                        |
|   /usr/local/apache/bin/apachectl startssl
|                                                        |
| Thanks for using Apache.       The Apache Group        |
|                                http://www.apache.org/  |
+--------------------------------------------------------+

- 위와 같이 출력되면 설치 완료. httpd.conf 파일을 수정한 후 apache 데몬 실행. 설치중에 패스워드 설정을 하였다면, 패스워드를 입력하고 데몬을 실행한다. ssl 서버로 시작하기 위해서 startssl 명령으로 시작.

[root@hostway.co.kr apache_1.3.33]# /usr/local/apache/bin/apachectl startssl


5. PHP 설치 (DB,imap,gd,apache)

[root@hostway.co.kr apache_1.3.33]# cd /usr/local/src/php-4.3.10
[root@hostway.co.kr php-4.3.10]# ./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-apxs=/usr/local/apache/bin/apxs --enable-sysvshm=yes --enable-sysvsem=yes --enable-debug=no --enable-track-vars=yes --enable-url-fopen-wrapper=yes --with-ttf --with-png-dir=/usr --with-zlib-dir --with-jpeg-dir=/usr --with-gdbm=/usr --enable-ftp --with-tiff-dir=/usr --enable-memory-limit --enable-mbstring --with-expat-dir=/usr --enable-sockets --enable-wddx --with-freetype-dir=/usr --enable-bcmath --enable-mbstr-enc-trans --enable-mbregex --enable-exif --with-gd --enable-gd-native-ttf --enable-gd-imgstrttf --enable-calendar --with-openssl=/usr

- php configure시에 에러가 발생하는 경우는 서버에 필요한 패키지가 설치되어 있지 않기 때문이거나 필요한 패키지의 버전이 낮아서 발생하는 경우가 대부분이다. 어떤패키지를 체크하는 부분에서 에러가 발생하는지 확인 한 후 yum install 명령으로 필요한 패키지를 설치 한 후 다시 configure 하거나, 필요하지 않는 패키지일 경우 configure 옵션에서 삭제하고 configure를 다시 실행한다.
[root@hostway.co.kr php-4.3.10]# make
[root@hostway.co.kr php-4.3.10]# make install
[root@hostway.co.kr php-4.3.10]# cp php.ini-dist /usr/local/php/lib/php.ini


6. ZendOptimizer 설치

[root@hostway.co.kr php-4.3.10]# cd ZendOptimizer-2.6.2-linux-glibc21-i386
[root@hostway.co.kr ZendOptimizer-2.6.2-linux-glibc21-i386]# ./install.sh

- install.sh 파일을 실행한 후 apache와 php관련된 정보를 입력한 후 설치를 완료한다.


7. httpd.conf 파일 수정후 apache 리스타트

- httpd.conf 파일에 php가 인식되도록 아래 구문을 추가한다.

   #################################################
    AddType application/x-httpd-php .php .html .htm
    AddType application/x-httpd-php-source .phps
   #################################################

- httpd.conf 수정후 apache 데몬은 재실행한다. ssl 서버로 시작하기 위해서 startssl 명령으로 시작.

[root@hostway.co.kr ZendOptimizer-2.6.2-linux-glibc21-i386]# cd /usr/local/apache/conf
[root@hostway.co.kr conf]# /usr/local/apache/bin/apachectl stop
[root@hostway.co.kr conf]# /usr/local/apache/bin/apachectl startssl


8. key 파일 생성 및 csr 코드 생성

- apache+mod_ssl 설치하면서 make certificate를 실행할 때 여러 정보를 입력했을 경우는 /usr/local/apache/conf/ssl.key, /usr/local/apache/conf/ssl.csr 등의 디렉토리가 생성되어 key 파일과 csr 코드가 자동으로 생성된다. 생성된 csr 코드를 입력해서 ssl 인증키 발급업체에 인증키 발급을 신청 한다.

- 아래는 수동으로 key 파일과 csr 코드를 생성하는 방법이다.

[root@hostway.co.kr conf]# openssl genrsa 1024 > hostway.co.kr.key
warning, not much extra random data, consider using the -rand option
Generating RSA private key, 1024 bit long modulus
............................................++++++
......++++++
e is 65537 (0x10001)
[root@hostway.co.kr conf]# openssl req -new -key hostway.co.kr.key -out hostway.co.kr.csr
Using configuration from /usr/local/ssl/openssl.cnf
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) [AU]:KR
State or Province Name (full name) [Some-State]:Gyunggido
Locality Name (eg, city) []:Sungnam
Organization Name (eg, company) [Internet Widgits Pty Ltd]:Hostway IDC
Organizational Unit Name (eg, section) []:Technical support Team
Common Name (eg, YOUR name) []:hostway
Email Address []:support@hostway.co.kr

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
lsh813:/wwws/conf# cat hostway.co.kr.csr

=================================================================
-----BEGIN CERTIFICATE REQUEST-----
MIIB9DCCAV0CAAAQAwgbMxCzAJBgNVBAYTAktSMRIwEAYDVQQIEwlHeXVuZ2dpZG
EDAOBgNVB3AecTB1N1bmduYW0xFDASBgNVBAoTC0hvc3R3YXkgSURDMR8wHQYDVQ
ExZUZWNobmlF5Wwgc3VwcG9ydCBUZWFtMSEwHwYDVQQDExhsc2g4MTMuc2l0ZXBy
b3RlY3QuY28eua3IxJDAirBgkqhkiG9w0BCQEWFXN1cHBvcnREGG9zdHdheS5jby
cjCBnzANBgkqhkriG9w0dvBAQEFAAOBjQAr3r3wgYkCgYEA41OyN6Ni+Qq0U1XMl
jKn6uFiNq1Y4LRjaI7gzIkP3Vq9N3lYY2bnbpENHkOLg3YcqmBz8awPHtzm0LZi5
S87ZPFNILhNdfsYLnbC+RI+rCw0XTwvEoGKqtbCmluu6OG5f0pOS7fuuZIYapXEj
78z/j3uGgh8mgp06iysCAwEAAaAAMA0EF46GSIb3DQEBBAUAA4GBAMNZ3iOBXFUl
DC8tq/DXDx8g47FqiTKYoSU028uttM3mMlPAEIV91S2RmLxEpBkK2Alevh7Wf+zE
TIpbeJTVeF5d3CPDgqXOghHff0O/Wbdzz2YoLl3hu9d7sp8kkq2M2tSGAvSM03ke
bUB0Lru0Wiz/XsfdX4bFCTfpxiLPpbnW
-----END CERTIFICATE REQUEST-----
=================================================================


9. 인증서 및 CA 인증서 설치

- 인증키 발급 업체에서 인증키를 발급받으면 www_hostway_co_kr.crt(인증서), www_hostway_co_kr.ca-bundle(CA 인증서)와 같이 두개의 파일을 발급받게 된다.
- 두 파일을 서버의 /usr/local/apache/conf/ssl.crt 디렉토리에 업로드한다.


10. SSL 가상호스트 설정

- httpd.conf 파일에 아래와 같은 형식으로 가상호스트 설정 부분을 추가한다.

<IfDefine SSL>

NameVirtualHost 211.239.151.254:443

<VirtualHost 211.239.151.254:443>
DocumentRoot "/home/hostway/public_html"
ServerName www.hostway.co.kr
ServerAdmin support@hostway.co.kr
ErrorLog /usr/local/apache/logs/error_log
TransferLog /usr/local/apache/logs/access_log

SSLEngine on

SSLCipherSuite ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP:+eNULL
SSLCertificateFile /usr/local/apache/conf/ssl.crt/www_hostway_co_kr.crt
SSLCertificateKeyFile /usr/local/apache/conf/ssl.key/hostway.co.kr.key
SSLCACertificateFile /usr/local/apache/conf/ssl.crt/www_hostway_co_kr.ca-bundle

<Files ~ ".(cgi|shtml|phtml|php3?)$">
    SSLOptions +StdEnvVars
</Files>
<Directory "/usr/local/apache/cgi-bin">
    SSLOptions +StdEnvVars
</Directory>

SetEnvIf User-Agent ".*MSIE.*"
         nokeepalive ssl-unclean-shutdown
         downgrade-1.0 force-response-1.0

CustomLog /usr/local/apache/logs/ssl_request_log
          "%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x "%r" %b"

</VirtualHost>

</IfDefine>


11. apache 데몬 리스타트

- 모든 설정이 완료된 후 apache 데몬을 리스타트 하고 https://도메인으로 사이트를 띄워서 인증키를 확인한다.
- 여기서 https://www.hostway.co.kr은 실제로는 ssl 설정이 되어 있지 않기 때문에 안뜨는게 정상 ^^

[root@hostway.co.kr conf]# /usr/local/apache/bin/apachectl stop
[root@hostway.co.kr conf]# /usr/local/apache/bin/apachectl startssl

- startssl로 데몬을 띄웠을때 정상적으로 동작하지 않으면 아래 사항을 확인한다.

* /usr/local/apache/logs 디렉토리 퍼미션 확인
* ssl_engine_log 확인
  에러 스트링 확인 방법 : openssl errstr 0B080074  (0B080074는 ssl_engine_log에서 출력된 에러 스트링 코드)
* /usr/local/apache/conf/ssl.crt 디렉토리 퍼미션 확인
* /usr/local/apache/conf/ssl.key 디렉토리 퍼미션 확인
* csr 코드 확인 : openssl req -noout -text -in *.csr