Ubuntu搭建网站

使用 Nginx Git搭建静态网站、自签SSL证书

常用指令

systemctl

  1. 重新加载systemctl所有
1
sudo systemctl daemon-reload
  1. systemctl加载Nginx
1
sudo systemctl reload nginx.service
  1. systemctl重启Nginx
1
sudo systemctl restart nginx.service
  1. systemctl停止Nginx
1
sudo systemctl stop nginx.service
  1. systemctl删除Nginx
1
sudo systemctl disable nginx.service 
  1. systemctl启动Nginx
1
sudo systemctl start nginx.service
  1. systemctl开启自启Nginx
1
sudo systemctl enable nginx.service 

重启系统

1
sudo shutdown -r now

重启Nginx

1
sudo nginx -s reload

Hexo 静态网站其他配置

参考本站点:可以在本站搜索中输入 ubuntu搭建git服务器及配置

GIT服务器

可以使用如下makefile文件,会在指定目录下创建4个文件,以及配置好默认主分支的自动化部署功能。可以先执行查看,然后再重新配置,makefile设置成root权限,同时更改用户权限到root下运行;

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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
DATE_TIME = $(shell date '+%Y-%m-%d %H:%M:%S')	##获取时间
CURRENT_DIR:=$(shell pwd) ## 获取当前目录
CURRENT_GROUP = $(id -gn) ##获取当前用户组
# 使用join函数合并路径

GIT_INIT =sudo git init
GIT_BARE = $(GIT_INIT) --bare

## 打开文件
OPEN_FILE = sudo vim

## 创建文件
CREATE_FILE = sudo touch

## 修改文件为可执行权限
SET_FILE_X = sudo chmod +x

## 删除文件以及文件夹
DELETE_FOF = sudo rm -rf

## 站点目录
WEBSITE_DIR = website

## 源代码目录
SOURCE_CODE_DIR = SourceCode

HELP_CONTENT = \
make make默认程序 \n\
make INIT_SourceCode 创建源代码仓库 \n\
make ADD_SourceCode_post-receive 添加源代码仓库自动化部署文件 \n\
make A_EDIT_SourceCode_post-receive 自动创建源代码仓库自动化部署文件 \n\
make M_EDIT_SourceCode_post-receive 手动创建源代码仓库自动化部署文件 \n\
make INIT_website 创建网站仓库 \n\
make ADD_website_post-receive 添加网站仓库自动化部署文件 \n\
make A_EDIT_website_post-receive 自动创建网站仓库自动化部署文件 \n\
make M_EDIT_website_post-receive 手动创建网站仓库自动化部署文件 \n\
make INIT_PRO 初始化整个项目文件 \n\
make RM_SourceCode 删除源代码仓库 \n\
make RM_website 删除网站仓库 \n\
make RM_PRO 删除整个项目 \n\
make DELETEALL 删除整个项目以外,还可以删除指定的文件 \n\
make help 帮助信息 \n\
更多请查看:https://www.goupos.cn/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F/Ubuntu/Ubuntu%E6%90%AD%E5%BB%BA%E7%BD%91%E7%AB%99.html

DEFINE: ##默认目标
@echo "为防止错误运行,默认目标只做提示打印,如果需要帮助,输入:make help\n$(DATE_TIME)"
@echo "Ubuntu平台"
@echo "$(HELP_CONTENT)"

INIT_SourceCode: $(git -v)
$(GIT_BARE) $(SOURCE_CODE_DIR).git
$(GIT_INIT) $(SOURCE_CODE_DIR)

ADD_SourceCode_post-receive: INIT_SourceCode
@echo "$(CURRENT_DIR)"
@echo $(join $(CURRENT_DIR),/$(SOURCE_CODE_DIR).git/hooks/post-receive)
$(CREATE_FILE) $(join $(CURRENT_DIR),/$(SOURCE_CODE_DIR).git/hooks/post-receive)
$(SET_FILE_X) $(join $(CURRENT_DIR),/$(SOURCE_CODE_DIR).git/hooks/post-receive)



A_EDIT_SourceCode_post-receive: ADD_SourceCode_post-receive
@echo "#!/bin/bash" >$(join $(CURRENT_DIR),/$(SOURCE_CODE_DIR).git/hooks/post-receive)
@echo "unset GIT_DIR" >>$(join $(CURRENT_DIR),/$(SOURCE_CODE_DIR).git/hooks/post-receive)
@echo "git --work-tree=$(join $(CURRENT_DIR),/$(SOURCE_CODE_DIR)) --git-dir=$(join $(CURRENT_DIR),/$(SOURCE_CODE_DIR).git) checkout -f" >>$(join $(CURRENT_DIR),/$(SOURCE_CODE_DIR).git/hooks/post-receive)

M_EDIT_SourceCode_post-receive:
$(OPEN_FILE) $(join $(CURRENT_DIR),/$(SOURCE_CODE_DIR).git/hooks/post-receive)

INIT_website: $(git -v)
## sudo git init --bare website.git
$(GIT_INIT) $(WEBSITE_DIR)
$(GIT_BARE) $(WEBSITE_DIR).git

ADD_website_post-receive: INIT_website
@echo "$(CURRENT_DIR)"
$(CREATE_FILE) $(join $(CURRENT_DIR),/$(WEBSITE_DIR).git/hooks/post-receive)
$(SET_FILE_X) $(join $(CURRENT_DIR),/$(WEBSITE_DIR).git/hooks/post-receive)


A_EDIT_website_post-receive: ADD_website_post-receive
@echo "#!/bin/bash" >$(join $(CURRENT_DIR),/$(WEBSITE_DIR).git/hooks/post-receive)
@echo "unset GIT_DIR" >>$(join $(CURRENT_DIR),/$(WEBSITE_DIR).git/hooks/post-receive)
@echo "git --work-tree=$(join $(CURRENT_DIR),/website) --git-dir=$(join $(CURRENT_DIR),/website.git) checkout -f" >>$(join $(CURRENT_DIR),/$(WEBSITE_DIR).git/hooks/post-receive)

M_EDIT_website_post-receive:
$(OPEN_FILE) $(join $(CURRENT_DIR),/$(WEBSITE_DIR).git/hooks/post-receive)

INIT_PRO: A_EDIT_SourceCode_post-receive A_EDIT_website_post-receive
@echo "创建整个项目文件"
ls -l
@echo "显示所有用户组名:"
@grep -v '^$$' /etc/group | awk -F: '{ print $$1 }'
@echo "设置新建文件夹用户组名,上面为所有用户,如果直接回车不会设置用户组,如果需要设置在下面输入:"
@read input_test; \
sudo chown -R $$input_test:$$input_test $(shell ls | grep -v makefile)
ls -l

RM_SourceCode: $(cd SourceCode)
$(DELETE_FOF) SourceCode.git
$(DELETE_FOF) SourceCode

RM_website: $(cd website)
$(DELETE_FOF) $(WEBSITE_DIR).git
$(DELETE_FOF) $(WEBSITE_DIR)


RM_PRO: RM_SourceCode RM_website

DELETEALL:
@echo "警告:这将会删除当前目录下所有文件"
rm -rf $(shell ls | grep -v makefile)
@echo "输入删除不需要的文件"
@read deletafile; \
rm -rf $$deletafile


help:
@echo "$(HELP_CONTENT)"

WEBSITE_DIR : 设置网站站点目录根目录

SOURCE_CODE_DIR: 设置网站站点源码目录

支持的命令有

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
make  make默认程序
make INIT_SourceCode 创建源代码仓库
make ADD_SourceCode_post-receive 添加源代码仓库自动化部署文件
make A_EDIT_SourceCode_post-receive 自动创建源代码仓库自动化部署文件
make M_EDIT_SourceCode_post-receive 手动创建源代码仓库自动化部署文件
make INIT_website 创建网站仓库
make ADD_website_post-receive 添加网站仓库自动化部署文件
make A_EDIT_website_post-receive 自动创建网站仓库自动化部署文件
make M_EDIT_website_post-receive 手动创建网站仓库自动化部署文件
make INIT_PRO 初始化整个项目文件
make RM_SourceCode 删除源代码仓库
make RM_website 删除网站仓库
make RM_PRO 删除整个项目
make DELETEALL 删除整个项目以外,还可以删除指定的文件
make help 帮助信息

如果初次使用直接可以使用make INIT_PRO即可看到全部文件;如果需要调整自动化部署可以使用make M_EDIT_SourceCode_post-receivemake M_EDIT_website_post-receive 去编辑

安装 Nginx

  1. 首先更新你的包索引:
1
sudo apt update
  1. 安装Nginx
1
sudo apt install nginx
  1. 安装完成后,启动Nginx服务:
1
sudo systemctl start nginx
  1. (可选)设置 Nginx 开机自启:
1
sudo systemctl enable nginx
  1. 你可以通过以下命令检查Nginx服务的状态:
1
sudo systemctl status nginx

如果需要配置Nginx,个人域名可以编辑到 /etc/nginx/nginx.conf 文件夹中;

Nginx默认的配置在文件/etc/nginx/sites-available/

Nginx默认配置的网页在/var/www/html 该目录下

配置Nginx

配置文件说明(可以不看该章节)

listen [::]:80Nginx配置文件中这个[]表示的是使用ipv6格式,那么真正表示的ipv6地址就是::,那么就很简单了::表示的就是全为0ipv6地址(作用可以类比0.0.0.0参考:

default_server nginx 默认虚拟主机

运行以下命令可以检查Nginx服务器上支持的加密套件(可以不执行该命令):

1
openssl ciphers -v 'HIGH:!aNULL:!MD5:!kEDH'

这将列出与指定字符串匹配的所有加密套件,并显示每个套件的详细信息。如果命令输出显示了与你期望的加密套件相匹配的行,那么你的配置应该是正确的。

配置网站文件

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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
server  {
listen 80; #支持IPV4
listen [::]:80; #支持IPV6
server_name your_domain_name; #绑定域名
index index.htm index.html index.php; #默认文件
# root /usr/localBlog/Blog; #网站根目录
# include location.conf; #调用其他规则,也可去除

# 网站加密使用 不加密不要使用
auth_basic "Input name password"; # 验证时的提示信息
auth_basic_user_file /usr/password/password; # 认证文件

location / {
root /usr/localBlog/Blog; #网站根目录
index index.html index.htm; #设置默认页
try_files $uri $uri/ /index.html;
}
}


server {
listen 443 ssl http2; #支持IPV4
listen [::]:443 ssl http2; #支持IPV6
server_name your_domain_name; #绑定域名
index index.htm index.html index.php; #默认文件

ssl on; # 开启 Nginx SSL 模块
ssl_certificate /usr/Nginx/fullchain.crt; # 网站新证书路径
ssl_certificate_key /usr/Nginx/private.key; # 网站新私钥路径
# 启用 OCSP Stapling(加速证书状态验证,减少用户等待,增强隐私)
ssl_stapling on;
ssl_stapling_verify on;
resolver 8.8.8.8 114.114.114.114 223.5.5.5 119.29.29.29 180.76.76.76 valid=300s; # DNS 解析器(用于查询 OCSP 服务器)
resolver_timeout 5s; # 解析超时时间

# 启用 ssl session 缓存
ssl_session_cache shared:SSL:1m;
# 缓存SSL握手产生的参数和加密密钥的时长
ssl_session_timeout 10m;
# 使用的加密套件的类型
ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384;
# 表示使用的TLS协议的类型
ssl_protocols TLSv1.2 TLSv1.3;
# 加密套件有限选择服务器的加密套件
ssl_prefer_server_ciphers on;

ssl_ecdh_curve secp384r1; # 指定强椭圆曲线(增强ECDHE安全性)

# HSTS(强制浏览器使用 HTTPS,防止降级攻击)
add_header Strict-Transport-Security "max-age=5184000; includeSubDomains; preload" always;
# 防 MIME 类型嗅探(防止浏览器将文本文件解析为可执行脚本)
add_header X-Content-Type-Options "nosniff" always;
# 防点击劫持(禁止 iframe 嵌套,保护页面内容)
add_header X-Frame-Options "SAMEORIGIN" always; # 仅允许同域 iframe 嵌套(可选:DENY 完全禁止)
# 防 XSS 攻击(启用浏览器内置 XSS 过滤器)
add_header X-XSS-Protection "1; mode=block" always; # 检测到 XSS 时阻止页面加载
# 内容安全策略 CSP(限制资源加载来源,核心防御 XSS 和注入攻击,按需调整)
# add_header Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline' https://trusted-cdn.com; style-src 'self' 'unsafe-inline' https://trusted-cdn.com; img-src 'self' data: https://trusted-cdn.com; connect-src 'self'; frame-src 'self'; object-src 'none'" always;


# 网站加密使用 不加密不要使用
auth_basic "Input name password"; # 验证时的提示信息
auth_basic_user_file /usr/password/password; # 认证文件


# root /usr/localBlog/Blog; #网站根目录
# include location.conf; #调用其他规则,也可去除

# 正则表达式精准匹配 .htpasswd 文件 .git 目录及其中所有内容
location ~ /\.htpasswd$|/\.git($|/) { return 301 https://github.com/microsoft/SmartHotel360-Website; }

location / {
root /usr/localBlog/Blog; #网站根目录
index index.html index.htm; #设置默认页
try_files $uri $uri/ /index.html;
server_tokens off; # 隐藏版本信息
}
}

your_domain_name 替换成你的域名

/usr/Nginx/fullchain.crt 替换成你的证书路径和你的证书

/usr/Nginx/private.key 替换成你的证书密钥路径和你的证书密钥

/usr/localBlog/Blog 替换成你的网页路径

/usr/password/password 替换成你的加密文件

Nginx 反向代理配置

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
server  {
listen 80;
listen [::]:80;
server_name your_domain_name; #绑定域名


location / {
client_max_body_size 200M;

proxy_pass http://localhostgitea:2000; #由外部端口转到本地端口,就不用打开3000端口
proxy_set_header Connection $http_connection;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;

}
}


server {
listen 443 ssl;
listen [::]:443 ssl;
server_name your_domain_name; #绑定域名

ssl on; # 开启 Nginx SSL 模块
ssl_certificate /usr/local/Certificate/sslsky/Nginx/fullchain.crt; # 网站新证书路径
ssl_certificate_key /usr/local/Certificate/sslsky/Nginx/private.key; # 网站新私钥路径

# 启用 ssl session 缓存
ssl_session_cache shared:SSL:1m;
# 缓存SSL握手产生的参数和加密密钥的时长
ssl_session_timeout 10m;
# 使用的加密套件的类型
ssl_ciphers 'HIGH:!aNULL:!MD5:!kEDH';
# 表示使用的TLS协议的类型
ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3;
# 加密套件有限选择服务器的加密套件
ssl_prefer_server_ciphers on;

location / {
client_max_body_size 200M;

proxy_pass http://localhostgitea:2000;
proxy_set_header Connection $http_connection;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;

}
}
  1. localhostgitea 是本地一个IP地址也可以是另一个IP地址,进行;当为本地的时候,可以通过 /etc/hosts 设置,设置如下
1
127.0.0.10      localhostgitea
  1. 设置成别的 IP 地址,就可以替换为如下
1
proxy_pass http://192.168.2.3:2000;

反向代理的原理就是, 通过网站 80 或者 443端口,访问进来,然后通过该代理直接跳转到另一个IP和端口,以上面示例说明,这里通过网站进入后,会跳转到localhostgitea:2000的服务上面。就相当于搭建了一个桥梁,让两条路通过桥梁进行交流

Nginx http 跳转 https

1
2
3
4
5
6
7
8
9
10
11
12
13
server {
listen 80;
server_name www.example.com;

# 重定向所有HTTP流量到HTTPS http://www.example.com 重定向 https://www.example.com
return 301 https://$server_name$request_uri;

location / {
# 重定向所有HTTP流量到HTTPS http://www.example.com/ 重定向 https://www.example.com/
# 这里与上面不同,末尾加了一个斜杠
return 301 https://$server_name$request_uri;
}
}

alias 定义别名

Nginx 在处理请求时可以根据这个别名找到对应的文件系统路径。

配置了一个 server 块,其中包含一个 location 块,该块使用alias指令定义了一个别名,并将其指向/usr/share/nginx/html/images目录:

1
2
3
4
5
6
7
8
9
server {
listen 80;
server_name localhost;

location /images/ {
alias /usr/share/nginx/html/images/;
# 这里可以添加其他需要的配置,例如权限、日志记录等
}
}

当Nginx接收到以/images/开头的请求时,它会去/usr/share/nginx/html/images/目录下寻找相应的文件。注意alias指令的路径末尾有没有斜线都可以,但是location块中的路径必须以斜线结尾。

确保在使用alias时,请求的URI不会超出指定的文件系统路径。例如,如果URI是/images/../../etc/passwd,这可能会导致安全问题。在使用alias时,应当确保只将alias指令用于安全的上下文中。

对服务器进行保护

  1. 当网页以及 Nginx 都配置好的时候,修改文件夹中/etc/nginx/sites-available/default,第一步先拷贝文件,第二步修改配置;
1
cp default default.bak
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
62
63
64
65
66
server {
listen 10000 default_server;
listen [::]:10000 default_server;

# SSL configuration
#
listen 10001 ssl default_server;
listen [::]:10001 ssl default_server;
ssl_certificate /usr/fullchain.crt; # 网站新证书路径
ssl_certificate_key /usr/private.key; # 网站新私钥路径

# 启用 ssl session 缓存
ssl_session_cache shared:SSL:1m;
# 缓存SSL握手产生的参数和加密密钥的时长
ssl_session_timeout 10m;
# 使用的加密套件的类型
ssl_ciphers 'HIGH:!aNULL:!MD5:!kEDH';
# 表示使用的TLS协议的类型
ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3;
# 加密套件有限选择服务器的加密套件
ssl_prefer_server_ciphers on;

#
# Note: You should disable gzip for SSL traffic.
# See: https://bugs.debian.org/773332
#
# Read up on ssl_ciphers to ensure a secure configuration.
# See: https://bugs.debian.org/765782
#
# Self signed certs generated by the ssl-cert package
# Don't use them in a production server!
#
# include snippets/snakeoil.conf;

root /var/www/html;

# Add index.php to the list if you are using PHP
# index index.html index.htm index.nginx-debian.html;

server_name aaaaaa.com; #虚假网站

location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
return 444;
# try_files $uri $uri/ =404;
}

# pass PHP scripts to FastCGI server
#
#location ~ \.php$ {
# include snippets/fastcgi-php.conf;
#
# # With php-fpm (or other unix sockets):
# fastcgi_pass unix:/run/php/php7.4-fpm.sock;
# # With php-cgi (or other tcp sockets):
# fastcgi_pass 127.0.0.1:9000;
#}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
  1. 修改程序中端口号,修改不常用端口号,增加一点困难

生成自签名SSL证书(选配)

给空白主机生成自签名SSL证书

首先登陆 SSH,进入放置 SSL 目录.

  1. SSL证书目录

可以放入任何目录,但是要注意存放安全

1
cd /usr/local/nginx/conf/ssl
  1. 生成 (私钥)key

openssl genpkey或openssl genrsa选择一个按照步骤执行下去即可:

如下命令只能生成RSA类型证书,可以选择如下两个代码中其中一条执行即可

  1. 生成带有DES3加密的私钥:(选择第1条,就不要选择第2条)
1
openssl genrsa -des3 -out none.key 2048 

生成一个2048位的带有DES3加密的私钥,并将其保存在none.key文件中。运行命令后,它会提示你输入密码,务必记住这个密码,因为在后续的操作中你会需要它。因为以后要给 nginx 使用,每次 reload nginx 配置时候都要你验证这个 PAM 密码的。

  1. 生成不带加密的私钥:(选择第2条,就不要选择第1条)
1
openssl genrsa -out none.key 2048

这个命令将生成一个2048位的不带加密的私钥,并将其保存在none.key文件中。

注意:这个命令只在交互式环境中工作,也就是你在终端中直接运行这个命令时。如果你在脚本中运行这个命令,它会失败,因为没有交互式输入。在脚本中,你可以使用openssl的命令行参数来指定密码,例如:

1
openssl genrsa -des3 -passout pass:mypassword -out none.key 2048

这个例子中,mypassword就是你要使用的密码。这样你就可以在脚本中使用这个命令而无需交互式输入。

genpkey命令是OpenSSL中用于生成密钥对的通用命令,支持多种类型的密钥生成,包括但不限于RSA、DSA、EC等。它提供了更大的灵活性,允许用户指定密钥的类型、大小和其他选项。例如,使用genpkey命令生成RSA私钥的语法如下:

1
openssl genpkey -algorithm RSA -out none.key -pkeyopt rsa_keygen_bits:2048

OpenSSL的genpkey和genrsa命令都可以用于生成密钥对,但它们在功能和用法上存在一些区别。

首先,‌genpkey‌命令是OpenSSL中用于生成密钥对的通用命令,支持多种类型的密钥生成,包括但不限于RSA、DSA、EC等。它提供了更大的灵活性,允许用户指定密钥的类型、大小和其他选项。例如,使用genpkey命令生成RSA私钥的语法如下:

1
openssl genpkey -algorithm RSA -out none.key -pkeyopt rsa_keygen_bits:2048

这条命令生成一个2048位的RSA私钥。

相比之下,‌genrsa‌命令是专门用于生成RSA私钥的命令。它的语法相对简单,主要用于快速生成RSA私钥。例如:

1
openssl genrsa -out none.key 2048

这条命令同样生成一个2048位的RSA私钥,但仅限于RSA密钥的生成。

主要区别‌在于:

  • 适用范围‌:genpkey适用于多种类型的密钥生成,而genrsa仅限于生成RSA私钥。
  • 灵活性‌:genpkey提供了更多的选项和灵活性,允许用户选择不同的密钥类型和参数,而genrsa的功能相对单一,仅用于生成RSA私钥。
  • 语法复杂性‌:虽然genpkey的语法相对复杂,但它能够处理更多种类的密钥,而genrsa虽然简单,但仅适用于RSA密钥的生成。

综上所述,选择使用genpkey还是genrsa取决于具体需求:如果需要生成非RSA类型的密钥或需要更多的灵活性,应使用genpkey;如果仅需生成RSA密钥且追求简单快捷,则genrsa是更好的选择‌。

更多命浏览器搜索

  1. 这里为了避免麻烦可以删除生成时候必须输入密码:
1
mv none.key xxx.key
1
openssl rsa -in xxx.key -out none.key
1
rm xxx.key
  1. 然后根据这个 key 文件生成证书请求文件:
1
openssl req -new -key none.key -out none.csr
  1. 命令生成时候要填信息 如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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]:XXXX 省
Locality Name (eg, city) []:XXXX 市
Organization Name (eg, company) [Internet Widgits Pty Ltd]:XXXX 公司名称
Organizational Unit Name (eg, section) []:IT 部门
Common Name (e.g. server FQDN or YOUR name) []:127.0.0.1 授权证书(ipv4:127.0.0.1 ipv6: ::1)网址
Email Address []: 联系邮箱(可不填写)

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []: 密码(可不填写)
An optional company name []:XXXX 公司名称
  1. 最后根据这 2 个文件(none.key none.csr)生成 crt 证书文件:
1
sudo openssl x509 -req -days 365 -in none.csr -signkey none.key -out none.crt

这里 365 天是证书有效期,有效期大家随意。最后使用到的文件是 key 和 crt 文件。最后将生成的文件的路径放到这两个参数后面,具体的配置,在 配置网站文件中,有说明;

1
2
ssl_certificate    /usr/none.crt; # 网站新证书路径
ssl_certificate_key /usr/none.key; # 网站新私钥路径

443端口访问 防止扫描

负载均衡(选配)

当有多服务器时候可以配置该项,也可以不配置、还要注意代理与非代理的区别,这些配置的前提条件每台服务器都有相同的配置文件。

方案一:DNS+2台服务器NGINX负载均衡

为什么要使用该方案:服务器A为轻量服务器,受到各种限制,所以将大量请求引导到B服务器处理。

服务器默认HTTP端口80;服务器默认HTTPS端口443,记得一定要开启相应的端口号;图中红色虚线就是Nginx负载均衡的配置,起到了一个类似DNS的作用,

  1. 到购买的域名服务商,给同一个域名添加不同IP;如上图,在 aa.com的服务商中,给www.aa.com域名添加服务器A、服务器B的IP地址, 这样DNS服务器就记录了两个IP值,访问网站时候就可以靠DNS进行分配具体访问那个服务器;
  2. 在服务器A中的/etc/nginx/conf.d/目录中创建nginx.conf添加如下内容(Nginx负载均衡配置):
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
# 配置nginx均衡负载功能

upstream http-aa-com{ #http访问的地址以及端口
server 192.168.2.3:80 weight=10; #weight=1 指向B服务器
server 127.0.0.1:81 weight=1; #本机服务器
}

upstream https-aa-com{ # https访问的地址以及端口
server 192.168.2.3:443 weight=10; #weight=1 指向B服务器
server 127.0.0.1:444 weight=1; #本机服务器
}

server {

listen 80;
listen [::]:80;
server_name www.aa.com;

location / {
proxy_pass http://http-aa-com;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
# proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}

server {

listen 443 ssl;
listen [::]:443 ssl;
server_name www.aa.com;

location / {
proxy_pass http://https-aa-com;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
# proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

}
  1. 需要修改A服务器www.aa.com域名的端口号,配置如下,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
server  {
listen 81;
listen [::]:81;
server_name www.aa.com; #绑定域名
... #该部分可以参考前面的 配置网站文件
}


server {
listen 444 ssl;
listen [::]:444 ssl;
server_name www.aa.com; #绑定域名
... #该部分可以参考前面的 配置网站文件
}

使用该配置需要修改的点:

www.aa.com:需要修改为你域名;

将图片中B服务器地址:替换为你的服务器地址;

方案二:2台服务器NGINX负载均衡

为什么要使用该方案:

服务器默认HTTP端口80;服务器默认HTTPS端口443,记得一定要开启相应的端口号;图中红色虚线就是Nginx负载均衡的配置,起到了一个类似DNS的作用

  1. 到购买的域名服务商,给 aa.com域名添加服务器A的IP;如上图,这样DNS服务器就记录了A服务器的IP值(与方案A不同,这里只需要记录一台服务器地址);
  2. 在服务器A中的/etc/nginx/conf.d/目录中创建nginx.conf添加如下内容(Nginx负载均衡配置):
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
# 配置nginx均衡负载功能

upstream http-aa-com{ #http访问的地址以及端口
server 192.168.2.3:80 weight=10; #weight=1 指向B服务器
server 127.0.0.1:81 weight=1; #本机服务器
}

upstream https-aa-com{ # https访问的地址以及端口
server 192.168.2.3:443 weight=10; #weight=1 指向B服务器
server 127.0.0.1:444 weight=1; #本机服务器
}

server {

listen 80;
listen [::]:80;
server_name www.aa.com;

location / {
proxy_pass http://http-aa-com;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
# proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}

server {

listen 443 ssl;
listen [::]:443 ssl;
server_name www.aa.com;

location / {
proxy_pass http://https-aa-com;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
# proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

}
  1. 需要修改A服务器www.aa.com域名的端口号,配置如下,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
server  {
listen 81;
listen [::]:81;
server_name www.aa.com; #绑定域名
... #该部分可以参考前面的 配置网站文件
}


server {
listen 444 ssl;
listen [::]:444 ssl;
server_name www.aa.com; #绑定域名
... #该部分可以参考前面的 配置网站文件
}

使用该配置需要修改的点:

www.aa.com:需要修改为你域名;

将图片中B服务器地址:替换为你的服务器地址;

多台服务器肯定有更多的方案,根据需求配置;

Nginx负载均衡流量中转还是直连

中转就在负载均衡中开启如下语句,默认直连不开启

1
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

Nginx配置检查

  1. 检查格式是否错误
1
sudo nginx -t

运行Nginx

通过常用命令启动Nginx或者重新加载Nginx等操作;

Nginx对网站设置密码验证(选配)

很多时候我们需要对一些网站进行密码保护,不让别人访问,就需设置密码校验,

网站加密,激活nginx配置文件中如下两个参数配置
auth_basic “Input name password”; # 验证时的提示信息
auth_basic_user_file /usr/password/password; # 认证文件

创建密码

  1. 安装工具
1
sudo apt-get install apache2-utils
  1. 创建用户名和密码,该文件位置可以随意放置,但是建议一定要记得放在哪个位置,还有尽量都放在一起,容易找到管理,如下是我的设置,我放在/usr/password目录下,password是我创建的一个管理专门管理密码的目录
1
sudo htpasswd -bc password admin admin123
1
2
3
4
5
6
7
8
-c:创建一个加密文件;
-n:不更新加密文件,只将加密后的用户名密码显示在屏幕上;
-m:默认采用MD5算法对密码进行加密;
-d:采用CRYPT算法对密码进行加密;
-p:不对密码进行进行加密,即明文密码;
-s:采用SHA算法对密码进行加密;
-b:在命令行中一并输入用户名和密码而不是根据提示输入密码;
-D:删除指定的用户。

password: 存储该用户文件名称,可以修改成自己记得住的名称 ; admin 是用户名; 密码:admin123,默认采用MD5加密方式。

  1. 在原有密码文件中增加下一个用户
1
sudo htpasswd -b password.user admin1 admin1234

去掉-c选项,即可在第一个用户之后添加第二个用户,依此类推。

  1. 利用htpasswd命令删除用户名和密码
1
sudo htpasswd -D password.user admin1
  1. 利用htpasswd命令修改密码
1
sudo htpasswd -b password.user admin admin123456789

修改nginx配置文件

找到 nginx 配置文件,因为我们要对整个站点开启验证,所以在配置文件中的第一个server修改如下:

1
2
3
4
5
6
7
8
9
server {
.......
#新增下面两行
auth_basic "Input name password"; #这里是验证时的提示信息
auth_basic_user_file /usr/password;
location /{
......
}
}

重新加载、重启 nginx

1
2
sudo systemctl reload nginx
sudo systemctl restart nginx

查看Nginx占用端口

可以在开启防火墙的时候,把这些端口打开,防止端口关闭

1
netstat -luntp|grep nginx

防火墙

查看 ubuntu 防火墙是否激活

1
ufw status

如下图属于激活状态:

查看防火墙_240922

防火墙未激活 所有端口都是打开的,直接查看 安装 Nginx

防火墙激活需要配置的端口 80 端口是 http 协议; 443 端口是 https 协议,根据自己需求配置:

1
2
ufw allow 80
ufw allow 443

如下图:

防火墙开启IPV4_IPV6_80_443port_240922

开启防火墙

1
sudo ufw enable

关闭防火墙

1
sudo ufw disable

如果要删除防火墙中规则

输入如下命令,查看所有规则编号id

1
sudo ufw status numbered

如下图

防火墙规则编号_240922

删除对应id的规则

1
sudo ufw delete 2 

如下图

防火墙删除规则后_240922

禁止外部某个端口比如80

1
sudo ufw delete allow 80

重启防火墙

1
sudo ufw reload

如果修改了Nginx中默认监听端口

那么也要在ufw中开启该端口,不然无法访问网页;
有关防火墙的其他操作,可以在本站搜索中输入ufw可以看到。