Web服务器的配置与安装ssl证书

本文最后更新于:2023年4月2日 晚上

Web服务器的配置与安装ssl证书

注意:以下教程在Ubuntu20.04和Ubuntu22.04系统中实施成功,其他系统仅供参考。

Nginx

1、Nginx编译安装

ps:安装能够支持IPv6的Nginx服务器

1
2
3
4
5
6
7
8
9
10
11
sudo update
sudo apt install build-essential libpcre3 libpcre3-dev zlib1g zlib1g-dev libssl-dev libgd-dev libxml2 libxml2-dev uuid-dev -y
mkdir -p /opt/data/source
cd /opt/data/source
wget http://tengine.taobao.org/download/tengine-2.3.2.tar.gz
sudo tar -zxvf tengine-2.3.2.tar.gz
cd tengine-2.3.2/
./configure --prefix=/usr/local/software/nginx/ --with-ipv6
make & make install
systemctl enable nginx
systemctl restart nginx

2、Nginx配置文件

nginx.conf全局配置文件

1
vim /etc/nginx/nginx.conf

几乎不用怎么设置可以直接使用,下面是我最多就是改个错误自动跳转页面的配置

http{}下添加如下配置信息

1
2
3
4
5
6
7
8
9
10
11
12
13
error_page 400 = https://error.kbai.cc/400/index.html;
error_page 403 = https://error.kbai.cc/403/index.html;
error_page 404 = https://error.kbai.cc/404/index.html;
error_page 500 = https://error.kbai.cc/500/index.html;
error_page 501 = https://error.kbai.cc/501/index.html;
error_page 502 = https://error.kbai.cc/502/index.html;
error_page 503 = https://error.kbai.cc/503/index.html;
error_page 504 = https://error.kbai.cc/504/index.html;
error_page 505 = https://error.kbai.cc/505/index.html;
error_page 506 = https://error.kbai.cc/506/index.html;
error_page 507 = https://error.kbai.cc/507/index.html;
error_page 509 = https://error.kbai.cc/509/index.html;
error_page 510 = https://error.kbai.cc/510/index.html;

如下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
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
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
worker_connections 768;
# multi_accept on;
}

http {

##
# Basic Settings
##

sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
client_max_body_size 1024m;
# server_tokens off;
fastcgi_intercept_errors on;

# server_names_hash_bucket_size 64;
# server_name_in_redirect off;

include /etc/nginx/mime.types;
default_type application/octet-stream;

##
# SSL Settings
##

ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;

##
# Logging Settings
##

access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;

##
# Gzip Settings
##

gzip on;

# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

##
# Virtual Host Configs
##
error_page 400 = https://error.kbai.cc/400/index.html;
error_page 403 = https://error.kbai.cc/403/index.html;
error_page 404 = https://error.kbai.cc/404/index.html;
error_page 500 = https://error.kbai.cc/500/index.html;
error_page 501 = https://error.kbai.cc/501/index.html;
error_page 502 = https://error.kbai.cc/502/index.html;
error_page 503 = https://error.kbai.cc/503/index.html;
error_page 504 = https://error.kbai.cc/504/index.html;
error_page 505 = https://error.kbai.cc/505/index.html;
error_page 506 = https://error.kbai.cc/506/index.html;
error_page 507 = https://error.kbai.cc/507/index.html;
error_page 509 = https://error.kbai.cc/509/index.html;
error_page 510 = https://error.kbai.cc/510/index.html;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}

#mail {
# # See sample authentication script at:
# # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
# # auth_http localhost/auth.php;
# # pop3_capabilities "TOP" "USER";
# # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
# server {
# listen localhost:110;
# protocol pop3;
# proxy on;
# }
#
# server {
# listen localhost:143;
# protocol imap;
# proxy on;
# }
#}

3、Nginx单个网站配置文件

直接在/etc/nginx/sites-enabled目录下创建各个网站独立的配置文件,一个网站一个.conf配置文件即可。

1
2
cd /etc/nginx/sites-enabled
vim [你要配置的那个网站的名字(配置文件名一定要全英文)].conf

根据网站需求挑选使用下面的单个网站的配置文件模版。

http配置

反向代理模式
1
2
3
4
5
6
7
8
9
10
11
12
13
14
server {
listen 80;
# listen [::]80; #支持IPv6的Nginx版本才能使用本配置
proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
server_name [你的域名];
location / {
proxy_pass [http://你要代理的URL/];
}
}
目录模式
1
2
3
4
5
6
7
8
9
server {
listen 80;
# listen [::]80; #支持IPv6的Nginx版本才能使用本配置
server_name [你的域名];
location / {
root /var/www/html/;
index index.html;
}
}
跳转模式-URL重定向
1
2
3
4
5
6
server{
listen 80;
# listen [::]80; #支持IPv6的Nginx版本才能使用本配置
server_name [你的域名];
rewrite ^/(.*)$ [要跳转的URL]$1 permanent;
}

https配置-SSL配置

SSL配置参考文档:免费生成SSL证书并自动续订和安装证书 - 开摆工作室博客 (kbai.cc)

反向代理模式
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
server{
listen 443;
# listen [::]443; #支持IPv6的Nginx版本才能使用本配置
proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
server_name [你的域名];
ssl on;
ssl_certificate [证书文件地址];
ssl_certificate_key [私钥文件地址];
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
ssl_prefer_server_ciphers on;
location / {
proxy_pass [http://你要代理的URL/];
}
}
server{
listen 80;
# listen [::]80; #支持IPv6的Nginx版本才能使用本配置
server_name [你的域名];
rewrite ^/(.*)$ https://[你的域名]/$1 permanent;
}
目录模式
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
server{
listen 443;
# listen [::]443; #支持IPv6的Nginx版本才能使用本配置
server_name [你的域名];
ssl on;
ssl_certificate [证书文件地址];
ssl_certificate_key [私钥文件地址];
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
ssl_prefer_server_ciphers on;
location / {
root /var/www/html;
index index.html;
}
}
server{
listen 80;
# listen [::]80; #支持IPv6的Nginx版本才能使用本配置
server_name [你的域名];
rewrite ^/(.*)$ https://[你的域名]/$1 permanent;
}
跳转模式-URL重定向
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
server{
listen 443;
# listen [::]443; #支持IPv6的Nginx版本才能使用本配置
server_name [你的域名];
ssl on;
ssl_certificate [证书文件地址];
ssl_certificate_key [私钥文件地址];
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
ssl_prefer_server_ciphers on;
rewrite ^/(.*)$ [要跳转的URL]$1 permanent;
}
server{
listen 80;
# listen [::]80; #支持IPv6的Nginx版本才能使用本配置
server_name [你的域名];
rewrite ^/(.*)$ [要跳转的URL]$1 permanent;
}

安装SSL证书

注意:以acme.sh生成的证书为例。

1
2
ssl_certificate /root/.acme.sh/[你的域名]_ecc/[你的域名].cer;
ssl_certificate_key /root/.acme.sh/[你的域名]_ecc/[你的域名].key;

取消http自动跳转https

直接把listen 80;所在的那一套server{}更换成http一模一的就好了。

如下示例

反向代理模式
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
server{
listen 443;
# listen [::]443; #支持IPv6的Nginx版本才能使用本配置
proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
server_name [你的域名];
ssl on;
ssl_certificate [证书文件地址];
ssl_certificate_key [私钥文件地址];
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
ssl_prefer_server_ciphers on;
location / {
proxy_pass [http://你要代理的URL/];
}
}
server {
listen 80;
# listen [::]80; #支持IPv6的Nginx版本才能使用本配置
server_name [你的域名];
location / {
proxy_pass [http://你要代理的URL/];
}
}

4、检查配置并重启服务

每改完一次配置文件记得重启服务

1
systemctl restart nginx

如果启动报错,使用以下命令来检查

1
nginx -t

Apache

1、安装Apache

1
2
3
4
sudo update
sudo apt install apache2
systemctl enable apache2
systemctl restart apache2

2、Apache配置文件

apache2.conf全局配置文件

1
vim /etc/apache2/apache2.conf

几乎不用怎么设置可以直接使用,下面是我最多就是改个错误自动跳转页面的配置

apache2.conf文件最下面下添加如下配置信息

1
2
3
4
5
ErrorDocument 404 https://error.kbai.cc/404/index.html
ErrorDocument 403 https://error.kbai.cc/403/index.html
ErrorDocument 500 https://error.kbai.cc/500/index.html
ErrorDocument 503 https://error.kbai.cc/503/index.html
ErrorDocument 504 https://error.kbai.cc/504/index.html

3、Apache单个网站配置文件

直接在/etc/apache2/sites-enabled目录下创建各个网站独立的配置文件,一个网站一个.conf配置文件即可。

1
2
cd /etc/apache2/
vim [你要配置的那个网站的名字(配置文件名一定要全英文)].conf

根据网站需求挑选使用下面的单个网站的配置文件模版。

http配置

1
2
3
4
5
6
7
8
9
10
<VirtualHost *:80>
ServerName [你的域名]
ServerAlias [你的根域名]
DocumentRoot "/var/www/html/"
<Directory "/var/www/html/">
AllowOverride all
Order allow,deny
Allow from all
</Directory>
</VirtualHost>

https配置-SSL配置

SSL配置参考文档:免费生成SSL证书并自动续订和安装证书 - 开摆工作室博客 (kbai.cc)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<VirtualHost *:443>
ServerName [你的域名]
ServerAlias [你的根域名]
DocumentRoot "/var/www/html/"
<Directory "/var/www/html/">
AllowOverride all
Order allow,deny
Allow from all
</Directory>
SSLEngine on
SSLCertificateFile [证书文件地址]
SSLCertificateKeyFile [私钥文件地址]
SSLCertificateChainFile [证书文件地址]
</VirtualHost>

http自动跳转https

单个网站配置只需要在单个网站配置最下面追加如下配置即可。

1
2
3
4
5
6
<VirtualHost *:80>
ServerName [你的域名]
ServerAlias [你的根域名]
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*) https://%{SERVER_NAME}:6$1 [L,R]
</VirtualHost>

全局配置的话

编辑全局配置文件

1
2
cd /etc/apache2/
vim 000-default.conf

在文件</VirtualHost>上面添加如下配置

1
2
RewriteCond   %{HTTPS} !=on
RewriteRule ^(.*) https://%{SERVER_NAME}:6$1 [L,R]

安装SSL证书

注意:以acme.sh生成的证书为例。

1
2
3
SSLCertificateFile /root/.acme.sh/[你的域名]_ecc/[你的域名].cer
SSLCertificateKeyFile /root/.acme.sh/[你的域名]_ecc/[你的域名].key
SSLCertificateChainFile /root/.acme.sh/[你的域名]_ecc/fullchain.cer

4、检查配置并重启服务

每改完一次配置文件记得重启服务

1
systemctl restart apache2

如果启动报错,使用以下命令来检查

1
apache2 -t

📣特别声明

此文章全部都是依靠自己的学习理解来写的原创文章,难免可能会出现有错误的地方,

如果大家以后阅读的时候发现有问题,那就麻烦到下方评论区来进行错误指出,使错误尽快做出改正,

在此,感谢大家的阅读与支持!🤝💦🎉

🍻支持一下

觉得我写的好的话可以支持一下我哦~持续关注我,会更新其他好玩且实用的项目。

👋如果我能帮助到你,那就请我喝杯🧋呗~👇👇👇


本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!