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

打印 上一主题 下一主题

主题 511|帖子 511|积分 1533

  Nginx 是开源、高性能、高可靠的 Web 和反向代理服务器,而且支持热部署,险些可以做到 7 * 24 小时不中断运行,纵然运行几个月也不需要重新启动。在工作中,我们常常会用到需要搭建文件服务器的环境,这里就以在linux下搭建文件服务器为例,表明编译nginx和搭建服务器的过程。
一、nginx编译安装

1、下载nginx



  • nginx下载网站
  • wget下载命令
  1. wget http://nginx.org/download/nginx-1.25.2.tar.gz
复制代码
2、解压压缩包

  1. tar -zxvf nginx-1.25.2.tar.gz
复制代码
3、创建用户和用户组

  1. useradd -M -s /sbin/nologin nginx
复制代码
4、编译安装nginx

  1. # 依次执行下面命令
  2. cd nginx-1.25.2
  3. ./configure \
  4. --prefix=/usr/local/nginx \
  5. --user=nginx \
  6. --group=nginx \
  7. --without-http_rewrite_module \
  8. --without-http_gzip_module
  9. make && make install
  10. #让系统识别nginx的操作命
  11. ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/               
复制代码
假如在编译过程中报错需要依靠包,执行以下命令安装依靠
  1. #nginx的配置及运行需要pcre、zlib、openssl等软件包的支持,因此需要安装这些软件的开发包,以便提供相应的库和头文件。
  2. yum -y install pcre-devel zlib-devel openssl-devel gcc gcc-c++ make
复制代码
依靠安装完成重新执行./configure命令
5、查抄、启动、重启、制止 nginx服务的命令

  1. nginx -t                                                                #检查配置文件是否配置正确
  2. #启动
  3. nginx                                                                       
  4. #停止
  5. cat /usr/local/nginx/logs/nginx.pid                #先查看nginx的PID号
  6. kill -3 <PID号>
  7. kill -s QUIT <PID号>
  8. killall -3 nginx
  9. killall -s QUIT nginx
  10. #重载
  11. kill -1 <PID号>
  12. kill -s HUP <PID号>
  13. killall -1 nginx
  14. killall -s HUP nginx
  15. #日志分割,重新打开日志文件
  16. kill -USR1 <PID号>
  17. #平滑升级
  18. kill -USR2 <PID号
复制代码

二、设置nginx文件下载服务器

1、设置nginx.conf文件



  • 到nginx.conf文件目录
  1. cd  /usr/local/nginx/conf/
复制代码


  • 先备份nginx.cong文件
  1. cp nginx.conf nginx.conf_bak
复制代码


  • 编辑/usr/local/nginx/conf/nginx.conf文件,
    将 user nobody 改成:user root并取消解释

    设置以下部门
  1.     # 显示目录
  2.     autoindex on;
  3.     # 显示文件大小
  4.     autoindex_exact_size on;
  5.     # 显示文件时间
  6.     autoindex_localtime on;
  7.     # 防止中文乱码
  8.     charset utf-8;
  9.     server {
  10.         listen       8888;
  11.         #配置了监听端口此条不生效
  12.         server_name  localhost;
  13.         #文件服务器本地存储路径
  14.         root /root/nginx_storge;
  15.     }
复制代码



  • 创建/root/nginx_storge文件夹
  1. mkdir /root/nginx_storge
复制代码
2、查抄并启动nginx

  1. # 检查nginx配置
  2. nginx -t
  3. #启动nginx
  4. nginx -c /usr/local/nginx/conf/nginx.conf
  5. #重载nginx
  6. nginx -s reload
复制代码

3、测试下载

在cd /root/nginx_storge/中新建几个文件和文件夹

在浏览器中输入地址:http://192.168.86.129:8888/(根据本身的ip修改)可以看到文件信息,点击文件可以下载

三、自动启动nginx的启动脚本

  在现实的工作中,我们需要在差异的地方启动nginx,这种环境下,我们就可以通过自界说脚本的方式来实现该功能。下面我们就以一个视频下载为例来简单说明内容的写法。
1 目录接口

我们起首需要创建一个目录,目录的命名不紧张,可以按照各自的业务来,由于脚本是按照相对路径来的,用的时候只需要将该文件夹直接拷贝已往,就可以直接执行,文件夹中的内容结构如下:
---- server.conf #下载路径相干的设置
---- start.sh #执行脚本
---- nginx #nginx相干的文件夹
-------- conf
------------ nginx.conf #nginx的设置文件
-------- logs
------------ access.log #访问日志文件 ,一开始只需要创建一个空文件即可
------------ error.log #错误日志文件 ,一开始只需要创建一个空文件即可
------------ nginx.pid #pid文件 ,一开始只需要创建一个空文件即可
2、server.conf内容

server.conf是设置文件,重要是下载的端口和服务器上面下载文件的绝对路径
  1. recordVideoDownloadPord=8888
  2. recordVideoDownloadRootPath=/home/filePath
复制代码
3、nginx.conf内容

nginx.conf是nginx设置文件的模板,里面是重要的设置框架,现实内容会在执行start.sh时根据设置更换
  1. user  root;
  2. worker_processes  1;
  3. error_log /usr/local/nginx/logs/error.log  info;
  4. pid        /usr/local/nginx/logs/nginx.pid;
  5. events {
  6.     worker_connections  1024;
  7. }
  8. http {
  9.     default_type  application/octet-stream;
  10.     log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
  11.                       '$status $body_bytes_sent "$http_referer" '
  12.                       '"$http_user_agent" "$http_x_forwarded_for"';
  13.     access_log /usr/local/nginx/logs/access.log  main;
  14.     sendfile        on;
  15.     #tcp_nopush     on;
  16.     #keepalive_timeout  0;
  17.     keepalive_timeout  65;
  18.     #gzip  on;
  19.         # 显示目录
  20.     autoindex on;
  21.     # 显示文件大小
  22.     autoindex_exact_size on;
  23.     # 显示文件时间
  24.     autoindex_localtime on;
  25.     # 防止中文乱码
  26.     charset utf-8;
  27.     server {
  28.         listen 8888;
  29.         #配置了监听端口此条不生效
  30.         server_name  localhost;
  31.         #文件服务器本地存储路径
  32.         root /home/filePath;
  33.         access_log /usr/local/nginx/logs/access.log  main;
  34.     #    ssl_certificate      cert.pem;
  35.     #    ssl_certificate_key  cert.key;
  36.     }
  37. }
复制代码
4、start.sh文件

  1. #!/bin/bash
  2. local_path=$(pwd)
  3. echo "localPath: $local_path"
  4. nginx_error_log="$local_path/nginx/logs/error.log  info"
  5. nginx_pid="$local_path/nginx/logs/nginx.pid"
  6. nginx_access_log="$local_path/nginx/logs/access.log  main"
  7. nginx_config_file="$local_path/nginx/conf/nginx.conf"
  8. #get config  
  9. port=`sed '/^recordVideoDownloadPord=/!d;s/.*=//' $local_path/server.conf`
  10. root_path=`sed '/^recordVideoDownloadRootPath=/!d;s/.*=//' $local_path/server.conf`
  11.             
  12. echo "read config port : $port"
  13. echo "read config root : $root_path"
  14. #replace nginxConfigFile
  15. sed -i  "s|error_log .*;$|error_log ${nginx_error_log};|g" $nginx_config_file
  16. sed -i  "s|access_log .*;$|access_log ${nginx_access_log};|g" $nginx_config_file
  17. sed -i  "s|pid .*;$|pid        ${nginx_pid};|g" $nginx_config_file
  18. sed -i  "s|listen .*;$|listen ${port};|g" $nginx_config_file
  19. sed -i  "s|root .*;$|root ${root_path};|g" $nginx_config_file
  20.    
  21. #stop already started nginx
  22. if [ -f "$nginx_pid" ]; then
  23.     pid=$(cat $nginx_pid)
  24.     if  ps -p $pid  > /dev/null
  25.     then
  26.         echo "nginx is running pid=$pid, begin stop nginx "
  27.         kill -3 $pid
  28.     fi
  29. fi
  30. echo "begin start nginx"
  31. /usr/local/nginx/sbin/nginx -c $nginx_config_file
复制代码
5、启动项目

起首需要在nginx/logs下面新建nginx.pid文件,执行命令如下
  1. touch nginx/logs/nginx.pid
复制代码
将server.conf设置好后,执行start.sh文件,就可以启动项目,每次重启也只需要执行start.sh文件即可。
  1. ./start.sh
复制代码

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

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

立聪堂德州十三局店

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表