php模块服务配置
配置如下
vim /usr/local/nginx_php/etc/php-fpm.conf
[global]
pid = /usr/local/nginx_php/var/run/php-fpm.pid
error_log = /usr/local/nginx_php/var/log/php-fpm.log
[www]
listen = /tmp/php-fcgi.sock
user = php-fpm ;运行用户
group = php-fpm ;组
listen.owner = nobody ;监听php-fcgi.sock的用户
listen.group = nobody
pm = dynamic ;生成进程
pm.max_children = 50 可以启动多少进程
pm.start_servers = 10 一开始启动多少进程
pm.min_spare_servers = 5 最少启动的进程数
pm.max_spare_servers = 25 最多启动的进程数
pm.max_requests = 500 到达多少请求后自动结束进程
rlimit_files = 1024 一次请求的最大字节数
重新启动php
/etc/init.d/php-fpm restart
其中php配置文件中的php-fcgi.sock文件所有属主及属组是php文件中定义的用户,
也就是这里的nobody,否则php无法解析php页面
nginx配置文件如下(按照本次环境不会出现问题)
user nobody nobody; #启动nginx服务的用户与组
worker_processes 2; #启动nginx服务的工作进程
error_log logs/nginx_error.log crit; #错误日志,以及等级
pid /usr/local/nginx/logs/nginx.pid; #nginx服务进程PID
worker_rlimit_nofile 51200;
events
{
use epoll;
worker_connections 6000;
}
http
{
include mime.types;
default_type application/octet-stream;
server_names_hash_bucket_size 3526;
server_names_hash_max_size 4096;
log_format combined_realip ‘$remote_addr $http_x_forwarded_for [$time_local]‘
‘$host "$request_uri"$status‘
‘"$http_referer""$http_user_agent"‘
sendfile on;
tcp_nopush on;
keepalive_timeout 30;
client_header_timeout 3m ;
client_body_timeout 3m ;
send_timeout 3m ;
connection_pool_size 256 ;
client_header_buffer_size 1k ;
large_client_header_buffers 8 4k ;
request_pool_size 4k ;
output_buffers 4 32k ;
postpone_output 1460 ;
client_max_body_size 10m ;
client_body_buffer_size 10m ;
client_body_temp_path /usr/local/nginx/client_body_temp ;
proxy_temp_path /usr/local/nginx/proxy_temp ;
fastcgi_temp_path /usr/local/nginx/fastcgi_temp ;
fastcgi_intercept_errors on ;
tcp_nodelay on ;
gzip_min_length 1k ;
gzip_buffers 4 8k ;
gzip_comp_level 5 ;
gzip_http_version 1.1;
gzip_types text/plain application/x-javascript/css text/htm ;
#application/xml;
server
{
listen 80;
server_name localhost;
index index.html index.htm index.php ;
root /usr/local/http;
location ~\.php$
{
include fastcgi_params;
fastcgi_pass unix:/tmp/php-fcgi.sock ;
fastcgi_index index.php ;
fastcgi_param SCRIPT_FILENAME /usr/local/http$fastcgi_script_name ;
}
}
}
nginx启动脚本(根据自己nginx所在目录修改)
#!/bin/bash
# chkconfig: - 30 21
# description: http service.
# Source Function Library
. /etc/init.d/functions
# Nginx Settings
NGINX_SBIN="/usr/local/nginx/sbin/nginx"
NGINX_CONF="/usr/local/nginx/conf/nginx.conf"
NGINX_PID="/usr/local/nginx/var/nginx.pid"
RETVAL=0
prog="Nginx"
start() {
echo -n $"Starting $prog: "
mkdir -p /dev/shm/nginx_temp
daemon $NGINX_SBIN -c $NGINX_CONF
RETVAL=$?
echo
return $RETVAL
}
stop() {
echo -n $"Stopping $prog: "
killproc nginx -TERM
rm -rf /dev/shm/nginx_temp
RETVAL=$?
echo
return $RETVAL
}
reload(){
echo -n $"Reloading $prog: "
killproc -p $NGINX_PID $NGINX_SBIN -HUP
RETVAL=$?
echo
return $RETVAL
}
restart(){
stop
start
}
configtest(){
$NGINX_SBIN -c $NGINX_CONF -t
return 0
}
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
restart
;;
configtest)
configtest
;;
*)
echo $"Usage: $0 {start|stop|reload|restart|configtest}"
RETVAL=1
esac
exit $RETVAL
给予启动文件权限
chmod 755 /etc/init.d/nginx
将nginx加入系统开机启动
chkconfig --add nginx
chkconfig nginx on
nginx跟php有两种通信方式
php配置文件中修改通信方式
listen = /tmp/php-fcgi.sock 或
listen = 127.0.0.1:9000
nginx配置文件中修改
在 location ~\.php$
{
下找到fastcgi_pass unix:/tmp/php-fcgi.sock ;
修改成和php中相同通信方式
fastcgi_pass unix:/tmp/php-fcgi.sock ; 或
fastcgi_pass 127.0.0.1:9000 ;
nginx虚拟主机
在主配置文件中添加
include vhosts/*.conf;
并在当前目录下创建vhosts目录
mkdir vhosts
在此目录下创建以.conf结尾的虚拟主机配置文件,写入如下内容
server
{
listen 80 ;
server_name 网站域名;
index index.html index.htm index.php ;
root /网页目录;
location ~\.php$
{
include fastcgi_params;
fastcgi_pass unix:/tmp/php-fcgi.sock ;
fastcgi_index index.php ;
fastcgi_param SCRIPT_FILENAME /网页目录$fastcgi_script_name ;
}
}
也可以直接在主配置文件中复制server段内容进行修改