Page 1 of 1

Cài đặt NextCloud + LEMP Stack trên Centos 7

PostPosted:15 Nov 2018 16:35
by attravel
Nhắc tới Nextcloud, chúng ta phải biết về ownCloud trước. Công ty ownCloud do Frank Karlitschek thành lập được phát triển từ năm 2010 - là một mã nguồn mở miễn phí và ứng dụng web mạnh mẽ để đồng bộ hóa dữ liệu, chia sẻ tập tin, và lưu trữ từ xa của các tập tin. ownCloud được viết bằng ngôn ngữ PHP/javascript. Nó được thiết kế để làm việc với một số hệ thống quản lý cơ sở dữ liệu, bao gồm cả MySQL, MariaDB, SQLite, Oracle Database, và PostgreSQL. Hơn nữa ownCloud có thể được triển khai trên tất cả các nền tảng như : Linux, Macintosh, Windows và Android. Đây là một ứng dụng chạy trên hệ thống mạnh mẽ, nền tảng độc lập, linh hoạt về cấu hình và khả năng sử dụng, dễ dàng sử dụng ứng dụng mã nguồn mở. Dựa trên tinh thần là hoàn toàn miễn phí nên nó đã phát triển 1 cách nhanh chóng. Nhưng một vài năm trước đây, mọi thứ bắt đầu thay đổi. Thay vì hướng tới cộng đồng để phát triển thì các dự án đã được hướng theo ý tưởng khác. Các doanh nghiệp đã phải trả tiền để hỗ trợ. Các phàn nàn từ cộng đồng Open Source ngày 1 dài về việc owncloud đã bỏ qua những feedback từ phía khách hàng, giao diện người dùng bị cắt xén nhiều hay việc release sản phẩm mà chức năng update bị lỗi.

Với những vấn đề lớn này, Frank đã từ chức và thành lập NextCloud cùng rất nhiều thành viên nòng cốt của ownCloud cũ. Nextcloud là một máy chủ chia sẻ tập tin cho phép bạn lưu trữ các nội dung cá nhân của bạn, như tài liệu và hình ảnh giống như Dropbox. Sự khác biệt với Nextcloud là tất cả các tính năng của nó là open source. Nó cũng trả về sự quản lý và các dữ liệu nhạy cảm cho bạn, do đó loại trừ việc sử dụng một dịch vụ đám mây lưu trữ của bên thứ ba. Nextcloud là một ngã ba của ownCloud và nó phát triển một cách nhanh chóng để đạt được thành công và rất nhiều người dùng ownCloud đang chuyển từ ownCloud sang Nextcloud.
Và hôm nay mình sẽ hướng dẫn các bạn cài đặt nó trên Centos 7 :mrgreen:

Bước 1 - Cài đặt Nginx và PHP7-FPM trên CentOS 7

Trước khi bắt đầu cài đặt Nginx và php7-fpm, chúng ta phải thêm kho gói EPEL. Cài đặt nó với lệnh yum này.
Code: Select all
yum -y install epel-release
Cài đặt Nginx từ kho EPEL.
Code: Select all
yum -y install nginx
Cài đặt Remi Repo
Code: Select all
yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm
Cài đặt yum-ultils
Code: Select all
yum install yum-utils
1 trong những chương trình được cung cấp bởi yum-ultils là yum-config-manager ,bạn có thể sử dụng nó để kích hoạt kho lưu trữ Remi làm kho lưu trữ mặc định để cài đặt các phiên bản PHP khác nhau như được hiển thị.
Enable Cài đặt php7.2 ( muốn dùng phiên bản thấp hơn thì sửa số )
Code: Select all
yum-config-manager --enable remi-php72
Cài đặt PHP :
Code: Select all
yum -y install php php-fpm php72-php-gd php72-php-ldap php-mbstring php72-php-mbstring php-mysql php-xml  php-pecl-apcu  php-pecl-apcu-devel php-xmlrpc php-gd php-pecl-zip 
Check Version PHP :
Code: Select all
php --version
Code: Select all
PHP 7.2.12 (cli) (built: Nov  6 2018 16:40:25) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
Code: Select all
Bước 2  Cấu Hình PHP7-FPM
Trong bước này, chúng ta sẽ cấu hình php-fpm để chạy với Nginx. Php7-fpm sẽ chạy dưới nginx của người dùng và nghe trên cổng 9000.

Chỉnh sửa tập tin cấu hình php7-fpm mặc định với vim.
Code: Select all
vi /etc/php-fpm.d/www.conf
Sửa User & Group từ Apache sang Nginx :
Code: Select all
user = nginx
group = nginx
Đảm bảo php-fpm đang chạy dưới cổng máy chủ.
Code: Select all
listen = 127.0.0.1:9000
Bỏ dấu ghi chú # tại các dòng sau :
Code: Select all
env[HOSTNAME] = $HOSTNAME
env[PATH] = /usr/local/bin:/usr/bin:/bin
env[TMP] = /tmp
env[TMPDIR] = /tmp
env[TEMP] = /tmp
:wq

Lưu tệp và thoát trình soạn thảo vim.

Tiếp theo, tạo một thư mục mới cho đường dẫn phiên trong thư mục '/ var / lib /' và thay đổi chủ sở hữu thành người dùng 'nginx'.
Code: Select all
mkdir -p /var/lib/php/session
chown nginx:nginx -R /var/lib/php/session/
Bây giờ bắt đầu php-fpm và Nginx, sau đó cho phép các dịch vụ bắt đầu lúc khởi động.
Code: Select all
sudo systemctl start php-fpm
sudo systemctl start nginx
 
sudo systemctl enable php-fpm
sudo systemctl enable nginx
Bước 3 - Cài đặt và cấu hình MariaDB
Phiên bản MariaDB mà mình lựa chọn là 10.3

Tạo một tệp repo mới /etc/yum.repos.d/mariadb.repo và thêm đoạn mã dưới đây thay đổi url cơ sở theo phiên bản và kiến ​​trúc hệ điều hành.
Code: Select all
#vi /etc/yum.repos.d/mariadb.repo
Code: Select all
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.3/rhel7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1
Cài đặt Mariadb Server
Code: Select all
yum install MariaDB-server MariaDB-client
Start Mariadb và Enable khi khởi động
Code: Select all
# systemctl enable mariadb.service
# systemctl start mariadb.service
Cấu hình Mariadb Server
Code: Select all
mysql_secure_installation
Nhập mật khẩu gốc của bạn khi được yêu cầu.
Code: Select all
Set root password? [Y/n] Y
New password:
Re-enter new password:
 
Remove anonymous users? [Y/n] Y
Disallow root login remotely? [Y/n] Y
Remove test database and access to it? [Y/n] Y
Reload privilege tables now? [Y/n] Y
Mật khẩu root của MariaDB đã được thiết lập, bây giờ chúng ta có thể đăng nhập vào shell mysql để tạo một cơ sở dữ liệu mới và một người dùng mới cho Nextcloud. Tôi sẽ tạo một cơ sở dữ liệu mới có tên ' nextcloud_db ' và một người dùng ' nextclouduser ' với mật khẩu ' nextclouduser @ '. Chọn một mật khẩu an toàn để cài đặt của bạn!
Code: Select all
mysql -u root -p
Type Password
Nhập vào truy vấn mysql bên dưới để tạo cơ sở dữ liệu mới và người dùng mới.
Code: Select all
create database nextcloud_db;
create user nextclouduser@localhost identified by 'nextclouduser@';
grant all privileges on nextcloud_db.* to nextclouduser@localhost identified by 'nextclouduser@';
flush privileges;
Cơ sở dữ liệu nextcloud_db với người dùng 'nextclouduser' đã được tạo.

Bước 4 - Tạo chứng chỉ SSL tự ký cho Nextcloud

Trong hướng dẫn này, tôi sẽ chạy nextcloud với một kết nối https cho máy khách. Bạn có thể sử dụng SSL miễn phí như để mã hóa hoặc tạo chứng chỉ SSL tự ký. Tôi sẽ tạo tệp chứng chỉ SSL tự ký của riêng mình với lệnh OpenSSL.

Tạo một thư mục mới cho tệp SSL.
Code: Select all
mkdir -p /etc/nginx/cert/
Và tạo tệp chứng chỉ SSL mới bằng lệnh openssl bên dưới.
Code: Select all
openssl req -new -x509 -days 365 -nodes -out /etc/nginx/cert/nextcloud.crt -keyout /etc/nginx/cert/nextcloud.key
Cuối cùng, thay đổi quyền của tất cả các tệp chứng chỉ thành 600 với chmod.
Code: Select all
chmod 700 /etc/nginx/cert
chmod 600 /etc/nginx/cert/*
Bước 5 - Tải xuống và cài đặt Nextcloud

Chúng tôi sẽ tải về Nextcloud với wget trực tiếp đến máy chủ, vì vậy chúng tôi phải cài đặt wget đầu tiên. Ngoài ra, chúng tôi cần chương trình giải nén. Cài đặt cả hai ứng dụng với yum.
Code: Select all
yum -y install wget unzip
Vào thư mục / tmp và tải xuống phiên bản Nextcloud 14 ổn định mới nhất từ ​​trang web Nextcloud với wget.
Code: Select all
cd /tmp
wget https://download.nextcloud.com/server/releases/nextcloud-14.0.3.zip 
Giải nén tập tin zip nextcloud và di chuyển nội dung của nó vào thư mục /usr/share/nginx/html/
Code: Select all
unzip nextcloud-14.0.0.3
mv nextcloud/ /usr/share/nginx/html/
Tiếp theo, vào thư mục gốc của trang web Nginx và tạo một thư mục 'data' mới cho Nextcloud.
Code: Select all
cd /usr/share/nginx/html/
mkdir -p nextcloud/data/
Thay đổi chủ sở hữu của thư mục 'nextcloud' thành người dùng và nhóm 'nginx'.
Code: Select all
chown nginx:nginx -R nextcloud/
Bước 6 - Cấu hình máy chủ ảo Nextcloud trong Nginx

Trong bước 5, chúng tôi đã tải về mã nguồn Nextcloud và cấu hình nó để chạy dưới máy chủ web Nginx. Nhưng chúng tôi vẫn cần phải cấu hình một máy chủ ảo cho Nextcloud. Tạo một tệp cấu hình máy chủ ảo mới 'nextcloud.conf' trong thư mục Nginx 'conf.d'.
Code: Select all
cd /etc/nginx/conf.d/
vi nextcloud.conf
Dán cấu hình máy chủ ảo Nextcloud bên dưới.
Code: Select all
upstream php-handler {
    server 127.0.0.1:9000;
    #server unix:/var/run/php5-fpm.sock;
}
 
server {
    listen 80;
    #server_name đổi sang domain hoặc ip của bạn
    server_name cloud.nextcloud.co;
    # enforce https
    return 301 https://$server_name$request_uri;
}
 
server {
    listen 443 ssl;
    #server_name đổi sang domain hoặc ip của bạn
    server_name cloud.nextcloud.co;
 
    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 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;
        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;
    }
}
Lưu tệp và thoát vim.

Bây giờ kiểm tra cấu hình Nginx để đảm bảo rằng không có lỗi, Sau đó khởi động lại dịch vụ.
Code: Select all
nginx -t
systemctl restart nginx
Bước 7 - Cấu hình SELinux và FirewallD cho Nextcloud

Trong hướng dẫn này, chúng ta sẽ để SELinux ở chế độ thực thi, vì vậy chúng ta cần một công cụ quản lý SELinux gói mới để cấu hình SELinux cho Nextcloud.

Cài đặt các công cụ quản lý SELinux bằng lệnh này.
Code: Select all
yum -y install policycoreutils-python
Sau đó thực hiện các lệnh dưới đây với tư cách người dùng root để cho phép Nextcloud chạy dưới SELinux. Hãy nhớ thay đổi thư mục Nextcloud trong trường hợp bạn sử dụng một thư mục khác.
Code: Select all
semanage fcontext -a -t httpd_sys_rw_content_t '/usr/share/nginx/html/nextcloud/data(/.*)?'
semanage fcontext -a -t httpd_sys_rw_content_t '/usr/share/nginx/html/nextcloud/config(/.*)?'
semanage fcontext -a -t httpd_sys_rw_content_t '/usr/share/nginx/html/nextcloud/apps(/.*)?'
semanage fcontext -a -t httpd_sys_rw_content_t '/usr/share/nginx/html/nextcloud/assets(/.*)?'
semanage fcontext -a -t httpd_sys_rw_content_t '/usr/share/nginx/html/nextcloud/.htaccess'
semanage fcontext -a -t httpd_sys_rw_content_t '/usr/share/nginx/html/nextcloud/.user.ini'
 
restorecon -Rv '/usr/share/nginx/html/nextcloud/'
Tiếp theo, chúng ta sẽ kích hoạt dịch vụ firewalld và mở các cổng HTTP và HTTPS cho Nextcloud.

Bắt đầu firewalld và kích hoạt nó để bắt đầu lúc khởi động.
Code: Select all
systemctl start firewalld
systemctl enable firewalld
Bây giờ hãy mở các cổng HTTP và HTTPS bằng lệnh tường lửa-cmd, sau đó tải lại tường lửa.
Code: Select all
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --reload
Tất cả các cấu hình máy chủ được thực hiện.

Bước 8 - Trình hướng dẫn cài đặt Nextcloud

Mở trình duyệt web của bạn và nhập tên miền Nextcloud của bạn, tên miền của tôi là: cloud01.attravel.vn. Bạn sẽ được chuyển hướng đến kết nối https an toàn.

Nhập tên người dùng và mật khẩu quản trị mong muốn của bạn rồi nhập thông tin đăng nhập cơ sở dữ liệu của bạn. Nhấp vào ' Hoàn tất thiết lập '.

Nextcloud đã được cài đặt với Nginx, PHP7-FPM và MariaDB trên máy chủ CentOS 7.

Bài viết dựa trên 1 vài hướng dẫn cấu hình ở website Howtoforget , Sai chỗ nào AE chém nhẹ tay :mrgreen:

Re: Cài đặt NextCloud + LEMP Stack trên Centos 7

PostPosted:15 Nov 2018 21:54
by nguyenoanh
Cảm ơn bạn.
Nhưng sao không bắt đầu bằng việc cài VPSSIM mà lại bắt đầu từ việc cài VPS thế ?
Nó phức tạp hơn kha khá nhiều ;)

Re: Cài đặt NextCloud + LEMP Stack trên Centos 7

PostPosted:15 Nov 2018 23:22
by attravel
Cái nextcloud chủ yếu xài cho cá nhân cùng doanh nghiệp vừa và nhỏ :mrgreen: , vpssim e thử 1 lần nhưng không được , bác Oánh tích hợp được cái này vào vpssim thì ngon quá

Re: Cài đặt NextCloud + LEMP Stack trên Centos 7

PostPosted:16 Nov 2018 08:11
by nguyenoanh
attravel wrote:
15 Nov 2018 23:22
Cái nextcloud chủ yếu xài cho cá nhân cùng doanh nghiệp vừa và nhỏ :mrgreen: , vpssim e thử 1 lần nhưng không được , bác Oánh tích hợp được cái này vào vpssim thì ngon quá
okie bạn :)