原创 服务器
第一:安装前准备
新建用户nginx 用于运行nginx和php
1, 本文的系统环境
[root@test ~]#uname -a
Linux FWQ-BJ-ZW-GS-04F8-005-100 2.6.32-279.el6.x86_64 #1 SMP Fri Jun 22 12:19:21 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux
[root@test ~]#more /etc/redhat-release
CentOS release 6.3 (Final)
[root@test ~]#
2, 本文所需软件
或使用yum安装
yum -y install mysql php-mysql php-fpm
具体的php-fpm的源可以看我的blog
centos6下安装php-fpm http://jedy82.blog.51cto.com/425872/1068331
nginx-1.2.4.tar.gz 最新稳定版 官网(www.nginx.net)可下载
php-5.4.8.tar.bz2 最新稳定版 官网(www.php.net)可下载
3, 创建相关目录和用户名
[root@test ~]# useradd -r nginx -s /sbin/nologin
4,安装所需的依赖包
[root@test ~]# yum install -y pcre-devel pcre libcurl-devel
5,安装编辑工具
[root@test ~]# yum install -y make gcc
第二:PHP的安装和配置:
1,安装php所需的函数库
[root@test ~]# yum install -y libjpeg-6b libjpeg-devel libpng libpng-devel zlib zlib-devel freetype freetype-devel libXpm libXpm-devel libxml2 libxml2-devel bzip2 bzip2-devel gd
2, 源码安装php
[root@test ~]# tar -jxvf php-5.4.8.tar.bz2
[root@test ~]# cd php-5.4.8
[root@test php-5.4.8]# ./configure '--prefix=/usr/local/php' '--enable-fpm' '--with-curl' '--with-curlwrappers' '--with-gd' '--enable-mbstring' '--with-mysql' '--enable-zip' '--with-libdir=lib64' '--with-zlib'
[root@test php-5.4.8]# make
[root@test php-5.4.8]# make install
[root@test php-5.4.8]# cd
3,生成配置文件
[root@test ~]# cp /root/php-5.4.8/php.ini-development /usr/local/php/etc/php.ini
[root@test ~]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
4, 编辑配置文件
[root@test ~]# cp /usr/local/php/etc/php.ini /usr/local/php/etc/php.ini.bak
[root@test ~]# vi /usr/local/php/etc/php.ini
修改以下内容,
####################################################################
short_open_tag = Off 改成 On
display_errors = On 改成 Off
display_startup_errors = On 改成 Off
track_errors = On 改成 Off
####################################################################
[root@test ~]# vi php-fpm.conf
修改以下内容修改后如下
####################################################################
[global]
error_log = log/php-fpm.log \\ 去掉注释
log_level = notice \\ 去掉注释[www]
user = nginx
group = nginx
listen = /dev/shm/php-fpm.sockpm = dynamic
pm.max_children = 50
pm.start_servers = 20
pm.min_spare_servers = 10
pm.max_spare_servers = 30####################################################################
第三: NGINX的安装和配置
1, 源码安装nginx
[root@test ~]# tar -zxvf nginx-1.2.4.tar.gz
[root@test ~]# cd nginx-1.2.4[root@test nginx-1.2.4]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx
[root@test nginx-1.2.4]# make
[root@test nginx-1.2.4]# make install
[root@test nginx-1.2.4]# cd
[root@test ~]# chown -R nginx.root /usr/local/nginx/*
2, 编辑配置文件
[root@test ~]# cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak
[root@test ~]# vi /usr/local/nginx/conf/nginx.conf
####################################################################
user nginx;
worker_processes 1;#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;#pid logs/nginx.pid;
events {
worker_connections 1024;
}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;
sendfile on;
#tcp_nopush on;#keepalive_timeout 0;
keepalive_timeout 65;#gzip on;
server {
listen 80 ; \\ 端口
server_name 192.168.211.13; \\ 服务器ip
index index.html index.php;
root /www/html; \\ 主目录location /
{
if (-f $request_filename) {
break;
}
if (!-f $request_filename) {
rewrite ^/(.+)$ /index.php?url=$1 last;break;
}error_page 404 /404.html;
location = /404.html {
root /www/html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /www/html;
}
}location ~ .*\.(php|php5)?$ \\ php部分
{
fastcgi_pass unix:/dev/shm/php-fpm.sock;
fastcgi_index index.php;
include fastcgi.conf;
}
location /p_w_picpaths/ { \\ 额外的目录
alias /var/www/p_w_picpaths/;
}
access_log /var/log/nginx/test.log; \\ 日志
}
}
####################################################################
3, 定义错误页面
[root@test ~]#
[root@test ~]# vi /usr/local/nginx/html/50x.html
####################################################################
The page you are looking for is temporarily unavailable.
Please try again later.
####################################################################
4, 启动服务
[root@test ~]# /usr/local/php/sbin/php-fpm -c /usr/local/php/etc/php.ini -y /usr/local/php/etc/php-fpm.conf
[root@test ~]# /usr/local/nginx/sbin/nginx
5, 验证程序启动
[root@test ~]# ps -aux | grep php 有如下内容,说明php-fpm已启动
php-fpm: master process[root@test ~]# ps -aux | grep nginx 有如下内容,说明nginx已启动
nginx: worker process[root@test ~]# netstat -tlnp 有80端口和nginx进程,说明nginx已启动
6, 加入开机启动
[root@test ~]# echo "/usr/local/php/sbin/php-fpm -c /usr/local/php/etc/php.ini -y /usr/local/php/etc/php-fpm.conf" >>/etc/rc.local
[root@test ~]# echo /usr/local/nginx/sbin/nginx >>/etc/rc.local关于以服务方式启动nginx的方法,请参看http://jedy82.blog.51cto.com/425872/890161
第四:测试:
[root@test ~]# mkdir /www/html && cp /usr/local/nginx/html/index.html /www/html/
[root@test ~]# vi /www/html/test.php
####################################################################
phpinfo();
?>
####################################################################
关闭selinux
打开防火墙的80端口
在浏览器里输入http://ip 出现nginx欢迎信息 说明nginx 启动成功
在浏览器里输入http://ip/test.php 出现php信息 说明nginx php 集成成功