天天看点

linux+nginx+php-fpm搭建学习笔记

文章内内容均参考网络上各类文章后自行整理,感谢原文作者的分享.

os环境centos release 6.6

一,安装配置nginx

下载目前nginx最新的稳定版本nginx-1.8.0

# wget http://nginx.org/download/nginx-1.8.0.tar.gz

解压编译安装

# tar -xvf nginx-1.8.0.tar.gz# cd nginx-1.8.0

# ./configure --prefix=/usr/local/nginx --with-http_realip_module --with-http_sub_module \

--with-http_gzip_static_module --with-http_stub_status_module  --with-pcre

# make && make install

如果提示错误找不到pcre就用yum装一下

# yum install pcre-devel -y

建立nginx用户

# useradd -m -s /sbin/nologin nginx

修改nginx默认配置文件,有一些为新添

# vim /usr/local/nginx/conf/nginx.conf

配置文件修改后,运行nginx命令查看是否有错误

# /usr/local/nginx/sbin/nginx -t

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

输出正确信息后启动服务

# /usr/local/nginx/sbin/nginx

关闭服务命令,-s 后可加stop quit reopen reload

# /usr/local/nginx/sbin/nginx -s stop

添加开机启动

# echo "/usr/local/nginx/sbin/nginx" >>/etc/rc.local

随后在/var/www/html下新建个index.html,随意写点东西,然后打开http://localhost查看

二,编译安装php-fpm

编译过程中缺少的包用yum provides搜索后安装,不一一记录

# wget http://cn2.php.net/get/php-5.6.15.tar.bz2/from/this/mirror -o phpphp-5.6.15.tar.bz2

# tar -xvf phpphp-5.6.15.tar.bz2

# cd phpphp-5.6.15.tar.bz2

# ./configure \

--prefix=/usr/local/php5.6 \

--with-config-file-path=/usr/local/php5.6/etc \

--enable-fpm \

--with-mysql=shared,mysqlnd \

--with-fpm-user=php-fpm \

--with-fpm-group=php-fpm \

--with-libxml-dir \

--with-gd \

--with-jpeg-dir \

--with-png-dir \

--with-freetype-dir \

--with-iconv-dir \

--with-zlib-dir \

--enable-sockets=shared \

--enable-soap \

--enable-gd-native-ttf \

--enable-ftp \

--enable-mbstring \

--enable-exif \

--with-pear \

--with-curl \

--with-openssl

建立用户,拷贝两份配置文件,因为将准备启动两个不同端口的php-fpm进程

# useradd -m -s /sbin/nologin php-fpm

# cd /usr/local/php5.6

# cp etc/php-fpm.conf.default etc/php-fpm-9000.conf

# cp etc/php-fpm.conf.default etc/php-fpm-9001.conf

其中一份配置文件内容如下,两份配置文件端口号需配置不同,日志可以写同一个

# cat php-fpm-9000.conf |grep  "^[^;]" -n

测试配置文件是否正确

# /usr/local/php5.6/sbin/php-fpm -t -y etc/php-fpm-9000.conf

notice: configuration file etc/php-fpm-9000.conf test is successful

分别启动php-fpm两个服务进程

# /usr/local/php5.6/sbin/php-fpm -y etc/php-fpm-9000.conf

# /usr/local/php5.6/sbin/php-fpm -y etc/php-fpm-9001.conf

# netstat -nlp |grep fpm

tcp  0  0  127.0.0.1:9000  0.0.0.0:*    listen      29925/php-fpm       

tcp  0  0 127.0.0.1:9001  0.0.0.0:*    listen      29938/php-fpm

关闭服务的方法

# killall php-fpm

或单个获取pid关闭

# cat var/run/php-fpm-9000.pid | xargs kill

测试php

在/var/www/html下新建个phpinfo.php,写入

修改页面文件权限

# chmod 777 /var/www/html -r

# 然后打开http://localhost查看,显示php配置信息,配置完成