Linux下LNMP环境搭建(Nginx的源码安装)

Linux下LNMP环境搭建最后一步,nginx安装并解析php

cd /usr/local/src/
wget http://nginx.org/download/nginx-1.6.2.tar.gz #下载速度很慢,建议使用 http://download.chinaunix.net/
tar zxvf nginx-1.6.2.tar.gz
cd nginx-1.6.2
yum install -y pcre-devel
./configure --prefix=/usr/local/nginx --with-pcre
make
make install

启动nginx
/usr/local/nginx/sbin/nginx

编写nginx启动脚本
vim /etc/init.d/nginx

#!/bin/bash
# chkconfig: - 30 21
# description: http service.
# Source Function Library
. /etc/init.d/functions
# Nginx Settings

NGINX_SBIN="/usr/local/nginx/sbin/nginx"
NGINX_CONF="/usr/local/nginx/conf/nginx.conf"
NGINX_PID="/usr/local/nginx/logs/nginx.pid"
RETVAL=0
prog="Nginx"

start() {
        echo -n $"Starting $prog: "
        mkdir -p /dev/shm/nginx_temp
        daemon $NGINX_SBIN -c $NGINX_CONF
        RETVAL=$?
        echo
        return $RETVAL
}

stop() {
        echo -n $"Stopping $prog: "
        killproc -p $NGINX_PID $NGINX_SBIN -TERM
        rm -rf /dev/shm/nginx_temp
        RETVAL=$?
        echo
        return $RETVAL
}

reload(){
        echo -n $"Reloading $prog: "
        killproc -p $NGINX_PID $NGINX_SBIN -HUP
        RETVAL=$?
        echo
        return $RETVAL
}

restart(){
        stop
        start
}

configtest(){
    $NGINX_SBIN -c $NGINX_CONF -t
    return 0
}

case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  reload)
        reload
        ;;
  restart)
        restart
        ;;
  configtest)
        configtest
        ;;
  *)
        echo $"Usage: $0 {start|stop|reload|restart|configtest}"
        RETVAL=1
esac
exit $RETVAL

保存后,执行
chmod a+x /etc/init.d/nginx
chkconfig --add nginx
chkconfig nginx on

配置解析php
vi /usr/local/nginx/conf/nginx.conf

    

location ~ \.php$ {
     root           html;
     fastcgi_pass   127.0.0.1:9000;
     fastcgi_index  index.php;
     fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html$fastcgi_script_name;
     include        fastcgi_params;
}

重新加载 /usr/local/nginx/sbin/nginx -s reload
vim /usr/local/nginx/html/1.php
增加
<?php phpinfo();

问题
启动过程中遇到nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use),为nginx进程重复占用端口(nginx先监听了ipv4的80端口之后又监听了ipv6的80端口),查看杀死进程重新启动即可 service nginx start

启动后测试localhost无法浏览器浏览,使用 curl localhost -I 200ok
考虑到服务器防火墙并未关闭
vi /etc/selinux/config
1) 重启后生效
开启: chkconfig iptables on
关闭: chkconfig iptables off

2) 即时生效,重启后失效
开启: service iptables start
关闭: service iptables stop

浏览器访问:http://192.168.46.131/1.php

Tags: Linux

添加新评论