원문은 https://github.com/yakmoz/ref/tree/master/apach_tomcat_os_x_mod_jk 에 있음. 업데이트는 github 문서에만 적용됩니다. 

OS X (10.9.2 mavericks) 에서의 아파치 톰캣 연동

원래 보통 작은 규모의 사이트들이나, 엔지니어 혹은 개발자들이 연습삼아 apache 를 앞단에 두고(80포트) 자바 웹 서비스는 WAS (여기서는 톰캣)로 보내는 형태를 만들어 이용했다.

여기의 문서는 보통 다른 문서의 목표와 다르다. 이 문서의 목표는 아파치로 오는 요청을 죄다 톰캣으로 보내게 설정할 것이다. 보통은 그렇게 하지 않는다. 그러므로 원하는 내용에 맞지 않을 수 있으니 그에 맞는 메일을 찾거나 이 내용을 보고 학습하여 수정하기 바란다. 레퍼런스는 인터넷에 널렸다. :) gook luck~

준비물

  • 아파치 (이미 설치되어 있다)
  • 톰캣 (homebrew 로 설치할 것이다)
  • mod_jk 아파치에 설치될 모듈로 톰캣과 이어주는 역할을 한다. 이에 대한 추가설명은 따로 하겠다.

톰캣 설치

톰캣의 경우는 맥의 패키지 관리툴인 Homebrew 를 이용해서 설치했다. Homebrew 의 사용법은 홈페이지에서 확인바란다. Juntai Park 님이 한글화에 참여하여 홈페이지를 무려 한글로!!! 볼 수 있으니 겁먹지 마시라.

homebrew 가 이미 설치되어있다면 간단히

brew install tomcat 

로 설치하면 된다.

그렇게 해서 설치되는 버전은 7버전대이며, 패스가 잡히는 곳에 sh 이 존재하므로 catalina 해보면 설명이 나오는걸 볼 수 있다.

brew 로 설치하는 이유는, 리눅스등을 써보신분은 알겠지만 패키지 관리자를 이용하면 업데이트나 설치/제거등이 용이하다. 패키지 관리자 없이 설치한 패키지가 업데이트될때마다 하나하나 지우고 깔거나 업데이트,패치 등을 찾아서 적용하는것은 상당히 번거러운 작업이다.

과거에는 macport 등을 썼었지만 현재는 homebrew 로 대동단결이라고 봐도 된다.

mod_jk.so

아파치와 톰캣을 이어주는 방법은 한가지가 아니다. mod_jk 를 이용한 방법과 proxy 를 이용한 방법등이 있다. 여기서는 mod_jk 를 이용할 것이다.

아파치는 여러 모듈을 추가하여 기능을 확장할 수 있다. 그 확장모듈중 하나가 mod_jk 인데 파일로는 mod_jk.so 파일이다. 문제는 해당 so파일 ... 즉 dynamic lib 파일은 컴파일된 바이너리 파일로 보통 제공된다. 그 대상들은 널리 쓰이는 윈도나 리눅스등이다. 즉 맥버전으로 컴파일되어 생성된 바이너리를 mod_jk 를 제공하는 아파치사이트에서는 찾아볼 수 없다. 소스만 있을뿐이다.

해서 맥유저는 해당 소스를 받아와서 직접 컴파일하여 so 파일을 생성해야한다.

해당 글은 별도로 작성해놨으니 참고 바란다. 링크

그 글에 따라 직접 컴파일 해도 되고, 아니면 컴파일 해놓은(매버릭스 버전) so 파일을 그냥 다운로드 해서 써도 된다.

아파치의 구조

아파치는 나름 복잡하다. 상용에서도 많이 쓰이는 웹서버이다. 사실상 입구를 담당하고 그뒤로 WAS 에게 포워딩 하는 구조로 많이 쓰인다.

아파치의 설정은 httpd.conf 에 다 담을수도 있지만 설정을 파일로 할 수도 있다. 우리는 앞으로 JkWorkersFile 파일과 JkMountFile 파일에 설정을 쓰게 하고 그걸 일게 아파치설정을 할 것이다.

해당 파일들에 대한 설명은 아파치 톰캣 커넥터 글을 읽으면 알 수 있다.

우선 살펴보자면

Directive설명
JkWorkersFile톰캣 서블릿 컨테이너를 위한 worker 파일 이름이다. 설정의 글로벌 파트에 해당하며 선언은 오직 한번만 허용된다. 만약JkWorkerProperty 설정을 이용하지 않으면, 올바른 JkWorkersFile 파일을 정의해야한다. 기본값은 없다.
JkMountFile여러개의 맵핑정보를 worker 에게 매핑해주는 정보를 담은 파일 . 보통 uriworkermap.properties 파일로 불리운다. 상속에 대한 룰은 JkMountCopy 를 살펴보라. 기본값은 없다.

이런 설정을 이용할 것이다.

설정을 위한 기본위치

Apache

  • 설정 : /etc/apache2/이하
  • 모듈(.so) : /usr/libexec/apache2 이하

설정

  • 우선 httpd.conf 파일을 열어서 LoadModule 들이 설정되어 있는 라인에가서
LoadModule jk_module libexec/apache2/mod_jk.so

내용을 추가한다. 물론 해당 .so 파일이 설정에서 가리키는 위치에 있어야 한다.

  • jk_module 설정을 외부로 빼도 되고 그냥 httpd.conf 에 넣어도 된다.
<IfModule jk_module>
    JkWorkersFile /etc/apache2/workers.properties
    JkLogFile     /var/mod_jk.log
    JkLogLevel    info
    JkMountFile   /etc/apache2/uriworkermap.properties
</IfModule>
  • 해당 설정을 보면 앞에서 언급한 JkWorkersFile 과 JkMountFile ,2가지 설정을 이용하는걸 알 수 있다. 그설정들에 정의된 파일, 2개를 수정할것이다. 아마 2개의 파일은 이미 존재하고 있을것이다.

우선 workers.properties 를 수정하다.worker1 이라고 선언하고 내용을 설정한다.

worker.list=worker1
worker.worker1.port=8009
worker.worker1.host=localhost
worker.worker1.type=ajp13
worker.worker1.lbfactor=1
  • 다음 uriworkermap.properties 를 수정하자.
/*=worker1
/*.jsp=worker1

사실 jsp 만 톰캣에 가도 되는데 그냥 /* 로 다 넘기고자 했다. woker1 으로 맵핑해둔다.

서버재시작

톰캣을 기동하고, 아파치를 기동/재기동 할 것이다. 아파치의 경우는 아마 sudo 를 앞에 붙여야 제대로 될 것이다. (ex. sudo apachectl start)

  • 톰캣 기동
catalina start

만약 그런파일이 없거나.. 등 패스에 걸려있지 않다고 보이면 직접 가서 실행하면 된다. 보통은 /usr/local/bin 에 링크로 존재한다.

다음은 아파치을 기동 혹은 재기동하자.

  • 아파치 기동
apachectl stop
apachectl start

실행되고 있지 않았다면 stop 시에 에러가 날 것이다. 무시하고 start 하자.

이렇게 하고 우선 톰캣이 제대로 떴는지 보기위해 http://localhost:8080 으로 접근해보자. 톰캣페이지가 잘 보인다면 톰캣은 잘 뜬것이다. (8080 포트를 변경했다면 바꾼 포트로 접근바란다)

다음 http://localhost 로 접근해보자. 만약 it worked 던가가 나오면 다시 http://localhost/a.jsp 도 한번 해보기 바란다. 파일이 없을테니 404 가 보일텐데 잘보면 아파치 톰캣의 404 란걸 알 수 있다.

그럼 우선 jsp 파일을 제대로 톰캣에 보낸건 맞다. 그냥 / 도 톰캣에 보내게 해놨기에 localhost 로 접근해도 톰캣이 나와야 한다.

본인의 경우는 잘 됐는데 잘 안된다면 하나하나 확인하거나 다른 가이드 문서나 아파치 설정 문서를 잘 보기 바란다.



from http://hints.macworld.com/article.php?story=20010117003458918

위의 내용을 참고해서 아래를 붙여넣어 테스트 결과 잘됨. (포트는 80으로 되더라는...)

(


<IfModule mod_proxy.c>

    ProxyRequests On


    <Directory proxy:*>

        Order deny,allow

        #Deny from all

        Allow from all

        #NOTE: Replace '.your_domain.com' with your IP number(s)!

    </Directory>

    #

    # Enable/disable the handling of HTTP/1.1 "Via:" headers.

    # ("Full" adds the server version; "Block" removes all outgoing Via: headers)

    # Set to one of: Off | On | Full | Block

    #

    ProxyVia On

</IfModule>


-----------------------------------------------------------------------------------------


아래는 전체 메시지이다.


How and why to use the Apache proxy server Apps
Even if you have no interest in serving web pages from your new OS X box, there's at least one feature of Apache (the built-in web server) that you might want to put to use - the proxy server. 

A proxy server is nothing more than a server which sits between a client (such as a web browser) and a real server (such as a web host). It intercepts all requests sent by the client and decides if it can handle the request itself. If it cannot, it then passes the request on to the real server.

Why might you find this useful? There are two primary reasons. First, if you're a parent, you can use the proxy server to control which sites your kids can and cannot have access to. This may make you feel slightly more comfortable leaving them alone in front of the machine ... although any child with some level of net experience will be able to find ways to get what they wanted anyway. 

Since the proxy will block sites that you specify, you can also use it to block ad servers such as www.doubleclick.net (and there goes any chance of ever having advertisers on this site ... want to get blacklisted ... just explain how to block ad servers! ;-) 

The second usage is for caching web content locally. If you have a connection that's shared between multiple computers, you can use the proxy to store pages locally. That way, if you browse cnn.com and your spouse visits the site 30 seconds later from another machine, they will get a locally cached page which will be served very quickly. 

Read the rest of this article if you'd like instructions on setting up Apache's proxy server.

The first step is to make sure your web server is stopped. You can do this through the System Preferences -> Network -> Services tab, or via the terminal. Since we're going to do the rest of this in a terminal session, you might as well start there. Open a terminal, and login as root ("su" and enter your password). Then type
apachectl stop
This will stop the webserver. 

Once Apache is stopped, you'll want to make a backup copy of your configuration file, in case you make a mistake:
cd /Library/WebServer/Configuration
cp apache.conf apache.bak
Note that a backup file may already exist; you can use a new name if you'd like to keep everything intact. 

Pick your favorite editor, and edit the file named "apache.conf". I'm using vi, but emacs and pico will also work. 

First we need to load and add the proxy module to the server. Find the section (it's around line 231 in my config):
#LoadModule digest_module      /System/Library/Apache/Modules/mod_digest.so
#LoadModule proxy_module /System/Library/Apache/Modules/libproxy.so
#LoadModule cern_meta_module /System/Library/Apache/Modules/mod_cern_meta.so
Remove the comment ("#") from the second line. 

Now search for this section (it's around line 269 in my file):
#AddModule mod_digest.c
#AddModule mod_proxy.c
#AddModule mod_cern_meta.c
Again, remove the comment mark from the second line. 

These two steps activated the proxy server module. Now we need to do some basic configuration. Search for the section that begins as follows (it's around line 937 in my file):
# Proxy Server directives. Uncomment the following lines to
# enable the proxy server:
Uncomment all the lines that are code (not comments). When you are done, it should look like this:
# Proxy Server directives. Uncomment the following lines to
# enable the proxy server:
#
<IfModule mod_proxy.c>
ProxyRequests On

<Directory proxy:*>
Order deny,allow
#Deny from all
Allow from .your_domain.com
#NOTE: Replace '.your_domain.com' with your IP number(s)!
</Directory>
#
# Enable/disable the handling of HTTP/1.1 "Via:" headers.
# ("Full" adds the server version; "Block" removes all outgoing Via: headers)
# Set to one of: Off | On | Full | Block
#
ProxyVia On
#
# To enable the cache as well, edit and uncomment the following lines:
# (no cacheing without CacheRoot)
#
CacheRoot "/Library/WebServer/ProxyCache"
CacheSize 5
CacheGcInterval 4
CacheMaxExpire 24
CacheLastModifiedFactor 0.1
CacheDefaultExpire 1
NoCache a_domain.com another_domain.edu joes.garage_sale.com
</IfModule>
# End of proxy directives.
Notice two small things. First, Deny from all is left commented out -- otherwise, you wouldn't be able to browse the web at all! Second, you need to enter the IP numbers that you will allow to use the proxy server in the Allow from line. If you have more than one, enter them separated by a space. 

That's it for basic configuration. The cache settings are all very tweakable, and I highly recommend reading this article on the apache.org website for full details on all the variables and what they mean. 

If you want to use the proxy server to block sites, you need to add the ProxyBlock directive. This command can go basically anywhere in the apache.conf file, but I added it in the proxy section, so that it would be easy to find. The format is simple, as you can see in this example:
ProxyBlock playboy.com penthouse.com adserver.ugo.com
Any sites listed will be blocked from browsing, and return errors when called from within pages, as you can see here:

 

Once you've added the sites you would like blocked (note that it's useful for debugging to include one at first, so you can verify the proxy server is working correctly), save your changes and then restart the webserver with:
apachectl start
Remember to then disconnect from your root editing session! 

The final step is to tell your browser that you want to use the proxy server to serve your web pages. In Explorer, go into Edit-Preferences and the Proxies tab, and check the Web Proxy box. Enter 127.0.0.1 as the server address. You should now be good to go; test it by trying to browse one of the sites you listed in the ProxyBlock statement. You should get a message like this: 

Proxy Error 
You don't have permission to access http://www.playboy.com/ on this server. 

Although this is a long tutorial, setting up a proxy server is relatively trivial, and could save you some time and relieve some of your worries about what sites your kids are hitting in your absence.
    •     
  (16 votes cast)  
[49,788 views]  

How and why to use the Apache proxy server | 4 comments | Create New Account
  
Click here to return to the 'How and why to use the Apache proxy server' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Much easier way
Authored by: slava_pestov on Jan 20, '01 12:28:07AM

A much easier way to block web sites that works with Linux and might work with OSX as well is to edit your /etc/hosts file and add lines like the following to it:

0.0.0.0 www.site-to-block.com
0.0.0.0 www.another-site-to-block.com

and so on.

changes to /etc/hosts take effect in subsequently-running applications; already running ones are unaffected, as the file is only read when apps are started.

403 Forbidden error
Authored by: johnnydaboy on Jan 06, '02 12:11:08AM

I tried this and now all I get are 403 Forbidden errors when I try to surf through the proxy server. The Apache log shows the following line:

127.0.0.1 - - [05/Jan/2002:23:14:47 -0500] "GET http://www.nytimes.com/ HTTP/1.0" 403 285

I suppose the 127.0.0.1 localhost is the address of the requesting "machine," in this case, the proxy server. I don't know if that is correct -- the supposition or the setting. The part that I am merely curious about is the HTTP/1.0 part. I thought everything was up to HTTP 1.1. Ideas?

If it's of any consequence, I have DSL and a Linksys router with static addressing.

Can anyone lend me a hand?

Nevermind - 403 Forbidden error
Authored by: johnnydaboy on Jan 07, '02 08:33:24PM

I set up the proxy server on the machine that I work on, just to do it and see if it would work. I set the proxy server to the loopback address but I didn't <Allow> the loopback address in the proxy config file. Duh. Hope this helps anyone else trying this.

This is not a two star essay!
Authored by: jfaughnan on Mar 04, '10 07:22:42PM

Out of 22K page views and top rank on Google for OS X Proxy and it gets 1 star?! 

Problem is only four people voted and I suspect they have personal reasons not to like proxy servers (sorry kids).

I gave it top ranking. If you like it, thank the author.

---
--
John Faughnan
jfaughnan@spamcop.net
www.faughnan.com


+ Recent posts