CentOS7 安装 Nginx

一. 安装nginx

1. 将nginx放到yum repro库中
rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
2. 查看nginx信息
yum info nginx
3. 使用yum安装ngnix
yum install nginx
4. 启动nginx
service nginx start
5. 查看nginx版本
nginx -v
6. 访问nginx
curl -i localhost
7. nginx配置文件位置
/etc/nginx/
主配置文件是 /etc/nginx/ 下的 nginx.conf,
另外一个是 /etc/nginx/conf.d/ 下的 default.conf
8. 实践:

目的:修改服务名,接着从外部访问这个服务
操作:

a. 修改nginx配置文件
vim /etc/nginx/conf.d/default.conf

修改server_name部分:server_name yytest.com;

b. 重载服务
/usr/sbin/nginx -s reload
c. 从外部访问nginx服务(192.168.10.11)

如在客户机(192.168.10.10)的浏览器访问:http://yytest.com

d. 你发现访问不了:

原因1,你没有在hosts文件做映射;

原因2,及时你在hosts文件中了映射,由于nginx服务器的80端口堵塞或防火墙没关

e. 解决办法:

步骤一:修改客户机(192.168.10.10)的hosts文件,使用SwitchHosts工具添加 192.168.10.11 yytest.com

步骤二:关闭防火墙,具体下文有说明

9. nginx常用操作
启动:
$ service nginx start (centos7是systemctl start nginx.service )
重启:
$ service nginx restart  或者 /usr/sbin/nginx –s reload
停止:
$ service nginx stop  或者 /usr/sbin/nginx –s stop
测试配置文件是否正常:
$ /usr/sbin/nginx –t
10. 防火墙开启端口
$ firewall-cmd --zone=public --add-port=80/tcp --permanent
命令含义:
--zone #作用域
--add-port=80/tcp  #添加端口,格式为:端口/通讯协议
--permanent   #永久生效,没有此参数重启后失效
重启防火墙
firewall-cmd --reload

二. 设置nginx开机启动

1. 在/etc/init.d/目录下创建nginx文件
vim /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 $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:.*--user=" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
   if [ -n "$user" ]; then
      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
    fi
}

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
2. 保存退出之后,给其增加可执行权限
chmod +x /etc/init.d/nginx
3. 设置脚本开机自启动
chkconfig --add nginx

chkconfig nginx on
4. 验证
reboot

ps -e |grep nginx
5. 启动、停止、重启tomcat
/etc/init.d/nginx start
/etc/init.d/nginx stop

service nginx start
service nginx stop
service nginx restart

三. 配置nginx上传文件大小

在 nginx.conf 的 http 中添加

client_max_body_size 900m;         //指定最大上传文件大小

其他相关配置

client_body_buffer_size 20M;       //指定用户请求体所使用的buffer的最大值
client_header_buffer_size 20m;     //指定来自客户端请求头的headebuffer大小

四. 其他

  1. nginx 下 conf.d 目录下文件的优先级是按文件名字母序来的。找不到域名的时候默认找conf.d里的第一个文件。
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值