rpm快速安装nginx+fastcgi
2010-02-24 10:31:34| 分类: web服务 | 标签:nginx web服务器 eaccelerator fastcgi
|字号
订阅
1. lnmp环境安装
这里采用光盘中的rpm方式进行安装。当然也可以通过yum来安装全部包。nginx,php通过rpm方式安装,mysql客户端rpm安装,数据库源码安装。
配置环境centos5.3。还需要安装一些基础包如gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel等。
nginx/spawn-fcgi下载地址:
http://dl.rashost.com/centos5-i386/nginx-0.7.61-1.i386.rpm
http://dl.rashost.com/centos5-i386/spawn-fcgi-1.6.2-1.32.i386.rpm
# rpm -ivh nginx-0.7.61-1.i386.rpm
# rpm -ivh spawn-fcgi-1.6.2-1.32.i386.rpm
# rpm -ivh perl-DBI-1.52-2.el5.i386.rpm
# rpm -ivh mysql-5.0.45-7.el5.i386.rpm
# rpm -ivh mysql-devel-5.0.45-7.el5.i386.rpm
# rpm -ivh perl-DBD-MySQL-3.0007-2.el5.i386.rpm
# rpm -ivh mysql-server-5.0.45-7.el5.i386.rpm
# rpm -ivh php-cli-5.1.6-23.el5.i386.rpm
# rpm -ivh php-common-5.1.6-23.el5.i386.rpm
# rpm -ivh php-5.1.6-23.el5.i386.rpm
# rpm -ivh php-gd-5.1.6-23.el5.i386.rpm
# rpm -ivh php-pdo-5.1.6-23.el5.i386.rpm
# rpm -ivh php-mysql-5.1.6-23.el5.i386.rpm
# rpm -ivh php-mbstring-5.1.6-23.el5.i386.rpm
# rpm -ivh php-xml-5.1.6-23.el5.i386.rpm
# rpm -ivh php-ncurses-5.1.6-23.el5.i386.rpm
2. nginx php_cgi启动
# chkconfig --add nginx
# chkconfig nginx on
# service nginx start
fastcgi启动程序如下:
# ll /usr/bin/*cgi
-rwxr-xr-x 1 root root 2715264 2009-01-21 /usr/bin/php-cgi
-rwxr-xr-x 1 root root 17856 10-15 15:44 /usr/bin/spawn-fcgi
3. 创建php_cgi启动脚本
# vi /etc/init.d/php_cgi
#!/bin/sh
#
# php-cgi - php-fastcgi swaping via spawn-fcgi
#
# chkconfig: - 85 15
# description: Run php-cgi as app server
# processname: php-cgi
# config: /etc/sysconfig/phpfastcgi (defaults RH style)
# pidfile: /var/run/php_cgi.pid
# Note: See how to use this script :
# http://www.cyberciti.biz/faq/rhel-fedora-install-configure-nginx-php5/
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
spawnfcgi="/usr/bin/spawn-fcgi"
php_cgi="/usr/bin/php-cgi"
prog=$(basename $php_cgi)
server_ip=127.0.0.1
server_port=9000
server_user=apache
server_group=apache
server_childs=30
pidfile="/var/run/php_cgi.pid"
# do not edit, put changes in /etc/sysconfig/phpfastcgi
[ -f /etc/sysconfig/phpfastcgi ] && . /etc/sysconfig/phpfastcgi
start() {
[ -x $php_cgi ] || exit 1
[ -x $spawnfcgi ] || exit 2
echo -n $"Starting $prog: "
daemon $spawnfcgi -a ${server_ip} -p ${server_port} -u ${server_user} -g ${server_group} -P ${pidfile} -C ${server_childs}
-f ${php_cgi}
retval=$?
echo
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc -p ${pidfile} $prog -QUIT
retval=$?
echo
[ -f ${pidfile} ] && /bin/rm -f ${pidfile}
return $retval
}
restart(){
stop
sleep 2
start
}
rh_status(){
status -p ${pidfile} $prog
}
case "$1" in
start)
start;;
stop)
stop;;
restart)
restart;;
status)
rh_status;;
*)
echo $"Usage: $0 {start|stop|restart|status}"
exit 3
esac
4. php_cgi启动
# chmod +x /etc/init.d/php_cgi
# chkconfig --add php_cgi
# chkconfig php_cgi on
# service php_cgi start
# netstat -tunlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 2719/php-cgi
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 2228/mysqld
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 2656/nginx.conf
5. php扩展安装
(1).安装memcache
# tar zxvf memcache-2.2.5.tgz
# cd memcache-2.2.5/
# /usr/bin/phpize
# ./configure --with-php-config=/usr/bin/php-config
# make
# make install
# 说明:memcache库的位置
Installing shared extensions: /usr/lib/php/modules/
# cd ..
(2).安装eaccelerator php加速
# tar jxvf eaccelerator-0.9.5.3.tar.bz2
# cd eaccelerator-0.9.5.3/
# /usr/bin/phpize
# ./configure --enable-eaccelerator=shared
--with-php-config=/usr/bin/php-config
# make
# make install
Installing shared extensions: /usr/lib/php/modules/
# cd ../
(3).安装ImageMagick是Linux下非常强大的图象处理函数与GD类似. Jpeg v6b、libPng、FreeType 的要在安装imagemagick之前先装好,否则imagemagick没法读取
jpeg和png图片,字体文件也读不了。下面是安装Imagemagick时./configure的结果,可以查看imagickMagick是否支持哪些格式的图片以及一些环境配置:
# tar zxvf ImageMagick.tar.gz
# cd ImageMagick-6.5.1-2/
#./configure
# make
# make install
# cd ../
(4).安装imagick(连接PHP和ImageMagick的通道)
# tar zxvf imagick-2.2.2.tgz
# cd imagick-2.2.2/
# /usr/bin/phpize
# ./configure --with-php-config=/usr/bin/php-config
# make
# make install
Installing shared extensions: /usr/lib/php/modules/
# cd ../
(5).修改php.ini文件,已使php支持扩展的功能
可以通过两种方式进行,一种是以下这种方式,直接修改php.ini配置文件,一种是/etc/php.d/直接创建配置文件加入也可以。可以参数gd.ini文件再进行修改。
# vi etc/php.ini
查找
extension_dir = "./"
修改为
Installing shared extensions: /usr/lib/php/modules/
并在此行后增加以下几行,然后保存:
extension = "memcache.so"
extension = "imagick.so"
再查找output_buffering = Off
修改为output_buffering = On
(6).配置eAccelerator加速PHP:
# mkdir -p /usr/local/eaccelerator_cache
# vi /usr/local/php/etc/php.ini
按shift+g键跳到配置文件的最末尾,加上以下配置信息:
[eaccelerator]
extension=eaccelerator.so
eaccelerator.shm_size="64"
eaccelerator.cache_dir="/usr/local/eaccelerator_cache"
eaccelerator.enable="1"
eaccelerator.optimizer="1"
eaccelerator.check_mtime="1"
eaccelerator.debug="0"
eaccelerator.filter=""
eaccelerator.shm_max="0"
eaccelerator.shm_ttl="3600"
eaccelerator.shm_prune_period="3600"
eaccelerator.shm_only="0"
eaccelerator.compress="1"
eaccelerator.compress_level="9"
4. 测试
(1) 修改本机hosts文件
192.168.162.20 www.koumm.com
(2) www.koumm.com虚拟主机
# vi /etc/nginx/conf.d/www.koumm.com.conf
server {
listen 80;
server_name www.koumm.com;
index index.php index.html index.htm;
root /data/www;
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/www/$fastcgi_script_name;
include fastcgi_params;
}
if (!-e $request_filename)
{
rewrite "^/([0-9]+)/message\.html" /message.php?uid=$1 last;
rewrite "^/index\.html" /index.php last;
rewrite "^/test\.html" /test.php last;
}
#log
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
access_log /data/logs/www.koumm.com.log main;
}
# nginx -t 测试配置文件
# service nginx restart 重启服务
(3) php测试
# vi /data/www/test.php
添加以下行:
<?php
phpinfo();
?>
在浏览器中输入: http://www.koumm.com/test.php进行测试。
(4)mysql登陆
# mysqladmin -uroot password 'admin'
# mysql -uroot -padmin
mysql>
本文可以实现apache php与nginx php fastcgi方式