Nginx安装:

下载解压安装


进入Nginx官网站 http://nginx.org/,进入后选择最新的版本,进入页面后在 Stable version 中选择 nginx-* 右键复制链接,然后用 wget 下载

1
[root@iZ2844brz0xZ ~]# wget http://nginx.org/download/nginx-1.12.0.tar.gz

如果提示wget命令未找到,则执行

1
[root@iZ2844brz0xZ ~]# sudo yum install wget

下载完用 ll 命令查看刚刚下载获得的 nginx-*.tar.gz 文件,用下面命令解压它

1
[root@iZ2844brz0xZ ~]# tar -zxvf nginx-1.12.0.tar.gz

-z: 表示使用gzip的属性。
-x: 解开一个压缩文件的参数指令。
-v: 表示压缩的过程中显示文件!这个常用,但不建议用在背景执行过程!
-f: 使用档名,请留意,在 f 之后要立即接档名喔!不要再加参数!

参考资料


安装Nginx前,需要安装以下三个依赖包:

  1. gzip 模块需要 zlib

  2. rewrite 模块需要 pcre

  3. ssl 功能需要 openssl

https://www.openssl.org/source/

1
[root@iZ2844brz0xZ ~]# wget https://www.openssl.org/source/openssl-fips-2.0.14.tar.gz

http://www.zlib.net/

1
[root@iZ2844brz0xZ ~]# wget http://www.zlib.net/zlib-1.2.11.tar.gz

(http://www.pcre.org/)[http://www.pcre.org/]

1
[root@iZ2844brz0xZ ~]# wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.39.tar.gz

下载完成后安装,依赖包安装顺序依次为: opensslzlibpcre, 然后安装 Nginx


1.安装openssl

1
2
3
4
5
[root@iZ2844brz0xZ ~]# tar -zxvf openssl-fips-2.0.14.tar.gz
[root@iZ2844brz0xZ ~]# cd openssl-fips-2.0.14/
[root@iZ2844brz0xZ openssl-fips-2.0.14]# ./config
[root@iZ2844brz0xZ openssl-fips-2.0.14]# make
[root@iZ2844brz0xZ openssl-fips-2.0.14]# make install

2.安装zlib

1
2
3
4
5
[root@iZ2844brz0xZ ~]# tar -zxvf zlib-1.2.11.tar.gz
[root@iZ2844brz0xZ ~]# cd zlib-1.2.11/
[root@iZ2844brz0xZ zlib-1.2.11]# ./configure
[root@iZ2844brz0xZ zlib-1.2.11]# make
[root@iZ2844brz0xZ zlib-1.2.11]# make install

3.安装pcre

1
2
3
4
5
[root@iZ2844brz0xZ ~]# tar -zxvf pcre-8.39.tar.gz
[root@iZ2844brz0xZ ~]# cd pcre-8.39/
[root@iZ2844brz0xZ pcre-8.39]# ./configure
[root@iZ2844brz0xZ pcre-8.39]# make
[root@iZ2844brz0xZ pcre-8.39]# make install

4.安装nginx

1
2
3
4
5
[root@iZ2844brz0xZ ~]# tar -zxvf nginx-1.12.0.tar.gz
[root@iZ2844brz0xZ ~]# cd nginx-1.12.0/
[root@iZ2844brz0xZ nginx-1.12.0]# ./configure --with-pcre=../pcre-8.39 --with-zlib=../zlib-1.2.11 --with-openssl=../openssl-fips-2.0.14
[root@iZ2844brz0xZ nginx-1.12.0]# make
[root@iZ2844brz0xZ nginx-1.12.0]# make install

检测是否安装成功

1
2
[root@iZ2844brz0xZ nginx-1.12.0]# cd /usr/local/nginx/sbin/
[root@iZ2844brz0xZ sbin]# ./nginx -t

若出现下方信息表示安装成功

1
2
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

最后一步:启动Nginx

1
[root@iZ2844brz0xZ sbin]# ./nginx

查看端口

1
[root@iZ2844brz0xZ sbin]# netstat -ntlp

出现以下结果

这里写图片描述


至此,Nginx安装结束

参考资料:

http://www.cnblogs.com/hzh19870110/p/6100661.html 作者:grhlove123

配置php-fpm

进入php的 sbin 目录下启动 php-fpm

1
2
[root@iZ2844brz0xZ ~]# cd /usr/local/php7/sbin/
[root@iZ2844brz0xZ sbin]# ./php-fpm

(若提示没有权限,在命令前加上 sudo 即可。)

发现提示下方信息:

1
2
3
[15-Apr-2017 17:06:21] ERROR: failed to open configuration file '/usr/local/php7/etc/php-fpm.conf': No such file or directory (2)
[15-Apr-2017 17:06:21] ERROR: failed to load configuration file '/usr/local/php7/etc/php-fpm.conf'
[15-Apr-2017 17:06:21] ERROR: FPM initialization failed

告诉我们在 /usr/local/php7/etc/ 目录下没有找到 php-fpm.conf 配置文件。

那我们进入对应文件夹查看文件

1
2
[root@iZ2844brz0xZ sbin]# cd /usr/local/php7/etc/
[root@iZ2844brz0xZ etc]# ll

显示

1
2
3
4
total 16
-rw-r--r-- 1 root root 1239 Apr 15 07:33 pear.conf
-rw-r--r-- 1 root root 4468 Apr 15 07:33 php-fpm.conf.default
drwxr-xr-x 2 root root 4096 Apr 15 07:33 php-fpm.d

发现 php-fpm.conf 多了一个 default 结尾,我们只需要把文件重新命名就好了

1
[root@iZ2844brz0xZ etc]# mv php-fpm.conf.default php-fpm.conf --利用mv移动命令更改名字

重新进入php的 sbin 目录下启动 php-fpm

1
2
[root@iZ2844brz0xZ etc]# cd /usr/local/php7/sbin/
[root@iZ2844brz0xZ sbin]# ./php-fpm

这时候提示:

1
2
3
4
[15-Apr-2017 17:08:23] WARNING: Nothing matches the include pattern '/usr/local/php7/etc/php-fpm.d/*.conf' from /usr/local/php7/etc/php-fpm.conf at line 125.
[15-Apr-2017 17:08:23] ERROR: No pool defined. at least one pool section must be specified in config file
[15-Apr-2017 17:08:23] ERROR: failed to post process the configuration
[15-Apr-2017 17:08:23] ERROR: FPM initialization failed

告诉我们在 /usr/local/php7/etc/php-fpm.d/ 目录下没有找到 .conf 文件,问题出在125行,我们用vim编辑文件

1
[root@iZ2844brz0xZ sbin]# vim /usr/local/php7/etc/php-fpm.conf

进入后输入命令 :125 跳转到125行

1
include=/usr/local/php7/etc/php-fpm.d/*.conf

给我们的信息是要找 php-fpm.d 目录下的以 .conf 结尾的文件,输入:q! 退出,进入对应文件夹,查看文件

1
2
[root@iZ2844brz0xZ sbin]# cd /usr/local/php7/etc/php-fpm.d/
[root@iZ2844brz0xZ php-fpm.d]# ll

发现以下文件

1
2
total 20
-rw-r--r-- 1 root root 18521 Apr 15 07:33 www.conf.default

重命名

1
[root@iZ2844brz0xZ php-fpm.d]# mv www.conf.default www.conf

回到 sbin 目录下重新启动 php-fpm

1
2
[root@iZ2844brz0xZ php-fpm.d]# cd /usr/local/php7/sbin/
[root@iZ2844brz0xZ sbin]# ./php-fpm

若没有提示信息表明php-fpm启动成功啦~

用以下命令查看fpm进程

1
[root@iZ2844brz0xZ ~]# ps aux | grep php-fpm

配置Nginx

进入Nginx目录,编辑 nginx.conf 配置文件

1
2
3
[root@iZ2844brz0xZ nginx]# cd /usr/local/nginx/
[root@iZ2844brz0xZ nginx]# cd conf/
[root@iZ2844brz0xZ conf]# vim nginx.conf

看到 server 模块下有:

1
2
3
4
location / {
root html;
index index.html index.htm;
}

这段表示所有的文件都会进入html这个目录下,我们现在编写与php相关的语句

在下面我们可以找到配置文件已经帮我们写好了我们只需要把前面的注释符号 # 去掉就好了

1
2
3
4
5
6
7
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi_params;
}

输入:wq 保存退出

进入nginx的html 目录,新建一个文件测试一下

1
2
3
[root@iZ2844brz0xZ conf]# cd ..
[root@iZ2844brz0xZ nginx]# cd html/
[root@iZ2844brz0xZ html]# vim test.php

输入

1
2
<?php
phpinfo();

忘了说,修改完配置文件记得要重新启动nginx哦

1
2
[root@iZ2844brz0xZ ~]# cd /usr/local/nginx/sbin/
[root@iZ2844brz0xZ sbin]# ./nginx -s reload

打开网址,运行成功~


附:

Nginx和php-fpm的运行原理 作者:zilu (写的很棒~)