【本文档所介绍的内容适用于公司测试/生产常见的p,HTML等web环境部署,即常见的LAMP环境部署】

一:nginx部署前准备:

1.1相关软件以及系统

系统要求:Centos 6(以上) (64位)

相关中间件:nginx:1.8.0

1.2相关系统依赖包安装检查准备

1.2.1 检查系统自带nginx是否安装

rpm -qa | grep nginx

如有安装,请使用以下命令卸载相关程序 

yum remove nginx -y

1.2.2 安装编译nginx需要的依赖包

yum install gcc openssl-devel pcre-devel zlib-devel -y

二:Nginx正式部署安装:

2.1.1到nginx官网下载nginx软件,如下所示

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

2.1.2 添加运行nginx服务账号

groupadd -r nginx 
useradd -r -g nginx -s /bin/false -M nginx

2.1.3 解压编译安装nginx

cd ~
tar -zxf /root/nginx-1.8.0.tar.gz -C /usr/local/src
./configure \
 --prefix=/usr/local/nginx \
 --sbin-path=/usr/local/nginx/sbin/nginx \
 --conf-path=/etc/nginx/nginx.conf  \
 --error-log-path=/var/log/nginx/error.log \
 --http-log-path=/var/log/nginx/access.log  \
 --pid-path=/var/run/nginx/nginx.pid  \
 --lock-path=/var/lock/nginx.lock \
 --user=nignx \
 --group=nginx \
 --with-http_ssl_module \
 --with-http_stub_status_module \
 --with-http_gunzip_module      \
 --with-http_gzip_static_module \
 --http-client-body-temp-path=/var/tmp/nginx/client/ \
 --http-proxy-temp-path=/var/tmp/nginx/proxy/ \
 --http-fastcgi-temp-path=/var/tmp/nginx/fastcgi/ \
 --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi/ \
 --http-scgi-temp-path=/var/tmp/nginx/scgi/ \
 --with-pcre 
 make && make install

备注:

安装完后nginx的相关路径如下:

nginx启动命令:/usr/local/nginx/sbin

默认网页文件存放路径:/usr/local/nginx/

相关配置文件路径:/etc/nginx

访问,错误日志文件路径:/var/log/nginx


2.1.4 使用sed修改nginx的配置文件,指定nginx的运行用户

sed  '3 iuser nginx;' -i /etc/nginx/nginx.conf

修改后,nginx主配置文件内容如下

wKioL1XlRibSeLVJAAArTxY9Fm4816.jpg

注意不修改nginx的运行用户的话,会报如下错误,如下图所示,

wKiom1XlQ4TiA1BiAABSokbp4f4323.jpg

2.1.5 启动nginx服务

/usr/local/nginx/sbin/nginx

注意:启动nginx可能会报以下错误,如下图所示,

wKioL1XlQqvTd6ewAABxP_KYI2E469.jpg

这时需要按照提示创建相关目录,如下:

mkdir -p /var/tmp/nginx

这时重新执行nginx命令就可以正常启动nginx服务了,如图所示

wKioL1XlR-CQczH_AAGFO8tIe6E403.jpg


不过为了方便nginx启动关闭,一般常常将其做成服务启动脚本,代码如下

#!/bin/sh
#
# nginx - this script starts and stops the nginx daemin
#
# chkconfig:   - 85 15
# description:  Nginx is an HTTP(S) server, HTTP(S) reverse \
#               proxy and IMAP/POP3 proxy server
# processname: nginx
# config:      /usr/local/nginx/conf/nginx.conf
# pidfile:     /usr/local/nginx/logs/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="/etc/nginx/nginx.conf"
lockfile=/var/lock/nginx.lock
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
}
restart() {
   configtest || return $?
   stop
   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


脚本建立完成后,给到它执行的权限并添加到开机自启动即可,命令如下

chmod 777 /etc/init.d/nginx 
chkconfig --add nginx
chkconfig --level 2345 nginx on

这时就可以利用系统自带的服务命令启动nginx服务,如下

wKiom1XlUKexCRsiAAB7j-YoWXs189.jpg


下面就可访问nginx的主页面了,如下:

wKiom1XlUIXws1VsAAFDzZuwmpE051.jpg                                                             

至此,nginx部署就基本OK了!