最近在研究nginx,之前一直配lamp鉴于目前nginx的评论和流行,想逐步切换到lnmp中琛.
nginx和php的运行方式和apache的挂载方式不同,是以php-fpm独立启动php解释器,通过socket和nginx通信.也就相当于把php的进程给解耦出来了.
鉴于centos yum安装方式的稳定和高效 我想php-fpm还是以yum的安装方式,然后再编译安装nginx,之所以nginx编译安装是因为后期遇到新需求重新编译方便.
Webtatic源
Centos 5.X rpm -Uvh http://mirror.webtatic.com/yum/el5/latest.rpm CentOs 6.x rpm -Uvh http://mirror.webtatic.com/yum/el6/latest.rpm CentOs 7.X rpm -Uvh https://mirror.webtatic.com/yum/el7/epel-release.rpm rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
安装php-fpm,因为我之前加过webtatic的源所以直接运行以下命令.
yum install php54w-fpm
下面安装nginx
#先yum装依赖包 yum install gcc gcc-c++ autoconf automake zlib zlib-devel openssl openssl-devel pcre-devel #添加用户nginx groupadd -r nginx useradd -s /sbin/nologin -g nginx -r nginx #下载稳定版nginx wget http://nginx.org/download/nginx-1.10.1.tar.gz tar -xzf nginx-1.10.1.tar.gz cd nginx-1.10.1 #开始编译 ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-pcre --user=nginx --group=nginx make make install
下面加一个启动脚本
touch /etc/init.d/nginx chmod a+x /etc/init.d/nginx vim /etc/init.d/nginx
#!/bin/bash
# nginx This shell script takes care of starting and stopping
# nginx
#
# chkconfig: - 13 68
# description: nginx is a web server
### BEGIN INIT INFO
# Provides: $named
# Short-Description: start|stop|status|restart|configtest
### END INIT INFO
#variables
NGINX_BIN="/usr/local/nginx/sbin/nginx"
NGINX_CONF="/usr/local/nginx/conf/nginx.conf"
NGINX_PID="/usr/local/nginx/logs/nginx.pid"
NETSTAT="/bin/netstat"
alter=$1
prog=nginx
#load system function
. /etc/rc.d/init.d/functions
#function:echo ok or error
function if_no {
if [ $2 == 0 ]; then
echo -n $"$1 ${prog}:" && success && echo
else
echo -n $"$1 ${prog}:" && failure && echo
fi
}
#start nginx
function start {
if [ -s ${NGINX_PID} ]; then
echo "nginx already running"
else
if [ `${NETSTAT} -tnpl | grep nginx | wc -l` -eq 0 ]; then
rm -f ${NGINX_PID} 2>/dev/null
${NGINX_BIN} -c ${NGINX_CONF}
if_no start $?
else
${NETSTAT} -tnpl | grep nginx | awk '{ print $7}' | cut -d '/' -f 1 > ${NGINX_PID}
if_no start $?
fi
fi
}
#stp nginx
function stop {
if [ -s ${NGINX_PID} ]; then
cat ${NGINX_PID} | xargs kill -QUIT
if_no stop $?
else
if [ `${NETSTAT} -tnpl | grep nginx | wc -l` -eq 0 ]; then
rm -f ${NGINX_PID} 2>/dev/null
if_no stop 0
else
rm -f ${NGINX_PID} 2>/dev/null
kill `${NETSTAT} -tnpl | grep nginx | awk '{ print $7}' | cut -d '/' -f 1`
if_no stop $?
fi
fi
}
function restart {
if [ -s ${NGINX_PID} ]; then
cat ${NGINX_PID} | xargs kill -HUP
if_no restart $?
else
stop
sleep 1
start
fi
}
function status {
${NETSTAT} -tnpl | grep nginx | grep LISTEN
[ $? == 0 ] && echo "nginx is running" || echo "nginx is not running"
}
function configtest {
${NGINX_BIN} -t
}
case $alter in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
status)
status
;;
configtest)
configtest
;;
*)
echo "use:${NGINX} {start|stop|restart|status|configtest}"
;;
esac
修改 /conf/nginx.conf
server {
listen 8080;
root /data1/demo/;
server_name demo.com;
location / {
index index.php;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
access_log logs/esf-access.log main;
}
添加脚本
chkconfig --add nginx chkconfig nginx on
这样就算配置好了