您的位置:首页 > 运维架构 > Nginx

Linux下 nginx + php 环境搭建

2015-08-01 13:30 573 查看
一、 安装nginx

1. 在/etc/yum.repos.d/ 目录下创建以文件 nginx.repo

vi /etc/yum.repos.d/nginx.repo

内容如下:
[nginx] name=nginx repo baseurl=http://nginx.org/packages/rhel/6/$basearch/ gpgcheck=0 enabled=1

2. 安装nginx

# yum install nginx

二、安装MySQL

http://blog.sina.com.cn/s/blog_505bf9af01012kkh.html

三、安装php5.2.13和php-5.2.13-fpm-0.5.13

安装php需要安装其他依赖:libxml2和libxml2-devel包

yum
install libxml2

yum install libxml2-devel -y

1. 下载 php-5.2.13.tar.gz 和 php-5.2.13-fpm-0.5.13.diff.gz

2. 安装

# tar zxvf php-5.2.13.tar.gz

# gzip -cd php-5.2.13-fpm-0.5.13.diff.gz | patch -d php-5.2.13 -p1

# cd php-5.2.13

# ./configure --prefix=/usr/local/php --enable-fastcgi --enable-fpm --with-mysql=/usr/local/mysql

# make

# make install

3. 配置php

cp php.ini-dist /usr/local/lib/php.ini

4. 管理FastCGI进程

/usr/local/php/bin/php-cgi --fpm 或者 /usr/local/php/sbin/php-fpm start

建议采用第二种方式启动FastCGI进程。

如果执行 /usr/local/php/sbin/php-fpm start 报错,一般是linux权限问题,请给/usr/local/php目录读写权限。

/usr/local/php/sbin/php-fpm还有其他参数,具体为start|stop|quit|restart|reload|logrotate。

每个启动参数的含义如下 start,启动PHP的FastCGI进程。

stop,强制终止PHP的FastCGI进程。

quit,平滑终止PHP的FastCGI进程。

restart,重启PHP的FastCGI进程。

reload,重新加载PHP的php.ini。

logrotate,重新启用log文件。

reload是个很重要的参数,它可以在PHP的FastCGI进程不中断的情况下重新加载改动过的php.ini,因此通过php-fpm可以平滑变更FastCGI模式下的PHP设置。

在FastCGI进程启动后,其监听的IP地址和端口也随即启动,可以通过ps和netstat查看相关信息。

# netstat -antl|grep 9000

# ps -ef|grep php-cgi

四、配置Nginx来支持PHP

1. 配置/etc/nginx/conf.d/default.conf
server {

listen 80;

server_name localhost;

#charset koi8-r;

#access_log /var/log/nginx/log/host.access.log main;

location / {

root /etc/nginx/html;

index index.html index.htm index.php;

}

#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 /etc/nginx/html;

}

# 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;

fastcgi_param SCRIPT_FILENAME /etc/nginx/html$fastcgi_script_name;

include fastcgi_params;

}

}
或者 在/etc/nginx/conf.d 创建一个配置文件testlink.conf,内容为:

server {

listen 80;

server_name testlink.conversant.com.cn;

location / {

root /opt/app/testlink;

index index.php index.html index.htm;

}

# 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;

fastcgi_param SCRIPT_FILENAME /opt/app/testlink$fastcgi_script_name;

include fastcgi_params;

}

}

2. 启动nginx

# nginx

3. 重新启动 php-fpm

/usr/local/php/sbin/php-fpm restart

五、测试Nginx对PHP的解析功能

这里在/etc/nginx/html目录下创建一个phpinfo.php文件,内容如下:

<?php phpinfo(); ?>

重新启动 php-fpm

# /usr/local/php/sbin/php-fpm restart

重启 nginx

# nginx -s reload

然后通过浏览器访问http://192.168.1.32/,默认会在浏览器显示“Welcome to Nginx!”表示Nginx正常运行。

接着在浏览器中访问http://192.168.1.32/phpinfo.php,如果PHP能够正常解析,会出现PHP安装配置以及功能列表统计信息。

至此安装配置nginx php完成。

参考文章: /article/4225201.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: