小白搭建nextcloud

環境說明

這次搭建 nextcloud 使用的是 centos7.6(1810) 服務器,使用最小化安裝

使用的 LNMP 環境搭建

本次操作全程使用的root權限,普通用戶注意使用sudo

安裝 Nginx

爲了大家都能安裝成功,同時避免麻煩,所以使用 yum 安裝。

配置 epel 源

yum install -y epel-release

安裝

yum install -y nginx

安裝PHP

添加php的源

rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm

安裝PHP及必要擴展

yum install -y php70w-devel php70w-pear php70w-pecl php70w-gd php70w-opcache php70w-cli php70w-pdo php70w-process php70w-pecl-apcu php70w-mcrypt php70w-mysql php70w-fpm php70w-pecl-memcached php70w-common php70w-xml php70w-mbstring php70w-pecl-igbinary php70w-json php70w-pecl-apcu-devel  php70w-intl

配置php-fpm

php-fpm默認的使用用戶是apache

vim /etc/php-fpm.d/www.conf 
user = nginx                                   //將用戶和組都改爲nginx
group = nginx
listen = 127.0.0.1:9000
env[HOSTNAME] = $HOSTNAME //將以下幾行,去掉註釋
env[PATH] = /usr/local/bin:/usr/bin:/bin
env[TMP] = /tmp
env[TMPDIR] = /tmp
env[TEMP] = /tmp

爲php創建目錄

mkdir -p /var/lib/php/session
chown -R nginx:nginx /var/lib/php/session/

安裝數據庫

其實可以不用安裝數據庫,但是爲了顯得我會使用數據庫所以還是弄一下

注意數據庫版本

5.7以上mysql的數據庫初始密碼要到log裏面找

安裝mariadb

我沒有使用mysql,centos7以後yum默認使用mariadb作爲數據庫

yum install -y mariadb mariadb-server mariadb-devel

配置mariadb

默認安裝mariadb是沒有密碼

配置mariadb首先要啓動服務

systemctl start mariadb

接下來進入數據庫修改root密碼,兩種方法任選一種

初始化數據庫(方法一)

mysql_secure_installation

除了root password 要設置兩次密碼其他的可以全部回車

直接改密碼懶得初始化(方法二)

mysql -u root -p

沒有密碼直接回車

進入服務器後直接修改密碼

set password for root@'localhost' = password('123456');

注意後面的123456就是修改的密碼,爲了安全請設置八位以上帶大小寫和特殊的密碼。

爲服務創建數據庫和授權用戶

注意:以下所有操作經需進入數據庫操作

創建數據庫

爲nextcloud創建一個數據庫

create database nextcloud_db;

創建授權用戶

create user nextcloud@'localhost' identified by '123456';
grant all on nextcloud_db.* to nextcloud@'localhost';

也可以用下面的命令一部到位

grant all privileges on nextcloud_db.* to nextclou@localhost identified by 'n123456';

配置SSL證書

我的證書是在阿里雲申請的免費證書,你們也可以用下面的方法。

創建整數目錄

mkdir /etc/nginx/cert/

不管是自己生成的SSL證書,還是運營商申請的證書,全放在這個目錄,方便配置。

自己生成證書

openssl req -new -x509 -days 365 -nodes -out /etc/nginx/cert/nextcloud.crt -keyout /etc/nginx/cert/nextcloud.key

這個證書只能使用一年,要續訂可以刪掉之前生成的證書,然後再次生成。(這是我猜的)

# 會出現下面的選項需要填寫,可以隨便填。 

Country Name (2 letter code) [XX]:cn                                           //國家

State or Province Name (full name) []:yunnan                                  //省份

Locality Name (eg, city) [Default City]:yuxi                               //地區名字

Organization Name (eg, company) [Default Company Ltd]:XPSL                     //公司名

Organizational Unit Name (eg, section) []:Technology                           //部門

Common Name (eg, your name or your server's hostname) []:xpsl                 //CA主機名

Email Address []:[email protected]                                                        //Email地址

下載安裝nextcloud

下載原文件

官網地址爲: https://nextcloud.com/install/

yum install -y wget unzip
wget https://download.nextcloud.com/server/releases/nextcloud-13.0.2.zip
unzip nextcloud-13.0.2.zip

配置網站空間

nextcloud本質是一個php網站,既然這樣就等爲它配置一個目錄。

我的目錄設置在 /usr/share/nginx/html 下

mv nextcloud /usr/share/nginx/html
cd !$
mkdir nextcloud/data
chown -R nginx:nginx nextcloud

配置網站nginx虛擬環境

進入nginx配置目錄

cd /etc/nginx/conf.d/

添加新網站配置文件

vim nextcloud.conf          # 注意必須以".conf"結尾

複製粘貼下面是內容

upstream php-handler {
    server 127.0.0.1:9000;
    #server unix:/var/run/php5-fpm.sock;
}


server {
    listen 80;
    server_name www.houziyu.topt;       # 這是網站的url
    # enforce https
rewrite ^(.*)$ https://$host$1 permanent;
}


server {
    listen 443 ssl;
    server_name www.houziyu.topt;       # 這是網站的url
    
    # 注意,這裏填寫的是ssl證書公玥與私玥
    ssl_certificate /etc/nginx/cert/nextcloud.crt;      
    ssl_certificate_key /etc/nginx/cert/nextcloud.key;

    # 這個文件是直接摘抄別人的,具體可以自己看註釋。
    # Add headers to serve security related headers
    # Before enabling Strict-Transport-Security headers please read into this
    # topic first.
    add_header Strict-Transport-Security "max-age=15768000;
    includeSubDomains; preload;";
    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/html/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;
    }


    # set max upload size
    client_max_body_size 10240M; # 上傳文件最大限制,php.ini中也要修改,最後優化時會提及。
    fastcgi_buffers 64 4K;

    # Disable gzip to avoid the removal of the ETag header
    gzip on;
    gzip_vary on;
    gzip_comp_level 4;
    gzip_min_length 256;
    gzip_proxied expired no-cache no-store private no_last_modified no_etag auth;
    gzip_types application/atom+xml application/javascript application/json application/ld+json application/manifest+json application/rss+xml application/vnd.geo+json application/vnd.ms-fontobject application/x-font-ttf application/x-web-app-manifest+json application/xhtml+xml application/xml font/opentype image/bmp image/svg+xml image/x-icon text/cache-manifest text/css text/plain text/vcard text/vnd.rim.location.xloc text/vtt text/x-component text/x-cross-domain-policy;


    # 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;
        fastcgi_param HTTPS on;
        #Avoid sending the security headers twice
        fastcgi_param modHeadersAvailable true;
        fastcgi_param front_controller_active true;
        fastcgi_pass php-handler;
        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)
        # Before enabling Strict-Transport-Security headers please read into
        # this topic first.
        add_header Strict-Transport-Security "max-age=15768000;includeSubDomains; preload;";
        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;
    }
}

修改ngnix默認配置

vim /etc/nginx/nginx.conf 

註釋 默認server

#   server {
#       listen       80 default_server;
#       listen       [::]:80 default_server;
#       server_name  _;
#       root         /usr/share/nginx/html;

#       # Load configuration files for the default server block.
#       include /etc/nginx/default.d/*.conf;

#       location / {
#       }

#       error_page 404 /404.html;
#           location = /40x.html {
#       }

#       error_page 500 502 503 504 /50x.html;
#           location = /50x.html {
#       }
#   }

nginx配置檢查

nginx -t

如果出現

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

那就可以進行下一步

啓動服務並開啓開機自啓

systemctl start nginx
systemctl enable nginx
systemctl start php-fpm
systemctl enable php-fpm
systemctl start mariadb
systemctl enable mariadb

打開服務端口防火牆

檢查是否啓動firewall服務

systemctl status firewalld

打開80和443端口,數據庫使用的是本地數據庫可以不用設置防火牆。

firewall-cmd --add-port=80/tcp --permanent
firewall-cmd --add-port=443/tcp --permanent
firewall-cmd --reload

關閉selinux

因爲不會用還是關了算了

vim /etc/selinux/config

SELINUX=disabled    (添加這句話寫在中間)

網頁設置nextcloud

看視頻,有道雲筆記不能上傳圖片,我也沒有辦法。
在這裏插入圖片描述

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章