Nginx+PHP+FastCGI的部署

小编辑 发布于 2010/05/17 08:56
阅读 2K+
收藏 8

nginx(发音同 engine x)是一款俄羅斯的程式設計師Igor Sysoev所開發轻量级的網頁伺服器、反向代理伺服器以及电子邮件(IMAP/POP3)代理服务器,并在BSD-like协议下发行,可以在 UNIX, GNU/Linux, BSD, Mac OS X, Solaris, 以及 Microsoft Windows等操作系统中运行,据说这款web容器在高并发链接下的工作效率要大大高于Apache,所有要好好研究研究,本节中将简要的介绍下部署 nginx+php+fastcgi环境

[root@server5 ~]# yum -y install 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 e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel openldap openldap-devel nss_ldap
openldap-clients     //安装GCC编译器和基本的库文件RPM包,这些库在编译PHP的时候需要调用
[root@server5 ~]# cd /usr/local/src/tarbag/
[root@server5 tarbag]# wget http://www.nginx.com.cn/download/nginx-0.8.20.tar.gz       //nginx主程序包
[root@server5 tarbag]# wget http://cn.php.net/get/php-5.2.10.tar.gz/from/this/mirror     //PHP主程序包
[root@server5 tarbag]# wget http://uploads.ithrough.net/download/linux-vps-pack/mysql-5.1.38.tar.gz    //MYSQL主程序包
[root@server5 tarbag]# wget ftp://ftp.eenet.ee/pub/FreeBSD/distfiles/libmcrypt-2.5.8.tar.gz       //安全线程 加密库主程序包
Libmcrypt is a thread-safe library providing a uniform interface to access several block and stream encryption algorithms.

[root@server5 tarbag]# wget http://acelnmp.googlecode.com/files/mhash-0.9.9.9.tar.gz     //mhash库主程序包
Mhash is a free (under GNU Lesser GPL) library which provides a uniform interface to a large number of hash algorithms. These algorithms can be used to compute checksums, message digests, and other signatures.

[root@server5 tarbag]# wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-7.9.tar.bz2       //正则表达式 扩展主程序包
[root@server5 tarbag]# wget http://php-fpm.org/downloads/php-5.2.10-fpm-0.5.11.diff.gz          //php的FastCGI进程管 理补丁包

[root@server5 tarbag]# tar -zxvf libmcrypt-2.5.8.tar.gz -C ../software/    //编译安装libmcrypt,mhash库
[root@server5 tarbag]# cd ../software/libmcrypt-2.5.8/
[root@server5 libmcrypt-2.5.8]# ./configure && make && make install
[root@server5 tarbag]# tar -zxvf mhash-0.9.9.9.tar.gz -C ../software/
[root@server5 tarbag]# cd ../software/mhash-0.9.9.9/
[root@server5 mhash-0.9.9.9]# ./configure && make && make install
[root@server5 mhash-0.9.9.9]# ln -s /usr/local/lib/* /usr/lib

编译安装:mysql
[root@server5 tarbag]# tar -zxvf mysql-5.1.38.tar.gz -C ../software/
[root@server5 tarbag]# cd ../software/mysql-5.1.38/
[root@server5 mysql-5.1.38]# groupadd mysql
[root@server5 mysql-5.1.38]# useradd -g mysql mysql
[root@server5 mysql-5.1.38]# ./configure --prefix=/usr/local/mysql --enable-assembler --with-extra-charsets=complex --enable -thread-safe-client --with-big-tables --with-readline --with-ssl --with-embedded-server --enable-local-infile --with-plugins=innobase

编译安装PHP:
[root@server5 mysql-5.1.38]# cd /usr/local/src/tarbag/
[root@server5 tarbag]# tar -zxvf php-5.2.10.tar.gz -C ../software/
[root@server5 tarbag]# cp php-5.2.10-fpm-0.5.11.diff.gz ../software/
[root@server5 tarbag]# cd ../software/
[root@server5 software]# gzip -cd php-5.2.10-fpm-0.5.11.diff.gz |patch -d php-5.2.10 -p1
[root@server5 software]# cd php-5.2.10
[root@server5 php-5.2.10]# ./configure --prefix=/usr/local/php5 --with-config-file-path=/usr/local/php5/etc --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-iconv-dir --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir --enable-xml --disable-rpath --enable-iscard-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 --with-mhash --enable-pcntl --enable-sockets --with-ldap --with-ldap-sasl --with-xmlrpc --enable-zip --enable-soap --without-pear
[root@server5 php-5.2.10]# make && make install
[root@server5 php-5.2.10]# cp php.ini-dist /usr/local/php5/etc/php.ini

编译安装pcre:
[root@server5 ~]# cd /usr/local/src/tarbag/
[root@server5 tarbag]# tar -jxvf pcre-7.9.tar.bz2 -C ../software/
[root@server5 tarbag]# cd ../software/pcre-7.9/
[root@server5 pcre-7.9]# ./configure && make && make install

编译安装nginx:
[root@server5 pcre-7.9]# cd ..
[root@server5 software]# tar -zxvf ../tarbag/nginx-0.8.20.tar.gz
[root@server5 software]# cd nginx-0.8.20/
[root@server5 nginx-0.8.20]# groupadd www
[root@server5 nginx-0.8.20]# useradd -g www www
root@server5 nginx-0.8.20]# ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

Configuration summary
+ using system PCRE library
+ using system OpenSSL library
+ md5: using OpenSSL library
+ sha1 library is not used
+ using system zlib library

nginx path prefix: "/usr/local/nginx"
nginx binary file: "/usr/local/nginx/sbin/nginx"
nginx configuration prefix: "/usr/local/nginx/conf"
nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
nginx pid file: "/usr/local/nginx/logs/nginx.pid"
nginx error log file: "/usr/local/nginx/logs/error.log"
nginx http access log file: "/usr/local/nginx/logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
[root@server5 nginx-0.8.20]# make && make install

修改fpm主配置文件,将运行的用户和组改为www
[root@server5 ~]# grep -A 2 'www' /usr/local/php5/etc/php-fpm.conf
<value name="user">www</value>                        

Unix group of processes
<value name="group">www</value>

[root@server5 ~]# getenforce //关闭selinux,否则需要修改php的content,Apache亦是如此
Permissive    

[root@server5 ~]# /usr/local/php5/sbin/php-fpm start //启动fpm
Starting php_fpm done

[root@server5 ~]# netstat -ntpl |grep 9000   //侦听回环地址的9000端口,若PHP和nginx不在同一台服务器上,则需监听在内网地址上
tcp        0      0 127.0.0.1:9000              0.0.0.0:*                   LISTEN      30739/php-cgi

[root@server5 ~]# ps -ef |grep php |grep -v 'grep' |wc -l
6
[root@server5 ~]# /usr/local/nginx/sbin/nginx -t     //测试nginx配置文件语法
the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@server5 ~]# /usr/local/nginx/sbin/nginx   //启动nginx
[root@server5 ~]# cat > /usr/local/nginx/html/info.php <<EOF
> <?php
> phpinfo();
> ?>
> EOF

[root@server5 ~]# grep -v '^#' /usr/local/nginx/conf/nginx.conf |grep -v '#' |uniq //修改主配置文件如下
user www;
worker_processes 1;

events {
worker_connections 1024;
}

http {
include       mime.types;
default_type application/octet-stream;

sendfile        on;

keepalive_timeout 65;

server {
listen       80;
server_name server5;

location / {
root   html;
index index.php index.html index.htm;
}

error_page   500 502 503 504 /50x.html;
location = /50x.html {
root   html;
}

location ~ \.php$ {
root           html;
fastcgi_pass   127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
include        fastcgi_params;
}

}

}

[root@server5 ~]# kill -HUP `cat /usr/local/nginx/logs/nginx.pid` //重启nginx

Nginx+PHP+FastCGI的部署

Nginx+PHP+FastCGI的部署

Nginx+PHP+FastCGI的部署

PHP-FPM开机自动启动的实现:
[root@server5 ~]# cp /usr/local/php5/sbin/php-fpm /etc/init.d/
[root@server5 ~]# head -3 /etc/init.d/php-fpm
#! /bin/sh
#chkconfig: 35 85 15
#description: php-fpm
[root@server5 ~]# chkconfig --add php-fpm


[root@server5 ~]# chkconfig php-fpm on
[root@server5 ~]# service php-fpm restart
Shutting down php_fpm done
Starting php_fpm done

nginx开机自动启动的实现:
[root@server5 ~]# cat /etc/init.d/nginx
#!/bin/sh
#chkconfig: 35 85 15
#description: nginx
#function: use this script to stop,start,restart nginx....
#author:lw.yang

nginx_BIN=/usr/local/nginx/sbin/nginx
nginx_CONF=/usr/local/nginx/conf/nginx.conf
nginx_PID=/usr/local/nginx/logs/nginx.pid
nginx_PORT=`/bin/netstat -ntpl |grep nginx |grep 80 |wc -l`

case $1 in
start)
if [ $nginx_PORT = 0 ];then
echo "staring nginx..."
$nginx_BIN
else echo "starting naginx failed,Address already in use..."
exit 2
fi
;;

stop)
echo "stoping nginx..."
if   [ -f $nginx_PID ];then
kill -QUIT `cat $nginx_PID`
else    echo "nginx is no running...."
fi
;;

status)
if   [ -f $nginx_PID ];then
echo "nginx is running..."
else    echo "nginx is stop..."
fi
;;
restart)
if   [ -f $nginx_PID ];then
kill -HUP `cat $nginx_PID`
else    echo "nginx is no running...."
fi
;;

*)
echo "Usage: $0 {start|stop|status|restart}"
exit 1
;;
esac

[root@server5 ~]# chmod +x /etc/init.d/nginx
[root@server5 ~]# chkconfig --add nginx
[root@server5 ~]# chkconfig nginx on
[root@server5 ~]# service nginx start
starting naginx failed,Address already in use...
[root@server5 ~]# service nginx stop
stoping nginx...
[root@server5 ~]# service nginx start
staring nginx...
[root@server5 ~]# service nginx status
nginx is running...
[root@server5 ~]# service nginx restart
[root@server5 ~]# echo $?

加载中
返回顶部
顶部