Microsoft MVP성태의 닷넷 이야기
VC++: 49. 소스 코드로부터 php5apache2_2.dll 생성하는 방법 [링크 복사], [링크+제목 복사],
조회: 22226
글쓴 사람
정성태 (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

비밀번호

댓글 작성자
 




[1]  2  3  4  5  6  7  8  9  10  11  12  13  14  15  ...
NoWriterDateCnt.TitleFile(s)
13617정성태5/5/2024324닷넷: 2257. C# - float (단정도 실수) 저장소의 비트 구조파일 다운로드1
13616정성태5/3/2024335닷넷: 2256. ASP.NET Core 웹 사이트의 HTTP/HTTPS + Dual mode Socket (IPv4/IPv6) 지원 방법파일 다운로드1
13615정성태5/3/2024276닷넷: 2255. C# 배열을 Numpy ndarray 배열과 상호 변환
13614정성태5/2/2024353닷넷: 2254. C# - COM 인터페이스의 상속 시 중복으로 메서드를 선언
13613정성태5/1/2024601닷넷: 2253. C# - Video Capture 장치(Camera) 열거 및 지원 포맷 조회파일 다운로드1
13612정성태4/30/2024679오류 유형: 902. Visual Studio - error MSB3021: Unable to copy file
13611정성태4/29/2024800닷넷: 2252. C# - GUID 타입 전용의 UnmanagedType.LPStruct - 두 번째 이야기파일 다운로드1
13610정성태4/28/2024872닷넷: 2251. C# - 제네릭 인자를 가진 타입을 생성하는 방법 - 두 번째 이야기
13609정성태4/27/2024961닷넷: 2250. PInvoke 호출 시 참조 타입(class)을 마샬링하는 [IN], [OUT] 특성파일 다운로드1
13608정성태4/26/20241042닷넷: 2249. C# - 부모의 필드/프로퍼티에 대해 서로 다른 자식 클래스 간에 Reflection 접근이 동작할까요?파일 다운로드1
13607정성태4/25/20241060닷넷: 2248. C# - 인터페이스 타입의 다중 포인터를 인자로 갖는 C/C++ 함수 연동
13606정성태4/24/20241005닷넷: 2247. C# - tensorflow 연동 (MNIST 예제)파일 다운로드1
13605정성태4/23/2024985닷넷: 2246. C# - Python.NET을 이용한 파이썬 소스코드 연동파일 다운로드1
13604정성태4/22/2024994오류 유형: 901. Visual Studio - Unable to set the next statement. Set next statement cannot be used in '[Exception]' call stack frames.
13603정성태4/21/20241038닷넷: 2245. C# - IronPython을 이용한 파이썬 소스코드 연동파일 다운로드1
13602정성태4/20/20241016닷넷: 2244. C# - PCM 오디오 데이터를 연속(Streaming) 재생 (Windows Multimedia)파일 다운로드1
13601정성태4/19/20241036닷넷: 2243. C# - PCM 사운드 재생(NAudio)파일 다운로드1
13600정성태4/18/20241081닷넷: 2242. C# - 관리 스레드와 비관리 스레드
13599정성태4/17/2024986닷넷: 2241. C# - WAV 파일의 PCM 사운드 재생(Windows Multimedia)파일 다운로드1
13598정성태4/16/20241036닷넷: 2240. C# - WAV 파일 포맷 + LIST 헤더파일 다운로드2
13597정성태4/15/20241063닷넷: 2239. C# - WAV 파일의 PCM 데이터 생성 및 출력파일 다운로드1
13596정성태4/14/20241140닷넷: 2238. C# - WAV 기본 파일 포맷파일 다운로드1
13595정성태4/13/20241089닷넷: 2237. C# - Audio 장치 열기 (Windows Multimedia, NAudio)파일 다운로드1
13594정성태4/12/20241115닷넷: 2236. C# - Audio 장치 열람 (Windows Multimedia, NAudio)파일 다운로드1
13593정성태4/8/20241136닷넷: 2235. MSBuild - AccelerateBuildsInVisualStudio 옵션
[1]  2  3  4  5  6  7  8  9  10  11  12  13  14  15  ...