本教程适用于以下操作系统版本
- Ubuntu 22.04 (Jammy Jellyfish)
- Ubuntu 20.04 (Focal Fossa)
- Ubuntu 18.04 (Bionic Beaver)
本页内容
- 前提条件
- 安装 Apache、PHP 和 MariaDB
- 为 Nextcloud 创建数据库
- 下载 Nextcloud
- 为 Nextcloud 创建 Apache 虚拟主机
- 访问 Nextcloud Web 界面
- 为 Nextcloud 启用 SSL
- 结语
Nextcloud 是一款免费开源的文件共享与同步解决方案,可帮助你将个人文档、文件、照片及其他内容集中存储在一个位置。它与 Dropbox、Google Drive、iCloud 等其他云存储方案非常相似。如果你担心隐私问题,应该改用 Nextcloud,因为你可以将 Nextcloud 安装在自己的服务器上。你可以把文件和其他文档上传到 Nextcloud 服务器,再将它们全部同步到台式机、笔记本或智能手机上。
在本教程中,我们将向你展示如何在 Ubuntu 22.04 服务器上安装 Nextcloud。
前提条件
- 一台运行 Ubuntu 22.04 的服务器。
- 一个指向该服务器 IP 的有效域名。
- 服务器上已配置 root 密码。
安装 Apache、PHP 和 MariaDB
开始之前,服务器上必须已安装 LAMP 环境。如果尚未安装,可使用以下命令安装:
apt install apache2 mariadb-server php php-cli php-fpm php-json php-intl php-imagick php-pdo php-mysql php-zip php-gd php-mbstring php-curl php-xml php-pear php-bcmath apache2 libapache2-mod-php -y
安装完所有软件包后,编辑 PHP 配置文件并修改一些默认设置:
nano /etc/php/8.1/apache2/php.ini
修改以下几行:
date.timezone = UTC memory_limit = 512M upload_max_filesize = 500M post_max_size = 500M max_execution_time = 300
保存并关闭文件,然后重启 Apache 服务以使更改生效:
systemctl restart apache2
为 Nextcloud 创建数据库
Nextcloud 使用 MariaDB 数据库作为数据库后端,因此你需要在 MariaDB 中创建一个数据库和用户。
首先,使用以下命令连接到 MariaDB shell:
mysql
连接到 MariaDB 后,使用以下命令创建数据库和用户:
CREATE DATABASE nextcloud;
CREATE USER 'nextcloud'@'localhost' identified by 'password';
接着,使用以下命令为 Nextcloud 数据库授予所有权限:
GRANT ALL PRIVILEGES ON nextcloud.* TO 'nextcloud'@'localhost';
接着,使用以下命令刷新权限并退出 MariaDB:
FLUSH PRIVILEGES;
QUIT;
下载 Nextcloud
在撰写本文时,Nextcloud 的最新版本为 24.0.1。你可以使用以下命令下载:
wget https://download.nextcloud.com/server/releases/nextcloud-24.0.1.zip
下载完成后,使用以下命令解压已下载的文件:
unzip nextcloud-24.0.1.zip
接着,使用以下命令将解压后的目录移动到 Apache 网站根目录:
mv nextcloud /var/www/html/
接着,使用以下命令更改 Nextcloud 目录的所有权和权限:
chown -R www-data:www-data /var/www/html/nextcloud
chmod -R 775 /var/www/html/nextcloud
为 Nextcloud 创建 Apache 虚拟主机
接着,你需要为 Nextcloud 创建一个 Apache 虚拟主机配置文件。可使用以下命令创建:
nano /etc/apache2/sites-available/next.conf
添加以下几行:
<VirtualHost *:80>
ServerAdmin
[email protected]
DocumentRoot /var/www/html/nextcloud
ServerName next.example.com
ErrorLog /var/log/apache2/nextcloud-error.log
CustomLog /var/log/apache2/nextcloud-access.log combined
<Directory /var/www/html/nextcloud>
Options +FollowSymlinks
AllowOverride All
Require all granted
SetEnv HOME /var/www/html/nextcloud
SetEnv HTTP_HOME /var/www/html/nextcloud
<IfModule mod_dav.c>
Dav off
</IfModule>
</Directory>
</VirtualHost>
保存并关闭文件,然后使用以下命令激活 Apache 虚拟主机及其他所需的 Apache 模块:
a2ensite next
a2enmod rewrite dir mime env headers
接着,重启 Apache 服务以使更改生效:
systemctl restart apache2
你也可以使用以下命令查看 Apache 的状态:
systemctl status apache2
你将看到如下 Apache 状态输出:
? apache2.service - The Apache HTTP Server
Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
Active: active (running) since Fri 2022-06-17 15:04:27 UTC; 4s ago
Docs: https://httpd.apache.org/docs/2.4/
Process: 16746 ExecStart=/usr/sbin/apachectl start (code=exited, status=0/SUCCESS)
Main PID: 16750 (apache2)
Tasks: 6 (limit: 2292)
Memory: 14.7M
CPU: 98ms
CGroup: /system.slice/apache2.service
??16750 /usr/sbin/apache2 -k start
??16751 /usr/sbin/apache2 -k start
??16752 /usr/sbin/apache2 -k start
??16753 /usr/sbin/apache2 -k start
??16754 /usr/sbin/apache2 -k start
??16755 /usr/sbin/apache2 -k start
Jun 17 15:04:27 ubuntu2204 systemd[1]: Starting The Apache HTTP Server...
访问 Nextcloud Web 界面
现在,打开 Web 浏览器,通过 URL http://next.example.com 访问 Nextcloud Web 界面。你将看到如下画面:


设置管理员用户名、密码和数据库,然后点击 Install 按钮。Nextcloud 安装完成后,你将看到如下画面:

点击 Install recommended apps 以安装所有必需的应用程序。你将看到如下画面:

点击 All files。你将在如下画面中看到 Nextcloud 存储管理器:

点击 Your photos。你将在如下画面中看到 Nextcloud 照片管理器:

为 Nextcloud 启用 SSL
此外,建议为 Nextcloud 域名启用 SSL 以实现安全通信。首先,使用以下命令安装 Certbot 软件包:
apt-get install python3-certbot-apache -y
安装完成后,运行以下命令,使用 Let's Encrypt SSL 保护你的网站:
certbot --apache -d next.example.com
系统将提示你提供邮箱并接受服务条款,如下所示:
Saving debug log to /var/log/letsencrypt/letsencrypt.log Plugins selected: Authenticator standalone, Installer None Enter email address (used for urgent renewal and security notices) (Enter 'c' to cancel): [email protected] - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Please read the Terms of Service at https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must agree in order to register with the ACME server at https://acme-v02.api.letsencrypt.org/directory - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - (A)gree/(C)ancel: A - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Would you be willing to share your email address with the Electronic Frontier Foundation, a founding partner of the Let's Encrypt project and the non-profit organization that develops Certbot? We'd like to send you email about our work encrypting the web, EFF news, campaigns, and ways to support digital freedom. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - (Y)es/(N)o: Y Plugins selected: Authenticator apache, Installer apache Obtaining a new certificate Performing the following challenges: http-01 challenge for next.example.com Enabled Apache rewrite module Waiting for verification... Cleaning up challenges Created an SSL vhost at /etc/apache2/sites-available/next-le-ssl.conf Enabled Apache socache_shmcb module Enabled Apache ssl module Deploying Certificate to VirtualHost /etc/apache2/sites-available/next-le-ssl.conf Enabling available site: /etc/apache2/sites-available/next-le-ssl.conf
接着,选择是否将 HTTP 流量重定向到 HTTPS,如下所示:
Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 1: No redirect - Make no further changes to the webserver configuration. 2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for new sites, or if you're confident your site works on HTTPS. You can undo this change by editing your web server's configuration. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 2
输入 2 并按 Enter,为你的网站安装 Let's Encrypt SSL:
Enabled Apache rewrite module Redirecting vhost in /etc/apache2/sites-enabled/next.conf to ssl vhost in /etc/apache2/sites-available/next-le-ssl.conf - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Congratulations! You have successfully enabled https://next.example.com You should test your configuration at: https://www.ssllabs.com/ssltest/analyze.html?d=next.example.com - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - IMPORTANT NOTES: - Congratulations! Your certificate and chain have been saved at: /etc/letsencrypt/live/next.example.com/fullchain.pem Your key file has been saved at: /etc/letsencrypt/live/next.example.com/privkey.pem Your cert will expire on 2022-09-21. To obtain a new or tweaked version of this certificate in the future, simply run certbot again with the "certonly" option. To non-interactively renew *all* of your certificates, run "certbot renew" - If you like Certbot, please consider supporting our work by: Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate Donating to EFF: https://eff.org/donate-le
结语
恭喜!你已在 Ubuntu 22.04 上成功安装了带有 Apache 和 Let's Encrypt SSL 的 Nextcloud。现在你可以上传文件、文档和照片,并将它们同步到你的电脑、笔记本和移动设备上。