Microsoft MVP성태의 닷넷 이야기
VC++: 49. 소스 코드로부터 php5apache2_2.dll 생성하는 방법 [링크 복사], [링크+제목 복사],
조회: 31690
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일
(연관된 글이 2개 있습니다.)
소스 코드로부터 php5apache2_2.dll 생성하는 방법

지난번에, PHP 환경을 소스 코드로부터 빌드하는 방법을 알아봤었습니다.

PHP 소스를 윈도우 환경에서 빌드하기
; https://www.sysnet.pe.kr/2/0/1042

그때도 언급했지만 PHP의 빌드된 버전을 다운로드한 것과 소스 코드로부터 빌드한 결과물에 상당한 차이가 있었습니다. 그중에서, 아파치의 httpd.conf에 등록해 줘야 하는 php5apache2_2.dll 파일이 누락되어 있다고 했는데요. 이제는 제법 모듈에 대한 이해도 했으니,

윈도우에서 Apache Module 컴파일 (VC++)
; https://www.sysnet.pe.kr/2/0/1051

윈도우에서 Apache Module - Content Handler 컴파일
; https://www.sysnet.pe.kr/2/0/1056

php5apache2_2.dll을 소스 코드로부터 생성하는 방법을 알아봐야겠습니다. ^^




이를 위해 우선 소스 코드를 찾아야 하는데, php-5.3.6.tar.gz을 풀어놓은 폴더들을 보니 "D:\phpbuild\php53dev\vc9\x86\php-5.3.6\sapi\apache2handler"가 바로 php5apache2_2.dll에 대한 소스 코드라고 예상되더군요. 해당 폴더에 보면, config.w32라는 파일이 있는데 아래와 같은 내용을 포함하고 있습니다.

// vim:ft=javascript
// $Id: config.w32 259731 2008-05-14 03:13:17Z auroraeosrose $

ARG_ENABLE('apache2handler', 'Build Apache 2.x handler', 'no');

if (PHP_APACHE2HANDLER != "no") {
    if (PHP_ZTS == "no") {
        WARNING("Apache2 module requires an --enable-zts build of PHP on windows");
    } else if (CHECK_HEADER_ADD_INCLUDE("httpd.h", "CFLAGS_APACHE2HANDLER", PHP_PHP_BUILD + "\\include\\apache2") &&
            CHECK_LIB("libhttpd.lib", "apache2handler", PHP_PHP_BUILD + "\\lib\\apache2") &&
            CHECK_LIB("libapr.lib", "apache2handler", PHP_PHP_BUILD + "\\lib\\apache2") &&
            CHECK_LIB("libaprutil.lib", "apache2handler", PHP_PHP_BUILD + "\\lib\\apache2")
            ) {
        SAPI('apache2handler', 'mod_php5.c sapi_apache2.c apache_config.c php_functions.c',
                'php' + PHP_VERSION + 'apache2.dll',
                '/D PHP_APACHE2_EXPORTS /I win32');
    } else {
        WARNING("Could not find apache2 libraries/headers");
    }
}

ARG_ENABLE('apache2-2handler', 'Build Apache 2.2.x handler', 'no');

if (PHP_APACHE2_2HANDLER != "no") {
    if (PHP_ZTS == "no") {
        WARNING("Apache2 module requires an --enable-zts build of PHP on windows");
    } else if (CHECK_HEADER_ADD_INCLUDE("httpd.h", "CFLAGS_APACHE2_2HANDLER", PHP_PHP_BUILD + "\\include\\apache2_2") &&
            CHECK_LIB("libhttpd.lib", "apache2_2handler", PHP_PHP_BUILD + "\\lib\\apache2_2") &&
            CHECK_LIB("libapr-1.lib", "apache2_2handler", PHP_PHP_BUILD + "\\lib\\apache2_2") &&
            CHECK_LIB("libaprutil-1.lib", "apache2_2handler", PHP_PHP_BUILD + "\\lib\\apache2_2")
            ) {
        SAPI('apache2_2handler', 'mod_php5.c sapi_apache2.c apache_config.c php_functions.c',
                'php' + PHP_VERSION + 'apache2_2.dll',
                '/D PHP_APACHE2_EXPORTS /I win32',
                'sapi\\apache2_2handler');
    } else {
        WARNING("Could not find apache2.2 libraries/headers");
    }
}

config.w32 파일이 어떻게 해석되는지는 정확히 알 수 없으나, 대충의 감각으로 보면 'apache2handler' 옵션이 설정되어 있으면 php5apache2.dll 파일이 생성되고, apache2-2handler 옵션이 설정되어 있으면 php5apache2_2.dll을 생성해 내는 것으로 보입니다. 이걸 보면서 지난번 글의 마지막에 옮겨두었던 configure --help 출력물이 생각나더군요.

D:\phpbuild\php53dev\vc9\x86\php-5.3.6>configure --help
Options that enable extensions and SAPI will accept 'yes' or 'no' as a
parameter. They also accept 'shared' as a synonym for 'yes' and request a
shared build of that module. Not all modules can be built as shared modules;
configure will display [shared] after the module name if can be built that
way.

... [생략] ...
  --with-apache-libs            Where to find Apache 1.3 libraries
  --enable-apache2filter        Build Apache 2.x filter
  --enable-apache2-2filter      Build Apache 2.2.x filter
  --enable-apache2handler       Build Apache 2.x handler
  --enable-apache2-2handler     Build Apache 2.2.x handler
  --with-apache-hooks           Build Apache 1.3.x (hooks) version of PHP
  --disable-cgi                 Build CGI version of PHP
  --disable-cli                 Build CLI version of PHP
... [생략] ...

그럴 듯하지요. ^^ 그래서, 이번에는 PHP 소스 코드 빌드 과정에서 위의 옵션을 포함해 보았습니다. (지난번에 이미 한번 빌드를 해서 환경 구축을 했으므로 그 부분은 생략합니다.)

먼저, "시작" / "Microsoft Windows SDKK v6.1" / "CMD Shell"을 실행시키고, 다음과 같이 일련의 명령을 내려 보았는데,

D:\Program Files\Microsoft SDKs\Windows\v6.1>setenv /x86 /xp /release

D:\Program Files\Microsoft SDKs\Windows\v6.1>cd /d d:\phpbuild

d:\phpbuild>.\bin\phpsdk_setvars.bat

d:\phpbuild>REM phpsdk.bat
d:\phpbuild>cd D:\phpbuild\php53dev\vc9\x86\php-5.3.6

D:\phpbuild\php53dev\vc9\x86\php-5.3.6>configure --disable-all --enable-cli --enable-apache2-2handler --enable-apache2handler
Saving configure options to config.nice.bat
Checking for cl.exe ...  <in default path>
  Detected compiler MSVC9 (Visual C++ 2008)
  Detected 32-bit compiler
...[생략]...
-------------------------------------------
|               |                         |
-------------------------------------------
| Build type    | Release                 |
| Thread Safety | Yes                     |
| Compiler      | MSVC9 (Visual C++ 2008) |
| Architecture  | x86                     |
-------------------------------------------


Type 'nmake' to build PHP

D:\phpbuild\php53dev\vc9\x86\php-5.3.6>nmake

Microsoft (R) Program Maintenance Utility Version 9.00.30729.01
Copyright (C) Microsoft Corporation.  All rights reserved.

Recreating build dirs
   Creating library Release_TS\php5ts.lib and object Release_TS\php5ts.exp
SAPI sapi\cli build complete

결과는 실망스럽게도 아파치 핸들러 관련 DLL이 전혀 생성되지 않았습니다. 혹시 위의 글을 보고, 뭐가 잘못되었는지 아시는 분이 계시다면 댓글 부탁드립니다. ^^




PHP 빌드 중에 자연스럽게 php5apache2.dll / php5apache2_2.dll 파일을 생성하는 것은 일단 포기하고, D:\phpbuild\php53dev\vc9\x86\php-5.3.6\sapi\apache2handler 폴더에 있는 파일들을 가지고 직접 빌드하기로 했습니다. 보니까, makefile은 없고 php5apache2.dsp 파일이 보여서 Visual Studio 2008로 열었습니다.

로드한 후, 빌드 설정을 다음과 같이 "Debug_TS"에서 "Release_TS"로 변경해 줍니다. 만약, "Debug_TS"로 빌드하고 싶다면 기존 PHP 모듈을 Debug 모드로 빌드해 두는 것이 좋습니다. (그래야만 Debug_TS에서의 모든 경로 설정이 자연스럽게 연결됩니다.)

how_to_build_phphandler_1.png

곧바로 빌드하면 httpd.h/apr_strings.h 헤더 파일들을 찾을 수 없다고 나옵니다.

fatal error C1083: Cannot open include file: 'httpd.h': No such file or directory   d:\phpbuild\php53dev\vc9\x86\php-5.3.6\sapi\apache2handler2\php_apache.h
fatal error C1083: Cannot open include file: 'apr_strings.h': No such file or directory d:\phpbuild\php53dev\vc9\x86\php-5.3.6\sapi\apache2handler2\php_functions.c

옛날 같으면 당황했겠지만 ^^ 아파치 소스 코드를 빌드해 본 경험으로 어디서 찾을 수 있을지 바로 감이 왔습니다. 즉, 아파치 빌드 결과물의 include 폴더에 모두 있는 파일이었으므로 (lib 폴더와 함께) 다음과 같이 복사했습니다.

how_to_build_phphandler_2.png

D:\httpd_build\Apache22\include
==> D:\phpbuild\php53dev\vc9\x86\deps\include\apache2_2

D:\httpd_build\Apache22\lib
==> D:\phpbuild\php53dev\vc9\x86\deps\lib\apache2_2

이제, Visual Studio에 헤더 및 라이브러리 파일을 찾는 경로를 알려주면 되겠군요. ^^

[그림: Include 경로 포함 - "Configuration Properties" / "C/C++" / "Additional Include Directories"]
how_to_build_phphandler_3.png

[그림: Lib 경로 포함 - "Configuration Properties" / "Linker" / "Additional Library Directories"]
how_to_build_phphandler_4.png

이렇게 설정하고 다시 빌드하면 다음과 같은 오류가 발생합니다.

fatal error LNK1181: cannot open input file 'libapr.lib'

이상하군요. 아파치 빌드 결과물에는 libapr-1.lib 파일명으로 되어 있는 것을 왜 PHP 핸들러에서는 libarp.lib로 찾는지 모르겠습니다. 일단은, 프로젝트 속성 창에서 이름을 다음과 같이 바꿔주었습니다.

[그림: Lib 파일명 변경 - "Configuration Properties" / "Linker" / "Input" / "Additional Dependencies"]
how_to_build_phphandler_5.png

이제 빌드하고 나면 정상적으로 "D:\phpbuild\php53dev\vc9\x86\php-5.3.6\Release_TS" 폴더에 php5apache2.dll 파일명으로 DLL이 생성됩니다. 기왕 하는 김에 이름도 바꿔주면 좋겠군요. 프로젝트 속성창에서 "Linker" / "Output File" 설정을 ".\..\..\Release_TS/php5apache2_2.dll"으로 바꿔 주고 다시 빌드를 했습니다.

마침내, "D:\phpbuild\php53dev\vc9\x86\php-5.3.6\Release_TS" 폴더(디버그인 경우 Debug_TS)에 php5apache2_2.dll 파일을 얻어내는 데 성공했습니다. 휴~~~ 힘들군요. ^^;




물론 테스트를 해봐야겠지요. ^^

기존에 아파치 소스 코드로부터 빌드했던 테스트 폴더인 "D:\httpd_build\Apache22" 하위에 "phpmodule"을 두고 이전에 빌드한 php 및 이 글을 통해 새롭게 얻은 php5apache2_2.dll 파일을 복사하고 테스트용 PHP를 방문하면 정상적으로 로드되는 것을 확인할 수 있습니다.

디버깅까지 한번 해볼까요? ^^

역시, 아파치 모듈 디버깅을 했던 것처럼, 이제 프로젝트의 php5apache2_2.dll 빌드 결과물에 대한 출력 폴더를 "D:\httpd_build\Apache22\phpmodule"로 설정하고 디버깅 프로세스를 httpd.exe로 지정합니다.

참고로, 디버그 모드로 php5apache2_2.dll을 빌드하는 경우, 의존성이 다음과 같습니다.

  • PHP5TS_DEBUG.DLL
  • LIBHTTPD.DLL
  • LIBAPR-1.DLL
  • LIBAPRUTIL-1.DLL

"D:\httpd_build\Apache22\bin" 폴더에 해당 DLL들이 모두 있는지 확인한 다음 "Shift + F2" 키를 누르면 정상적으로 디버깅이 진행되는 것을 확인할 수 있습니다.

PHP 페이지를 웹 브라우저를 통해서 방문할 때마다 실행되는 메서드 - 즉 핸들러를 찾아볼까요?

Apache Module 컴파일 글을 읽으셨다면 어렵지 않습니다. mod_php5.c에 있는 php5_module을 통해서,

AP_MODULE_DECLARE_DATA module php5_module = {
    STANDARD20_MODULE_STUFF,
    create_php_config,      /* create per-directory config structure */
    merge_php_config,       /* merge per-directory config structures */
    NULL,                   /* create per-server config structure */
    NULL,                   /* merge per-server config structures */
    php_dir_cmds,           /* command apr_table_t */
    php_ap2_register_hook   /* register hooks */
};

php_ap2_register_hook 함수에 handler가 지정될 것임을 알 수 있고, 이어서 sapi_apache2.c 파일의 php_ap2_register_hook 함수로부터,

void php_ap2_register_hook(apr_pool_t *p)
{
    ap_hook_pre_config(php_pre_config, NULL, NULL, APR_HOOK_MIDDLE);
    ap_hook_post_config(php_apache_server_startup, NULL, NULL, APR_HOOK_MIDDLE);
    ap_hook_handler(php_handler, NULL, NULL, APR_HOOK_MIDDLE);
    ap_hook_child_init(php_apache_child_init, NULL, NULL, APR_HOOK_MIDDLE);
}

ap_hook_handler 함수 호출을 통해 php_handler가 우리가 찾던 PHP 웹 페이지 방문 시 실행되는 함수임을 알 수 있고, BP(BreakPoint)를 걸어서 확인할 수 있습니다. ^^

how_to_build_phphandler_6.png

첨부한 파일은 위와 같은 과정을 거쳐 생성된 프로젝트를 포함합니다.



[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]

[연관 글]






[최초 등록일: ]
[최종 수정일: 6/27/2021]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 




... 121  122  123  124  125  126  127  128  129  130  131  132  133  [134]  135  ...
NoWriterDateCnt.TitleFile(s)
1738정성태8/23/201423384.NET Framework: 456. C# - CAS를 이용한 Lock 래퍼 클래스파일 다운로드1
1737정성태8/20/201420839VS.NET IDE: 93. Visual Studio 2013 동기화 문제
1736정성태8/19/201426862VC++: 79. [부연] CAS Lock 알고리즘은 과연 빠른가? [2]파일 다운로드1
1735정성태8/19/201419372.NET Framework: 455. 닷넷 사용자 정의 예외 클래스의 최소 구현 코드 - 두 번째 이야기
1734정성태8/13/201421110오류 유형: 237. Windows Media Player cannot access the file. The file might be in use, you might not have access to the computer where the file is stored, or your proxy settings might not be correct.
1733정성태8/13/201427372.NET Framework: 454. EmptyWorkingSet Win32 API를 사용하는 C# 예제파일 다운로드1
1732정성태8/13/201435735Windows: 99. INetCache 폴더가 다르게 보이는 이유
1731정성태8/11/201428175개발 환경 구성: 235. 점(.)으로 시작하는 파일명을 탐색기에서 만드는 방법
1730정성태8/11/201423380개발 환경 구성: 234. Royal TS의 터미널(Terminal) 연결에서 한글이 깨지는 현상 해결 방법
1729정성태8/11/201419351오류 유형: 236. SqlConnection - The requested Performance Counter is not a custom counter, it has to be initialized as ReadOnly.
1728정성태8/8/201431595.NET Framework: 453. C# - 오피스 파워포인트(Powerpoint) 파일을 WinForm에서 보는 방법파일 다운로드1
1727정성태8/6/201421790오류 유형: 235. SignalR 오류 메시지 - Counter 'Messages Bus Messages Published Total' does not exist in the specified Category. [2]
1726정성태8/6/201420636오류 유형: 234. IIS Express에서 COM+ 사용 시 SecurityException - "Requested registry access is not allowed" 발생
1725정성태8/6/201422586오류 유형: 233. Visual Studio 2013 Update3 적용 후 Microsoft.VisualStudio.Web.PageInspector.Runtime 모듈에 대한 FileNotFoundException 예외 발생
1724정성태8/5/201427425.NET Framework: 452. .NET System.Threading.Thread 개체에서 Native Thread Id를 구하는 방법 - 두 번째 이야기 [1]파일 다운로드1
1723정성태7/29/201459797개발 환경 구성: 233. DirectX 9 예제 프로젝트 빌드하는 방법 [3]파일 다운로드1
1722정성태7/25/201422136오류 유형: 232. IIS 500 Internal Server Error - NTFS 암호화된 폴더에 웹 애플리케이션이 위치한 경우
1721정성태7/24/201425429.NET Framework: 451. 함수형 프로그래밍 개념 - 리스트 해석(List Comprehension)과 순수 함수 [2]
1720정성태7/23/201423400개발 환경 구성: 232. C:\WINDOWS\system32\LogFiles\HTTPERR 폴더에 로그 파일을 남기지 않는 설정
1719정성태7/22/201427239Math: 13. 동전을 여러 더미로 나누는 경우의 수 세기(Partition Number) - 두 번째 이야기파일 다운로드1
1718정성태7/19/201436676Math: 12. HTML에서 수학 관련 기호/수식을 표현하기 위한 방법 - MathJax.js [4]
1716정성태7/17/201436387개발 환경 구성: 231. PC 용 무료 안드로이드 에뮬레이터 - genymotion
1715정성태7/13/201431486기타: 47. 운영체제 종료 후에도 USB 외장 하드의 전원이 꺼지지 않는 경우 [3]
1714정성태7/11/201421552VS.NET IDE: 92. Visual Studio 2013을 지원하는 IL Support 확장 도구
1713정성태7/11/201445318Windows: 98. 윈도우 시스템 디스크 용량 확보를 위한 "Package Cache" 폴더 이동 [1]
1712정성태7/10/201433864.NET Framework: 450. 영문 윈도우에서 C# 콘솔 프로그램의 유니코드 출력 방법 [3]
... 121  122  123  124  125  126  127  128  129  130  131  132  133  [134]  135  ...