您的位置:首页 > 运维架构 > Nginx

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

2016-04-20 23:33 901 查看
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那个一定要下载最新版,以为之前的那个心跳漏洞。
cd /lamp


wget http://zlib.net/zlib-1.2.8.tar.gz[/code] 
tar -zxvf zlib-1.2.7.tar.gz


cd zlib-1.2.7


./configure


make


make install


wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.32.tar.gz[/code] 
tar -zxvf pcre-8.21.tar.gz


cd pcre-8.21


./configure


make


make install


wget http://www.openssl.org/source/openssl-1.0.2.tar.gz[/code] 
tar zxvf openssl-1.0.2.tar.gz


cd openssl-1.0.2.tar.gz


./config  # 注意是config,不是configure


make


make install


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

安装nginx

我同样也是下载的官网目前为止的最新版:nginx-1.7.10
wget http://nginx.org/download/nginx-1.7.10.tar.gztar -zxvf nginx-1.7.10.tar.gzcd nginx-1.7.10.tar.gz
下载解压完成,下面就是编译了:
./configure --prefix=/usr/local/nginx \--sbin-path=/usr/local/nginx/nginx \--conf-path=/usr/local/nginx/nginx.conf \--pid-path=/usr/local/nginx/nginx.pid \--with-http_ssl_module \--with-pcre=/lamp/pcre-8.32 \--with-zlib=/lamp/zlib-1.2.7 \--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
开始编译:
[root@localhost nginx-1.7.10.tar.gz] make ...[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了:
查看下端口:
netstat -tnl|grep 8080tcp        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种
[root@localhost nginx]# ps -ef|grep nginx


root     31660     1  0 17:15 ?        00:00:00 nginx: master process ./nginx


nobody   31954 31660  0 17:52 ?        00:00:00 nginx: worker process


root     31968 15419  0 17:52 pts/3    00:00:00 grep nginx


[root@localhost nginx]kill 31660


[root@localhost nginx]/usr/local/nginx/nginx


或者平滑升级,推荐这个
/usr/local/nginx/nginx -s reload
我们刚才把web目录改成 /usr/local/www 目录下,我们在下面新建一个index.php文件:
vi /usr/local/www/index.php


<?php


echo phpinfo();


打开浏览器输入:127.0.0.1:8080/index.php
看到熟悉的 PHP Version 5.4.11 表示成功了。

开启自启动nginx

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


#


# This script will be executed *after* all the other init scripts.


# You can put your own initialization stuff in here if you don't


# want to do the full Sys V style init stuff.




touch /var/lock/subsys/local


/usr/local/apache/bin/apachectl start


/usr/local/bin/redis-server /etc/redis.conf


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


/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端口号加入白名单,不做访问限制。
# Firewall configuration written by system-config-firewall# Manual customization of this file is not recommended.*filter:INPUT ACCEPT [0:0]:FORWARD ACCEPT [0:0]:OUTPUT ACCEPT [0:0]-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT-A INPUT -p icmp -j ACCEPT-A INPUT -i lo -j ACCEPT-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT-A INPUT -m state --state NEW -m tcp -p tcp --dport 8080 -j ACCEPT-A INPUT -j REJECT --reject-with icmp-host-prohibited-A FORWARD -j REJECT --reject-with icmp-host-prohibitedCOMMIT
然后,重启下 iptables。
service iptables restart
好。再次访问下: http://192.168.155.128:8080/index.php 应该就能看到phpinfo页面了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: