详解FreeBSD 8下部署Apache+MySQL+PHP 5

众所周知,在生产环境下配置Apache+PHP 5环境或Nginx+PHP 5环境还是很复杂的,即大家熟悉的LAMP或NAMP架构,就算按照详细的部署文档来安装的话也是一个较长和麻烦的过程;但这一切在FreeBSD下用ports就简化了,本人感觉特别适应于开发环境,特将FreeBSD 8下的部署过程记录如下,方便与大家交流共享。

一、安装Apache2.2

cd /usr/ports/www/apache22
make install clean

去掉IPV6
添加MySQL

安装完成后,需要编辑/etc/rc.conf文件,添加下面内容到/etc/rc.conf:

apache22_enable="YES"

配置Apache 2.2

安装完成后,备份/usr/local/etc/apache22/httpd.conf文件:

cp /usr/local/etc/apache22/httpd.conf /usr/local/etc/apache22/httpd.conf.bak

编辑/usr/local/etc/apache22/httpd.conf文件,以使Apache22 Server支持PHP:

vim /usr/local/etc/apache22/httpd.conf

1.大约354行

在AddType application/x-gzip .gz .tgz下面添加以下内容:

AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps

2.大约212行

添加’index.php’ 到’DirectoryIndex’ 主目录索引:

DirectoryIndex index.php index.html

3.第152行

DocumentRoot "/home/www"  #存放web的路径,根据自己需求更改

4.第178行

<Directory "/home/www">   #存放web的路径,这个根据自己需求更改

二、安装PHP 5.2

cd /usr/ports/lang/php52
make install clean

选择如下安装选项:

去掉IPV6
添加CLI、CGI、APACHE、SUHOSION、FASTCGI、PATHINFO

三、安装PHP 5.2-extensions扩展

cd /usr/ports/lang/php52-extensions
make config
make install clean

make config安装添加选项如下:

添加BZ2、CALENDAR、CTYPE、GD、GETTEXT、ICONV、MBSTRING、MCRYPT、MHASH、MYSQL、MYSQLI、OPENSSL、PCRE、POSIX、SESSION、SOCKETS、TOKENIZER、ZIP、ZLIB。

我在线上服务器的centos5.4下,一个个手动源码编译PHP 5的扩展包,感觉是件非常痛苦的事情;Apache如是,Nginx下亦如是;在FreeBSD 8下一切都简单了,这个过程有点长。

复制/usr/local/etc/php.ini-dist为/usr/local/etc/php.ini:

cp /usr/local/etc/php.ini-dist /usr/local/etc/php.ini

四、安装ZendOptimizer

cd /usr/ports/devel/ZendOptimizer
make install clean

安装完成提示如下:

********************************************************************************
You have installed the ZendOptimizer package.
Edit /usr/local/etc/php.ini and add:
[zend]
zend_optimizer.optimization_level=15
zend_extension_manager.optimizer="/usr/local/lib/php/20060613/Optimizer"
zend_extension_manager.optimizer_ts="/usr/local/lib/php/20060613/Optimizer_TS"
zend_extension="/usr/local/lib/php/20060613/ZendExtensionManager.so"
zend_extension_ts="/usr/local/lib/php/20060613/ZendExtensionManager_TS.so"
*********************************************************************

编辑php.ini,复制以上[Zend]内容到文本末尾:

vim /usr/local/etc/php.ini

重新启动你的Web Server.

/usr/local/etc/rc.d/apache22 restart

测试Apche Server对PHP和ZendOptimizer支持,在/home/www里面建立一个index.php,输入以下内容:

<?
phpinfo();
?>

我这里安装此版本为5.2.13,新的PHP已近更新至5.3.3,但是老版本的ZendOptimizer对其不支持,所以本着稳定就好的原则,继续使用5.2.13;

五、安装MySQL Server5.0

cd /usr/ports/databases/mysql50-server
make install clean

配置MySQL

安装完成后,需要编辑/etc/rc.conf文件,添加下面内容到/etc/rc.conf:

mysql_enable="YES"

复制文件:

cp /usr/local/share/mysql/my-medium.cnf /etc/my.conf

启动MySQL服务:

/usr/local/etc/rc.d/mysql-server start

新装的mysql是没有密码的,使用mysqladmin命令更改MySQL密码:

/usr/local/bin/mysqladmin -uroot password ‘你的密码’

例如:/usr/local/bin/mysqladmin -uroot password 12345678

六、安装PHPmyadmin

1.安装

cd /usr/ports/databases/phpmyadmin
make fetch  #只下载,不安装

PHPmyadmin解压以后直接使用了:

cp /usr/ports/distfiles/phpMyAdmin-3.3.2-all-languages.tar.bz2 /home/www    #/home/www为apache指定路径
tar zxvf phpMyAdmin-3.3.2-all-languages.tar.bz2
mv phpMyAdmin-3.3.2-all-languages phpmyadmin      #phpmyadmin文件名就为外部地址路径http://xxx.xxx.xxx.xxx/phpmyadmin

2.配置

cd /home/www/phpmyadmin
cp config.sample.inc.php  config.inc.php

3.修改

ee config.inc.php
cfg[‘blowfish_secret’]=’host’;            #随便输入,不要留空这个一定要设置
cfg[‘Servers’][$i][‘auth_type’]=’cookie’#设置认证方式,默认即可

4.设置权限

chmod 755 config.inc.php

这时候你就可以在内网用root等用户进行登陆管理了,PHPMyadmin的强大是有目共睹的,这也是它现在作为Linux/unix发行版的默认软件之一。

值得注意的是,基于线上环境的严谨性,我一般采用64位的Cenots系统,软件均采用源码安装;而以上所述,均是出于开发环境的考虑,即快速方便的部署测试服务器,如果用于线上环境,还需要注意的细巧还有许多,比如PHP要禁用危险的函数、Apache要考虑其Web安全及SSL证书(一个不小心就要考虑支持多域名的SSL证书)等、MySQL要考虑生产环境下的压力及备份等,这里细节繁多就不细述了。

原文链接:http://developer.51cto.com/art/201010/231233.htm

FreeBSD最小化安装+diablo-jdk+Apache与Tomcat整合

这篇文章算是补交很久之前的一篇作业,也顺便平息坛子里面很多同学的疑问!
我先说明一下配置环境的大概情况:

FreeBSD 6.2-RELEASE
diablo-jdk-1.5.0.07.01
apache-2.2.3
tomcat-5.5.23

具体的版本信息显示如下:

[root@FreeBSD ~] $ pkg_info
apache-2.2.3        Version 2.2 of Apache web server with prefork MPM.
bash-3.1.17         The GNU Project’s Bourne Again SHell
cvsup-without-gui-16.1h_2 General network file distribution system optimized for CVS
diablo-jdk-1.5.0.07.01 Java Development Kit 1.5.0_07.01
expat-2.0.0_1       XML 1.0 parser written in C
fontconfig-2.3.2_6,1 An XML-based font configuration API for X Windows
freetype2-2.2.1_1   A free and portable TrueType font rendering engine
gettext-0.14.5_2    GNU gettext package
gmake-3.81_1        GNU version of ‘make’ utility
javavmwrapper-2.3   Wrapper script for various Java Virtual Machines
libdrm-2.0.2        Userspace interface to kernel Direct Rendering Module servi
libiconv-1.9.2_2    A character set conversion library
mod_jk-ap2-1.2.21,1 Apache JK module for connecting to Tomcat using AJP1X
perl-5.8.8          Practical Extraction and Report Language
pkg-config-0.21     A utility to retrieve information about installed libraries
tomcat-5.5.23       Open-source Java web server by Apache, 5.5.x branch
xorg-libraries-6.9.0 X11 libraries and headers from X.Org

好,来看看具体的做法吧!
第一步:安装FreeBSD,采取最小化安装的步骤。
这里没有什么特别的地方,只是提醒一下,安装完成后注意要更新到最新的ports tree !
由于是最小化安装,所以需要通过 sysinstall 去把下面几个package安装上(via CD-ROM) :

cvsup-without-gui
perl
gmake
bash

完成安装后,更新ports tree,我使用了cvsup.FreeBSDChina.org 作为镜像!
第二步:安装 JDK -FreeBSD 发布的 JDK 1.5 软件包
罗嗦一句,从源代码编译JDK的时代已经过去了,我是懒人,所以喜欢用pkg_add !
如果你想从ports来编译JDK,可以看看这里:FreeBSD中如何编译JDK,以FreeBSD 5.x/JDK 1.4.x为例!URL:http://www.freebsdchina.org/forum/viewtopic.php?t=30353

2.1 下载diablo-jdk的软件包:diablo-jdk-freebsd6.i386.1.5.0.07.01.tbz
2.2 查找diablo-jdk的依赖项:

[root@FreeBSD /usr/ports] $ make search name=diablo-jdk-1.5.0.07.01
Port:   diablo-jdk-1.5.0.07.01
Path:   /usr/ports/java/diablo-jdk15
Info:   Java Development Kit 1.5.0_07.01
Maint:  glewis@FreeBSD.org
B-deps: expat-2.0.0_1 fontconfig-2.3.2_6,1 freetype2-2.2.1_1 libdrm-2.0.2 pkg-config-0.21 xorg-libraries-6.9.0
R-deps: expat-2.0.0_1 fontconfig-2.3.2_6,1 freetype2-2.2.1_1 javavmwrapper-2.3 libdrm-2.0.2 pkg-config-0.21 xorg-libraries-6.9.0
WWW:    http://www.FreeBSDFoundation.org/downloads/java.shtml

可以看到有如下几个packages需要安装:

expat-2.0.0_1
fontconfig-2.3.2_6,1
freetype2-2.2.1_1
libdrm-2.0.2
pkg-config-0.21
xorg-libraries-6.9.0
javavmwrapper-2.3

2.3 从网络安装diablo-jdk所需要的软件包 (并非ports的编译安装方式)

[root@FreeBSD ~] $ setenv PACKAGESITE ftp://ftp.freebsdchina.org/pub/FreeBSD/ports/i386/packages-6.2-release/Latest/
[root@FreeBSD ~] $ pkg_add -r expat
[root@FreeBSD ~] $ pkg_add -r freetype
[root@FreeBSD ~] $ pkg_add -r fontconfig
[root@FreeBSD ~] $ pkg_add -r … 省略 …
[root@FreeBSD ~] $ pkg_add -r xorg-libraries
[root@FreeBSD ~] $ pkg_add -r javavmwrapper

注意:这里用pkg_add的时候,不带软件包的版本号!
* 有没有Debian/Ubuntu中的apt-get的感觉呀? ^^!
2.4 安装diablo-jdk,直接找到我们下载的jdk软件包文件,用pkg_add添加:

[root@FreeBSD ~] $ pkg_add diablo-jdk-freebsd6.i386.1.5.0.07.01.tbz
[root@FreeBSD ~] $ /usr/local/diablo-jdk1.5.0/bin/java -version
java version “1.5.0”
Java(TM) 2 Runtime Environment, Standard Edition (build diablo-1.5.0-b01)
Java HotSpot(TM) Client VM (build diablo-1.5.0_07-b01, mixed mode)

提醒:

安装的时候,会出来SUN的许可协议,同意即可.
安装时会说javawmwrapper版本有点高,可以不用理会.

第三步:安装Tomcat
3.1 抓tomcat的档案下来:

[root@FreeBSD ~] $ cd /usr/ports/www/tomcat55
[root@FreeBSD tomcat55] $ make
===>  Vulnerability check disabled, database not found
=> apache-tomcat-5.5.23.tar.gz doesn’t seem to exist in /usr/ports/distfiles/.
=> Attempting to fetch from http://www.apache.org/dist/tomcat/tomcat-5/v5.5.23/bin/.
apache-tomcat-5.5.23.tar.gz                   100% of 5837 kB   16 kBps 00m00s
===>  Extracting for tomcat-5.5.23
=> MD5 Checksum OK for apache-tomcat-5.5.23.tar.gz.
=> SHA256 Checksum OK for apache-tomcat-5.5.23.tar.gz.
===>  Patching for tomcat-5.5.23
Installation settings:
Destination directory:    /usr/local/tomcat5.5
Location of JDK:          /usr/local/diablo-jdk1.5.0
Location of Java port:    java/diablo-jdk15
Running as (user/group):  www/www
HTTP port:                8180
Shutdown listener port:   8005
AJP 1.3 connector port:   8009
Logfile stdout:           /usr/local/tomcat5.5/logs/stdout.log
Logfile stderr:           /usr/local/tomcat5.5/logs/stderr.log
===>  Applying FreeBSD patches for tomcat-5.5.23
>> Removing unneeded files… [ DONE ]
>> Customizing server.xml… [ DONE ]
===>  Configuring for tomcat-5.5.23

3.2 安装tomcat

[root@FreeBSD tomcat55] $ make install
===>  Installing for tomcat-5.5.23
===>   tomcat-5.5.23 depends on file: /usr/local/diablo-jdk1.5.0/bin/java – found
===>   Generating temporary packing list
===>  Checking if www/tomcat55 already installed
>> Creating destination directory… [ DONE ]
>> Copying files to destination directory…
Installing local configuration file: /usr/local/tomcat5.5/conf/Catalina/localhost/host-manager.xml
Installing local configuration file: /usr/local/tomcat5.5/conf/Catalina/localhost/manager.xml
Installing local configuration file: /usr/local/tomcat5.5/conf/catalina.policy
Installing local configuration file: /usr/local/tomcat5.5/conf/catalina.properties
Installing local configuration file: /usr/local/tomcat5.5/conf/context.xml
Installing local configuration file: /usr/local/tomcat5.5/conf/logging.properties
Installing local configuration file: /usr/local/tomcat5.5/conf/server-minimal.xml
Installing local configuration file: /usr/local/tomcat5.5/conf/server.xml
Installing local configuration file: /usr/local/tomcat5.5/conf/tomcat-users.xml
Installing local configuration file: /usr/local/tomcat5.5/conf/web.xml
[ DONE ]
>> Creating log files… [ DONE ]
>> Creating symlink to tools.jar… [ DONE ]

第四步:安装Apache Web Server

[root@FreeBSD ~] $ pkg_add -r apache22
Fetching ftp://ftp.freebsdchina.org/pub/FreeBSD/ports/i386/packages-6.2-release/Latest/apache22.tbz…

第五步:Apache Web Server 与 Tomcat 的整合
5.1 必备的组件mod_jk的安装

[root@FreeBSD /usr/ports/www/mod_jk] $ make
===>  Vulnerability check disabled, database not found
=> tomcat-connectors-1.2.21-src.tar.gz doesn’t seem to exist in /usr/ports/distfiles/.
=> Attempting to fetch from http://www.apache.org/dist/tomcat/tomcat-connectors/jk/source/jk-1.2.21/.
=> Attempting to fetch from http://www.apache.org/dist/tomcat/tomcat-connectors/jk/source/jk-1.2.21/.
tomcat-connectors-1.2.21-src.tar.gz           100% of 1303 kB   27 kBps 00m00s
===>  Extracting for mod_jk-ap2-1.2.21,1
=> MD5 Checksum OK for tomcat-connectors-1.2.21-src.tar.gz.
=> SHA256 Checksum OK for tomcat-connectors-1.2.21-src.tar.gz.
===>  Patching for mod_jk-ap2-1.2.21,1
===>  Applying FreeBSD patches for mod_jk-ap2-1.2.21,1
===>   mod_jk-ap2-1.2.21,1 depends on file: /usr/local/sbin/apxs – found
===>   mod_jk-ap2-1.2.21,1 depends on executable in : gmake – found
===>   mod_jk-ap2-1.2.21,1 depends on file: /usr/local/sbin/apxs – found
===>  Configuring for mod_jk-ap2-1.2.21,1
…省略 …
…省略 …
…省略 …
Making all in common
gmake[1]: Entering directory `/usr/ports/www/mod_jk/work/tomcat-connectors-1.2.21-src/native/common’
gmake[1]: Nothing to be done for `all’.
gmake[1]: Leaving directory `/usr/ports/www/mod_jk/work/tomcat-connectors-1.2.21-src/native/common’
Making all in apache-2.0
gmake[1]: Entering directory `/usr/ports/www/mod_jk/work/tomcat-connectors-1.2.21-src/native/apache-2.0′
gmake[1]: Nothing to be done for `all’.
gmake[1]: Leaving directory `/usr/ports/www/mod_jk/work/tomcat-connectors-1.2.21-src/native/apache-2.0′
[root@FreeBSD /usr/ports/www/mod_jk] $ make install

5.2 编辑 /usr/local/etc/apache22/httpd.conf 文件,添加如下内容:

LoadModule jk_module libexec/apache/mod_jk.so

5.3 建立文件 /usr/local/etc/apache22/Includes/tomcat-apache.conf ,内容如下:

[root@FreeBSD Includes] $ cat tomcat-apache.conf
JkWorkersFile /usr/local/tomcat5.5/conf/workers.properties
JkLogFile /var/log/mod_jk.log
JkLogLevel info
JkMount /*.jsp ajp13
JkMount /jsp-examples/* ajp13
JkMount /servlets-examples/* ajp13
JkMount /servlet/* ajp13

* JkMount 是把tomcat中的URI与Apache的URI做一个映射,也就是说:
Apache 中的:http://localhost/jsp-examples/some-jsp-file.js
<—- 对应 —>
Tomcat 中的:http://localhost:<port>/jsp-examples/some-jsp-file.jsp
5.4 创建 mod_jk 的日志文件,并设定其归属的用户和组:

[root@FreeBSD ~] $ touch /var/log/mod_jk.log
[root@FreeBSD ~] $ chown www:www /var/log/mod_jk.log

5.5 建立 /usr/local/tomcat5.5/conf/workers.properties 文件:

[root@FreeBSD ~] $ cat /usr/local/tomcat5.5/conf/workers.properties
workers.tomcat_home=/usr/local/tomcat5.5
workers.java_home=/usr/local/diablo-jdk1.5.0
ps=/
worker.list=ajp12, ajp13
worker.ajp13.port=8009
worker.ajp13.host=localhost
worker.ajp13.type=ajp13

5.5 更改 workers.properties 文件归属的用户和组:

[root@FreeBSD ~] $ chown www:www /usr/local/tomcat5.5/conf/workers.properties

5.6 确定 tomcat 配置文件 server.xml 中有开启AJP功能:

<!– Define an AJP 1.3 Connector —>
<Connector className=”org.apache.ajp.tomcat4.Ajp13Connector”
port=”8009″ minProcessors=”5″ maxProcessors=”75″
acceptCount=”10″ debug=”0″/>

* 这段不能被被注视掉!
第六步:进行测试
6.1 启动服务

[root@FreeBSD ~] $ /usr/local/etc/rc.d/tomcat55 onestart
Starting tomcat55.
[root@FreeBSD ~] $ /usr/local/etc/rc.d/apache22 onestart
Performing sanity check on apache22 configuration:
Syntax OK
Starting apache22.

6.2 测试
http://localhost/ —> 出现Apache的信息:It works!
http://localhost/index.jsp —> 出现Tomcat欢迎页面
这样就说明Apache将JSP的处理交给了Tomcat进行了!
原文请参考:http://blog.csdn.net/xport/archive/2007/05/02/1594517.aspx

原文链接:http://www.freebsdchina.org/forum/topic_30435.html