Mac OSX 下整合 Apache 和 Tomcat

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介: Tomcat, XAMPP, and mod_jk I recently upgraded to Snow Leopard (more on that later, it did not go so smoothly), and decided that rather than s...

Tomcat, XAMPP, and mod_jk

I recently upgraded to Snow Leopard (more on that later, it did not go so smoothly), and decided that rather than simply port my existing web development stack (which was rickety), I would try to build something clean and new that has all the features I need without eating all my available system resources.

My primary goal was to distance myself as much as possible from OS X’s flawed *AMP stack. Why flawed? Well, out-of-the-box, OS X doesn’t have MySQL, so you have to download, install, and manage that separately—a small pain in the ass, but a pain in the ass nonetheless. Further, the OS X compile of PHP is unnecessarily hobbled by a lack of image manipulation modules and is a missing the PDO module for MySQL. The last time I checked, it’s impossible to recompile PHP for OS X 10.5, which means that image manipulation is right out and installing PDO becomes far more difficult. I spent a weekend wrestling with recompiling MySQL with development headers just so I could build the PDO module for OS X and plug it in. Without PDO, my PHP object-relational mapper of choice (Doctrine PHP) simply won’t work.

My secondary goal is to have something that handles Tomcat Java server gracefully. In the past, I’ve spent a lot of time futzing about with settings to make Tomcat less of a resource-devouring monster, as well as integrating it more seamlessly with my local development environment. In general, I don’t appreciate appending a port number to the end of my URLs, and starting every new Java project with localhost:8080 seems like getting started with the wrong foot forward.

The tool to glue Apache and Tomcat together is mod_jk, a module that allows you to virtual host Tomcat webapps on your Apache server. I’ve played around with loading it with the OS X version of Apache in the past, but I never really got it working on my mac before. There was always one more configuration thing I was missing before I gave up and got back to doing real work.

In the past, I’ve been very impressed by the XAMPP project for OS X. It’s a simple drop-in replacement for the boxed OS X *AMP stack and it has almost every module I could ever want for web development. It comes with its own installation of MySQL, and both can be managed with a simple-but-robust control panel or a variety of shell scripts. Further, the files are laid out very simply in the xamppfiles directory, which makes it easy to find the configuration or log file that I’m looking for. It also means that if I want to get rid of XAMPP, I can be reasonably sure that it will be gone when I delete it from my Applications directory. Strangely, knowing I can cleanly delete a piece of software gives me confidence that it will operate correctly while it’s installed.

So, having settled on gluing XAMPP together with Tomcat via mod_jk, I set about downloading, installing, and stabbing at configuration files madly. The first thing I did was install XAMPP, which can be found at the Apache Friends OS X page, and the latest version of Tomcat, which can be found at the Tomcat Project page.

Tomcat

Tomcat comes in a tarball, which I extracted in my Downloads folder. I then kicked open Terminal and ran the following:

cd Downloads
mv Downloads/apache-tomcat-6.0.20 Tomcat
sudo mv Tomcat /Library/

Which places my Tomcat home directory in a place that’s easy to access when installing new webapps (new webapps will go in /Library/Tomcat/webapps). I also dragged the webapps directory into my sidebar for easy access. I then updated the tomcat-users.xml file in /Library/Tomcat/conf/ to allow me access to the Tomcat manager:

<tomcat-users>
<role rolename="manager" />
<user username="tomcat" password="admin" roles="manager" />
</tomcat-users>

I can now log into the manager app with username/password of tomcat/admin. You should probably pick something else, as I eventually did.

After that, I had to make sure the startup and shutdown scripts that Tomcat uses were executable. I also removed the scripts written for other platforms that I wouldn’t be using:

cd /Library/Tomcat/bin
rm *.bat *.exe

The next step is to write a more-robust startup/shutdown script. I wrote a simple shell script that’s accessible to me wherever I am that allows me to start and stop Tomcat quickly. In /usr/local/bin I added a file called tomcat and gave it executable privileges and opened my favorite text editor and added this inside it:

#!/bin/bash
case $1 in
start)
growlnotify -t "Tomcat" -m "Tomcat is starting up."
/Library/Tomcat/bin/startup.sh
;;
stop)
growlnotify -t "Tomcat" -m "Tomcat is shutting down."
/Library/Tomcat/bin/shutdown.sh
;;
restart)
growlnotify -t "Tomcat" -m "Tomcat is restarting."
/Library/Tomcat/bin/shutdown.sh
/Library/Tomcat/bin/startup.sh
;;
*)
echo "Usage: tomcat [ start | stop | restart ]"
;;
esac

Note the calls to growlnotify. If you have Growl installed and went out of your way to install the extras, you can call growlnotify on the command line to fire off an attractive growl notification. This is very handy if you want to embed calls to startup and shut down in other scripts (I often write a build script for java projects that shuts down Tomcat, installs the webapp, then starts Tomcat again). You can remove these lines if you aren’t interested in that, though. Save and close when you’re done—you’ll probably need to give your admin password.

Now, from anywhere in Terminal, you can call tomcat start, tomcat stop, or tomcat restart and expect it to perform those actions properly. You can check if Tomcat is running by visiting http://localhost:8080/

mod_jk

Now we have XAMPP working, and Tomcat working, but we want them to be working together, listening on the same port, without stepping on each other’s toes. To do this, we first need to acquire and install mod_jk, configure it, and then create the proper hostfile entry.

You can download mod_jk from the Apache Tomcat Connector project page. Download the macosx version for your processor; it should be named something like mod_jk-VERSION-httpd-VERSION.so. Make sure you download the one that’s appropriate for your version of Apache; In my case, I downloaded the httpd-2.2.4 version.

Rename the .so file to mod_jk.so and place it in /Applications/XAMPP/xamppfiles/modules.

The next step is getting mod_jk configured properly. To do this, we’re going to create a new configuration file in /Applications/XAMPP/xamppfiles/etc/extras called httpd-modjk.conf. In it, add the following:

LoadModule jk_module modules/mod_jk.so 
<IfModule jk_module> 
# Where to put jk shared memory 
JkShmFile     /Applications/XAMPP/xamppfiles/etc/mod_jk.shm 
# Where to put jk logs 
JkLogFile     /Applications/XAMPP/xamppfiles/logs/mod_jk.log 
# Set the jk log level [debug/error/info] 
JkLogLevel    info 
# Select the timestamp log format 
JkLogStampFormat "[%a %b %d %H:%M:%S %Y] " 
JkWorkerProperty worker.list=default 
JKWorkerProperty worker.default.type=ajp13 
JKWorkerProperty worker.default.host=localhost 
JKWorkerProperty worker.default.port=8009 
</IfModule> 
NameVirtualHost *:80 
<VirtualHost *:80> 
ServerName tomcat 
ServerAdmin root@localhost 
DocumentRoot "/Library/Tomcat/webapps" 
ErrorLog "logs/tomcat-error_log" 
CustomLog "logs/tomcat-access_log" common 
JkMount /* default 
</VirtualHost>

Then, somewhere near the end of your /Applications/XAMPP/xamppfiles/etc/httpd.conf add:

Include /Applications/XAMPP/etc/extra/httpd-modjk.conf

That will load the configuration file we just created.

The final step to get this all glued together involves editing your /etc/hosts file. Open it in your favorite text editor and add the following to the end of it:

127.0.0.1 tomcat

You should now be able to start up Tomcat (using tomcat start on the command line), start Apache (with XAMPP), and browse to http://tomcat/ and see the Tomcat welcome page.

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
2月前
|
Java 应用服务中间件 Apache
简介Nginx,Tomcat和 Apache
简介Nginx,Tomcat和 Apache
简介Nginx,Tomcat和 Apache
|
2月前
|
存储 Java 应用服务中间件
玩转Apache Tomcat
玩转Apache Tomcat
28 0
|
3月前
|
Oracle Java 关系型数据库
淇℃伅 [main] org.apache.catalina.startup.VersionLoggerListener.log Server.鏈嶅姟鍣ㄧ増鏈�: Apache Tomcat/8.5.
淇℃伅 [main] org.apache.catalina.startup.VersionLoggerListener.log Server.鏈嶅姟鍣ㄧ増鏈�: Apache Tomcat/8.5.
28 1
|
4月前
|
Java 应用服务中间件 Spring
Tomcat【部署 01】安装包版本说明+安装+参数配置+启动(JDK11+最新版apache-tomcat-10.0.12)
Tomcat【部署 01】安装包版本说明+安装+参数配置+启动(JDK11+最新版apache-tomcat-10.0.12)
63 0
|
4月前
|
Cloud Native 应用服务中间件 Apache
电子好书分享《Apache Tomcat 的云原生演进》
电子好书分享《Apache Tomcat 的云原生演进》
31 1
|
5月前
|
Java Apache 流计算
Mac 下安装Apache Flink
Mac 下安装Apache Flink
65 0
|
5月前
|
Cloud Native 安全 应用服务中间件
带你读《Apache Tomcat的云原生演进》——卷首语
带你读《Apache Tomcat的云原生演进》——卷首语
|
5月前
|
Cloud Native 应用服务中间件 Apache
带你读《Apache Tomcat的云原生演进》——Tomcat的技术内幕和在喜马拉雅的实践(1)
带你读《Apache Tomcat的云原生演进》——Tomcat的技术内幕和在喜马拉雅的实践(1)
117 1
带你读《Apache Tomcat的云原生演进》——Tomcat的技术内幕和在喜马拉雅的实践(1)
|
5月前
|
Cloud Native Java 应用服务中间件
带你读《Apache Tomcat的云原生演进》——Tomcat的技术内幕和在喜马拉雅的实践(2)
带你读《Apache Tomcat的云原生演进》——Tomcat的技术内幕和在喜马拉雅的实践(2)
125 0
带你读《Apache Tomcat的云原生演进》——Tomcat的技术内幕和在喜马拉雅的实践(2)
|
5月前
|
缓存 Cloud Native 应用服务中间件
带你读《Apache Tomcat的云原生演进》——Tomcat的技术内幕和在喜马拉雅的实践(3)
带你读《Apache Tomcat的云原生演进》——Tomcat的技术内幕和在喜马拉雅的实践(3)
117 0
带你读《Apache Tomcat的云原生演进》——Tomcat的技术内幕和在喜马拉雅的实践(3)

推荐镜像

更多