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

https://www.zybuluo.com/phper/note/73025

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

php linux nginx

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

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

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

安装nginx

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

安装nginx的依赖包

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

用源码方式安装:

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

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

cd/lamp

wget http://zlib.net/zlib-1.2.8.tar.gz

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

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

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.gz

tar-zxvf nginx-1.7.10.tar.gz

cd 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|grep8080

tcp000.0.0.0:80800.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

利用YUM安裝的,直接編輯/ETC/nginx/conf.d/default.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 }附加:

在centos 7環境下,用YUM安裝,nginx 1.8.0在原文件內修改這一句

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name

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

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

[root@localhost nginx]#ps-ef|grep nginx

root316601017:15?00:00:00nginx:master process./nginx

nobody3195431660017:52?00:00:00nginx:worker process

root3196815419017:52pts/300:00:00grep nginx

[root@localhost nginx]kill31660

[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

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--dport22-j ACCEPT

-A INPUT-m state--state NEW-m tcp-p tcp--dport80-j ACCEPT

-A INPUT-m state--state NEW-m tcp-p tcp--dport3306-j ACCEPT

-A INPUT-m state--state NEW-m tcp-p tcp--dport8080-j ACCEPT

-A INPUT-j REJECT--reject-withicmp-host-prohibited

-A FORWARD-j REJECT--reject-withicmp-host-prohibited

COMMIT

然后,重启下 iptables。service iptables restart

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值