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. 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 / {
index index.html index.htm;
}
## 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;###}##### denyaccess to .htroot aches, docifum #### 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;ss#ftls. ;#### ssl_ciphers high:!anull:!md5;#### ssl_prefer_server_ciphers on;#### location / {#### root html;##### index index.html index.htm;################ 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;##profprod-#pronxy 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 ^/(.*)$ //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-iphostremote_addr;
##如果 好吧 好吧 好吧D 她 她 她 她 她 hostr;
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 /var/webroot/www.jb51.net/.htaccess;
# 開啟網域跳轉,則當存取出錯後,其他網域會自動跳到www.jb51.net
server_name_in_redirect on;
}
7、安裝最新版本php( php5.3.14 )
#cd /usr /local/src/
tar xjvf php-5.3.14.tar.bz2
cd php-5.3.14執行:
如果報錯,可能是你的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 \
make && make install
cp php.ini-production /opt/web/php/etc/php.ini
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[path] = /usr/local/bin:/usr/bin:/bin
env[tmp] = /tmp
env[temp] = / tmp
8、啟動php-fpm
複製程式 程式碼如下:
/opt/web/php/sbin/php-fpm
10、在瀏覽器網址列輸入:http://php.jb51.net/tz.php
成功的話,可以看到phpinfo()輸出的資訊
以上是CentOS環境中怎麼部署nginx、php和虛擬主機的詳細內容。更多資訊請關注PHP中文網其他相關文章!