如何在 Ubuntu 22.04 LTS 上使用 Apache 安装 Nextcloud 和 Let's Encrypt 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 Web 根目录:
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
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 浏览器并使用 URL http://next.example.com 访问 Nextcloud Web UI。您应该看到以下屏幕:
定义您的管理员用户名、密码、数据库,然后单击安装按钮。安装 Nextcloud 后,您应该看到以下屏幕:
单击安装推荐应用安装所有必需的应用程序。您应该看到以下屏幕:
单击所有文件。您应该在以下屏幕上看到 Nextcloud 存储管理器:
点击您的照片。您应该在以下屏幕上看到 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
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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 的 Nextcloud 和 Let's Encrypt SSL。您现在可以上传文件、文档和照片,并将它们与您的计算机、笔记本电脑和移动设备同步。