ubuntu18.04安装nginx并支持https和文件上传
在ubuntu10.4server下安装nginx并支持上传和https功能
环境:ubuntu18.04server
在root权限下操作
1. 安装编译环境
1 2 | cd apt -y install make gcc libpcre3 libpcre3-dev openssl libssl-dev zlib1g-dev build-essential unzip |
2. 下载并解压nginx源码
1 2 | wget http: //nginx.org/download/nginx-1.15.8.tar.gz tar -zxvf nginx-1.8.1.tar.gz |
3. 下载并解压上传模块
1 2 | wget https: //github.com/fdintino/nginx-upload-module/archive/refs/heads/master.zip unzip master.zip |
5. 移动nginx和上传模块到指定目录
1 2 | mv nginx-1.8.1 /usr/local/nginx mv nginx-upload-module-master /usr/local/nginx/ |
6. 生成服务器密钥和证书用于https,
6.1 进入openssl下执行以下命令
1 | openssl |
6.2 生成CA私钥
1 | openssl> genrsa -des3 -out ca.key 1024 |
6.3 去除CA私钥密码
1 | openssl> rsa -in ca.key -out ca.key |
6.3 生成CA
1 | openssl> req - new -x509 -key ca.key -out ca.crt -days 365 |
6.4 重新进入openssl生成用户证书等
1 2 | openssl> exit openssl |
6.4 生成用户私钥
1 | openssl> genrsa -des3 -out server.key 1024 |
6.5 去除用户私钥中的密码
1 | openssl> rsa -in server.key -out server.key |
6.5 生成证书申请文件
1 | openssl> req - new -key server.key -out server.csr |
6.6 通过CA生成证书
1 | openssl> x509 -req -in server.csr -CA ca.crt -CAkey ca.key -set_serial 01 -out server.crt -days 365 |
6.7 需要的文件为用户私钥server.key和用户证书server.crt,将用户私钥和用户证书移动到nginx下目录CA中
1 2 3 | mkdir -p /usr/local/nginx/CA mv server.key /usr/local/nginx/CA mv server.crt /usr/local/nginx/CA |
7. 编译nginx
1 2 3 | cd /usr/local/nginx ./configure --prefix=/usr/local/nginx --conf-path=/etc/nginx/nginx.conf --with-http_ssl_module --add-module=./nginx-upload-module-master make |
8. 修改配置文件/usr/local/nginx/conf/nginx.conf,替换为以下内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | user root; worker_processes 1; pid logs/nginx.pid; events { worker_connections 1024; } http { sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; include /usr/local/nginx/conf/mime.types; default_type application/octet-stream; server { client_max_body_size 100g; listen 80 default_server; listen [::]:80 default_server; server_name _; autoindex on; listen 443 ssl; listen [::]:443 default_server; server_name localhost; ssl_certificate /usr/local/nginx/CA/server.crt; ssl_certificate_key /usr/local/nginx/CA/server.key; ssl_session_cache shared:SSL:1m; ssl_session_timeout 5m; ssl_ciphers HIGH:!aNULL:!MD5; ssl_prefer_server_ciphers on; location / { root /usr/local/nginx/html; autoindex on; } location /upload { upload_store /usr/local/nginx/html/upload; upload_store_access user:rw; 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" ; upload_pass_form_field "^submit$|^description$" ; upload_pass @mock; } location @mock { return 200 "ok\r\n" ; } } } |
9. 创建上传文件夹支持文件上传功能
1 2 | mkdir /usr/local/nginx/html/upload chmod 777 /usr/local/nginx/upload/ |
10. 增加上传文件测试网页/usr/local/nginx/html/test_upload.html,内容如下
1 2 3 4 5 6 7 8 9 10 11 12 | <html> <head> <title>Test upload</title> </head> <body> <h2>Select files to upload</h2> <form enctype= "multipart/form-data" action= "/upload" method= "post" > <input type= "file" name= "file1" ><br> <input type= "submit" name= "submit" value= "Upload" > </form> </body> </html> |
11. 安装nginx
1 | make install |
12. 检查nginx配置文件正确性
1 | /usr/local/nginx/sbin/nginx -t |
13. 开启nginx
1 | /usr/local/nginx/sbin/nginx |
14. 测试文件上传,访问http://127.0.0.1/upload,上传文件之后在/usr/local/nginx/html/upload下是否有文件存在
15. 测试https,访问https://127.0.0.1/
16 测试http,访问http://127.0.0.1/
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了