1、http://nginx.org/下载最新的nginx

现在最新的版本是nginx-1.9.1   下载.tar.gz包 ,解压。

timeless@timeless-HP-Pavilion-g4-Notebook-PC:/usr/local/src/nginx-1.9.$ ./configure

configure 过程中可能会遇到错误

./configure: error: the HTTP rewrite module requires the PCRE library.   //大体意思是重写模块需要pcre模块
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.

解决办法

http://www.pcre.org/       编译安装便可

执行操作

./configure  &&make &&make install

到现在为止,已经安装nginx ,接下来我们要把 nginx 与php 结合起来。

2、首先介绍知识:

nginx本身不能处理PHP,它只是个web服务器,当接收到请求后,如果是php请求,则发给php解释器处理,并把结果返回给客户端。

nginx一般是把请求发fastcgi管理进程处理,fascgi管理进程选择cgi子进程处理结果并返回被nginx

本文以php-fpm为例介绍如何使nginx支持PHP

什么是PHP-FPM

PHP-FPM是一个PHP FastCGI管理器,是只用于PHP的,可以在 http://php-fpm.org/download下载得到.

PHP-FPM其实是PHP源代码的一个补丁,旨在将FastCGI进程管理整合进PHP包中。必须将它patch到你的PHP源代码中,在编译安装PHP后才可以使用。

新版PHP已经集成php-fpm了,不再是第三方的包了,推荐使用。PHP-FPM提供了更好的PHP进程管理方式,可以有效控制内存和进程、可以平滑重载PHP配置,比spawn-fcgi具有更多优点,所以被PHP官方收录了。

就是FastCGI Process Manager,是一种可选的PHP FastGCI执行模式,有一点很有特点的应用,尤其是一个繁忙的网站中:

(1) 可适应的进行再生(NEW!)

(2) 基本的统计功能(Apache's mod_status)

(3) 高级进程管理功能,能够优雅的停止/开始

(4) 能够使用不同的工作用户和不同的php.ini

(5) 输入,输出日志记录...

新的版本的php在./configure的时候带 –enable-fpm参数即可开启PHP-FPM,其它参数都是配置php的,具体选项含义可以查看这里

下面是我php 编译安装时候的选项:

./configure  --prefix=/usr/local/php/ --with-config-file-path=/etc/php5/cli/  --with-config-file-scan-dir=/etc/php5/mods-available/  --with-apxs2=/usr/local/apache243/bin/apxs  --with-mysql   --with-libxml-dir=/usr/local/libxml2/ --with-png-dir=/usr/local/libpng/ --with-jpeg-dir=/usr/local/jpeg8/ --with-freetype-dir=/usr/local/freetype/  --with-zlib-dir=/usr/local/zlib/ --with-mcrypt=/usr/local/libmcrypt/ --with-mysqli  --enable-soap --enable-mbstring=all --enable-sockets --enable-memcache --with-pdo-mysql --with-pdo-pgsql --with-pgsql --with-curl -with-mcrypt  --enable-ftp  --with-gd --enable-gd-native-ttf  --with-openssl  --with-mhash  --enable-pcntl  --enable-sockets  --with-xmlrpc   --enable-zip  --enable-soap  --without-pear  --with-gettext  --disable-fileinfo --enable-magic-quotes --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex  --enable-mbstring  --enable-fpm

3、php编译安装完成,下面是对php-fpm运行用户进行设置

cd /usr/local/php
cp etc/php-fpm.conf.default etc/php-fpm.conf
vi etc/php-fpm.conf

修改
user = www-data
group = www-data

如果www-data用户不存在,那么先添加www-data用户
groupadd www-data
useradd -g www-data www-data

4、配置 nginx.conf文件

其中server段增加如下配置,注意标红内容配置,否则会出现No input file specified.错误

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:
#
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:; //端口不可以自己更改
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

5、创建测试文件

创建php文件

在/usr/local/nginx/html下创建index.php文件,输入如下内容

<?php
echo phpinfo();
?>

6、出现问题:

Sorry, the page you are looking for is currently unavailable.
Please try again later.

If you are the system administrator of this resource then you should check theerror log for details.

Faithfully yours, nginx.

先在一个目录下运行下test.html和test.php,结果html可以运行,php无法运行。

证实是php没有启动,我刚才也检测过php的进程,的确是没有php进程,找到:

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

开启后,一切恢复正常!

7、下面介绍一个  nginx 跟 php-fpm 相关的操作

nginx

(1)启动  

cd usr/local/nginx/sbin
./nginx
cd usr/local/nginx/sbin
./nginx

(2)重启

  更改配置重启nginx  

kill -HUP 主进程号或进程号文件路径
或者使用
cd /usr/local/nginx/sbin
./nginx -s reload

(3)判断配置文件是否正确 

nginx -t -c /usr/local/nginx/conf/nginx.conf
或者
cd /usr/local/nginx/sbin
./nginx -t

(4)关闭

  查询nginx主进程号

  ps -ef | grep nginx

  从容停止   kill -QUIT 主进程号

  快速停止   kill -TERM 主进程号

  强制停止   kill -9 nginx

  若nginx.conf配置了pid文件路径,如果没有,则在logs目录下

  kill -信号类型 '/usr/local/nginx/logs/nginx.pid'

php-fpm

(1)启动

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

php-fpm 5.4.7 如何关闭 重启?

php 5.4.7 下的php-fpm 不再支持 php-fpm 以前具有的 /usr/local/php/sbin/php-fpm (start|stop|reload)等命令,需要使用信号控制:

master进程可以理解以下信号

INT, TERM 立刻终止 QUIT 平滑终止 USR1 重新打开日志文件 USR2 平滑重载所有worker进程并重新载入配置和二进制模块

示例:

php-fpm 关闭:

kill -INT `cat /usr/local/php/var/run/php-fpm.pid`

php-fpm 重启:

kill -USR2 `cat /usr/local/php/var/run/php-fpm.pid`

可能遇到的问题:

登陆服务器之后进到nginx使用./nginx -s reload重新读取配置文件,发现报nginx: [error] open() "/usr/local/nginx1.9.1/logs/nginx.pid" failed (2: No such file or directory)错误,进到logs文件发现的确没有nginx.pid文件

[root@localhost sbin]# ./nginx -s reload

nginx: [error] open() "/usr/local/nginx/logs/nginx.pid" failed (2: No such file or directory)

解决方法:

root@timeless-HP-Pavilion-g4-Notebook-PC:/usr/local/nginx1.9.1/sbin#  /usr/local/nginx1.9.1/sbin/nginx -c /usr/local/nginx1.9.1/conf/nginx.conf
root@timeless-HP-Pavilion-g4-Notebook-PC:/usr/local/nginx1.9.1/sbin# ./nginx -s reload

总用量 12

-rw-r--r-- 1 root root 1246 12月 9 18:10 access.log

-rw-r--r-- 1 root root 516 12月 10 15:39 error.log

-rw-r--r-- 1 root root 5 12月 10 15:38 nginx.pid

看nginx.pid文件已经有了

nginx 环境搭建使用之入门的更多相关文章

  1. 安卓自动化测试(2)Robotium环境搭建与新手入门教程

    Robotium环境搭建与新手入门教程 准备工具:Robotium资料下载 知识准备: java基础知识,如基本的数据结构.语法结构.类.继承等 对Android系统较为熟悉,了解四大组件,会编写简单 ...

  2. Grunt JS构建环境搭建以及使用入门

    Grunt JS构建环境搭建以及使用入门 1.应用场景 一种自动化任务处理工具,对于日常的需求(代码规则检查.代码合并)可以实现自动化执行,只需要保留package.json和Gruntfile.js ...

  3. scala 入门Eclipse环境搭建及第一个入门经典程序HelloWorld

    scala 入门Eclipse环境搭建及第一个入门经典程序HelloWorld 学习了: http://blog.csdn.net/wangmuming/article/details/3407911 ...

  4. Linux下PHP+Nginx环境搭建

    PHP+Nginx环境搭建 作者:王宇阳( Mirror )^_^ 参考文章: ​ Nginx+PHP+MySQL安装参考 ​ PHP源码安装经验 ​ PHP源码环境搭建过程中常见问题 CentOS环 ...

  5. Nginx环境搭建与使用

    一.背景 之前测试的项目前后端的"路由"(负责把前端发过来的请求转发到相应的后端服务上)要用Nignx来取代原来的tomcat的http server功能,做这个替换的原因是Nig ...

  6. Nginx 环境搭建 (windows)

    Nginx 环境搭建 (windows) 资源 # nginx在线文档和支持 For online documentation and support please refer to nginx.or ...

  7. 【Nginx(一)】Centos下 Nginx环境搭建

    Nginx环境搭建 系统环境: 腾讯云 Linux Centos7 1.下载nginx压缩包 ,上传到腾讯云服务器 http://nginx.org/en/download.html 或者使用dock ...

  8. Docker学习之——Node.js+MongoDB+Nginx环境搭建(一)

    最近在学习Node.js相关知识,在环境搭建上耗费了不少功夫,故此把这个过程写下来同大家分享一下,今天我先来介绍一下Docker,有很多人都写过相关知识,还有一些教程,在此我只想写一下,我的学习过程中 ...

  9. Linux下.Net Core+Nginx环境搭建小白教程

    前言 对于接触.Net Core的我们来说之前从未接触过Linux,出于资源和性能及成本的考虑我们可能要将我们的环境搬到Linux下,这对于我们从未接触过Linux的童鞋们来说很棘手,那么我今天将带你 ...

随机推荐

  1. 使用devpartner的blockchecker检查c++内存错误

    在仿写stl的过程中,被一处内存错误卡了很久.当内存池需要多次malloc时会出现堆损坏的错误,初步判断是数组越界,但总是检查不出来.一开始用Dr.Memory检查不出来,就试了一下devpartne ...

  2. 网络子系统41_inet_peer平衡二叉树的删除

    //1.p存在左孩子,则使用p的左孩子的最右孩子替换p,然后重平衡树 //2.p不存在左孩子,则使用p的右孩子替换p,然后重平衡树 1.1 static void unlink_from_pool(s ...

  3. Oracle中trunc函数、round 函数、ceil函数和floor函数的使用

    Oracle中trunc函数.round 函数.ceil函数和floor函数的使用 1.1trunc函数处理数字 trunc函数返回处理后的数值,其工作机制与ROUND函数极为类似,只是该函数不对指定 ...

  4. 2013腾讯编程马拉松初赛第一场(3月21日) 湫湫系列故事——减肥记II ----线段树

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=4509 虽然制定了减肥食谱,但是湫湫显然克制不住吃货的本能,根本没有按照食谱行动! 于是,结果显而易见… 但 ...

  5. [置顶] Android布局管理器 - 详细解析布局实现

    布局管理器都是以ViewGroup为基类派生出来的; 使用布局管理器可以适配不同手机屏幕的分辨率,尺寸大小; 布局管理器之间的继承关系 : 在上面的UML图中可以看出, 绝对布局 帧布局 网格布局 相 ...

  6. 用Seam实现:图片上传 + 保存到数据库 + 从数据库读出图片并显示到页面中

    上传图片并保存到数据库 seam给我们提供了 s:fileUpload 标签以完成文件上传功能.使用该标签时,要在web.xml中声明一个Seam的过滤器: <filter> <fi ...

  7. Windows Live Writer的Markdown插件

    我新写了一个Windows Live Writer的Markdown插件,代码放在了github上. 介绍 这个项目是一个Windows Live Writer的Markdown插件.有了这个插件,你 ...

  8. 【设计模式 - 14】之命令模式(Command)

    1      模式简介 命令模式的定义: 命令模式将命令封装成对象,从而使调用一个命令变为调用一个对象的指定方法. 命令模式的优点: 1)        降低了系统耦合度: 2)        新的命 ...

  9. Swing的设计是MVC的典范

    无论你的项目是否用到了Swing技术,我都要说,Swing是一个设计优秀的Java包,它充满了大师的智慧.假设你学了Java却连一个Button还不会写,就象你学习Visual Basic却不会用Bu ...

  10. java websockect

    https://github.com/TooTallNate/Java-WebSocket (websockect类库包) http://blog.openlg.net/index.php/archi ...