部署時間:2012-07-24
OS環境:CentOS 6.1
nginx:nginx-1.2.2
PHP:PHP5.3.14
0、安裝依賴包
複製代碼 代碼如下:yum install openssl-devel pcre-devel zlib-devel libjpeg-devel libpng-devel freetype-devel gcc make
1、添加 www 使用者用來執行nginx 複製代碼 代碼如下:useradd -M -r -s /sbin/nologin -d /opt/web/ www
2、建立臨時目錄 複製代碼 代碼如下:mkdir -p /var/tmp/nginx/client/
mkdir -p /var/tmp/nginx/proxy/
mkdir -p /var/tmp/nginx/fcgi/
3、下載nginx最新穩定版原始碼 複製代碼 代碼如下:cd /usr/local/src/
wget http://nginx.org/download/nginx-1.2.2.tar.gz
4、解壓,編譯,安裝 複製代碼 代碼如下:tar vxzf nginx-1.2.2.tar.gz
cd nginx-1.2.2/
./configure \
--prefix=/opt/web/nginx \
--error-log-path=/var/log/nginx/error.log \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--user=www \
--group=www \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--http-log-path=/var/log/nginx/access.log \
--http-client-body-temp-path=/var/tmp/nginx/client/ \
--http-proxy-temp-path=/var/tmp/nginx/proxy/ \
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \
--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi/
make
make install
5、配置nginx 複製代碼 代碼如下:vim /opt/web/nginx/conf/nginx.conf
# 指定啟動使用者:
user www www;
# 進程數量,nginx作者認為一個就可以,根據自己的訪問量修改
worker_processes 1;
# 設定錯誤記錄檔:
#error_log logs/error.log notice;
#error_log logs/error.log info;
error_log /var/log/nginx/error.default.log;
pid /opt/web/nginx/nginx.pid;
events {
use epoll;
worker_connections 1024;
}
http {
charset utf-8;
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;
tcp_nodelay on;
#keepalive_timeout 0;
keepalive_timeout 65;
gzip on;
gzip_min_length 1000;
gzip_proxied any;
gzip_types text/plain text/css text/xml
application/x-javascript application/xml
application/atom+xml text/javascript;
server {
listen 80;
server_name localhost;
charset utf-8;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
#fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
#include fastcgi_params;
include fastcgi.conf;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443;
# server_name localhost;
# ssl on;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_timeout 5m;
# ssl_protocols SSLv2 SSLv3 TLSv1;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
proxy_read_timeout 200;
# Only retry if there was a communication error, not a timeout
# on the Tornado server (to avoid propagating "queries of death"
# to all frontends)
proxy_next_upstream error;
proxy_set_header X-Scheme $scheme;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# 引入虛擬機器主機檔案
include /opt/web/nginx/conf/sites/*.conf;
}
6、建立虛擬機器組態檔存放的目錄 複製代碼 代碼如下: mkdir /opt/web/nginx/conf/sites
這樣配置後,需要新增加虛擬機器主機的直接在 nginx/conf/sites/目錄下,添加設定檔即可
例如:現在有 www.jb51.net 網域名稱
建立:/opt/web/nginx/conf/sites/www.jb51.net.conf 檔案
內容如下: 複製代碼 代碼如下:server {
listen 80;
client_max_body_size 10M;
#多個網域名稱用空格分割,第一個為預設
server_name www.jb51.net jb51.net;
charset UTF-8;
index index.html index.htm index.php;
# 定義根目錄
set $root /var/webroot/www.jb51.net/;
# 佈建網站路徑
root $root;
# 防止瀏覽目錄
autoindex off;
if ($host != 'www.jb51.net') {
rewrite ^/(.*)$ http://www.jb51.net/$1 permanent;
}
# 防止.htaccess檔案被請求
location ~ /\.ht {
deny all;
}
error_page 404 /404.html;
index index.html index.htm;
location /uploads/ {
alias /data/webroot/www.jb51.net/uploads/;
}
try_files $uri @uwsgi;
location @uwsgi{
# 將其它的請求轉交給uwsgi
include uwsgi_params;
uwsgi_pass unix:/tmp/360ito_uwsgi.sock;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#proxy_pass http://localhost:5000;
}
# 將php類型的請求轉交給fastcgi
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
# 訪問日誌:
access_log /var/log/nginx/access.www.jb51.net.log;
# 載入.htaccess重寫檔案,注意,這裡不支援變數路徑
# 不能寫成 include $root/www.jb51.net/.htaccess;
# include /var/webroot/www.jb51.net/.htaccess;
# 開啟網域名稱跳轉,則當訪問出錯後,其他網域名稱會自動跳轉到 www.jb51.net
# 注意,這裡我說的是,僅僅當訪問出錯後,才會跳轉,所以,這裡並不能實現301重新導向!
server_name_in_redirect on;
}
7、安裝最新版本PHP( PHP5.3.14 ) 複製代碼 代碼如下:cd /usr/local/src/
wget http://cn.php.net/get/php-5.3.14.tar.bz2/from/this/mirror
tar xjvf php-5.3.14.tar.bz2
cd php-5.3.14
執行: 複製代碼 代碼如下:./buildconf --force
如果報錯,可能是你的 autoconf不是 2.13 版本的,PHP5.3.系列的bug,需要安裝 autoconf為2.13的版本: 複製代碼 代碼如下:CentOS : # yum install autoconf213
Debian : # apt-get install autoconf2.13
設定環境變數 複製代碼 代碼如下:# CentOS :
export PHP_AUTOCONF="/usr/bin/autoconf-2.13"
# Debian :
export PHP_AUTOCONF="/usr/bin/autoconf2.13"
再次運行:./buildconf --force ,出現 buildconf: autoconf version 2.13 (ok)
,則表示成功。
編譯安裝 PHP 複製代碼 代碼如下:./configure \
--prefix=/opt/web/php \
--with-config-file-path=/opt/web/php/etc \
--with-config-file-scan-dir=/opt/web/php/etc/conf.d \
--enable-fpm \
--with-fpm-user=www \
--with-fpm-group=www \
--with-mysql=/opt/db/Percona-Server-5.5.14-rel20.5 \
--with-mysqli=/opt/db/Percona-Server-5.5.14-rel20.5/bin/mysql_config \
--with-iconv-dir \
--with-freetype-dir \
--with-jpeg-dir \
--with-png-dir \
--with-zlib \
--with-libxml-dir \
--enable-xml \
--enable-mbstring \
--with-gd \
--enable-gd-native-ttf \
--with-openssl \
--enable-inline-optimization
make && make install
cp php.ini-production /opt/web/php/etc/php.ini
cd /opt/web/php/etc
cp php-fpm.conf.default php-fpm.conf
修改php-fpm.conf 啟用如下幾行,即去掉前面的分號(;) 複製代碼 代碼如下:pid = run/php-fpm.pid
error_log = log/php-fpm.log
log_level = notice
listen = 127.0.0.1:9000
listen.allowed_clients = 127.0.0.1
listen.owner = www
listen.group = www
listen.mode = 0666
user = www
group = www
pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 500
env[HOSTNAME] = $HOSTNAME
env[PATH] = /usr/local/bin:/usr/bin:/bin
env[TMP] = /tmp
env[TMPDIR] = /tmp
env[TEMP] = /tmp
8、啟動php-fpm 複製代碼 代碼如下:/opt/web/php/sbin/php-fpm
啟動nginx 複製代碼 代碼如下:/opt/web/nginx/sbin/nginx
9、測試一下 複製代碼 代碼如下: vim /var/webroot/www.jb51.net/tz.php
輸入和儲存 複製代碼 代碼如下:<?PHP
phpinfo();
?>
10、在瀏覽器地址欄輸入:http://php.jb51.net/tz.php
成功的話,可以看到phpinfo()輸出的資訊