係Ubuntu 18.04 上 安裝 NextCloud With LEMP Stack!(下)——安裝NextCloud

Posted on Tue, Jul 10, 2018 科技趣聞 Unix/Linux

前文:

序言

緊接前文,安裝好LEMP之後,可以開始安裝NextCloud了。

如果需要最新版本請前往 NextCloud.com

下載NextCloud

首先下載NextCloud:wget https://download.nextcloud.com/server/releases/nextcloud-13.0.4.tar.bz2

解壓縮:tar -xvjf nextcloud-13.0.4.tar.bz2 /var/www/

將整個folder賦予俾nginx user:chown www-data:www-data /var/www/nextcloud/ -R

建立database予以NextCloud

鍵入command登入mariadb:mariadb

創建新database俾nextcloud用:create database nextcloud;

創建新user俾nextcloud用:create user nextclouduser@localhost identified by 'password';

你亦可以生成隨機字串作爲密碼(可選):cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1

給予該user完整嘅database全權限:grant all privileges on nextcloud.* to nextclouduser@localhost identified by 'your-password';

更新database:`flush privileges;

離開:exit;

建立nginx 設定文件

建立新嘅nginx 設定文件:nano /etc/nginx/conf.d/nextcloud.conf

內容物(記得修改 server_name ):

server {
listen 80;
server_name <nextcloud.domain.com>; # 記得修改成你嘅domain

    # Add headers to serve security related headers
add_header X-Content-Type-Options nosniff;
add_header X-Frame-Options "SAMEORIGIN";
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.2-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-Frame-Options "SAMEORIGIN";
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 -t

expect output:

nginx: the configuration file /etc/nginx/nginx.conf syntaxis ok
nginx: configuration file /etc/nginx/nginx.conf testis successful

如果冇問題可以重開:systemctl reload nginx

安裝PHP modules

安裝NextCloud所需嘅php modules:apt install php-imagick php7.2-common php7.2-gd php7.2-json php7.2-curl php7.2-zip php7.2-xml php7.2-mbstring php7.2-bz2 php7.2-intl

於是應該可以打開browser瀏覽就應該會行個installer,填返相應嘅資料就ok。

啓用HTTPS

打開443 port:iptables -I INPUT -p tcp --dport 443 -j ACCEPT

安裝certbot嚟自動註冊證書:apt install certbot python3-certbot-nginx

使用certbot自動註冊:certbot --nginx --agree-tos --redirect --hsts --staple-ocsp --email <your email> -d <your nextcloud domain>

可以修改設定文件將所有HTTP redirect到 HTTPS:nano /etc/nginx/conf.d/nextcloud.conf

設定文件:

server {
    listen 80;
    server_name nextcloud.domain.com;
    return 301 https://nextcloud.domain.com$request_uri;
}

server {
    listen 443 ssl; # managed by Certbot
    ### 只修改以下 ###
    ssl_certificate /etc/letsencrypt/live/nextcloud.domain.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/nextcloud.domain.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
    ### 只修改以上 ###
    ......
    ......
    remaining configurations
    ......
}

之後再係設定文件度加入一行HSTS header:add_header Strict-Transport-Security "max-age=31536000" always;

開啓HTTP2 protocol,修改(可選):listen 443 ssl;

成:listen 443 ssl http2;

測試config是否正常:listen 443 ssl;

冇問題嘅可以重開:systemctl reload nginx

附送SSL測試網一個:

參考:

>

nginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successful```

重開nginx:systemctl reload nginx

之後測試PHP,創建測試文件:nano /var/www/html/info.php

填入:<?php phpinfo(); ?>

儲存離開,並訪問該文件:<your domain / ip>/info.php

會出現類似嘅頁面:

冇問題之後記得順手移除就ok啦:rm /var/www/html/info.php

完。

參考: