RHEL/CentOS Linux Tutorials

Simple NextCloud Installation with NGINX on CentOS 7

Install NGINX

Add NGINX latest repository

vi /etc/yum.repo.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
yum install nginx -y

Enable & Start service:

systemctl enable nginx
systemctl start nginx

Enable HTTP and HTTPS service on firewalld

firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https

Install MariaDB

Add MariaDB latest repository

vi /etc/yum.repo.d/mariadb.repo
<code>#</code> MariaDB 10.5 CentOS repository list - created 2020-11-24 04:47 UTC 
<code>#</code> <a class="rank-math-link" href="http://downloads.mariadb.org/mariadb/repositories/">http://downloads.mariadb.org/mariadb/repositories/</a> 
[mariadb] 
name = MariaDB 
baseurl = <a class="rank-math-link" href="http://yum.mariadb.org/10.5/centos7-amd64">http://yum.mariadb.org/10.5/centos7-amd64</a> 
gpgkey=<a href="https://yum.mariadb.org/RPM-GPG-KEY-MariaDB">https://yum.mariadb.org/RPM-GPG-KEY-MariaDB</a> 
gpgcheck=1

Install nginx, mariadb and some package needed…

yum install MariaDB-server MariaDB-client

Enable and Start service:

systemctl enable mariadb
systemctl start mariadb

Secure MariaDB Server

mysql_secure_installation
[[email protected] ~]# mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none):
OK, successfully used password, moving on…
Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.
Set root password? [Y/n] Y
New password:            #Enter your password
Re-enter new password:             #Re-enter password
Password updated successfully!
Reloading privilege tables..
… Success!
By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.
Remove anonymous users? [Y/n] Y
… Success!
Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] Y
… Success!
By default, MariaDB comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n] Y
Dropping test database…
… Success!
Removing privileges on test database…
… Success!
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n] Y
… Success!
Cleaning up…
All done! If you've completed all of the above steps, your MariaDB
installation should now be secure.
Thanks for using MariaDB!

Install PHP-FPM

yum install unzip epel-release https://rpms.remirepo.net/enterprise/remi-release-7.rpm yum-utils -y

Enable PHP 7.3 repo

yum-config-manager --enable remi-php73
yum install php php-fpm php-curl php-common php-gd php-json php-mbstring php-mysqlnd php-xml php-xmlrpc php-opcache php-mysql php-cli php-mcrypt php-zip -y

Config PHP-FPM

vi /etc/php-fpm.d/www.conf
user = apache         		      user = nginx
group = apache         		      group = nginx
listen = 127.0.0.1:9000    =====>     listen = /var/run/php_fpm.sock
;listen.owner = nobody         	      listen.owner = nginx
;listen.group = nobody        	      listen.group = nginx
;listen.mode = 0660        	      listen.mode = 0660

Start following service:

systemctl enable php-fpm
systemctl start php-fpm

Install NextCloud

Tải phiên bản mới nhất của NextCloud Server

wget https://download.nextcloud.com/server/releases/nextcloud-20.0.2.zip

Giải nén file nextcloud-20.0.2.zip

unzip nextcloud-20.0.2.zip
mv nextcloud /var/www/html/nextcloud
chown -R nginx.nginx /var/www/html/nextcloud

Thêm cấu hình virtual host NGINX

vi /etc/nginx/conf.d/nextcloud.conf

Nginx configuration

upstream php-handler {
    server unix:/var/run/php_fpm.sock;
}

server {
    listen 80;
    server_name [yourdomain or IP address];
    
    # 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;";
    #
    # WARNING: Only add the preload option once you read about
    # the consequences in https://hstspreload.org/. This option
    # will add the domain to a hardcoded list that is shipped
    # in all major browsers and getting removed from this list
    # could take several months.
    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;
    add_header Referrer-Policy no-referrer;

    # Remove X-Powered-By, which is an information leak
    fastcgi_hide_header X-Powered-By;

    # Path to the root of your installation
    root /usr/local/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;

    # The following rule is only needed for the Social app.
    # Uncomment it if you're planning to use this app.
    #rewrite ^/.well-known/webfinger /public.php?service=webfinger last;

    location = /.well-known/carddav {
      return 301 $scheme://$host:$server_port/remote.php/dav;
    }
    location = /.well-known/caldav {
      return 301 $scheme://$host:$server_port/remote.php/dav;
    }

    # set max upload size
    client_max_body_size 512M;
    fastcgi_buffers 64 4K;

    # Enable gzip but do not remove ETag headers
    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;

    location / {
        rewrite ^ /index.php;
    }

    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\/.+|oc[ms]-provider\/.+)\.php(?:$|\/) {
        fastcgi_split_path_info ^(.+?\.php)(\/.*|)$;
        set $path_info $fastcgi_path_info;
        try_files $fastcgi_script_name =404;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $path_info;
        #fastcgi_param HTTPS on;
        # Avoid sending the security headers twice
        fastcgi_param modHeadersAvailable true;
        # Enable pretty urls
        fastcgi_param front_controller_active true;
        fastcgi_pass php-handler;
        fastcgi_intercept_errors on;
        fastcgi_request_buffering off;
    }

    location ~ ^\/(?:updater|oc[ms]-provider)(?:$|\/) {
        try_files $uri/ =404;
        index index.php;
    }

    # Adding the cache control header for js, css and map files
    # Make sure it is BELOW the PHP block
    location ~ \.(?:css|js|woff2?|svg|gif|map)$ {
        try_files $uri /index.php$request_uri;
        add_header Cache-Control "public, max-age=15778463";
        # 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;";
        #
        # WARNING: Only add the preload option once you read about
        # the consequences in https://hstspreload.org/. This option
        # will add the domain to a hardcoded list that is shipped
        # in all major browsers and getting removed from this list
        # could take several months.
        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;
        add_header Referrer-Policy no-referrer;

        # Optional: Don't log access to assets
        access_log off;
    }

    location ~ \.(?:png|html|ttf|ico|jpg|jpeg|bcmap)$ {
        try_files $uri /index.php$request_uri;
        # Optional: Don't log access to other assets
        access_log off;
    }
}

Restart lại NGINX service

systemctl restart nginx

Tạo Database cho NextCloud

mysql -u root
CREATE USER 'username'@'localhost' IDENTIFIED BY 'password'; 
CREATE DATABASE IF NOT EXISTS nextcloud CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci; 
GRANT ALL PRIVILEGES on nextcloud.* to 'username'@'localhost'; FLUSH privileges;
exit;

Thiết lập NextCloud

Mở trình duyệt và nhập vào địa chỉ IP hoặc tên miền

Simple NextCloud Installation with NGINX on CentOS 7

Tags

About the author

CẬN THỊ

Thích đàn hát và mê công nghệ.

0 0 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments