首页 >  帮助中心 > 配置Nginx

配置Nginx

来源:原创 点击量(92) 发布时间:2016-04-14

安装前准备

1、主要软件包下载


Nginx下载
http://nginx.org/download/nginx-1.9.5.tar.gz


zlib下载


http://ncu.dl.sourceforge.net/project/libpng/zlib/1.2.8/zlib-1.2.8.tar.gz


pcre


ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre2-10.10.tar.bz2




首先安装Nginx先把Nginx需要的组件安装一下


pcre组件


[root@pp pcre2-10.10]# ./configure --prefix=/usr/local/huanjing/pcre
[root@pp pcre2-10.10]# make && make install
zlib组件


[root@pp zlib-1.2.8]# ./configure --prefix=/usr/local/huanjing/zlib


[root@pp zlib-1.2.8]# make && make install



通常情况下Nginx的组件的安装尽量使用yum来装这样不容易出现兼容问题,这里已使用yum来安装为例


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


安装Nginx先创建下Nginx访问的用户


创建匿名用户


[root@pp zlib-1.2.8]# groupadd www
[root@pp zlib-1.2.8]# useradd -g www -s /sbin/nologin  www



进入Nginx解压目录准备安装


[root@pp zlib-1.2.8]# cd /root/nginx-1.9.5
[root@pp nginx-1.9.5]# ./configure --prefix=/usr/local/huanjing/nginx --user=www --group=www --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --with-http_gzip_static_module --with-http_ssl_module 

提示木有问题:
Configuration summary
  + using system PCRE library
  + using system OpenSSL library
  + md5: using OpenSSL library
  + sha1: using OpenSSL library
  + using system zlib library

  nginx path prefix: "/usr/local/huanjing/nginx"
  nginx binary file: "/usr/local/huanjing/nginx/sbin/nginx"
  nginx configuration prefix: "/etc/nginx"
  nginx configuration file: "/etc/nginx/nginx.conf"
  nginx pid file: "/usr/local/huanjing/nginx/logs/nginx.pid"
  nginx error log file: "/var/log/nginx/error.log"
  nginx http access log file: "/var/log/nginx/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"
开始编译
[root@pp nginx-1.9.5]# make

    objs/src/http/modules/ngx_http_upstream_keepalive_module.o \
    objs/src/http/modules/ngx_http_upstream_zone_module.o \
    objs/ngx_modules.o \
    -lpthread -lcrypt -lpcre -lssl -lcrypto -ldl -lz
make[1]: Leaving directory `/root/nginx-1.9.5'
make -f objs/Makefile manpage
make[1]: Entering directory `/root/nginx-1.9.5'
sed -e "s|%%PREFIX%%|/usr/local/huanjing/nginx|" \
        -e "s|%%PID_PATH%%|/usr/local/huanjing/nginx/logs/nginx.pid|" \
        -e "s|%%CONF_PATH%%|/etc/nginx/nginx.conf|" \
        -e "s|%%ERROR_LOG_PATH%%|/var/log/nginx/error.log|" \
        < man/nginx.8 > objs/nginx.8
make[1]: Leaving directory `/root/nginx-1.9.5'
没用问题开始安装

[root@pp nginx-1.9.5]# make install
cp conf/mime.types '/etc/nginx/mime.types.default'
test -f '/etc/nginx/fastcgi_params'         || cp conf/fastcgi_params '/etc/nginx'
cp conf/fastcgi_params         '/etc/nginx/fastcgi_params.default'
test -f '/etc/nginx/fastcgi.conf'         || cp conf/fastcgi.conf '/etc/nginx'
cp conf/fastcgi.conf '/etc/nginx/fastcgi.conf.default'
test -f '/etc/nginx/uwsgi_params'         || cp conf/uwsgi_params '/etc/nginx'
cp conf/uwsgi_params         '/etc/nginx/uwsgi_params.default'
test -f '/etc/nginx/scgi_params'         || cp conf/scgi_params '/etc/nginx'
cp conf/scgi_params         '/etc/nginx/scgi_params.default'
test -f '/etc/nginx/nginx.conf'         || cp conf/nginx.conf '/etc/nginx/nginx.conf'
cp conf/nginx.conf '/etc/nginx/nginx.conf.default'
test -d '/usr/local/huanjing/nginx/logs'         || mkdir -p '/usr/local/huanjing/nginx/logs'
test -d '/var/log/nginx' ||         mkdir -p '/var/log/nginx'
test -d '/usr/local/huanjing/nginx/html'         || cp -R html '/usr/local/huanjing/nginx'
test -d '/var/log/nginx' ||         mkdir -p '/var/log/nginx'
make[1]: Leaving directory `/root/nginx-1.9.5'



编辑Nginx启动文件

[root@pp nginx]# vi /etc/rc.d/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: /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/huanjing/nginx/sbin/nginx"

prog=$(basename $nginx)

NGINX_CONF_FILE="/etc/nginx/nginx.conf"

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

lockfile=/var/lock/subsys/nginx

make_dirs() {

# make required directories

user=`$nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`

if [ -z "`grep $user /etc/passwd`" ]; then

useradd -M -s /bin/nologin $user

fi

options=`$nginx -V 2>&1 | grep 'configure arguments:'`

for opt in $options; do

if [ `echo $opt | grep '.*-temp-path'` ]; then

value=`echo $opt | cut -d "=" -f 2`

if [ ! -d "$value" ]; then

# echo "creating" $value

mkdir -p $value && chown -R $user $value

fi

fi

done

}

start() {

[ -x $nginx ] || exit 5

[ -f $NGINX_CONF_FILE ] || exit 6

make_dirs

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

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

############################################################
主要修改的地方

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


NGINX_CONF_FILE="/etc/nginx/nginx.conf"


设置开机启动


[root@pp nginx]# chkconfig --add nginx
[root@pp nginx]# chkconfig nginx on
添加80端口防火墙例外
[root@pp nginx]# iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT




  • 服务热线   18577765851