天天看点

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