搭建Nginx+PHP环境

搭建Nginx+PHP环境

一. 源码包编译安装部署web服务器

1.安装nginx必须的依赖包

[root@test01 ~]# yum -y install gcc openssl-devel pcre-devel zlib-devel

2.安装编译nginx,美国服务器,目前系统测试环境为CentOS6.3 软件版本为nginx-1.2.6

[root@test01 ~]# useradd nginx -s /sbin/nologin

[root@test01 ~]# tar zxvf nginx-1.2.6.tar.gz

[root@test01 ~]# cd nginx-1.2.6

[root@test01 nginx-1.2.6]# ./configure –user=nginx –group=nginx –prefix=/usr/local/nginx/ –with-http_stub_status_module –with-http_ssl_module

// –with-http_stub_status_module 安装允许状态模块

// –with-http_ssl_module 安装ssl模块

[root@test01 ~]# /usr/local/nginx/sbin/nginx-v//查看Nginx的相关环境配置信息是否正确

nginx version: nginx/1.2.6

[root@test01 ~]#

[root@test01 nginx-1.2.6]# make & make install //编译安装过程略

3.通过nginx自身脚本机器nginx服务器,并通过各种命令查看是否启动成功

[root@test01 ~]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf // -c指向nginx主配置文件

[root@test01 ~]# lsof -i :80//查看nginx进程号及运行情况

COMMAND PIDUSER FD TYPE DEVICE SIZE/OFF NODE NAME

nginx 21910root 9uIPv4 262148 0t0TCP *:http (LISTEN)

nginx 21911 nginx 9uIPv4 262148 0t0TCP *:http (LISTEN)

[root@test01 ~]# ps -ef | grep nginx//查看nginx进程号及运行情况

root 21910 10 10:41 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

nginx 21911 219100 10:41 ? 00:00:00 nginx: worker process

root 21957 217550 10:42 pts/0 00:00:00 grep nginx

[root@test01 ~]# netstat -nltp | grep 80//查看nginx进程监听端口

[root@test01 ~]# netstat -an |grep 80

tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN

4.通过windows服务器IE浏览器测试

扩展知识:

1)设置nginx开机自动启动,将nginx启动命令添加到/etc/rc.local

[root@test01 ~]# echo "/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf" >> /etc/rc.local

[root@test01 ~]# cat /etc/rc.local | grep nginx

/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

2)通过设置System V 脚本,使用server命令启动nginx服务

[root@test01 init.d]# vimnginx//在/etc/init.d/下创建nginx启动脚本文件

#!/bin/sh

#

# nginx – this script starts and stops the nginx daemon

#

# chkconfig: – 85 15

# description: Nginx is an HTTP(S) server, HTTP(S) reverse \

# proxy and IMAP/POP3 proxy server

# processname: nginx

# config: /etc/nginx/nginx.conf

# config: /etc/sysconfig/nginx

# pidfile: /var/run/nginx.pid

# Source function library.

. /etc/rc.d/init.d/functions

# Source networking configuration.

. /etc/sysconfig/network

# Check that networking is up.

[ "$NETWORKING" = "no" ] && exit 0

nginx="/usr/local/nginx/sbin/nginx"

prog=$(basename $nginx)

NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"

[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx

lockfile=/var/lock/subsys/nginx

start() {

[ -x $nginx ] || exit 5

[ -f $NGINX_CONF_FILE ] || exit 6

echo -n $"Starting $prog: "

daemon $nginx -c $NGINX_CONF_FILE

retval=$?

echo

[ $retval -eq 0 ] && touch $lockfile

return $retval

}

stop() {

echo -n $"Stopping $prog: "

killproc $prog -QUIT

retval=$?

echo

[ $retval -eq 0 ] && rm -f $lockfile

return $retval

killall -9 nginx

}

restart() {

configtest || return $?

stop

sleep 1

start

}

reload() {

configtest || return $?

echo -n $"Reloading $prog: "

killproc $nginx -HUP

RETVAL=$?

echo

}

force_reload() {

restart

}

configtest() {

$nginx -t -c $NGINX_CONF_FILE

}

rh_status() {

status $prog

}

rh_status_q() {

rh_status >/dev/null 2>&1

}

case "$1" in

start)

rh_status_q && exit 0

$1

;;

stop)

rh_status_q || exit 0

$1

;;

restart|configtest)

$1

;;

reload)

rh_status_q || exit 7

$1

;;

force-reload)

force_reload

;;

status)

rh_status

;;

condrestart|try-restart)

rh_status_q || exit 0

;;

*)

echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"

exit 2

esac

[root@test01 init.d]# chmod 755 nginx //修改脚本文件nginx的权限

[root@test01 init.d]# chkconfig –add nginx//将脚本文件加入chkconfig中

[root@test01 init.d]# chkconfig –level 35 nginx on//设置nginx开机在3和5级别自动启动

[root@test01 init.d]# chkconfig –list | grep nginx

重新开始吧!下次我会吸取教训,不让自己犯同样的错误的;

搭建Nginx+PHP环境

相关文章:

你感兴趣的文章:

标签云: