如何使用Nginx在Ubuntu 16.04上安装Nextcloud 13
早在2016年6月首次公开发布Nextcloud之后不久,我们就发布了有关如何在具有Apache Web服务器的Ubuntu 16.04上安装Nextcloud的教程。 但是许多人希望使用Nginx而不是Apache作为Web服务器。 因此,在本教程中,我们将看到如何使用Nginx在Ubuntu 16.04上安装Nextcloud 13。
注意:如果您使用的是Ubuntu 18.04,请按照以下教程操作:在具有Nginx的Ubuntu 18.04上安装Nextcloud(LEMP堆栈)
先决条件
首先,如果尚未安装LEMP,则应查看适用于Ubuntu 16.04的LEMP安装教程。 安装完成后,请回到这里继续阅读。
步骤1:下载NextCloud 13
将NextCloud服务器zip存档下载到您的服务器上。 在撰写本文时,最新的稳定版本是13.0.4。 您可能需要更改版本号。 转到https://nextcloud.com/install并单击下载按钮以检出最新版本。
wget https://download.nextcloud.com/server/releases/nextcloud-13.0.4.zip
提取它。
sudo apt install unzip unzip nextcloud-13.0.4.zip
新目录名为 nextcloud
将在当前工作目录中创建。 将新目录及其所有内容移至Nginx Web服务器的文档根目录
sudo mv nextcloud /usr/share/nginx/
然后,您还需要将Nginx用户(www-data)设置为NextCloud目录的所有者。
sudo chown www-data:www-data /usr/share/nginx/nextcloud/ -R
步骤2:在MariaDB中创建数据库和用户
使用以下命令登录MariaDB数据库服务器:
sudo mysql -u root
然后为Nextcloud创建数据库。 本教程将数据库命名为nextcloud。 您可以使用任何喜欢的名称。
create database nextcloud;
创建数据库用户。 同样,您可以为该用户使用您的首选名称。 用您的首选密码替换您的密码。
create user [email protected] identified by 'your-password';
授予该用户所有的权限 nextcloud
数据库。
grant all privileges on nextcloud.* to [email protected] identified by 'your-password';
刷新特权并退出。
flush privileges; exit;
步骤3:在MariaDB中启用二进制日志记录
编辑mysqld配置文件。
sudo nano /etc/mysql/mariadb.conf.d/50-server.cnf
在下面添加以下三行 [mysqld]
部分。
log-bin = /var/log/mysql/mariadb-bin log-bin-index = /var/log/mysql/mariadb-bin.index binlog_format = mixed
二进制日志的格式必须为 mixed
。 保存并关闭文件。 然后重新启动MariaDB服务。
sudo systemctl restart mysql
现在,在MariaDB中启用了二进制日志。
步骤4:为Nextcloud创建Nginx配置文件
创建一个 nextcloud.conf
归档在 /etc/nginx/conf.d/
目录。
sudo nano /etc/nginx/conf.d/nextcloud.conf
将以下文本放入文件中。 将红色文本替换为您的实际数据。 不要忘记为域名设置A记录。
server { listen 80; server_name nextcloud.your-domain.com; # Add headers to serve security related headers add_header X-Content-Type-Options nosniff; add_header X-XSS-Protection "1; mode=block"; add_header X-Robots-Tag none; add_header X-Download-Options noopen; add_header X-Permitted-Cross-Domain-Policies none; # Path to the root of your installation root /usr/share/nginx/nextcloud/; location = /robots.txt { allow all; log_not_found off; access_log off; } # The following 2 rules are only needed for the user_webfinger app. # Uncomment it if you're planning to use this app. #rewrite ^/.well-known/host-meta /public.php?service=host-meta last; #rewrite ^/.well-known/host-meta.json /public.php?service=host-meta-json # last; location = /.well-known/carddav { return 301 $scheme://$host/remote.php/dav; } location = /.well-known/caldav { return 301 $scheme://$host/remote.php/dav; } location ~ /.well-known/acme-challenge { allow all; } # set max upload size client_max_body_size 512M; fastcgi_buffers 64 4K; # Disable gzip to avoid the removal of the ETag header gzip off; # Uncomment if your server is build with the ngx_pagespeed module # This module is currently not supported. #pagespeed off; error_page 403 /core/templates/403.php; error_page 404 /core/templates/404.php; location / { rewrite ^ /index.php$uri; } location ~ ^/(?:build|tests|config|lib|3rdparty|templates|data)/ { deny all; } location ~ ^/(?:.|autotest|occ|issue|indie|db_|console) { deny all; } location ~ ^/(?:index|remote|public|cron|core/ajax/update|status|ocs/v[12]|updater/.+|ocs-provider/.+|core/templates/40[34]).php(?:$|/) { include fastcgi_params; fastcgi_split_path_info ^(.+.php)(/.*)$; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_path_info; #Avoid sending the security headers twice fastcgi_param modHeadersAvailable true; fastcgi_param front_controller_active true; fastcgi_pass unix:/run/php/php7.0-fpm.sock; fastcgi_intercept_errors on; fastcgi_request_buffering off; } location ~ ^/(?:updater|ocs-provider)(?:$|/) { try_files $uri/ =404; index index.php; } # Adding the cache control header for js and css files # Make sure it is BELOW the PHP block location ~* .(?:css|js)$ { try_files $uri /index.php$uri$is_args$args; add_header Cache-Control "public, max-age=7200"; # Add headers to serve security related headers (It is intended to # have those duplicated to the ones above) add_header X-Content-Type-Options nosniff; add_header X-XSS-Protection "1; mode=block"; add_header X-Robots-Tag none; add_header X-Download-Options noopen; add_header X-Permitted-Cross-Domain-Policies none; # Optional: Don't log access to assets access_log off; } location ~* .(?:svg|gif|png|html|ttf|woff|ico|jpg|jpeg)$ { try_files $uri /index.php$uri$is_args$args; # Optional: Don't log access to other assets access_log off; } }
保存并关闭文件。 测试Nginx配置,然后重新加载Nginx以使更改生效。
sudo nginx -t sudo systemctl reload nginx
步骤5:安装并启用PHP模块
运行以下命令以安装所需的PHP模块。
sudo apt install php7.0-common php7.0-gd php7.0-json php7.0-curl php7.0-zip php7.0-xml php7.0-mbstring
步骤6:启用HTTPS
现在,您可以通过输入Nextcloud安装的域名来访问浏览器中的Nextcloud Web安装向导。
nextcloud.your-domain.com
但是在输入任何敏感信息之前,我们应该在Nextcloud上启用安全的HTTPS连接。 我们可以从Let’s Encrypt获得免费的TLS证书。 运行以下命令,从官方PPA在Ubuntu 16.04上安装certbot(Let’s Encrypt)客户端。 的 software-properties-common
需要软件包才能在Ubuntu上添加PPA。 有时,默认情况下未在Ubuntu上安装它。 Python-certbot-nginx
是Nginx插件。
sudo apt install software-properties-common sudo add-apt-repository ppa:certbot/certbot sudo apt update sudo apt install certbot python-certbot-nginx
您可能还需要升级一些库。
sudo apt upgrade
接下来,运行以下命令以使用Nginx插件获取免费的TLS证书。
sudo certbot --nginx --agree-tos --redirect --staple-ocsp --email your-email-address -d nextcloud.your-domain.com
在几秒钟之内,您将看到如下消息,这表示已成功获取并安装了TLS证书。
在Web浏览器中完成安装
现在,您可以使用HTTPS连接访问Nextcloud Web安装向导。 要完成安装,您需要创建一个管理员帐户,输入Nextcloud数据文件夹的路径,输入先前创建的数据库详细信息。
数据文件夹是存储用户文件的位置。 为了安全起见,最好将数据目录放置在Nextcloud Web根目录之外。 因此,与其将用户文件存储在 /usr/share/nginx/nextcloud/data/
,我们可以将其更改为 /usr/share/nginx/nextcloud-data
,可以使用以下命令创建:
sudo mkdir /usr/share/nginx/nextcloud-data
然后确保Nginx用户(www-data
)具有对数据目录的写入权限。
sudo chown www-data:www-data /usr/share/nginx/nextcloud-data -R
完成后,您将看到Nextcloud的Web界面。 恭喜! 您可以开始将其用作私有云存储。
而已! 我希望本教程可以帮助您使用Nginx配置在Ubuntu 16.04上安装Nextcloud。 与往常一样,如果您发现此帖子有用,请订阅我们的免费新闻通讯。 您也可以在Google +,Twitter或喜欢我们的Facebook页面上关注我们。