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

时间:2021-03-08 22:52:47

一、编译安装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

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

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

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

  扩展:nginx配置文件 分析

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

 # nginx运行的用户名
user nginx;
# nginx启动进程,通常设置成和cpu的数量相等,这里为自动
worker_processes auto; # errorlog文件位置
error_log /var/log/nginx/error.log;
# pid文件地址,记录了nginx的pid,方便进程管理
pid /run/nginx.pid; # Load dynamic modules. See /usr/share/nginx/README.dynamic.
# 用来加载其他动态模块的配置
include /usr/share/nginx/modules/*.conf; # 工作模式和连接数上限
events {
# 每个worker_processes的最大并发链接数
# 并发总数:worker_processes*worker_connections
worker_connections 1024;
} # 与提供http服务相关的一些配置参数类似的还有mail
http {
# 设置日志的格式
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"'; # access_log记录访问的用户、页面、浏览器、ip和其他的访问信息
access_log /var/log/nginx/access.log main; # 这部分下面会单独解释
# 设置nginx是否使用sendfile函数输出文件
sendfile on;
# 数据包最大时发包(使用Nagle算法)
tcp_nopush on;
# 立刻发送数据包(禁用Nagle算法)
tcp_nodelay on;
# 链接超时时间
keepalive_timeout 65;
# 这个我也不清楚...
types_hash_max_size 2048; # 引入文件扩展名与文件类型映射表
include /etc/nginx/mime.types;
# 默认文件类型
default_type application/octet-stream; # Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf; # http服务上支持若干虚拟主机。
# 每个虚拟主机一个对应的server配置项
# 配置项里面包含该虚拟主机相关的配置。
server {
# 端口
listen 80 default_server;
listen [::]:80 default_server;
# 访问的域名
server_name _;
# 默认网站根目录(www目录)
root /usr/share/nginx/html; # Load configuration files for the default server block. include /etc/nginx/default.d/*.conf; # 默认请求
location / {
} # 错误页(404)
error_page 404 /404.html;
location = /40x.html {
} # 错误页(50X)
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}

    值得说明的几点
      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

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

  扩展: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部分

 server {
listen 80 default_server;
listen [::]:80 default_server;
# 这里改动了,也可以写你的域名
server_name localhost;
root /usr/share/nginx/html; # Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf; location / {
# 这里改动了 定义首页索引文件的名称
index index.php index.html index.htm;
} error_page 404 /404.html;
location = /40x.html {
} error_page 500 502 503 504 /50x.html;
location = /50x.html {
} # 这里新加的
# PHP 脚本请求全部转发到 FastCGI处理. 使用FastCGI协议默认配置.
# Fastcgi服务器和程序(PHP,Python)沟通的协议.
location ~ \.php$ {
# 设置监听端口
fastcgi_pass 127.0.0.1:9000;
# 设置nginx的默认首页文件(上面已经设置过了,可以删除)
fastcgi_index index.php;
# 设置脚本文件请求的路径
fastcgi_param SCRIPT_FILENAME /usr/local/webserver/nginx/html$fastcgi_script_name;
# 引入fastcgi的配置文件
include fastcgi_params;
}
}

  需要修改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的相关配置,成功,如果什么都没有输入,说明失败,重新以上步骤或者查找原因