centos環(huán)境下nginx+php搭建
我是在centos5環(huán)境下搭建的nginx服務(wù)器,使用php-fpm方式來(lái)驅(qū)動(dòng)php,下面描述下使用配置過(guò)程.
環(huán)境:
操作系統(tǒng) : centos 5
nginx-1.0.12
php-5.3.10
1. 安裝php-5.3.10
注 : php-fpm已經(jīng)作為一個(gè)模塊添加到了php代碼中,這里只需要在php編譯的時(shí)候增加
--enable-fpm
wget http://nginx.org/download/nginx-1.0.12.tar.gz
tar -zxvf nginx-1.0.12.tar.gz
cd nginx-1.0.12
./configure --prefix=nginx-root
make
make install
3. 配置php-fpm
先拷貝配置文件,在進(jìn)行編輯
Java代碼
cp phproot/etc/php-fpm.conf.default -> phproot/etc/php-fpm.conf
vi phproot/etc/php-fpm.conf
cp phproot/etc/php-fpm.conf.default -> phproot/etc/php-fpm.conf
vi phproot/etc/php-fpm.conf
這里只需要修改用戶和你想監(jiān)聽的端口即可
Java代碼
;- Unix user/group of processes
- ; Note: The user is mandatory. If the group is not set, the default user's group
- ; will be used.
- user = webadmin
- group = webadmin
-
- ; The address on which to accept FastCGI requests.
- ; Valid syntaxes are:
- ; 'ip.add.re.ss:port' - to listen on a TCP socket to a specific address on
- ; a specific port;
- ; 'port' - to listen on a TCP socket to all addresses on a
- ; specific port;
- ; '/path/to/unix/socket' - to listen on a unix socket.
- ; Note: This value is mandatory.
- listen = 127.0.0.1:9000
- ; Unix user/group of processes
- ; Note: The user is mandatory. If the group is not set, the default user's group
- ; will be used.
- user = webadmin
- group = webadmin
- ; The address on which to accept FastCGI requests.
- ; Valid syntaxes are:
- ; 'ip.add.re.ss:port' - to listen on a TCP socket to a specific address on
- ; a specific port;
- ; 'port' - to listen on a TCP socket to all addresses on a
- ; specific port;
- ; '/path/to/unix/socket' - to listen on a unix socket.
- ; Note: This value is mandatory.
- listen = 127.0.0.1:9000
復(fù)制代碼 可根據(jù)需求進(jìn)行優(yōu)化設(shè)置
4. 制作fpm啟動(dòng)服務(wù)
復(fù)制下面的代碼,vi /etc/init.d/php-fpm,保存
修改可執(zhí)行權(quán)限 chmod +x /etc/init.d/php-fpm
啟動(dòng) /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
復(fù)制代碼 5. 配置nginx
使用80端口,域名為www.demo.com
Java代碼# 設(shè)置字符集,如果多種字符集,不要設(shè)置- #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";
- }
- # 靜態(tài)資源緩存
- 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監(jiān)聽的端口和ip
- }
- fastcgi_index index.php;
- fastcgi_param SCRIPT_FILENAME web-root$fastcgi_script_name;
- include fastcgi_params;
- }
- }
-
- }
復(fù)制代碼 配置好后保存nginx.conf,
6. 啟動(dòng)nginx
Java代碼- nginx-root/bin/nginx -c nginx-root/conf/nginx.conf
- nginx-root/bin/nginx -c nginx-root/conf/nginx.conf
復(fù)制代碼 訪問(wèn)http://www.demo.com就可以了
注: www.demo.com需要綁定到hosts中
后續(xù)會(huì)增加rewrite的一些自己的理解和總結(jié)
|