免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2420 | 回复: 1
打印 上一主题 下一主题

centos环境下nginx+php搭建 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-03-01 17:21 |只看该作者 |倒序浏览
centos环境下nginx+php搭建








我是在centos5环境下搭建的nginx服务器,使用php-fpm方式来驱动php,下面描述下使用配置过程.

环境:
操作系统 : centos 5
nginx-1.0.12
php-5.3.10

1. 安装php-5.3.10
注 : php-fpm已经作为一个模块添加到了php代码中,这里只需要在php编译的时候增加
--enable-fpm

Java代码  
wget http://cn.php.net/distributions/php-5.3.10.tar.gz   
tar -zxvf php-5.3.10.tar.bz2   
cd php-5.3.10  
  
./configure --prefix=php-root --enable-fastcgi --with-mysql=/home/programs/mysql --enable-zend-multibyte --enable-zip --enable-discard-path --enable-force-cgi-redirect --with-libxml-dir --with-curl --with-openssl --with-mysqli --with-zlib --enable-mbstring --with-gd --with-mcrypt --enable-exif --enable-fpm --enable-force-cgi-redirect --enable-pdo --with-pdo-mysql=/home/programs/mysql --with-ttf --with-iconv --enable-xml --with-gd --with-jpeg-dir=/usr/local/  --with-png-dir=/usr/local --with-freetype-dir=/usr/include/freetype2/   
make   
make install  

wget http://cn.php.net/distributions/php-5.3.10.tar.gz
tar -zxvf php-5.3.10.tar.bz2
cd php-5.3.10

./configure --prefix=php-root --enable-fastcgi --with-mysql=/home/programs/mysql --enable-zend-multibyte --enable-zip --enable-discard-path --enable-force-cgi-redirect --with-libxml-dir --with-curl --with-openssl --with-mysqli --with-zlib --enable-mbstring --with-gd --with-mcrypt --enable-exif --enable-fpm --enable-force-cgi-redirect --enable-pdo --with-pdo-mysql=/home/programs/mysql --with-ttf --with-iconv --enable-xml --with-gd --with-jpeg-dir=/usr/local/  --with-png-dir=/usr/local --with-freetype-dir=/usr/include/freetype2/
make
make install


安装完成之后在php-root/lib/中新建php.ini文件,可以修改一些配置项

注:上面的配置需要根据环境的需求自己修改,以上包含了gd库、mysql、php-fpm等配置

2. 安装nginx-1.0.12
可能需要依赖:
模块依赖:
gcc
gzip     -- zlib
rewrite  -- pcre
ssl      -- openssl

Java代码
  1. yum install gcc gcc-c++ autoconf automake   
  2. yum -y install zlib zlib-devel   
  3. yum -y install openssl openssl-devel   
  4. yum -y install pcre pcre-devel  

  5. yum install gcc gcc-c++ autoconf automake
  6. yum -y install zlib zlib-devel
  7. yum -y install openssl openssl-devel
  8. yum -y install pcre pcre-devel
复制代码
下载安装nginx
Java代码
  1. wget http://nginx.org/download/nginx-1.0.12.tar.gz   
  2. tar -zxvf nginx-1.0.12.tar.gz   
  3. cd nginx-1.0.12  
  4. ./configure --prefix=nginx-root   
  5. make   
  6. make install  

  7. wget http://nginx.org/download/nginx-1.0.12.tar.gz
  8. tar -zxvf nginx-1.0.12.tar.gz
  9. cd nginx-1.0.12
  10. ./configure --prefix=nginx-root
  11. make
  12. make install
复制代码
3. 配置php-fpm
先拷贝配置文件,在进行编辑
Java代码
  1. cp phproot/etc/php-fpm.conf.default -> phproot/etc/php-fpm.conf   
  2. vi phproot/etc/php-fpm.conf  

  3. cp phproot/etc/php-fpm.conf.default -> phproot/etc/php-fpm.conf
  4. vi phproot/etc/php-fpm.conf
复制代码
这里只需要修改用户和你想监听的端口即可
Java代码  
;
  1. Unix user/group of processes   
  2. ; Note: The user is mandatory. If the group is not set, the default user's group   
  3. ;       will be used.   
  4. user = webadmin   
  5. group = webadmin   
  6.   
  7. ; The address on which to accept FastCGI requests.   
  8. ; Valid syntaxes are:   
  9. ;   'ip.add.re.ss:port'    - to listen on a TCP socket to a specific address on   
  10. ;                            a specific port;   
  11. ;   'port'                 - to listen on a TCP socket to all addresses on a   
  12. ;                            specific port;   
  13. ;   '/path/to/unix/socket' - to listen on a unix socket.   
  14. ; Note: This value is mandatory.   
  15. listen = 127.0.0.1:9000  

  16. ; Unix user/group of processes
  17. ; Note: The user is mandatory. If the group is not set, the default user's group
  18. ;       will be used.
  19. user = webadmin
  20. group = webadmin

  21. ; The address on which to accept FastCGI requests.
  22. ; Valid syntaxes are:
  23. ;   'ip.add.re.ss:port'    - to listen on a TCP socket to a specific address on
  24. ;                            a specific port;
  25. ;   'port'                 - to listen on a TCP socket to all addresses on a
  26. ;                            specific port;
  27. ;   '/path/to/unix/socket' - to listen on a unix socket.
  28. ; Note: This value is mandatory.
  29. listen = 127.0.0.1:9000
复制代码
可根据需求进行优化设置

4. 制作fpm启动服务
复制下面的代码,vi /etc/init.d/php-fpm,保存
修改可执行权限  chmod +x /etc/init.d/php-fpm
启动   /etc/init.d/php-fpm start
停止   /etc/init.d/php-fpm stop
重启   /etc/init.d/php-fpm restart

Java代码  
#!/bin/bash   
# php-fpm Startup script for php-fpm, a FastCGI implementation   
# this script was created by tony at 2010.07.21, based on jackbillow's nginx script.   
# it is v.0.0.1 version.   
# if you find any errors on this scripts,please contact tony.   
# by sending mail to tonytzhou at gmail dot com.   
#   
# chkconfig: - 85 15  
# description: php-fpm is an alternative FastCGI implementation, with some additional features useful for sites of any size, especially busier sites.   
#   
# processname: phpfpm   
# pidfile: /usr/local/var/run/phpfpm.pid   
# config: /usr/local/etc/phpfpm.conf   
  
phpfpm=/home/programs/php/sbin/php-fpm   
config=/home/programs/php/lib/php.ini   
pid=/home/programs/php/run/php-fpm.pid   
  
RETVAL=0  
prog="phpfpm"  
  
# Source function library.   
. /etc/rc.d/init.d/functions   
  
# Source networking configuration.   
. /etc/sysconfig/network   
  
# Check that networking is up.   
[ ${NETWORKING} = "no" ] && exit 0  
  
[ -x $phpfpm ] || exit 0  
  
# Start phpfpm daemons functions.   
start() {   
  
if [ -e $pid ];then   
   echo "phpfpm is already running...."  
   exit 1  
fi   
  
   echo -n $"Starting $prog: "  
   daemon $phpfpm -c ${config}   
   RETVAL=$?   
   echo   
   [ $RETVAL = 0 ] && touch /var/lock/subsys/phpfpm   
   return $RETVAL   
  
}   
  
# Stop phpfpm daemons functions.   
stop() {   
        echo -n $"Stopping $prog: "  
        killproc $phpfpm   
        RETVAL=$?   
        echo   
        [ $RETVAL = 0 ] && rm -f /var/lock/subsys/phpfpm /var/run/phpfpm.pid   
}   
  
# reload phpfpm service functions.   
reload() {   
  
    echo -n $"Reloading $prog: "  
    #kill -HUP `cat ${pid}`   
    killproc $phpfpm -HUP   
    RETVAL=$?   
    echo   
  
}   
  
# See how we were called.   
case "$1" in   
start)   
        start   
        ;;   
  
stop)   
        stop   
        ;;   
  
reload)   
        reload   
        ;;   
  
restart)   
        stop   
        start   
        ;;   
  
status)   
        status $prog   
        RETVAL=$?   
        ;;   
*)   
        echo $"Usage: $prog {start|stop|restart|reload|status|help}"  
        exit 1  
esac   
  
exit $RETVAL  

#!/bin/bash
# php-fpm Startup script for php-fpm, a FastCGI implementation
# this script was created by tony at 2010.07.21, based on jackbillow's nginx script.
# it is v.0.0.1 version.
# if you find any errors on this scripts,please contact tony.
# by sending mail to tonytzhou at gmail dot com.
#
# chkconfig: - 85 15
# description: php-fpm is an alternative FastCGI implementation, with some additional features useful for sites of any size, especially busier sites.
#
# processname: phpfpm
# pidfile: /usr/local/var/run/phpfpm.pid
# config: /usr/local/etc/phpfpm.conf

phpfpm=/home/programs/php/sbin/php-fpm
config=/home/programs/php/lib/php.ini
pid=/home/programs/php/run/php-fpm.pid

RETVAL=0
prog="phpfpm"

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0

[ -x $phpfpm ] || exit 0

# Start phpfpm daemons functions.
start() {

if [ -e $pid ];then
   echo "phpfpm is already running...."
   exit 1
fi

   echo -n $"Starting $prog: "
   daemon $phpfpm -c ${config}
   RETVAL=$?
   echo
   [ $RETVAL = 0 ] && touch /var/lock/subsys/phpfpm
   return $RETVAL

}

# Stop phpfpm daemons functions.
stop() {
        echo -n $"Stopping $prog: "
        killproc $phpfpm
        RETVAL=$?
        echo
        [ $RETVAL = 0 ] && rm -f /var/lock/subsys/phpfpm /var/run/phpfpm.pid
}

# reload phpfpm service functions.
reload() {

    echo -n $"Reloading $prog: "
    #kill -HUP `cat ${pid}`
    killproc $phpfpm -HUP
    RETVAL=$?
    echo

}

# See how we were called.
case "$1" in
start)
        start
        ;;

stop)
        stop
        ;;

reload)
        reload
        ;;

restart)
        stop
        start
        ;;

status)
        status $prog
        RETVAL=$?
        ;;
*)
        echo $"Usage: $prog {start|stop|restart|reload|status|help}"
        exit 1
esac

exit $RETVAL


5. 配置nginx
使用80端口,域名为www.demo.com


Java代码  
#user  nobody;   
user webadmin webadmin;   
worker_processes  4;   
#error_log  logs/error.log;   
#error_log  logs/error.log  notice;   
#error_log  logs/error.log  info;   
pid        logs/nginx.pid;   
# 指定文件描述符数量   
worker_rlimit_nofile 51200;   
events {   
    # 使用网络I/O模型,linux推荐使用epoll, FressBSD推荐私用kqueue   
    use epoll;   
    # 允许链接数   
    worker_connections  51200;   
}   
http {   
    include       mime.types;   
    default_type  application/octet-stream;   
      
    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  logs/access.log  main;   
      
    #autoindex off;   
    # 设置字符集,如果多种字符集,不要设置   
    #charset utf-8;   
      
    sendfile        on;   
    keepalive_timeout  65;   
      
    fastcgi_connect_timeout 300;   
    fastcgi_send_timeout 300;   
    fastcgi_read_timeout 300;   
    fastcgi_buffer_size 64k;   
    fastcgi_buffers 4 64k;   
    fastcgi_busy_buffers_size 128k;   
    fastcgi_temp_file_write_size 128k;   
      
    #开启gzip   
    gzip on;   
    gzip_min_length 1k;   
    gzip_buffers 4 16k;   
    gzip_http_version 1.1;   
    gzip_com_level 2;   
    gzip_types text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;   
    gzip_vary on;   
      
    server {   
        listen 80;   
        server_name www.demo.com;   
        index index.html index.htm index.php;   
        root web-root;   
           
        # 图片缓存   
        location ~* \.(?:ico|gif|jpe?g|png|bmp|swf)$ {   
                # Some basic cache-control for static files to be sent to the browser   
                expires max;   
                add_header Pragma public;   
                add_header Cache-Control "public, must-revalidate, proxy-revalidate";   
        }   
  
        # 静态资源缓存
  1.     location ~.*\.(js|css)?$   
  2.         {   
  3.             expires 1h;   
  4.         }   
  5.            
  6.         error_page   500 502 503 504  /50x.html;   
  7.         location = /50x.html {   
  8.             root   html;   
  9.         }   
  10.            
  11.         #   
  12.         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000  
  13.         #   
  14.         location ~ \.php$ {   
  15.             include /home/programs/nginx/conf/fastcgi_params;   
  16.             if ($uri !~ "^/statics/") {   
  17.                fastcgi_pass 127.0.0.1:9000; # fpm监听的端口和ip   
  18.             }   
  19.             fastcgi_index  index.php;   
  20.             fastcgi_param  SCRIPT_FILENAME  web-root$fastcgi_script_name;   
  21.             include        fastcgi_params;   
  22.         }   
  23.   
  24.     }   
  25.       
  26. }  
复制代码
#user  nobody;
user webadmin webadmin;
worker_processes  4;
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
pid        logs/nginx.pid;
# 指定文件描述符数量
worker_rlimit_nofile 51200;
events {
    # 使用网络I/O模型,linux推荐使用epoll, FressBSD推荐私用kqueue
    use epoll;
    # 允许链接数
  1. worker_connections  51200;
  2. }
  3. http {
  4.     include       mime.types;
  5.     default_type  application/octet-stream;
  6.    
  7.     log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
  8.                       '$status $body_bytes_sent "$http_referer" '
  9.                       '"$http_user_agent" "$http_x_forwarded_for"';
  10.     access_log  logs/access.log  main;
  11.    
  12.     #autoindex off;
复制代码
# 设置字符集,如果多种字符集,不要设置
    #charset utf-8;
  1. sendfile        on;
  2.     keepalive_timeout  65;
  3.    
  4.     fastcgi_connect_timeout 300;
  5.     fastcgi_send_timeout 300;
  6.     fastcgi_read_timeout 300;
  7.     fastcgi_buffer_size 64k;
  8.     fastcgi_buffers 4 64k;
  9.     fastcgi_busy_buffers_size 128k;
  10.     fastcgi_temp_file_write_size 128k;
  11.    
  12.     #开启gzip
  13.     gzip on;
  14.     gzip_min_length 1k;
  15.     gzip_buffers 4 16k;
  16.     gzip_http_version 1.1;
  17.     gzip_com_level 2;
  18.     gzip_types text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;
  19.     gzip_vary on;
  20.    
  21.     server {
  22.         listen 80;
  23.         server_name www.demo.com;
  24.         index index.html index.htm index.php;
  25.         root web-root;
复制代码
# 图片缓存
        location ~* \.(?:ico|gif|jpe?g|png|bmp|swf)$ {
                # Some basic cache-control for static files to be sent to the browser
                expires max;
                add_header Pragma public;
                add_header Cache-Control "public, must-revalidate, proxy-revalidate";
        }

        # 静态资源缓存
        location ~.*\.(js|css)?$
        {
            expires 1h;
        }
        
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        
        #
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
            include /home/programs/nginx/conf/fastcgi_params;
            if ($uri !~ "^/statics/") {
               fastcgi_pass 127.0.0.1:9000; # fpm监听的端口和ip
            }
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  web-root$fastcgi_script_name;
            include        fastcgi_params;
        }

    }
   
}


配置好后保存nginx.conf,

6. 启动nginx

Java代码
  1. nginx-root/bin/nginx -c nginx-root/conf/nginx.conf  

  2. nginx-root/bin/nginx -c nginx-root/conf/nginx.conf
复制代码
访问http://www.demo.com就可以了
注: www.demo.com需要绑定到hosts中

后续会增加rewrite的一些自己的理解和总结

论坛徽章:
0
2 [报告]
发表于 2012-03-01 22:41 |只看该作者
谢谢分享
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP