微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Nginx安装及配置的方法

这篇文章主要介绍了Nginx安装及配置的方法的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇Nginx安装及配置的方法文章都会有所收获,下面我们一起来看看吧。

环境准备

1. 操作系统

centos 6.4 x86_64

2.软件版本

Nginx 1.4.2

3.实验拓扑

4.安装yum源

[root@Nginx ~]# rpm -ivh http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
[root@web1 ~]# rpm -ivh http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
[root@web2 ~]# rpm -ivh http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm

5.各节点时间同步

[root@Nginx ~]# ntpdate 202.120.2.101
[root@web1 ~]# ntpdate 202.120.2.101
[root@web2 ~]# ntpdate 202.120.2.101

6.关闭防火墙与selinux

[root@Nginx ~]# service iptables stop 
[root@Nginx ~]# chkconfig iptables off 
[root@Nginx ~]# getenforce 
disabled
[root@web1 ~]# service iptables stop 
[root@web1 ~]# chkconfig iptables off 
[root@web1 ~]# getenforce 
disabled
[root@web2 ~]# service iptables stop 
[root@web2 ~]# chkconfig iptables off 
[root@web2 ~]# getenforce 
disabled

安装Nginx

1.解压

[root@Nginx src]# tar xf Nginx-1.4.2.tar.gz

2.新建Nginx用户与组

[root@Nginx src]# groupadd -g 108 -r Nginx 
[root@Nginx src]# useradd -u 108 -r -g 108 Nginx 
[root@Nginx src]# id Nginx 
uid=108(Nginx) gid=108(Nginx) 组=108(Nginx)

3.准备编译配置文件

[root@Nginx src]# yum install -y pcre-devel openssl-devel
[root@Nginx Nginx-1.4.2]# ./configure  --prefix=/usr  --sbin-path=/usr/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=Nginx  --group=Nginx  --with-http_ssl_module  --with-http_flv_module  --with-http_stub_status_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/fcgi/  --http-uwsgi-temp-path=/var/tmp/Nginx/uwsgi  --http-scgi-temp-path=/var/tmp/Nginx/scgi  --with-pcre

4.编译并安装

[root@Nginx Nginx-1.4.2]# make && make install

5.为Nginx提供sysv init脚本

[root@Nginx ~]# cat /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/sbin/Nginx" 
prog=$(basename $NginxNginx_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' -` 
  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

6.为此脚本赋予执行权限

[root@Nginx ~]# chmod +x /etc/init.d/Nginx

7.添加至服务管理列表,并让其开机自动启动

[root@Nginx ~]# chkconfig --add Nginx 
[root@Nginx ~]# chkconfig Nginx on 
[root@Nginx ~]# chkconfig Nginx --list 
Nginx       0:关闭  1:关闭  2:启用  3:启用  4:启用  5:启用  6:关闭

8.启动Nginx

[root@Nginx ~]# service Nginx start 
正在启动 Nginx:                      [确定]

9.查看一下端口

[root@Nginx ~]# netstat -ntlp | grep :80 
tcp    0   0 0.0.0.0:80         0.0.0.0:*          listen   3889/Nginx

10.测试一下

Nginx安装及配置的方法

关于“Nginx安装及配置的方法”这篇文章内容就介绍到这里,感谢各位的阅读!相信大家对“Nginx安装及配置的方法”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注编程之家行业资讯频道。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐