Nextcloud安装与https启用

最近升级了家里的服务器,结合这阵在公司私有网盘知识库的构建落地,有了Nextcloud的具体实施经验,所以在自己本地的服务器小主机上也引入了该服务。本文也由此而来,为有类似需求的网友带来一些帮助。本文的重点会是https的启用,Nextcloud的安装与部署采用的是Docker模式,所以只要有相关环境,会相对简单。


本文默认已经安装了docker 与docker-compose环境,若未安装,则先安装docker 与docker compose


1、基础环境


2、 docker&docker-compose启用服务

docker-comopse.yml配置

以下配置是加入了完整启用https的配置,初始化启动时,应该是可以正常启动后使用80端口访问的,但443的https端口对应的内容是无法访问的,因为还需要再配置一下,在3、https启用部份进行具体说明

在使用下面的配置是,注意注释内容,将其进行自定义修改一下,nextcloud毕竟是选来作来私有网盘的,不私有化配置一下,安全性也不好。

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
name: nextcloud
services:
db:
image: mariadb:10.5
container_name: nextcloud_db
restart: unless-stopped
volumes:
- db-data:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: your_password # 添加你的密码
MYSQL_PASSWORD: your_password # 添加你的密码
MYSQL_DATABASE: nextcloud
MYSQL_USER: nextcloud
networks:
- nextcloud_network
healthcheck:
test: ["CMD", "mysqladmin", "ping", "-h", "localhost"]
interval: 10s
retries: 5
start_period: 30s
timeout: 5s

app:
image: nextcloud
container_name: nextcloud_app
restart: unless-stopped
ports:
- "80:80" # 保留 HTTP 用于跳转
- "443:443" # 新增 HTTPS 端口
volumes:
- ./nextcloud-data:/var/www/html #这里的数据部份最好挂载到本地,方便备份之类的操作
- ./ssl:/etc/apache2/ssl # 这里需要进行ssl操作的配置文件挂载。
#- ./custom_apache.conf:/etc/apache2/sites-enabled/custom_apache.conf # 配置挂载
- ./custom_apache.conf:/etc/apache2/sites-available/custom_apache.conf # 配置挂载
- ./apache2.conf:/etc/apache2/apache2.conf # 这个镜像使用的是apache2来进行管理
# - ./apache-ssl.conf:/etc/apache2/sites-available/000-default-ssl.conf
environment:
- APACHE_ARGUMENTS=-D SSL #这里的配置都比较重要,都要加上
- OVERWRITEPROTOCOL=https
- APACHE_SERVER_NAME=serverName # 部分镜像支持此变量(网页8)

depends_on:
db:
condition: service_healthy
networks:
- nextcloud_network
healthcheck:
test: ["CMD", "curl", "-f", "https://localhost"]
interval: 30s
timeout: 10s
retries: 3

networks:
nextcloud_network:

volumes:
db-data:
driver: local
driver_opts:
type: bind
device: ./mysql
o: bind
nextcloud-data:
driver: local
driver_opts:
type: bind
device: ./next_data
o: bind

3、https启用部份进行具体说明

nextcloud的部署还是比较简单,按上面的配置基本上可以很轻松的将nextcloud服务启动起来,接下来开始我们的重头戏,https的启用。这也是我写这篇博客的意义所在,因为虽然网上有很多nextcloud和部署的文档,还有https的启用文档,但至少在我的部署过程中,或多或少是无法全部启用成功的,也就是某个博客可能解决了我一小步的问题,但全局下来是没有完整的帮助我直接将https成功启用。所以我想了下,结合自身结历,整理自己完整趟车https启用的全过程,以期对后面的朋友有所帮助。


nextcloud对应的https的启用有几个细节要重点注意

证书配置

  1. ssl证书的配置,这个有多种方式,但要选择apache的ssl证书

    1. 可以在阿里云上创建ssl证书,并下载下来使用,我这里暂时不再缀述,后面有时间再将如何配置选择ssl证书的操作完善下来
    2. 自定义生成证书,这个网上有较多教程,我在公司环境中是这样配置的,这样的好处是可以自动续约,我在这里加上几个站点的现成记录,不再缀述。
      1. ssl证书申请

    正常来说,最后你将获得

    1. *_chain.crt
    2. *.key
    3. *_public.crt

    三个文件,也有把chain文件进行合并的,这里不再缀述。


    将这三个文件迁移至ssl文件夹中

    image-20250426093129723

config.php配置

正常启动后,./next_data路径下会挂载出相应的nextcloud目录文件

image-20250426093731700

我们进行config文件夹找到config.php,这是我们nextcloud的关键配置文件,可以进行可信域名等诸多配置。


可信域名配置

1
2
3
4
5
6
'trusted_domains' =>
array (
#0 => '192.168.3.110',
0 => 'your_https_domain',#这里配置你的https 证书绑定的域名,不然你就算成功配置了https,进行页面也会提醒你的访问域名不可信
1 => '192.168.3.110',
),

注意:

  1. 这里配置域名的时候不要加端口号,从我的实际使用经历来讲,这里加了端口号和https或https会致使反而无法正常使用。
  2. 注意逗号的配置,这里本质是个数组,所以你在元素之间的逗 号是不能省的,这里php的逗 号可以也在最后一个元素上也加上,不影响使用,但元素之间的不能少,一句话,可多不可少。

apache2.conf 配置

在docker-compose.yml中,我们已经将我们的apache2.conf 给挂载出来了,这里直接在docker-compose文件夹地址处找到该文件进行配置即可

就是添加一行ServerName在文件最后

1
ServerName your_https_domain # 添加你的https domain在这里就行。
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# This is the main Apache server configuration file.  It contains the
# configuration directives that give the server its instructions.
# See http://httpd.apache.org/docs/2.4/ for detailed information about
# the directives and /usr/share/doc/apache2/README.Debian about Debian specific
# hints.
#
#
# Summary of how the Apache 2 configuration works in Debian:
# The Apache 2 web server configuration in Debian is quite different to
# upstream's suggested way to configure the web server. This is because Debian's
# default Apache2 installation attempts to make adding and removing modules,
# virtual hosts, and extra configuration directives as flexible as possible, in
# order to make automating the changes and administering the server as easy as
# possible.

# It is split into several files forming the configuration hierarchy outlined
# below, all located in the /etc/apache2/ directory:
#
# /etc/apache2/
# |-- apache2.conf
# | `-- ports.conf
# |-- mods-enabled
# | |-- *.load
# | `-- *.conf
# |-- conf-enabled
# | `-- *.conf
# `-- sites-enabled
# `-- *.conf
#
#
# * apache2.conf is the main configuration file (this file). It puts the pieces
# together by including all remaining configuration files when starting up the
# web server.
#
# * ports.conf is always included from the main configuration file. It is
# supposed to determine listening ports for incoming connections which can be
# customized anytime.
#
# * Configuration files in the mods-enabled/, conf-enabled/ and sites-enabled/
# directories contain particular configuration snippets which manage modules,
# global configuration fragments, or virtual host configurations,
# respectively.
#
# They are activated by symlinking available configuration files from their
# respective *-available/ counterparts. These should be managed by using our
# helpers a2enmod/a2dismod, a2ensite/a2dissite and a2enconf/a2disconf. See
# their respective man pages for detailed information.
#
# * The binary is called apache2. Due to the use of environment variables, in
# the default configuration, apache2 needs to be started/stopped with
# /etc/init.d/apache2 or apache2ctl. Calling /usr/bin/apache2 directly will not
# work with the default configuration.


# Global configuration
#

#
# ServerRoot: The top of the directory tree under which the server's
# configuration, error, and log files are kept.
#
# NOTE! If you intend to place this on an NFS (or otherwise network)
# mounted filesystem then please read the Mutex documentation (available
# at <URL:http://httpd.apache.org/docs/2.4/mod/core.html#mutex>);
# you will save yourself a lot of trouble.
#
# Do NOT add a slash at the end of the directory path.
#
#ServerRoot "/etc/apache2"

#
# The accept serialization lock file MUST BE STORED ON A LOCAL DISK.
#
#Mutex file:${APACHE_LOCK_DIR} default

#
# The directory where shm and other runtime files will be stored.
#

DefaultRuntimeDir ${APACHE_RUN_DIR}

#
# PidFile: The file in which the server should record its process
# identification number when it starts.
# This needs to be set in /etc/apache2/envvars
#
PidFile ${APACHE_PID_FILE}

#
# Timeout: The number of seconds before receives and sends time out.
#
Timeout 300

#
# KeepAlive: Whether or not to allow persistent connections (more than
# one request per connection). Set to "Off" to deactivate.
#
KeepAlive On

#
# MaxKeepAliveRequests: The maximum number of requests to allow
# during a persistent connection. Set to 0 to allow an unlimited amount.
# We recommend you leave this number high, for maximum performance.
#
MaxKeepAliveRequests 100

#
# KeepAliveTimeout: Number of seconds to wait for the next request from the
# same client on the same connection.
#
KeepAliveTimeout 5


# These need to be set in /etc/apache2/envvars
User ${APACHE_RUN_USER}
Group ${APACHE_RUN_GROUP}

#
# HostnameLookups: Log the names of clients or just their IP addresses
# e.g., www.apache.org (on) or 204.62.129.132 (off).
# The default is off because it'd be overall better for the net if people
# had to knowingly turn this feature on, since enabling it means that
# each client request will result in AT LEAST one lookup request to the
# nameserver.
#
HostnameLookups Off

# ErrorLog: The location of the error log file.
# If you do not specify an ErrorLog directive within a <VirtualHost>
# container, error messages relating to that virtual host will be
# logged here. If you *do* define an error logfile for a <VirtualHost>
# container, that host's errors will be logged there and not here.
#
ErrorLog ${APACHE_LOG_DIR}/error.log

#
# LogLevel: Control the severity of messages logged to the error_log.
# Available values: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the log level for particular modules, e.g.
# "LogLevel info ssl:warn"
#
LogLevel warn

# Include module configuration:
IncludeOptional mods-enabled/*.load
IncludeOptional mods-enabled/*.conf

# Include list of ports to listen on
Include ports.conf


# Sets the default security model of the Apache2 HTTPD server. It does
# not allow access to the root filesystem outside of /usr/share and /var/www.
# The former is used by web applications packaged in Debian,
# the latter may be used for local directories served by the web server. If
# your system is serving content from a sub-directory in /srv you must allow
# access here, or in any related virtual host.
<Directory />
Options FollowSymLinks
AllowOverride None
Require all denied
</Directory>

<Directory /usr/share>
AllowOverride None
Require all granted
</Directory>

<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>

#<Directory /srv/>
# Options Indexes FollowSymLinks
# AllowOverride None
# Require all granted
#</Directory>




# AccessFileName: The name of the file to look for in each directory
# for additional configuration directives. See also the AllowOverride
# directive.
#
AccessFileName .htaccess

#
# The following lines prevent .htaccess and .htpasswd files from being
# viewed by Web clients.
#
<FilesMatch "^\.ht">
Require all denied
</FilesMatch>


#
# The following directives define some format nicknames for use with
# a CustomLog directive.
#
# These deviate from the Common Log Format definitions in that they use %O
# (the actual bytes sent including headers) instead of %b (the size of the
# requested file), because the latter makes it impossible to detect partial
# requests.
#
# Note that the use of %{X-Forwarded-For}i instead of %h is not recommended.
# Use mod_remoteip instead.
#
LogFormat "%v:%p %h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" vhost_combined
LogFormat "%h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %O" common
LogFormat "%{Referer}i -> %U" referer
LogFormat "%{User-agent}i" agent

# Include of directories ignores editors' and dpkg's backup files,
# see README.Debian for details.

# Include generic snippets of statements
IncludeOptional conf-enabled/*.conf

# Include the virtual host configurations:
IncludeOptional sites-enabled/*.conf

ServerName your_serverName #即https配置的域名

修改custom_apache.conf

这个是我们启用https的核心文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerName your_https_domain #注意这里要配置,不要加端口号

SSLEngine on
SSLCertificateFile /etc/apache2/ssl/*_public.crt
SSLCertificateKeyFile /etc/apache2/ssl/*.key
SSLCertificateChainFile /etc/apache2/ssl/*.crt

SSLProtocol TLSv1.2 TLSv1.3
SSLCipherSuite ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256
SSLHonorCipherOrder on
SSLCompression off

DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/ssl_error.log
CustomLog ${APACHE_LOG_DIR}/ssl_access.log combined

<Directory /var/www/html>
Options FollowSymLinks

注意:这里的ssl文件名要修改成你的具体文件名

image-20250426095243349


到此配置文件配置就完成了,你可以直接重启nextloud

1
2
docker compose down -v #先关闭
docker compose up -d #再启动

到这里你基础上就可以看到

image-20250426095456851


但接下来还有几个坑要去踩,这几个坑趟过去了,才能算成功

几个坑

坑一

docker外的443端口已经监听(就是如上图,443端口已经绑到这个容器上了,不是主机可能存在nginx 443 和80端口的意思,如果nginx启用容器也无法正常启动,端口会冲突),但容器内的443端口是没有正常监听的。

核心原因主要是 容器内ssl配置资源没有正常启用,即刚才的ssl on这个配置并没有成功加载


要使这个配置能正常加载要做好以下几点

  1. ./custom_apache.conf:/etc/apache2/sites-available/custom_apache.conf # 配置挂载

将配置文件挂载到/etc/apache2/sites-available/ 路径下,这样才能正常启用,而不是挂到enable路径下。

因为我们的docker-compose文件中已经将此路径挂载到了相应位置,但在启动时还是无法正常访问的话,这里就主要是补充坑的处理情况。这里就是容器内部去手动启用这个配置,进行如下操作。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
docker exec -it nextcloud_app bash
# 1. 启用 SSL 支持
a2enmod ssl

# 2. 禁用默认自签站点(可选,防止冲突)
a2dissite default-ssl

# 3. 启用您的站点(只需基名或基名加 .conf)
a2ensite custom_apache
# 或者
a2ensite custom_apache.conf

# 4. 验证配置无误
apachectl configtest

# 5. 重载 Apache(Debian/Ubuntu 推荐使用 service 命令)
service apache2 reload

要是执行到apachectl configtest 一直不报错,最后报的是 Syntax OK ,恭喜你没有遇到其它问题了,接下来直接重启

service apache2 reload

这是在容器内重启,会导致你从容器内被退出,但重启只要不报错,那这里的ssl on的功能就算加载成功了

坑二

出现坑二的问题的朋友,一般说明网络这块相对会有些薄弱,一般坑二要处理好了,才能验证是否有坑一。

坑二就是国内是不能在非备案的情况下,直接访问443与80的。所以正常的操作方式是启用其它端口,而因为我们在这里docker服务上绑定的还是443,但你要在你的路由器/防火墙上,进行外网的端口转发配置,我用的华为路由器。通过NAT的功能的配置成了9009 -> 443.

而在具体的访问的时候,则要通过https_domain:9009进行访问,而之前的配置是不用变的,这里只是配置一个9009


总结

至此,通过上述操作,有较大可能是能成功启用https功能,但不得不说的是每个人的基本功不一样,我上文中可能省略了一些我觉得常识性的内容,对一些朋友来说可能不清楚,还是会导致一些问题出现,可以通过私信我进行交流。nextcloud不启用https有一些插件是无法正常使用的,所以建议朋友们花些心思配置一下,祝好!