[关闭]
@phper 2015-04-29T06:28:51.000000Z 字数 4351 阅读 16684

linux上nginx的安装启动以及配合php-fpm的使用

php linux nginx


nginx的牛逼之处就不用多说了,反正一个字:牛逼!

我很早之前在csdn上也写过一篇在Windows上安装nginx的文章,之前对它也是一知半解,也属于摸着石头过河吧,今天来看一下linux上的安装以及搭配php的使用。

我的机器是centos 6.2 。 php 版本是 5.4.11

安装nginx

如果软件下载失败或者被墙,可以下载我备份的云盘:

http://yunpan.cn/cZ2QJMSKVGsdU (提取码:06ec)

安装nginx的依赖包

nginx 依赖于 zlib pcre ssl 三个模块,安装之前要先安装它们,如果已经安装则忽略,我的机器其实在安装php的时候这些模块其实是有安装的,下面,我再来一次:

用源码方式安装:

这3个扩展 不需要指定安装目录,他们都默认安装在 /usr/local 目录下。

第一步,我将源代码统一下载到 /lamp 之下,基本上下载的都是最新版。openssl那个一定要下载最新版,以为之前的那个心跳漏洞。

  1. cd /lamp
  2. wget http://zlib.net/zlib-1.2.8.tar.gz
  3. tar -zxvf zlib-1.2.7.tar.gz
  4. cd zlib-1.2.7
  5. ./configure
  6. make
  7. make install
  8. wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.32.tar.gz
  9. tar -zxvf pcre-8.21.tar.gz
  10. cd pcre-8.21
  11. ./configure
  12. make
  13. make install
  14. wget http://www.openssl.org/source/openssl-1.0.2.tar.gz
  15. tar zxvf openssl-1.0.2.tar.gz
  16. cd openssl-1.0.2.tar.gz
  17. ./config # 注意是config,不是configure
  18. make
  19. make install

好,如果没什么错误的话,3个扩展都已经安装好了,如果出现错误信息,基本都会有提示,我安装下载很顺利,没啥问题。

安装nginx

我同样也是下载的官网目前为止的最新版:nginx-1.7.10

  1. wget http://nginx.org/download/nginx-1.7.10.tar.gz
  2. tar -zxvf nginx-1.7.10.tar.gz
  3. cd nginx-1.7.10.tar.gz

下载解压完成,下面就是编译了:

  1. ./configure --prefix=/usr/local/nginx \
  2. --sbin-path=/usr/local/nginx/nginx \
  3. --conf-path=/usr/local/nginx/nginx.conf \
  4. --pid-path=/usr/local/nginx/nginx.pid \
  5. --with-http_ssl_module \
  6. --with-pcre=/lamp/pcre-8.32 \
  7. --with-zlib=/lamp/zlib-1.2.7 \
  8. --with-openssl=/lamp/openssl-1.0.2

注意:这3个扩展的目录是他们的源代码目录不是安装目录,这点很容易搞错。

--with-pcre=/lamp/pcre-8.32 \
--with-zlib=/lamp/zlib-1.2.7 \
--with-openssl=/lamp/openssl-1.0.2

开始编译:

  1. [root@localhost nginx-1.7.10.tar.gz] make
  2. ...
  3. [root@localhost nginx-1.7.10.tar.gz] make install

一般这3个扩展目录指定正确,是不会报错的,很顺利的就成功了。

启动 nginx

nginx的默认端口是80。所以启动之前要确保80端口没有被占用,如果之前安装过Apache, 它也是80端口,那么需要kill掉Apache,
如果想保留80端口,也可以修改/usr/local/nginx/nginx.conf中36行 listen 为 8080或其他;

我这里改成8080端口

启动命令是:

/usr/local/nginx/nginx

不报错就ok了:

查看下端口:

  1. netstat -tnl|grep 8080
  2. tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN

然后,我们打开浏览器访问下:

localhost:8080

出现以下,表示nginx 安装成功。

Welcome to nginx!

If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.

配置nginx以支持php

之前我们将了很多关于php-fpm的,现在php安装php-fpm其实就是为了配合nginx使用的。

所以,我们需要编辑nginx的配置文件,我们编译的时候指定了的:/usr/local/nginx/nginx.conf

打开后,编辑以下几行,我简单的标记了一下:

 43         location / {
 44             root   /usr/local/www;   #web的根目录
 45             index  index.php index.html index.htm; # 加入index.php
 46         }


 65         location ~ \.php$ {
 66             root           /usr/local/www;          #web的根目录
 67             fastcgi_pass   127.0.0.1:9000;          #php-fpm的地址
 68             fastcgi_index  index.php;
 70             include        fastcgi.conf;
 71         }

简单的这样改一下,满足基本的php需求就可以了。下面我们重新启动一下nginx:

有2中方式,第1种是先kill,再启动。第2种是平滑启动,推荐第2种

  1. [root@localhost nginx]# ps -ef|grep nginx
  2. root 31660 1 0 17:15 ? 00:00:00 nginx: master process ./nginx
  3. nobody 31954 31660 0 17:52 ? 00:00:00 nginx: worker process
  4. root 31968 15419 0 17:52 pts/3 00:00:00 grep nginx
  5. [root@localhost nginx]kill 31660
  6. [root@localhost nginx]/usr/local/nginx/nginx

或者平滑升级,推荐这个

/usr/local/nginx/nginx -s reload

我们刚才把web目录改成 /usr/local/www 目录下,我们在下面新建一个index.php文件:

  1. vi /usr/local/www/index.php
  2. <?php
  3. echo phpinfo();

打开浏览器输入:127.0.0.1:8080/index.php

看到熟悉的 PHP Version 5.4.11 表示成功了。

开启自启动nginx

开机启动的配置文件是:/etc/rc.local ,vi加入 /usr/local/nginx/nginx 即可

  1. #!/bin/sh
  2. #
  3. # This script will be executed *after* all the other init scripts.
  4. # You can put your own initialization stuff in here if you don't
  5. # want to do the full Sys V style init stuff.
  6. touch /var/lock/subsys/local
  7. /usr/local/apache/bin/apachectl start
  8. /usr/local/bin/redis-server /etc/redis.conf
  9. /usr/local/php/sbin/php-fpm
  10. /usr/local/nginx/nginx

开启nginx的iptables限制

我们在本地访问127.0.0.1:8080/index.php,是可以打开的。 但是如果,在另外一台机子上访问:http://192.168.155.128:8080/index.php 不能访问,可能是这个8080端口号没有加入到iptables的白名单,所以需要加一下:

(PS: 如果你的nginx端口号是80,应该是已经在iptables白名单中了,如果能访问就不需要加了)

iptables的配置文件在这:/etc/sysconfig/iptables

我们vi 打开下,然后在倒数第二行上面加入:

-A INPUT -m state --state NEW -m tcp -p tcp --dport 8080 -j ACCEPT 

这句话的意思是将8080端口号加入白名单,不做访问限制。

  1. # Firewall configuration written by system-config-firewall
  2. # Manual customization of this file is not recommended.
  3. *filter
  4. :INPUT ACCEPT [0:0]
  5. :FORWARD ACCEPT [0:0]
  6. :OUTPUT ACCEPT [0:0]
  7. -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
  8. -A INPUT -p icmp -j ACCEPT
  9. -A INPUT -i lo -j ACCEPT
  10. -A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
  11. -A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
  12. -A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
  13. -A INPUT -m state --state NEW -m tcp -p tcp --dport 8080 -j ACCEPT
  14. -A INPUT -j REJECT --reject-with icmp-host-prohibited
  15. -A FORWARD -j REJECT --reject-with icmp-host-prohibited
  16. COMMIT

然后,重启下 iptables。

service iptables restart

好。再次访问下: http://192.168.155.128:8080/index.php 应该就能看到phpinfo页面了。

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注