How to install Nextcloud 16 on CentOS 7 HTTPS Nginx


Install Nginx and PHP7-FPM

First of all, add the EPEL repository, which contains Nginx:
# yum install epel-release -y
Next, install Nginx:
# yum install nginx -y



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

yum install -y nano yum-utils unzip curl wget \
bash-completion policycoreutils-python mlocate bzip2

Now, it’s possible to install PHP7-FPM and some Nextcloud dependencies:
# yum install php72w-fpm php72w-pecl-apcu-devel php72w-json php72w-pecl-apcu php72w-gd php72w-mcrypt php72w-cli php72w-pear php72w-xml php72w-mbstring php72w-pdo php72w php72w-cli php72w-common php72w-curl php72w-gd php72w-mbstring php72w-mysqlnd php72w-process php72w-xml php72w-zip php72w-opcache php72w-pecl-apcu php72w-intl php72w-pecl-redis php72w-pecl-imagick

Check the PHP version to be sure that everything went well, with:
# php -v

Configure PHP-FPM

After installation, a configuration of PHP is required for use with Nginx. With a text editor, edit the /etc/php-fpm.d/www.conf file. In there, search lines containing user and groupstrings and modify as follows:
user = nginx
group = nginx
In the same file, look for listen string, and modify that too:
listen = 127.0.0.1:9000
PHP will listen on port 9000.
Uncomment the following lines:
env[HOSTNAME] = $HOSTNAME
env[PATH] = /usr/local/bin:/usr/bin:/bin
env[TMP] = /tmp
env[TMPDIR] = /tmp
env[TEMP] = /tmp
Save and exit.
Create a new directory in /var/lib and change its owner to nginx user:
# mkdir -p /var/lib/php/session
# chown nginx:nginx -R /var/lib/php/session/
Start and enable both Nginx and PHP7-FPM:
# systemctl start php-fpm
# systemctl start nginx
# systemctl enable php-fpm
# systemctl enable nginx

Install MariaDB

yum -y install mariadb-server mariadb

systemctl start mariadb

systemctl enable mariadb

Then, configure the root account for MariaDB:
# mysql_secure_installation
Set root password? [Y/n]
New password: my_strong_root_password
Re-enter new password: my_strong_root_password

Remove anonymous users? [Y/n]
Disallow root login remotely? [Y/n]
Remove test database and access to it? [Y/n]
Reload privilege tables now? [Y/n]
Now, it’s time to login to MariaDB and configure it for use with Nextcloud:
# mysql -u root -p
In its shell:
mysql> CREATE DATABASE my_nextclouddb;
mysql> CREATE USER ncuser@localhost IDENTIFIED BY 'ncuser@';
mysql> GRANT ALL PRIVILEGES ON my_nextclouddb.* TO ncuser@localhost IDENTIFIED BY 'ncuser@';
mysql> FLUSH PRIVILEGES;
mysql> EXIT;

Generate a SSL certificate

For using Nextcloud with HTTPS connection with the client, you’ll need an SSL certificate. Generate a self-signed one with OpenSSL. First, create a new directory for that file:
# mkdir -p /etc/nginx/cert/
and generate it:
Go to Letsencrypt and create 
make sure combine crt file add to nginx config
/etc/nginx/cert/nextcloud.crt 
 /etc/nginx/cert/nextcloud.key

N.B: the /etc/nginx/cert/ will contain all the SSL certificates your server will require eventually.
Change permissions:
# chmod 700 /etc/nginx/cert
# chmod 600 /etc/nginx/cert/*


install Nextcloud

Now it’s time to download and install Nextcloud. Download the archive with:
# https://download.nextcloud.com/server/releases/latest.zip
Extract it and move to /usr/share/nginx/html/
# unzip latest.zip

# mv nextcloud/ /usr/share/nginx/html/
Create a new data directory for Nextcloud:
# mkdir -p /usr/share/nginx/html/nextcloud/data/
Change the owner of nextcloud to nginx user:
# chown nginx:nginx -R /usr/share/nginx/html/nextcloud

Configure a Virtual Host for Nextcloud

Create a new Virtual Host configuration file, /etc/nginx/conf.d/nextcloud.conf. There, paste the following configuration:
upstream php-handler {
    server 127.0.0.1:9000;
    #server unix:/var/run/php5-fpm.sock;
}
 
server {
    listen 80;
    server_name storage.mydomain.com;
    # enforce https
    return 301 https://$server_name$request_uri;
}
 
server {
    listen 443 ssl;
    server_name storage.mydomain.com;
 
    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;
    }
}
Save, exit and test Nginx with:
# nginx -t
Then, restart it:
# systemctl restart nginx


Firewall

Configure the firewall to allow access to the Nextcloud storage from external machines.
FirewallD:
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --reload
setenforce 0
nano /etc/selinux/config (enforce tp permissive)



5 Comments

දිරියක් වෙන්න අදහස් පෙළක් දාන්න

  1. Hi, so what command should i put if i want to install letsencrypt for the ssl ?
    thanks in advance

    ReplyDelete
    Replies
    1. and is all mariadb , php and nginx ... are all ok or newer version are up ?

      Delete
    2. i config with lets encrypt cert bot its really easy

      Delete
  2. https://certbot.eff.org/lets-encrypt/ubuntuxenial-nginx.html you can follow this one

    ReplyDelete
Post a Comment
Previous Post Next Post