CentOS用nginx搭建文件下载服务器

06-01 1230阅读

  Nginx 是开源、高性能、高可靠的 Web 和反向代理服务器,而且支持热部署,几乎可以做到 7 * 24 小时不间断运行,即使运行几个月也不需要重新启动。在工作中,我们经常会用到需要搭建文件服务器的情况,这里就以在linux下搭建文件服务器为例,解释编译nginx和搭建服务器的过程。

一、nginx编译安装

1、下载nginx

  • nginx下载网站
  • wget下载命令
    wget http://nginx.org/download/nginx-1.25.2.tar.gz
    

    2、解压压缩包

    tar -zxvf nginx-1.25.2.tar.gz
    

    3、创建用户和用户组

    useradd -M -s /sbin/nologin nginx
    

    4、编译安装nginx

    # 依次执行下面命令
    cd nginx-1.25.2
    ./configure \
    --prefix=/usr/local/nginx \
    --user=nginx \
    --group=nginx \
    --without-http_rewrite_module \
    --without-http_gzip_module --with-http_stub_status_module --with-http_ssl_module 
    make && make install
    #让系统识别nginx的操作命
    ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/		
    

    如果在编译过程中报错需要依赖包,执行以下命令安装依赖

    #nginx的配置及运行需要pcre、zlib、openssl等软件包的支持,因此需要安装这些软件的开发包,以便提供相应的库和头文件。
    yum -y install pcre-devel zlib-devel openssl-devel gcc gcc-c++ make
    

    依赖安装完成重新执行./configure命令

    5、检查、启动、重启、停止 nginx服务的命令

    nginx -t								#检查配置文件是否配置正确
    #启动
    nginx									
    #停止
    cat /usr/local/nginx/logs/nginx.pid		#先查看nginx的PID号
    kill -3 
    kill -s QUIT 
    killall -3 nginx
    killall -s QUIT nginx
    #重载
    kill -1 
    kill -s HUP 
    killall -1 nginx
    killall -s HUP nginx
    #日志分割,重新打开日志文件
    kill -USR1 
    #平滑升级
    kill -USR2 
            listen       8888;
            #配置了监听端口此条不生效
            server_name  localhost;
            #文件服务器本地存储路径
            root /root/nginx_storge;
        }
    
        worker_connections  1024;
    }
    http {
        default_type  application/octet-stream;
        log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                          '$status $body_bytes_sent "$http_referer" '
                          '"$http_user_agent" "$http_x_forwarded_for"';
        access_log /usr/local/nginx/logs/access.log  main;
        sendfile        on;
        #tcp_nopush     on;
        #keepalive_timeout  0;
        keepalive_timeout  65;
        #gzip  on;
            # 显示目录
        autoindex on;
        # 显示文件大小
        autoindex_exact_size on;
        # 显示文件时间
        autoindex_localtime on;
        # 防止中文乱码
        charset utf-8;
        server {
            listen 8888;
            #配置了监听端口此条不生效
            server_name  localhost;
            #文件服务器本地存储路径
            root /home/filePath;
            access_log /usr/local/nginx/logs/access.log  main;
        #    ssl_certificate      cert.pem;
        #    ssl_certificate_key  cert.key;
        }
    }
    
    h34、start.sh文件/h3 pre class="brush:python;toolbar:false"#!/bin/bash local_path=$(pwd) echo "localPath: $local_path" nginx_error_log="$local_path/nginx/logs/error.log info" nginx_pid="$local_path/nginx/logs/nginx.pid" nginx_access_log="$local_path/nginx/logs/access.log main" nginx_config_file="$local_path/nginx/conf/nginx.conf" #get config port=`sed '/^recordVideoDownloadPord=/!d;s/.*=//' $local_path/server.conf` root_path=`sed '/^recordVideoDownloadRootPath=/!d;s/.*=//' $local_path/server.conf` echo "read config port : $port" echo "read config root : $root_path" #replace nginxConfigFile sed -i "s|error_log .*;$|error_log ${nginx_error_log};|g" $nginx_config_file sed -i "s|access_log .*;$|access_log ${nginx_access_log};|g" $nginx_config_file sed -i "s|pid .*;$|pid ${nginx_pid};|g" $nginx_config_file sed -i "s|listen .*;$|listen ${port};|g" $nginx_config_file sed -i "s|root .*;$|root ${root_path};|g" $nginx_config_file #stop already started nginx if [ -f "$nginx_pid" ]; then pid=$(cat $nginx_pid) if ps -p $pid > /dev/null then echo "nginx is running pid=$pid, begin stop nginx " kill -3 $pid fi fi echo "begin start nginx" /usr/local/nginx/sbin/nginx -c $nginx_config_file

    5、启动项目

    首先需要在nginx/logs下面新建nginx.pid文件,执行命令如下

    touch nginx/logs/nginx.pid
    

    将server.conf配置好后,执行start.sh文件,就可以启动项目,每次重启也只需要执行start.sh文件即可。

    ./start.sh
    

    后记

      个人总结,欢迎转载、评论、批评指正

免责声明:我们致力于保护作者版权,注重分享,被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理! 图片声明:本站部分配图来自人工智能系统AI生成,觅知网授权图片,PxHere摄影无版权图库和百度,360,搜狗等多加搜索引擎自动关键词搜索配图,如有侵权的图片,请第一时间联系我们。

目录[+]

取消
微信二维码
微信二维码
支付宝二维码