linux下安装nginx

在安装nginx前首先要确认系统中安装了gcc、pcre-devel、zlib-devel、openssl-devel。

nginx下载地址:
http://nginx.org/

下载“nginx-1.19.4.tar.gz”

wget http://nginx.org/download/nginx-1.19.4.tar.gz

解压

## 解压
tar -zxvf nginx-1.19.4.tar.gz

##进入nginx目录
cd nginx-1.19.4

## 配置
./configure --prefix=/usr/local/nginx

# make
make

添加ssl模块支持

yum install -y openssl*
## 安装ssl模块
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
## 编译
make
## 覆盖安装文件
cp ./objs/nginx /usr/local/nginx/sbin/

运行上面的命令即可,等配置完配置完成后,运行命令

make

这里不要进行make install,否则就是覆盖安装

测试是否安装成功

# cd到刚才配置的安装目录
cd /usr/local/nginx/
./sbin/nginx -t

启动nginx

cd /usr/local/nginx/sbin
./nginx

修改配置后重新载入nginx配置

./nginx -s reload

打开日志文件

./nginx -s reopen

停止nginx

./nginx -s stop

引入自定义代理配置,修改nginx配置

cd /usr/local/nginx/conf
#修改配置文件
vim nginx.conf
#按i键进入编辑模式
i
#添加nginx配置
include /home/nginxConf/*.conf;
#退出编辑模块式
esc
#保存修改
:wq

如果安装nginx安装错误:c compiler cc is not found

原因是因为缺少 gcc-c++ 的包

解决办法很简单,执行:

yum -y install gcc-c++

安装PCRE

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

配置负载均衡

	
	upstream chuanbo.com {
		server 127.0.0.1:9006 weight=1;
	#	server 127.0.0.1:9007 weight=1;
		server 192.168.1.240:9001 weight=1;
	#	server 127.0.0.1:9008 weight=1;
	#	ip_hash;

	}

        server {
        listen      9001;
        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
			proxy_set_header X-Real-IP $remote_addr;
			proxy_set_header REMOTE-HOST $remote_addr;
			client_max_body_size 50m;
			client_body_buffer_size 256k;
			proxy_connect_timeout 1;
			proxy_send_timeout 30;
			proxy_read_timeout 60;
			proxy_buffer_size 256k;
			proxy_buffers 4 256k;
			proxy_busy_buffers_size 256k;
			proxy_temp_file_write_size 256k;
			proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;
			proxy_max_temp_file_size 128m;
			
            proxy_pass http://chuanbo.com;
			proxy_set_header Host $host:$server_port;
			proxy_set_header X-Forwarded-For $remote_addr;	
        }
		
	}

启动nginx

cd /usr/local/nginx/sbin
./nginx //启动nginx

设置nginx开机启动

1.在/etc/init.d创建nginx文件,并添加如下内容

vi /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
这个脚本的来自于nginx官网,网址:https://www.nginx.com/resources/wiki/start/topics/examples/redhatnginxinit/

`nginx="/usr/sbin/nginx"`//修改成自己的目录

`NGINX_CONF_FILE="/etc/nginx/nginx.conf"` //修改成自己的目录

2.保存文件后对文件设置权限

chmod a+x nginx

3.把脚本添加到系统服务

chkconfig --add /etc/init.d/nginx

4.启动nginx服务

service nginx start

5.关闭nginx服务

service nginx stop

6.添加到开机自启

chkconfig nginx on

nginx开启日志

server { 
  # access_log off;
	access_log  logs/access.log;
}

nginx日志备份脚本

#!/bin/bash
base_path='/www/server/nginx/logs/' #日志文件目录
log_name='access.log' #日志文件名
log_path_y=$(date -d yesterday '+%Y' )
log_path_md=$(date -d yesterday '+%m%d')
log_path_hms=$(date -d yesterday '+%H%M%S')
mkdir -p /home/enzo/nginx/nginx_logs/$log_path_y/$log_path_md #创建备份目录
mv $base_path$log_name /home/enzo/nginx/nginx_logs/$log_path_y/$log_path_md/$log_path_hms-$log_name #移动日志
touch $base_path$log_name #生成新日志文件
kill -USR1 $(cat /www/server/nginx/logs/nginx.pid) #nginx重读配置文件         

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值