Ubuntu上使用nginx搭建https文件服务器

1.这里我们使用nginx搭建https服务器,因为其设置比apache简单很多。

使用nginx 搭建web服务器网上有很多文档。

这里我使用sudo apt-get install nginx安装nginx,全部使用默认设置。

如果安装成功,在浏览器输入IP地址会弹出如下窗口:

到这一步你的基于nginx的web服务器已经搭建成功了。

下面是如何搭建https服务器以及如何把文件放到https服务器中。

2.搭建https服务器

可以参考:

http://www.cnblogs.com/yanghuahui/archive/2012/06/25/2561568.html

如果是默认安装nginx会安装在/usr/share/nginx/

进入/usr/share/nginx/创建conf文件夹。

2.1生成证书

可以通过以下步骤生成一个简单的证书:

首先,进入你想创建证书和私钥的目录。

创建服务器私钥,命令会让你输入一个口令:

$openssl genrsa -des3 -out server.key 1024

例如:

root@host:cd /usr/share/nginx/conf

root@host:/usr/share/nginx/conf# openssl genrsa -des3 -out server.key 1024

Generating RSA private key, 1024 bit long modulus

............................................++++++

..++++++

e is 65537 (0x10001)

Enter pass phrase for server.key:

Verifying - Enter pass phrase for server.key:

root@host:/usr/share/nginx/conf#

root@host:/usr/share/nginx/conf# ls -l

total 12

drwxr-xr-x 2 root root 4096  4月  1 15:09 ./

drwxr-xr-x 4 root root 4096  4月  1 15:05 ../

-rw-r--r-- 1 root root  963  4月  1 15:09 server.key

生成server.key时要输入pass phrase for server.key,我都是输入的123456

2.2创建签名请求的证书(CSR):

$openssl req -new -key server.key -out server.csr

例如:

root@host:/usr/share/nginx/conf# openssl req -new -key server.key -out server.csr

Enter pass phrase for server.key:

You are about to be asked to enter information that will be incorporated

into your certificate request.

What you are about to enter is what is called a Distinguished Name or a DN.

There are quite a few fields but you can leave some blank

For some fields there will be a default value,

If you enter '.', the field will be left blank.

-----

Country Name (2 letter code) [AU]:CN

State or Province Name (full name) [Some-State]:FJ

Locality Name (eg, city) []:XM

Organization Name (eg, company) [Internet Widgits Pty Ltd]:DNI

Organizational Unit Name (eg, section) []:RD

Common Name (e.g. server FQDN or YOUR name) []:TEST

Email Address []:dniplex2@163.com

 

Please enter the following 'extra' attributes

to be sent with your certificate request

A challenge password []:123456

An optional company name []:DNI   

You have new mail in /var/mail/root

root@host:/usr/share/nginx/conf#ls -l

total 16

drwxr-xr-x 2 root root 4096  4月  1 15:12 ./

drwxr-xr-x 4 root root 4096  4月  1 15:05 ../

-rw-r--r-- 1 root root  725  4月  1 15:12 server.csr

-rw-r--r-- 1 root root  963  4月  1 15:09 server.key

root@lu-host:/usr/share/nginx/conf#

创建server.csr时会要求输入一些信息。这个可以随便填写。

2.3在加载SSL支持的Nginx并使用上述私钥时除去必须的口令:

$ cp server.key server.key.org

$ openssl rsa -in server.key.org -out server.key

例如:

root@host:/usr/share/nginx/conf# cp server.key server.key.org

root@host:/usr/share/nginx/conf# openssl rsa -in server.key.org -out server.key

Enter pass phrase for server.key.org:

writing RSA key

root@host:/usr/share/nginx/conf# ls -l

total 12

-rw-r--r-- 1 root root 725  4月  1 15:12 server.csr

-rw-r--r-- 1 root root 887  4月  1 15:19 server.key

-rw-r--r-- 1 root root 963  4月  1 15:19 server.key.org

root@lu-host:/usr/share/nginx/conf# 3.2.4配置nginx

2.4最后标记证书使用上述私钥和CSR:

$openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt

例如:

root@host:/usr/share/nginx/conf# openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt

Signature ok

subject=/C=CN/ST=FJ/L=XM/O=DNI/OU=RD/CN=TEST/emailAddress=dniplex2@163.com

Getting Private key

root@host:/usr/share/nginx/conf# ls -l

total 24

drwxr-xr-x 2 root root 4096  4月  1 15:21 ./

drwxr-xr-x 4 root root 4096  4月  1 15:05 ../

-rw-r--r-- 1 root root  879  4月  1 15:21 server.crt

-rw-r--r-- 1 root root  725  4月  1 15:12 server.csr

-rw-r--r-- 1 root root  887  4月  1 15:19 server.key

-rw-r--r-- 1 root root  963  4月  1 15:19 server.key.org

root@host:/usr/share/nginx/conf#

 

2.5修改Nginx配置文件/etc/nginx/nginx.conf,让其包含新标记的证书和私钥:

如果没有server{}可以在http{}中添加。

server{

        server_name my_test;

        listen 443;

        root /usr/share/nginx;

        autoindex on;

        ssl on;

        ssl_certificate /usr/share/nginx/conf/server.crt;

        ssl_certificate_key /usr/share/nginx/conf/server.key;

}

重启nginx。

service nginx restart

这样就可以通过以下方式访问:

https://192.168.33.3

 

3如何把文件放到https服务器

创建download文件夹。在里面放上文件.

如下:

root@host:cd /usr/share/nginx/conf

root@host:/usr/share/nginx# mkdir download/

root@host:/usr/share/nginx#

将文件放入即可。

root@host:/usr/share/nginx#ls -l download/

test.zip

放好之后试试:

root@lu-host:/usr/share/nginx# curl --insecure https://192.168.33.3/download/test.zip  -o  /tmp/test.zip

如果能下载成功说明搭建成功。

也可以通过浏览器下载:

浏览器输入:“https://192.168.33.3”,选择download目录,选择test.zip开始下载。

 

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值