Linux系统下Nginx+PHP 环境安装配置

一、编译安装Nginx

  官网:http://wiki.nginx.org/Install

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

  # tar -zvxf nginx-1.10.3.tar.gz

  # cd nginx-1.10.3

  # ./configure --prefix=/usr/local/nginx

  # make && make install

  1.安装Nginx时报错

    a.错误提示:checking for OS
           + Linux 2.6.32-431.el6.x86_64 x86_64

          checking for C compiler ... not found

     执行指令  # yum -y install gcc gcc-c++ autoconf automake make

        如果安装报错可尝试如下指令后在安装gcc和gcc-c++: 

      1.     yum clean all
      2.     yum makecache

    b.错误提示:./configure: error: the HTTP rewrite module requires the PCRE library.

      安装pcre-devel解决问题
        # yum -y install pcre-devel


     错误提示:./configure: error: the HTTP cache module requires md5 functions
        from OpenSSL library. You can either disable the module by using
        --without-http-cache option, or install the OpenSSL library into the system,
        or build the OpenSSL library statically from the source with nginx by using
        --with-http_ssl_module --with-openssl=<path> options.

    解决办法:

    # yum -y install openssl openssl-devel

    或者一并安装:

    # yum -y install pcre-devel openssl openssl-devel

   c.错误提示:./configure: error: the HTTP gzip module requires the zlib library.

    # yum install -y zlib-devel

  2.启动nginx

  # /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

    或者

       # /usr/local/nginx/sbin/nginx 

        重启nginx

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

   停止操作
  停止操作是通过向nginx进程发送信号(什么是信号请参阅linux文 章)来进行的

  步骤1:查询nginx主进程号
    ps -ef | grep nginx
   在进程列表里 面找master进程,它的编号就是主进程号了。
  步骤2:发送信号
  从容停止Nginx:
    kill -QUIT 主进程号
  快速停止Nginx:
    kill -TERM 主进程号
  强制停止Nginx:
    pkill -9 nginx

  3.安装成功查看nginx进程

  # ps aux | grep nginx

 

  4.测试页面,直接输入服务器ip


   

  扩展:nginx配置文件 分析

    nginx.conf内容如下(只截取了没被注掉的部分):

 1 # nginx运行的用户名
 2 user nginx;
 3 # nginx启动进程,通常设置成和cpu的数量相等,这里为自动
 4 worker_processes auto;
 5 
 6 # errorlog文件位置
 7 error_log /var/log/nginx/error.log;
 8 # pid文件地址,记录了nginx的pid,方便进程管理
 9 pid /run/nginx.pid;
10 
11 # Load dynamic modules. See /usr/share/nginx/README.dynamic.
12 # 用来加载其他动态模块的配置
13 include /usr/share/nginx/modules/*.conf;
14 
15 # 工作模式和连接数上限
16 events {
17     # 每个worker_processes的最大并发链接数
18     # 并发总数:worker_processes*worker_connections
19     worker_connections 1024;
20 }
21 
22 # 与提供http服务相关的一些配置参数类似的还有mail
23 http {
24     # 设置日志的格式
25     log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
26                       '$status $body_bytes_sent "$http_referer" '
27                       '"$http_user_agent" "$http_x_forwarded_for"';
28 
29     # access_log记录访问的用户、页面、浏览器、ip和其他的访问信息
30     access_log  /var/log/nginx/access.log  main;
31 
32     # 这部分下面会单独解释
33     # 设置nginx是否使用sendfile函数输出文件
34     sendfile            on;
35     # 数据包最大时发包(使用Nagle算法)
36     tcp_nopush          on;
37     # 立刻发送数据包(禁用Nagle算法)
38     tcp_nodelay         on;
39     # 链接超时时间
40     keepalive_timeout   65;
41     # 这个我也不清楚...
42     types_hash_max_size 2048;
43 
44     # 引入文件扩展名与文件类型映射表
45     include             /etc/nginx/mime.types;
46     # 默认文件类型
47     default_type        application/octet-stream;
48 
49     # Load modular configuration files from the /etc/nginx/conf.d directory.
50     # See http://nginx.org/en/docs/ngx_core_module.html#include
51     # for more information.
52     include /etc/nginx/conf.d/*.conf;
53 
54     # http服务上支持若干虚拟主机。
55     # 每个虚拟主机一个对应的server配置项
56     # 配置项里面包含该虚拟主机相关的配置。
57     server {
58         # 端口
59         listen       80 default_server;
60         listen       [::]:80 default_server;
61         # 访问的域名
62         server_name  _;
63         # 默认网站根目录(www目录)
64         root         /usr/share/nginx/html;
65 
66         # Load configuration files for the default server block.
67 
68         include /etc/nginx/default.d/*.conf;
69 
70         # 默认请求
71         location / {
72         }
73 
74         # 错误页(404)
75         error_page 404 /404.html;
76             location = /40x.html {
77         }
78 
79         # 错误页(50X)
80         error_page 500 502 503 504 /50x.html;
81             location = /50x.html {
82         }
83     }
84 }

    值得说明的几点
      a.关于error_log 可以设置log的类型(记录什么级别的信息)有:debug、info、notice、warn、error、crit几种

      b.关于sendfile
      一般的网络传输过程
      硬盘 >> kernel buffer >> user buffer>> kernel socket buffer >>协议栈
      使用sendfile后
      硬盘 >> kernel buffer (快速拷贝到kernelsocket buffer) >>协议栈
      可以显著提高传输性能。

      c.tcp_nopush和tcp_nodelay
      tcp_nopush只有在启用了sendfile时才起作用,
      在启用tcp_nopush后,程序接收到了数据包后不会马上发出,而是等待数据包最大时一次性发出,可以缓解网络拥堵。(Nagle化)
      相反tcp_nodelay则是立即发出数据包.

二、编译安装php7.1.2
  1.解压php源码包
    tar -zxvf php-7.1.2.tar.gz
  2.创建安装目录 php 文件夹

  3.加载依赖包
    yum install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel
    为了防止编译时报错(错误提示:configure: error: mcrypt.h not found. Please reinstall libmcrypt.),需提前加载依赖包:

      yum install -y epel-release
      yum install -y libmcrypt-devel
    两个不能一起安装,因为CentOs6默认的yum源没有 libmcrypt-devel这个包,只能借助epel的yum源,所以先安装 epel,再安装 libmcrypt。

  4.编译

    ./configure --prefix=/usr/local/webserver/nginx/php --with-config-file-path=/usr/local/webserver/nginx/php/etc --with-gd --enable-fpm --enable-mbstring --enable-zip --with-mcrypt --with-openssl --with-freetype-dir --enable-gd-native-ttf

  5.重新编译和安装
    make && make install
  6.将php7目录php.ini-development复制到php文件中
    cp /usr/local/webserver/nginx/php-7.1.2/php.ini-development  /usr/local/webserver/nginx/php/lib/php.ini
  7.将php/etc中php-fpm.conf.default复制成php-fpm.conf和php-fpm.d中www.conf.default复制成www.conf
    cp php-fpm.conf.default php-fpm.conf
    cp www.conf.default www.conf
  8.将php-fpm.conf文件中error_log前面;删除
    error_log=/usr/local/webserver/php/var/log/php-fpm.log
  9.启动php-fpm服务
    ./sbin/php-fpm

   检测是否启动

    ps aux |grep php-fpm

   

  扩展:php-fpm设置成开机启动项
    1.将启动项文件添加到/etc/init.d文件下
      cp /usr/local/webserver/php-7.1.2/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm

    2.更改脚本权限

      chmod 775 /etc/init.d/php-fpm

    3.添加php-fpm服务
      chkconfig --add php-fpm
    4.查看服务
      chkconfig --list php-fpm
    如果php-fpm服务3:关闭(off) 4:关闭(off) 5:关闭(off)
    将其开启
    chkconfig --level 345 php-fpm on

三、配置nginx支持PHP

  因为只是配置PHP的服务器,而且只使用一个端口所以只需要改动server部分

 1 server {
 2         listen       80 default_server;
 3         listen       [::]:80 default_server;
 4         # 这里改动了,也可以写你的域名
 5         server_name  localhost;
 6         root         /usr/share/nginx/html;
 7 
 8         # Load configuration files for the default server block.
 9         include /etc/nginx/default.d/*.conf;
10 
11         location / {
12             # 这里改动了 定义首页索引文件的名称
13             index index.php index.html index.htm;
14         }
15 
16         error_page 404 /404.html;
17             location = /40x.html {
18         }
19 
20         error_page 500 502 503 504 /50x.html;
21             location = /50x.html {
22         }
23 
24         # 这里新加的
25         # PHP 脚本请求全部转发到 FastCGI处理. 使用FastCGI协议默认配置.
26         # Fastcgi服务器和程序(PHP,Python)沟通的协议.
27         location ~ .php$ {
28             # 设置监听端口
29             fastcgi_pass   127.0.0.1:9000;
30             # 设置nginx的默认首页文件(上面已经设置过了,可以删除)
31             fastcgi_index  index.php;
32             # 设置脚本文件请求的路径
33             fastcgi_param  SCRIPT_FILENAME  /usr/local/webserver/nginx/html$fastcgi_script_name;
34             # 引入fastcgi的配置文件
35             include        fastcgi_params;
36         }
37     }

  需要修改fastcgi_param SCRIPT_FILENAME指向对应目录即可(本机nginx 文件目录为 /usr/local/webserver/nginx/Nginx/html 或者 $document_root)

  注意,如果pfp-fpm配置文件侦听9001等端口,需要改对应文件

  重启Nginx  : 

    /usr/local/webserver/nginx/Nginx/sbin/nginx -s reload

  我们可以通过下面的方法判断Nginx配置是否成功。

    在Nginx的网站根目录(/usr/share/nginx/html)下创建一个php文件,随便起名我的是phpinfo.php

  内容如下:

  <?php
    phpinfo();
  ?>

  然后在浏览器里输入http://192.168.0.212/test.php(注: http://127.0.0.1/test.php 本地用这个也可)

  如果出现php的相关配置,成功,如果什么都没有输入,说明失败,重新以上步骤或者查找原因



原文地址:https://www.cnblogs.com/haw2106/p/9844672.html