天天看點

linux搭建php5 nginx

1  php配置:

首先,我們需要下載下傳安裝php5

cd /usr/local/src/

wget  http://cn2.php.net/get/php-5.5.33.tar.bz2/from/this/mirror  

官網http://php.net/downloads.php

確定安裝之前有安裝gd,png,curl,xml等等lib開發庫。如果不确定,執行以下指令:

yum install gcc make gd-devel libjpeg-devel libpng-devel libxml2-devel bzip2-devel libcurl-devel -y

以下參數支援,ftp,圖檔函數,pdo等支援,因為使用了php自帶的mysqlnd,是以不需要額外安裝mysql的lib庫了.如果你是64位系統,參數後面加上–with-libdir=lib64,如果不是可以跳過。

tar -xjf php-5.5.0.tar.bz2

cd php-5.5.0

./configure --prefix=/usr/local/php-5.5.0 --with-config-file-path=/usr/local/php-5.5.0/etc --with-bz2 --with-curl --enable-ftp --enable-sockets --disable-ipv6 --with-gd --with-jpeg-dir=/usr/local --with-png-dir=/usr/local --with-freetype-dir=/usr/local --enable-gd-native-ttf

--with-iconv-dir=/usr/local --enable-mbstring --enable-calendar --with-gettext --with-libxml-dir=/usr/local --with-zlib --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd --with-mysql=mysqlnd --enable-dom --enable-xml --enable-fpm --with-libdir=lib64

make

make install

備注:如果php不需要curl和ftp的支援,可以将以上的–with-curl –enable-ftp去掉. 如果你是專業的linux從業人員,你完全可以看着help來選擇你的安裝參數,如果你不是的話,我建議你直接複制黏貼我的配置參數.這樣可以少走一些彎路.

已經安裝完成了php,下面我們針對php,配置php 

cp php.ini-production /usr/local/php-5.5.0/etc/php.ini

cp /usr/local/php-5.5.0/etc/php-fpm.conf.default /usr/local/php-5.5.0/etc/php-fpm.conf

其實我們隻是使用它預設提供給我們的一個配置,當然大家也可以根據自己需要進行修改配置資訊,然後進行啟動php-fpm

/usr/local/php-5.5.0/sbin/php-fpm

執行以上指令,如果沒報錯一般情況下表示啟動正常,如果不放心,也可以通過端口判斷是php否啟動

# netstat -lnt | grep 9000

tcp 0 0 127.0.0.1:9000 0.0.0.0:* listen

2 nginx配置

http://nginx.org/en/download.html

下載下傳 wget http://nginx.org/download/nginx-1.6.3.tar.gz

cd nginx-1.6.3

./configure --prefix=/usr/local/nginx-1.6.3 --with-http_ssl_module --with-http_spdy_module --with-http_stub_status_module --with-pcre

make 

紹,讓大家大緻明白生成配置檔案的介紹

–with-http_stub_status_module:支援nginx狀态查詢

–with-http_ssl_module:支援https

–with-http_spdy_module:支援google的spdy,想了解請百度spdy,這個必須有ssl的支援

–with-pcre:為了支援rewrite重寫功能,必須制定pcre

最後輸出如下内容,表示configure ok了。

checking for zlib library ... found

 creating objs/makefile

configuration summary

 + using system pcre library

 + using system openssl library

 + md5: using openssl library

 + sha1: using openssl library

 + using system zlib library

nginx path prefix: "/usr/local/nginx-1.5.1"

 nginx binary file: "/usr/local/nginx-1.5.1/sbin/nginx"

 nginx configuration prefix: "/usr/local/nginx-1.5.1/conf"

 nginx configuration file: "/usr/local/nginx-1.5.1/conf/nginx.conf"

 nginx pid file: "/usr/local/nginx-1.5.1/logs/nginx.pid"

 nginx error log file: "/usr/local/nginx-1.5.1/logs/error.log"

 nginx http access log file: "/usr/local/nginx-1.5.1/logs/access.log"

 nginx http client request body temporary files: "client_body_temp"

 nginx http proxy temporary files: "proxy_temp"

 nginx http fastcgi temporary files: "fastcgi_temp"

 nginx http uwsgi temporary files: "uwsgi_temp"

 nginx http scgi temporary files: "scgi_temp"

# make //确定你的伺服器有安裝make,如果沒有安裝請執行yum install make

 # make install

好了,nginx的安裝還是非常的簡單的,當然這裡沒有過多的介紹關于nginx遇到的問題。因為問題可能類型會非常的多,大家可以根據具體的錯誤的提示的内容,直接google或者百度一下就可以了。下面我們來說說關于nginx的啟動、關閉、重置nginx的内容。

啟動:直接執行以下指令,nginx就啟動了,不需要改任何配置檔案,nginx配置多域名虛拟主機請參考後續文章.

/usr/local/nginx-1.5.1/sbin/nginx

測試一下我們的nginx是否可以成功的啟動了

[root@ns conf]# curl -s http://localhost | grep nginx.com

nginx.com.

接下來說說如何進行把nginx的服務關閉掉,我們可以通過下面的方式

/usr/local/nginx-1.5.1/sbin/nginx -s stop

當然,在我們的nginx運作過程中,可能你需要修改nginx的相關配置,可以重置加載配置資訊

/usr/local/nginx-1.5.1/sbin/nginx -s reload

mkdir /data/logs/nginx/ # 用于存放nginx日志.請看2.3的配置檔案

mkdir -p /data/site/test.94cto.com/ # 站點根目錄

vim /data/site/test.94cto.com/info.php

新視窗

儲存退出,在nginx.conf的http斷中加上如下内容:

server {

listen 80;

server_name test.94cto.com;

access_log /data/logs/nginx/test.94cto.com.access.log main;

index index.php index.html index.html;

root /data/site/test.94cto.com;

location /

{

try_files $uri $uri/ /index.php?$args;

}

location ~ .*\.(php)?$

expires -1s;

try_files $uri =404;

fastcgi_split_path_info ^(.+\.php)(/.+)$;

include fastcgi_params;

fastcgi_param path_info $fastcgi_path_info;

fastcgi_index index.php;

fastcgi_param script_filename $document_root$fastcgi_script_name;

fastcgi_pass 127.0.0.1:9000;

配置講解 

nginx将會連接配接回環位址9000端口執行php檔案,需要使用tcp/ip協定,速度比較慢.建議大家換成使用socket方式連接配接。将fastcgi_pass 127.0.0.1:9000;改成fastcgi_pass unix:/var/run/phpfpm.sock;

啟動nginx 

/usr/local/nginx-1.4.1/sbin/nginx

通路測試

# curl http://test.94cto.com/info.php

test php