Nginx + MySQL + FastCGI + PHP + eAccelerator 简易安装配置手册

(本文所介绍的安装方法在 Debian 下进行,主要介绍用编译方式进行安装的方法,使用 package 安装较为简单,本文不做介绍。)

第一部分 安装指南

基本步骤:

a. 解压源码包:

tar zxf foobar.tar.gz

b. 运行配置脚本:

cd foobar/

./configure –option=xxx ….

c. 编译源码:

make

d. 安装:

make install

具体编译安装步骤请参考以下安装脚本(点此下载):

# install script for mysql, php, nginx
# writed by hilyjiang 20080718

# mysql
apt-get install -y libncurses5-dev
tar zxf mysql-5.0.51b.tar.gz
cd mysql-5.0.51b/
./configure –prefix=/usr/local/mysql –without-debug –with-client-ldflags=-all-static –with-mysqld-ldflags=-all-static –enable-assembler –with-extra-charsets=gbk,gb2312,utf8 –with-pthread –enable-thread-safe-client
make
make install
cp support-files/my-medium.cnf /usr/local/mysql/my.cnf
cd ../

# php lib
cd php
tar zxvf libiconv-1.12.tar.gz
cd libiconv-1.12/
./configure –prefix=/usr/local
make
make install
cd ../

tar zxvf freetype-2.3.7.tar.gz
cd freetype-2.3.7/
./configure
make
make install
cd ../

tar zxvf libpng-1.2.29.tar.gz
cd libpng-1.2.29/
./configure
make
make install
cd ../

tar zxvf jpegsrc.v6b.tar.gz
cd jpeg-6b/
./configure –enable-static –enable-shared
make
make install
make install-lib
cd ../

tar zxvf libxml2-2.6.32.tar.gz
cd libxml2-2.6.32/
./configure
make
make install
cd ../

tar zxvf libmcrypt-2.5.8.tar.gz
cd libmcrypt-2.5.8/
./configure
make
make install
/sbin/ldconfig
cd libltdl/
./configure –enable-ltdl-install
make
make install
cd ../../

tar zxvf mhash-0.9.9.tar.gz
cd mhash-0.9.9/
./configure
make
make install
cd ../

cp /usr/local/lib/libmcrypt.* /usr/lib
ln -s /usr/local/lib/libmhash.so.2 /usr/lib/libmhash.so.2

tar zxvf mcrypt-2.6.6.tar.gz
cd mcrypt-2.6.6/
./configure
make
make install
cd ../

tar zxf gd-2.0.35.tar.gz
cd gd-2.0.35/
./configure
make
make install
cd ../

tar zxf libssh2-0.12.tar.gz
cd libssh2-0.12/
./configure
make
make install
cd ../

# php
apt-get install -y libcurl3-dev
apt-get install -y bzip2
tar jxf php-5.2.6.tar.bz2
gzip -cd php-5.2.6-fpm-0.5.8.diff.gz | patch -d php-5.2.6 -p1
cd php-5.2.6/
./configure –prefix=/usr/local/php –with-config-file-path=/usr/local/php/etc –with-mysql=/usr/local/mysql –with-mysqli=/usr/local/mysql/bin/mysql_config –with-iconv-dir=/usr/local –with-freetype-dir –with-jpeg-dir –with-png-dir –with-zlib –with-libxml-dir –enable-xml –disable-debug –disable-rpath –enable-discard-path –enable-safe-mode –enable-bcmath –enable-shmop –enable-sysvsem –enable-inline-optimization –with-curl –with-curlwrappers –enable-mbregex –enable-fastcgi –enable-fpm –enable-force-cgi-redirect –enable-mbstring –with-mcrypt –with-gd –enable-gd-native-ttf –with-openssl –enable-ftp
make
make install
mkdir /usr/local/php/etc
cp php.ini-recommended /usr/local/php/etc/php.ini
cd ../

# php extensions
# memcache
apt-get -y install autoconf
tar zxf memcache-2.2.3.tgz
cd memcache-2.2.3/
/usr/local/php/bin/phpize
./configure –with-php-config=/usr/local/php/bin/php-config
make
make install
cd ../

# ssh2
tar zxf ssh2-0.10.tgz
cd ssh2-0.10/
/usr/local/php/bin/phpize
./configure –with-php-config=/usr/local/php/bin/php-config
make
make install
cd ../

# eaccelerator
tar jxf eaccelerator-0.9.5.3.tar.bz2
cd eaccelerator-0.9.5.3/
/usr/local/php/bin/phpize
./configure –with-php-config=/usr/local/php/bin/php-config
make
make install
cd ../
cd ../

# nginx
cd nginx
dpkg -i libgoogle-perftools0_0.98-1_i386.deb
dpkg -i libgoogle-perftools-dev_0.98-1_i386.deb

tar zxf pcre-7.7.tar.gz
cd pcre-7.7
./configure
make
make install
cd ../

tar zxf nginx-0.6.32.tar.gz
cd nginx-0.6.32/
./configure –prefix=/usr/local/nginx –with-http_stub_status_module –with-google_perftools_module
make
make install
mkdir /usr/local/nginx/logs/logbydate/
cd ../
cd ../

维护要点:

a. 编译软件或库时,最好在 configure 时配置使用默认的安装路径如 –prefix=/usr/local,以避免和使用包安装的软件冲突。也可以专门建立一个独立的目录,如 /usr/local/mysql 或 /home/apache。

b. 对冗繁的安装操作,最好写个安装脚本以便日后在多个服务器上自动部署。

第二部分 配置和维护

主要配置文件打包于此:etc.zip

1. Nginx

·主要配置文件路径:

/usr/local/nginx/conf/nginx.conf

/usr/local/nginx/conf/fastcgi_params

/usr/local/nginx/conf/sites/*

·启动 nginx:

/usr/local/nginx/sbin/nginx

·停止 nginx:

kill -TERM `cat /usr/local/nginx/logs/nginx.pid`

·测试配置:

/usr/local/nginx/sbin/nginx –t

·重新加载配置:

kill –HUP `cat /usr/local/nginx/logs/nginx.pid`

·增加新站点:

在 conf/sites/ 下为新站点建立一个配置文件,命名规则为 sitename.enable。

(在 nginx.conf中设置只有以.enable结尾的配置文件才会被加载)

测试配置:

/usr/local/nginx/sbin/nginx –t

重新加载配置:

kill –HUP `cat /usr/local/nginx/logs/nginx.pid`

·按日切分日志:

使用以下脚本按日切分Nginx的日志:

#! /bin/sh

log_dir="/usr/local/nginx/logs"

date_postfix=`date +%Y%m%d`

src_log=${log_dir}/test.access.log

tmp_log=${log_dir}/test.access.log.tmp

des_log=${log_dir}/logbydate/test.access.${date_postfix}.log

if [ ! -f ${des_log} ]; then

mv ${src_log} ${des_log}

else

mv ${src_log} ${tmp_log}

cat ${tmp_log} >> ${des_log}

rm ${tmp_log}

fi

kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`

cron中设置每天 23:59 分执行该脚本:

$ crontab -uroot -e

# m h dom mon dow command

59 23 * * * /usr/local/nginx/logs/cronlog.sh

2. MySQL

·新建数据库服务器:

建立数据存放目录:

mkdir /home/db/testdb

chown mysql /home/db/testdb

安装 mysql 数据目录:

/usr/local/mysql/bin/mysql_install_db –basedir=/usr/local/mysql –datadir=/home/db/testdb/data –user=mysql

·拷贝并修改配置:

cp /usr/local/mysql/my.cnf /home/db/testdb/

/home/db/testdb/my.cnf:

[client]

port = 3306

socket = /home/db/testdb/mysql.sock

[mysqld]

port = 3306

socket = /home/db/testdb/mysql.sock

datadir = /home/db/testdb/data

pid-file = /home/db/testdb/mysql.pid

·建立启动和停止脚本:

/home/db/testdb/start.sh:

#! /bin/sh

/usr/local/mysql/bin/mysqld_safe –defaults-file=/home/db/testdb/my.cnf &

/home/db/testdb/stop.sh:

#! /bin/sh

/usr/local/mysql/bin/mysqladmin –defaults-file=/home/db/testdb/my.cnf -uroot -p shutdown

添加执行权限:

chmod +x /home/db/testdb/start.sh /home/db/testdb/stop.sh

·启动服务:

/home/db/testdb/start.sh

·停止服务:

/home/db/testdb/stop.sh

3. FastCGI-PHP(PHP-FPM

·主要配置文件路径:

/usr/local/php/etc/php.ini

/usr/local/php/etc/php-fpm.ini

·查看已加载PHP模块:

/usr/local/php/bin/php-cgi –m

·增加新的PHP模块:

编译安装:

tar zxf php-ext.tar.gz

cd php-ext/

/usr/local/php/bin/phpize

./configure –with-php-config=/usr/local/php/bin/php-config

make

make install

修改 /usr/local/php/etc/php.ini 的配置,在尾部加上 extension="php-ext.so"。

·启动FastCGI:

/usr/local/php/sbin/php-fpm start

·停止FastCGI:

/usr/local/php/sbin/php-fpm stop

·重启FastCGI:

/usr/local/php/sbin/php-fpm restart

·重新加载 FPM 配置:

/usr/local/php/sbin/php-fpm reload

4. 修改启动脚本

使用Tarball安装的程序,如果需要在系统启动时自动启动,需要在系统的启动脚本 /etc/rc.local 中增加相应项,如:

mkdir /tmp/eaccelerator/

chmod 777 /tmp/eaccelerator/

/home/db/testdb/start.sh

/usr/local/php/sbin/php-fpm start

/usr/local/nginx/sbin/nginx

— EOF —

3 thoughts on “Nginx + MySQL + FastCGI + PHP + eAccelerator 简易安装配置手册”

  1. 我正在学习ubuntu9.10下如何配置PHP环境,但遇到了一些问题:PHP5.3.2情况下,找不到合适的fpm包。因为我要用到SQLite3数据库,所以必须是在PHP5.3环境下的。听说nginx比较高效,所以想配置一下,但对ubuntu不熟悉,配置了一次还没有成功,很是郁闷。不知博主能不能在虚拟机下配置一边,再写一篇博文。

  2. to ddd:
    这个没有办法,我的博客只做自己的学习笔记。
    实在是抽不出时间专门给你写篇文章。

发表评论

电子邮件地址不会被公开。 必填项已用*标注