系统及软件版本

CentOS7.8 nginx1.17.7 php5.4.7 本机IP:10.24.61.198
创建程序用户
useradd -s /sbin/nologin -M nginx

数据保存目录

mkdir -p /data/nginx/data/share/upload/
mkdir -p /data/nginx/data/tmp/upload/
cd /data/nginx/data/tmp/upload/
mkdir 0 1 2 3 4 5 6 7 8 9 state

授权目录属主

chown -R nginx:nginx /data/nginx/

目录结构

[root@nginx-wh-01 data]# pwd/data/nginx/data
[root@nginx-wh-01 data]# tree
.
├── share
│ └── upload
└── tmp
└── upload
├── 0
├── 1
├── 2
├── 3
├── 4
├── 5
├── 6
├── 7
├── 8
├── 9
└── state

15 directories, 0 files
[root@nginx-wh-01 data]#
image.png

安装

nginx安装

安装必要依赖软件

yum remove nginx
yum -y install gcc gcc-c++ autoconf automake gd gd-devel zlib zlib-devel openssl openssl-devel pcre-devel

创建包存储目录

mkdir /data/rpms
cd //data/rpms

下载nginx二进制包

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

下载上传模块包

git clone https://github.com/hongzhidao/nginx-upload-module.git

下载上传模块进度包

git clone https://github.com/masterzen/nginx-upload-progress-module.git

安装nginx

解包

tar -xzvf nginx-1.17.7.tar.gz
cd nginx-1.17.7/

预配置

./configure —with-debug —prefix=/data/nginx —sbin-path=/usr/sbin/nginx --error-log-path=/data/nginx/logs/error.log —http-log-path=/data/nginx/logs/access.log —pid-path=/var/run/nginx.pid \
—lock-path=/var/run/nginx.lock —http-client-body-temp-path=/data/nginx/client_temp \
—http-proxy-temp-path=/data/nginx/proxy_temp —http-fastcgi-temp-path=/data/nginx/fastcgi_temp \
—http-uwsgi-temp-path=/data/nginx/uwsgi_temp —http-scgi-temp-path=/data/nginx/scgi_temp \
—user=nginx —group=nginx \
—add-module=/data/rpms/nginx-upload-module —add-module=/data/rpms/nginx-upload-progress-module \
—with-stream —with-http_image_filter_module —with-http_ssl_module —with-http_realip_module \
—with-http_addition_module —with-http_sub_module —with-http_dav_module —with-http_flv_module \
—with-http_mp4_module —with-http_gunzip_module —with-http_gzip_static_module —with-http_random_index_module \
—with-http_secure_link_module —with-http_stub_status_module —with-http_auth_request_module —with-file-aio \
—with-cc-opt=’-Wno-format-security -Wno-unused-but-set-variable -Wno-unused-result -D NGX_HAVE_OPENSSL_MD5_H=1 -D NGX_OPENSSL_MD5=1 -D NGX_HAVE_OPENSSL_SHA1_H=1 -O2 -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector —param=ssp-buffer-size=4 -m64 -mtune=generic’

编译&&安装

make && make install

配置nginx

[root@nginx-wh-01 nginx]# cat conf/nginx.confuser nginx;
worker_processes 4;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
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 logs/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
upload_progress proxied 8m;
gzip on;
server {
listen 80;
# auth_basic “Please input password”; #这里是验证时的提示信息
# auth_basic_user_file /etc/nginx/passwd/testpwd;
client_max_body_size 100g; # 这个配置表示最大上传大小,但是我没有验证过是否能传 100g 的文件
# Upload form should be submitted to this location
location /upload {
# Pass altered request body to this location
upload_pass /upload.php;
# 开启resumable
upload_resumable on;
# Store files to this directory
# The directory is hashed, subdirectories 0 1 2 3 4 5 6 7 8 9 should exist
# 记得修改目录的读写权限
upload_store /data/nginx/data/tmp/upload 1;
upload_state_store /data/nginx/tmp/upload/state;
# Allow uploaded files to be read by all
upload_store_access all:rw;
# Set specified fields in request body
upload_set_form_field “${upload_field_name}_name” $upload_file_name;
upload_set_form_field “${upload_field_name}_content_type” $upload_content_type;
upload_set_form_field “${upload_field_name}_path” $upload_tmp_path;
# Inform backend about hash and size of a file
upload_aggregate_form_field “${upload_field_name}_md5” $upload_file_md5;
upload_aggregate_form_field “${upload_field_name}_size” $upload_file_size;
upload_pass_form_field “^submit$|^description$”;
}
location ~ .php$ {
root html;
# fastcgi_pass unix:/run/php-fpm/php-fpm.sock;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME /data/nginx/html$fastcgi_script_name; #注意这块,之前使用上面的那条配置,一直报file not found,将$document_root换成网站根路径好了
include fastcgi_params;
}
location /myfiles {
alias /data/nginx/data/share/upload/; # 文件存放目录,注意要以 ‘/‘ 结尾;
index index.html; # 如果文件存放目录有 index.html,会跳转到 index.html;
autoindex on; # 自动列出目录下的文件;
autoindex_exact_size off; # 文件大小按 G、M 的格式显示,而不是 Bytes;
}
}
}
[root@nginx-wh-01 nginx]#

编辑上传php文件

[root@nginx-wh-01 nginx]# cat html/upload.php <?php
$header_prefix = ‘file’;
$slots = 6;
?>





<?php
if ($_POST){
echo “

Uploaded files:

“;
echo ““;
echo ““;
for ($i=1;$i<=$slots;$i++){
$key = $header_prefix.$i;
if (array_key_exists($key.”_name”, $_POST) && array_key_exists($key.”_path”,$_POST)) {
$tmp_name = $_POST[$key.”_path”];
$name = $_POST[$key.”_name”];
$content_type = $_POST[$key.”_content_type”];
$md5 = $_POST[$key.”_md5”];
$size = $_POST[$key.”_size”];
$final_path = “/export/share/upload”;
if (copy($tmp_name, “$final_path/$name”)) {
echo “SUCCESS!”;
} else {
echo “FAIL!”;
}
$scp_cmd = “scp team@:/export/share/upload/$name .”;
$wget_cmd = “wget http://
/files/upload/$name”;
echo ““;
}
}
echo “
NameLocationContent typeMD5SizeScp CommandWget Command
$name$final_path$content_type$md5$size$scp_cmd$wget_cmd
“;
}else{?>

Select files to upload


















<?php
}
?>


[root@nginx-wh-01 nginx]#

增加nginx 网页登录验证

这部分选配,如果做了就需要吧nginx.conf中的auth那两处打开
yum -y install httpd-tools
mkdir -p /etc/nginx/passwd/
htpasswd -c /etc/nginx/passwd/testpwd user1
输入2遍user1 登录密码
修改密码方法:http://lnmp.ailinux.net/htpasswd

增加nginx启动文件

这部分选配,也可以用ln -s做软连接就好
vim /lib/systemd/system/nginx.service
[root@vhost8 local]# cat /lib/systemd/system/nginx.service
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/run/nginx.pid
# Nginx will fail to start if /run/nginx.pid already exists but has the wrong
# SELinux context. This might happen when running nginx -t from the cmdline.
# https://bugzilla.redhat.com/show_bug.cgi?id=1268621
ExecStartPre=/usr/bin/rm -f /run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true
[Install]
WantedBy=multi-user.target

php安装

安装必要依赖软件

yum -y install gcc gcc-c++ glibc automake autoconf libtool make
yum -y install libmcrypt libmcrypt-devel mhash-devel libxslt-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel

cd ~

下载php二进制包

wget https://museum.php.net/php5/php-5.4.7.tar.gz

安装php

解包

tar -xzvf php-5.4.7.tar.gz
cd php-5.4.7

预配置

./configure —prefix=/data/php —enable-fpm —with-mcrypt —enable-mbstring —disable-pdo --with-curl —disable-debug —disable-rpath —enable-inline-optimization —with-bz2 —with-zlib \
—enable-sockets —enable-sysvsem —enable-sysvshm —enable-pcntl —enable-mbregex —with-mhash \
—enable-zip —with-pcre-regex —with-mysql —with-mysqli —with-gd —with-jpeg-dir

编译&&安装

make && make install

配置php

cd /data/php

复制配置文件

cp etc/php-fpm.conf.default etc/php-fpm.conf

编辑配置文件

vim etc/php-fpm.conf
修改
user = nginx
group = nginx

启动php-fpm服务

cd /data/php
./sbin/php-fpm &

启动nginx服务

systemctl enable nginx.service
systemctl start nginx
systemctl status nginx
#没有创建启动文件的化用ln -s将软连到sbin下,或直接绝对路径启动也可以

测试

首页展示

10.24.61.198 #本机IP
image.png

测试nginx连通php

在html目录下
echo “<?php phpinfo(); ?>” >test_php.php
地址栏访问这个php文件
image.png

文件概览

10.24.61.198/myfiles
image.png

文件上传

10.24.61.198/upload.php
image.png
image.png

再次概览验证

服务器上内部查看
image.png

index静态代码




Good good study,up up day!




Hello!



This is just a simple web page

  1. You can upload or download files
  2. Simple interface, simple use
  3. Happy use !<br /> </p><br /> <p><br /> Still need to work hard !<br /> </p><br /> </div><br /> <a href="[http://10.24.61.198/upload.php">](http://10.24.61.198/upload.php">)<br /> <button>upload</button><br /> <button type="button" class="btn btn-primary"><br /> </button><br /> </a><br /> <a href="[http://10.24.61.198/myfiles">](http://10.24.61.198/myfiles">)<br /> <button>download</button><br /> <button type="button" class="btn btn-primary"><br /> </button><br /> </div><br /> </div><br /></div><br />~ <br />

参考地址:
https://miopas.github.io/2018/07/24/nginx-file-server-upload/
https://blog.csdn.net/bigtree_3721/article/details/79870937
https://www.yanxurui.cc/posts/server/2017-03-21-NGINX-as-a-file-server/
https://blog.51cto.com/u_1460758/2466921 *