Mac OS X(Lion)下源码安装Nginx + PHP + MySQL

想要在Mac下源码方式安装软件,前提是:去苹果官方下载安装XCode,安装好后打开preference,安装Command Line Tools,里面包含gcc,make等所有工具。

I. 安装MYSQL
从官方下载 mysql-5.5.25a-osx10.6-x86_64.dmg二进制包,安装到/usrl/local/mysql
使用:sudo /usr/local/bin/msyqld_safe & 启动之.

OS X 系统安装的mysql默认是不用my.cnf配置文件的,仅是使用默认的数据库配置值。
如要进行数据库定制,可到'/usr/local/mysql/support-files/'文件夹底下,把里面的任一个.cnf配置文件复制到/etc/目录底下并修改文件名称为my.cnf。
把Mysql加入到launchd服务,使开机自动启动Mysql
在/Library/LaunchDaemons/目录创建文件com.mysql.mysql.plist(文件名任意):
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN"
"http://www.apple.com/DTDs/PropertyList-1.0.dtd" >
<plist version='1.0'>
  <dict>
    <key>Label</key>
    <string>com.mysql.mysql</string>
    <key>ProgramArguments</key>
    <array>
        <string>/usr/local/mysql/bin/mysqld_safe</string>
    </array>
    <key>RunAtLoad</key><true/>
    <key>KeepAlive</key><false/>
  </dict>
</plist>

II. 安装PHP
1. 安装PHP扩展准备
1).安装GD库
为了安装GD库到yum安装必要的包(libpng,libjpeg,freetype等)还要安装 : jpegsrc.v8d.tar.gz,libpng-1.2.50.tar.gz,freetype-2.4.10.tar.bz2
a. 安装jpegsrc.v8d.tar.gz
下载http://www.ijg.org/files/jpegsrc.v8d.tar.gz
进入jpeg-8d的源码目录,然后执行以下步骤:
./configure --enable-shared --enable-static  
make  
sudo make install   

b. 安装libpng-1.2.50.tar.gz
下载http://nchc.dl.sourceforge.net/project/libpng/libpng12/1.2.50/libpng-1.2.50.tar.gz
./configure  
make  
make install  

c. 安装freetype-2.4.10.tar.gz
下载http://www.freetype.org/download.html
./configure  
make  
make install


2).安装mcrypt及扩展
请参考 http://koda.iteye.com/blog/420991

2. 编译PHP
sudo ./configure --prefix=/usr/local/php --with-iconv=/opt/local --with-pdo-mysql=mysqlnd --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-zlib --enable-mbstring --with-gd --with-curl --with-freetype-dir=/usr --with-png-dir=/usr/lib --with-jpeg-dir=/usr/lib --enable-fpm --enable-zip --with-mcrypt --with-mhash  --enable-intl --with-openssl  --enable-exif --enable-soap
sudo make 
sudo make install  


参数补充说明:
--with-mcrypt --with-mhash
     如果未安装mcrypt扩展,删除该configure参数:
--with-iconv=/opt/local
    如果没有该参数,安装时候报错可能:
Undefined symbols for architecture x86_64:
  "_iconv_close", referenced from:
      _cconv_close in cconv-cconv.o
  "_iconv", referenced from:
      _cconv in cconv-cconv.o
  "_iconv_open", referenced from:
      _cconv_open in cconv-cconv.o
ld: symbol(s) not found for architecture x86_64

原因是iconv版本低,要么手工下载iconv编译升级;要么如果你安装了MacPort(如果iconv版本更高), 可以用在php configure运行时增加参数--with-iconv=/opt/local

3. 配置php-fpm并运行
PHP5.4内置了fastcgi支持。
1).  生成配置文件
   复制$PHP_DIR/etc/php-fpm.conf.default 到$PHP_DIR/etc/php-fpm.conf
   打开一下两行注释:
   pid = run/php-fpm.pid
   pm.start_servers = 20
   pm.min_spare_servers = 5
   pm.max_spare_servers = 35
   pm.max_children=50

2). 启动php-fpm
   sudo $PHP_DIR/sbin/php-fpm
3). 停止php-fpm
   sudo killall -9 php-fpm

4). 加入到服务中去并每次开机自动启动
使用launchctl来把php-fpm加入启动服务。建立/Library/LaunchDaemons/net.php.php-fpm.plist,内容如下:
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN"
"http://www.apple.com/DTDs/PropertyList-1.0.dtd" >
<plist version='1.0'>
  <dict>
    <key>Label</key>
    <string>net.php.php-fpm</string>
    <key>ProgramArguments</key>
    <array>
        <string>/usr/local/php/sbin/php-fpm</string>
        <string>--fpm-config</string>
        <string>/usr/local/php/etc/php-fpm.conf</string>
    </array>
    <key>RunAtLoad</key><true/>
    <key>KeepAlive</key><false/>
  </dict>
</plist>

启动之: sudo launchctl load -w /Library/LaunchDaemons/net.php.php-fpm.plist
停止之: sudo launchctl unload /Library/LaunchDaemons/net.php.php-fpm.plist
下次重新启动php-fpm就被启动了

III. 安装nginx
Nginx依赖PCRE,不过这个你在安装XCode时应该就默认安装好了。下载Nginix http://nginx.org/download/nginx-1.2.2.tar.gz
tar -xzvf nginx-1.2.2.tar.gz
cd nginx-1.2.2
./configure --prefix=/usr/local/nginx
make
make install

参考 http://kevinworthington.com/nginx-for-mac-os-x-lion-in-2-minutes/

启动: sbin/nginx (默认使用conf/nginx.conf作为配置文件)
停止: kill `cat /usr/local/nginx/logs/nginx.pid`
加入launchd: 创建/Library/LaunchDaemons/org.ngnix.nginx.plist
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN"
"http://www.apple.com/DTDs/PropertyList-1.0.dtd" >
<plist version='1.0'>
  <dict>
    <key>Label</key>
    <string>org.nginx.nginx</string>
    <key>ProgramArguments</key>
    <array>
        <string>/usr/local/nginx/sbin/nginx</string>
        <string>-c</string>
        <string>/usr/local/nginx/conf/nginx.conf</string>
    </array>
    <key>RunAtLoad</key><true/>
    <key>KeepAlive</key><false/>
  </dict>
</plist>

启动之:sudo launchctl load -w /Library/LaunchDaemons/org.ngnix.nginx.plist
停止之:sudo launchctl unload /Library/LaunchDaemons/org.ngnix.nginx.plist

猜你喜欢

转载自koda.iteye.com/blog/1609243