本文出自:http://http://www.huxun360.com/view_blog/27 http://galean.blog.51cto.com/7702012/1268352php
1.安裝pcre-develhtml
yum -y install gcc gcc-c++ rpm-build mysql* php* automake autoconf libtool make --skip-brokenmysql
pcre是Nginx 所需的pcre-devel庫,安裝pcre庫是爲了讓Nginx支持HTTPRewrite模塊
linux
pcre下載地址:http://down.51cto.com/data/904014nginx
[root@vb01 src]#unzip pcre-8.33.zipc++
[root@vb01 src]#cd pcre-8.33
[root@vb01 pcre-8.33]#./configuresql
[root@vb01 pcre-8.33]#make瀏覽器
[root@vb01 pcre-8.33]#make install服務器
2. 安裝nginxapp
下載地址:http://nginx.org/ 這裏下載的是nginx-1.4.2穩定版
[root@vb01 src]#tar zxvf nginx-1.4.2.tar.gz
[root@vb01 src]# cd nginx-1.4.2
[root@vb01 nginx-1.4.2]# ./configure --help #查看可以使用的模塊,好比:
--prefix=PATH set installation prefix # 設置安裝目錄爲PATH
--with-http_stub_status_module enable ngx_http_stub_status_module #用來啓用NginxStatus功能,以監控Nginx的當前狀態。
--with-google_perftools_module enable ngx_google_perftools_module #使Nginx支持google-perftools的模塊,使用TCMALLOC優化Nginx的性能時會用到。
[root@vb01 nginx-1.4.2]# ./configure --with-http_stub_status_module --prefix=/usr/local/nginx
[root@vb01 nginx-1.4.2]# make
[root@vb01 nginx-1.4.2]# make install
3.Nginx的全局配置
Nginx的整個配置配置文件是以block的形式組織的,每一個block通常以一個大括號「{}」來表示,block能夠分爲幾個層次,整個配置文件中main指令位於最高層,在main層下面能夠有Events,HTTP等層級,而在HTTP層中又包含server層,即server block,server block中又可分爲location層,而且一個server block中能夠包含多個location block.
[root@vb01 conf]# vi /usr/local/nginx/conf/nginx.conf
user nobody; #設置Nginx Worker進程運行的用戶,默認是nobody不用管。
worker_processes 1; #指定Nginx開啓的進程數,默認個就夠了
#error_log logs/error.log;
error_log logs/error.log notice; #定義錯誤日誌文件輸出文件和級別
#error_log logs/error.log info;
pid logs/nginx.pid; #Nginx進程ID存儲的位置
google_perftools_profiles /tmp/tcmalloc;
events {
worker_connections 1024; #指定鏈接數上限
}
4.http模塊的配置
http {
include mime.types; #配置文件包含文件的設定,特別對於多虛擬主機的設置時能夠減少主配置文件的複雜度。
default_type application/octet-stream; #設定默認類型爲二進制流,當文件類型未定義時使用這種類型,例如在沒有配置PHP環境時,Nginx是不予解析的,此時用瀏覽器訪問PHO文件就會出現下載窗口。
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;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
#location用於匹配網頁位置
location / {
root html; #設置網頁根目錄
index index.html index.htm; #index設置默認首頁地址
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
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;
keepalive_timeout 65;
5. 添加一臺server虛擬主機www.galean.com,根目錄爲/var/www/galean
server{
listen 80;
server_name www.galean.com;
access_log logs/galean.access.log main;
location / {
index index.html;
root /var/www/galean;
}
}
6.Nginx的啓動
/usr/local/nginx/sbin/nginx -t # 檢測配置文件的準確性,若是配置文件非默認目錄需用 "-c" 指定配置文件位置
/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
/usr/local/nginx/sbin/nginx -v #查看版本信息
nginx version: nginx/1.4.2
啓動Nginx
/usr/local/nginx/sbin/nginx
查看Nginx是否啓動
[root@vb01 conf]# ps -ef|grep nginx
nobody 13254 13253 0 10:12 ? 00:00:00 php-fpm: pool nginx
nobody 13255 13253 0 10:12 ? 00:00:00 php-fpm: pool nginx
nobody 13256 13253 0 10:12 ? 00:00:00 php-fpm: pool nginx
nobody 13257 13253 0 10:12 ? 00:00:00 php-fpm: pool nginx
nobody 13258 13253 0 10:12 ? 00:00:00 php-fpm: pool nginx
root 21621 1 0 14:47 ? 00:00:00 nginx: master process /opt/nginx/sbin/nginx
nobody 21622 21621 0 14:47 ? 00:00:00 nginx: worker process
打開瀏覽器 輸入IP能夠看到以下,說明服務器啓動成功
Nginx的關閉:
Nginx的關閉:kill -QUIT pid
Nginx的平滑重啓:kill -HUP pid
QUIT:表示處理玩當前請求後,關閉進程
HUP:表示從新加載配置,也就是關閉原有的進程,並開啓新的工做進程,此操做不會中斷用戶的訪問請求。
USER1:用於Nginx的日誌切換,也就是從新打開一個日誌文件。
USER2: 用於平滑升級可執行程序。
WINCH:從容關閉工做進程。
7.安裝PHP和PHP-FPM
我用的是yum默認安裝PHP,若是是編譯安裝在./configure編譯選項中要加上「--enable-fastcgi」來啓用PHP對FastCGI的支持,「--enable-fpm」是激活對FastCGI模式的fpm支持。
yum -y install php*
PHP全局配置文件是:/etc/php.ini
PHP-FPM引擎的配置文件是:/etc/php-fpm.conf
這裏咱們先看如何啓動 php-fpm:
1,查找php-fpm在sbin下的可執行啓動文件的位置,個人默認是/usr/sbin/php-fpm
[root@vb01 src]# locate php-fpm
/etc/php-fpm.conf #配置文件
/usr/sbin/php-fpm #啓動文件
2.查看可選項:
[root@vb01 src]# /usr/sbin/php-fpm -h
Usage: php-fpm [-n] [-e] [-h] [-i] [-m] [-v] [-t] [-p <prefix>] [-g <pid>] [-c <file>] [-d foo[=bar]] [-y <file>] [-D] [-F]
-c <path>|<file> Look for php.ini file in this directory
-n No php.ini file will be used
-d foo[=bar] Define INI entry foo with value 'bar'
-e Generate extended information for debugger/profiler
-h This help
-i PHP information
-m Show compiled in modules
-v Version number
-p, --prefix <dir>
Specify alternative prefix path to FastCGI process manager (default: /usr).
-g, --pid <file>
Specify the PID file location.
-y, --fpm-config <file>
Specify alternative path to FastCGI process manager config file.
-t, --test Test FPM configuration and exit
-D, --daemonize force to run in background, and ignore daemonize option from config file
-F, --nodaemonize
force to stay in foreground, and ignore daemonize option from config file
-R, --allow-to-run-as-root
Allow pool to run as root (disabled by default)
根據上邊提示 「-D」爲後臺啓動服務
[root@vb01 src]# /usr/sbin/php-fpm -D
查看是否啓動成功,php-fpm默認啓動監聽的是9000端口:
[root@vb01 src]# netstat -tnlpu|grep 9000
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 5841/php-fpm
那如何關閉php-fpm呢?可以使用以下命令關閉:
kill -QUIT ‘cat /var/run/php-fpm/php-fpm.pid’
8.配置Nginx來支持php
下面是Nginx下支持PHP解析的一個虛擬主機配置實例:
server{
listen 80;
server_name www.galean1.com;
location / {
index index.html index.php;
root /var/www/galean1;
}
location ~ \.php$ {
root /var/www/galean1;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
注意:
location指定的是PHP後綴的文件都交給127.0.0.1:9000來處理,這裏的IP地址和端口就是FastCGI進程監聽的IP地址和端口。
fastcgi_param指定PHP動態程序的主目錄,我這裏是$document_root,也就是/var/www/galean1。
接下來,從新啓動Nginx
測試Nginx對PHP的解析
[root@vb01 conf]# echo '<?php phpinfo();?>' > /var/www/galean1/phpinfo.php
而後在瀏覽器裏輸入http://www.galean1.com/phpinfo.php 看是否正確解析,會出現PHP安裝配置以及功能列表統計信息。
關於nginx啓動出錯,提示:/usr/local/nginx1.5/sbin/nginx: error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory
缺乏lib文件致使
[root@htuidc lib]# ldd /usr/local/nginx1.5/sbin/nginx
linux-vdso.so.1 => (0x00007fff9fb20000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x00007fc861b2f000)
libcrypt.so.1 => /lib64/libcrypt.so.1 (0x00007fc8618f8000)
libpcre.so.1 => not found
libc.so.6 => /lib64/libc.so.6 (0x00007fc861564000)
/lib64/ld-linux-x86-64.so.2 (0x00007fc861d52000)
libfreebl3.so => /lib64/libfreebl3.so (0x00007fc861302000)
libdl.so.2 => /lib64/libdl.so.2 (0x00007fc8610fd000)
能夠看出 libpcre.so.1 => not found 並無找到,進入/lib目錄中手動連接下
[root@htuidc lib]# cd /lib64
[root@htuidc lib64]# ln -s libpcre.so.0.0.1 libpcre.so.1
[root@htuidc lib64]# ldd /usr/local/nginx1.5/sbin/nginx
linux-vdso.so.1 => (0x00007fff3dfff000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x00007fb41ff27000)
libcrypt.so.1 => /lib64/libcrypt.so.1 (0x00007fb41fcf0000)
libpcre.so.1 => /lib64/libpcre.so.1 (0x00007fb41fac3000)
libc.so.6 => /lib64/libc.so.6 (0x00007fb41f730000)
/lib64/ld-linux-x86-64.so.2 (0x00007fb42014a000)
libfreebl3.so => /lib64/libfreebl3.so (0x00007fb41f4ce000)
libdl.so.2 => /lib64/libdl.so.2 (0x00007fb41f2c9000)