行业知识
CentOS用nginx搭建文件下载服务器
2026-12-03 07:16:00
来源:互联网转载
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_modulemake && 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 nginxkillall -s QUIT nginx#重载kill -1 kill -s HUP killall -1 nginxkillall -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 #!/bin/bashlocal_path=$(pwd)echo "localPath: $local_path"nginx_error_log="$local_path/nginx/logs/error.log info"nginx_pnginx_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 nginxConfigFilesed -i "s|error_log .*;$|error_log ${nginx_error_log};|g" $nginx_config_filesed -i "s|access_log .*;$|access_log ${nginx_access_log};|g" $nginx_config_filesed -i "s|pid .*;$|pid ${nginx_pid};|g" $nginx_config_filesed -i "s|listen .*;$|listen ${port};|g" $nginx_config_file sed -i "s|root .*;$|root ${root_path};|g" $nginx_config_file #stop already started nginxif [ -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 fifiecho "begin start nginx"/usr/local/nginx/sbin/nginx -c $nginx_config_file5、启动项目
首先需要在nginx/logs下面新建nginx.pid文件,执行命令如下
touch nginx/logs/nginx.pid
将server.conf配置好后,执行start.sh文件,就可以启动项目,每次重启也只需要执行start.sh文件即可。
./start.sh
后记
个人总结,欢迎转载、评论、批评指正
上一篇:上海怎么选择云服务器带宽?
下一篇:电脑划样机操作教程