Support #847
Updated by Daniel Curtis almost 7 years ago
{{>toc}}
This is a guide on setting up NextCloud 12 with Nginx on FreeBSD 11.
h1. Prepare the Environment
* Before installation of the components, make sure everything is up to date using the following command:
<pre>
pkg update -f && pkg upgrade
</pre>
* Create the nextcloud user:
<pre>
pw user add -n nextcloud -m -s /sbin/nologin -c "NextCloud"
</pre>
---
h1. Install Nginx
* Install Nginx
<pre>
pkg install nginx
</pre>
* Start and enable nginx at boot:
<pre>
echo 'nginx_enable="YES"' >> /etc/rc.conf
service nginx start
</pre>
* Create a configuration directory to make managing individual server blocks easier
<pre>
mkdir /usr/local/etc/nginx/conf.d
</pre>
* Edit the main nginx config file:
<pre>
vi /usr/local/etc/nginx/nginx.conf
</pre>
#* And strip down the config file and add the include statement at the end to make it easier to handle various server blocks:
<pre>
worker_processes 1;
error_log /var/log/nginx-error.log;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
# Load config files from the /etc/nginx/conf.d directory
include /usr/local/etc/nginx/conf.d/*.conf;
}
</pre>
---
h1. Install PHP
* Install PHP 5.6:
<pre>
pkg install php56
</pre>
* Configure the default PHP settings
<pre>
cp /usr/local/etc/php.ini-production /usr/local/etc/php.ini
</pre>
* Edit the PHP config:
<pre>
vi /usr/local/etc/php.ini
</pre>
#* And add the following to the end of the file to enable opcache:
<pre>
opcache.enable=1
opcache.enable_cli=1
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=10000
opcache.memory_consumption=128
opcache.save_comments=1
opcache.revalidate_freq=1
</pre>
* Create a directory for the php-fpm configs:
<pre>
mkdir /usr/local/etc/php-fpm.d
</pre>
* Edit @/usr/local/etc/php-fpm.conf@:
<pre>
vi /usr/local/etc/php-fpm.conf
</pre>
#* Make the following changes:
<pre>
include=/usr/local/etc/php-fpm.d/*.conf
</pre>
* Enable PHP-FPM at boot:
<pre>
echo 'php_fpm_enable="YES"' >> /etc/rc.conf
</pre>
* Restart nginx:
<pre>
service nginx restart
</pre>
---
h1. Install MySQL Server (Option 1)
* Start by installing the mysql56-server and mysql56-client packages:
<pre>
pkg install mysql56-{server,client}
</pre>
* Copy a base MySQL configuration to use:
<pre>
cp /usr/local/share/mysql/my-small.cnf /var/db/mysql/my.cnf
</pre>
* Edit the mariadb config to change the max packet size:
<pre>
vi /var/db/mysql/my.cnf
</pre>
#* and modify @max_allowed_packet@ to 32M
<pre>
max_allowed_packet = 32M
</pre>
* Enable and start MariaDB
<pre>
echo 'mysql_enable="YES"' >> /etc/rc.conf
service mysql-server start
</pre>
* Prepare the database for use by running the secure installation:
<pre>
mysql_secure_installation
</pre>
#* *NOTE*: +Choose a strong root password+ and answer +yes+ to all questions.
h2. Create MySQL Databases and Users
* Login to MySQL and create appropriate databases and users.
<pre>
mysql -u root -p
</pre>
#* and run the following SQL queries to create the *nextclouddb* database and *nextclouduser* user:
<pre>
CREATE DATABASE nextclouddb CHARACTER SET utf8;
CREATE USER 'nextclouduser'@'localhost' IDENTIFIED BY 'SuperSecretPassword';
GRANT ALL PRIVILEGES ON nextclouddb.* TO 'nextclouduser'@'localhost';
FLUSH PRIVILEGES;
quit
</pre>
---
h1. Install PostgreSQL (Option 2)
* Start by installing the mysql56-server and mysql56-client packages:
<pre>
pkg install postgresql95-{server,client} php56-{pdo_pgsql,pgsql}
</pre>
* Enable, initialize and start PostgreSQL
<pre>
echo 'postgresql_enable="YES"' >> /etc/rc.conf
service postgresql initdb
service postgresql start
</pre>
* Edit the pg_hba.conf file:
<pre>
vi /usr/local/pgsql/data/pg_hba.conf
</pre>
#* And add the following to the end of the file to enable password authentication:
<pre>
host all all samehost md5
</pre>
h2. Create PostgreSQL Databases and Users
* Log in to postgresql user account
<pre>
su - pgsql
</pre>
* Connect to postgresql database
<pre>
psql -d template1
</pre>
#* Create a user for NextCloud:
<pre>
CREATE USER nextclouduser WITH PASSWORD 'SuperSecretPassword' CREATEDB;
</pre>
#* Create the NextCloud database & grant all privileges on database
<pre>
CREATE DATABASE nextclouddb OWNER nextclouduser encoding='UNICODE';
GRANT ALL PRIVILEGES ON DATABASE nextclouddb TO nextclouduser;
</pre>
* Quit postgresql and exit the user:
<pre>
\q
exit
</pre>
---
h1. Install Nextcloud
* Install nextcloud:
<pre>
pkg install nextcloud
</pre>
* Create an *nextcloud.example.com server block* config file:
<pre>
vi /usr/local/etc/nginx/conf.d/nextcloud.example.com.conf
</pre>
#* Add the following:
<pre>
upstream nextcloud-handler {
server unix:/var/run/nextcloud.example.com-php-fpm.sock;
}
server {
listen 80;
server_name nextcloud.example.com;
# Path to the root of your installation
root /usr/local/www/nextcloud/;
# set max upload size
client_max_body_size 10G;
fastcgi_buffers 64 4K;
# Disable gzip to avoid the removal of the ETag header
gzip off;
rewrite ^/caldav(.*)$ /remote.php/caldav$1 redirect;
rewrite ^/carddav(.*)$ /remote.php/carddav$1 redirect;
rewrite ^/webdav(.*)$ /remote.php/webdav$1 redirect;
index index.php;
error_page 403 /core/templates/403.php;
error_page 404 /core/templates/404.php;
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location ~ ^/(?:\.htaccess|data|config|db_structure\.xml|README){
deny all;
}
location / {
# The following 2 rules are only needed with webfinger
rewrite ^/.well-known/host-meta /public.php?service=host-meta last;
rewrite ^/.well-known/host-meta.json /public.php?service=host-meta-json last;
rewrite ^/.well-known/carddav /remote.php/carddav/ redirect;
rewrite ^/.well-known/caldav /remote.php/caldav/ redirect;
rewrite ^(/core/doc/[^\/]+/)$ $1/index.html;
try_files $uri $uri/ =404;
}
location ~ \.php(?:$|/) {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_pass nextcloud-handler;
fastcgi_intercept_errors on;
}
# Adding the cache control header for js and css files
# Make sure it is BELOW the location ~ \.php(?:$|/) { block
location ~* \.(?:css|js)$ {
add_header Cache-Control "public, max-age=7200";
# Add headers to serve security related headers
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;
# Optional: Don't log access to assets
access_log off;
}
# Optional: Don't log access to other assets
location ~* \.(?:jpg|jpeg|gif|bmp|ico|png|swf)$ {
access_log off;
}
}
</pre>
* Create the temporary session folder and restrict its permissions:
<pre>
mkdir -p /usr/local/www/nextcloud/tmp
chmod o-rwx /usr/local/www/nextcloud/tmp
</pre>
* Create the nextcloud php-fpm pool config file:
<pre>
vi /usr/local/etc/php-fpm.d/nextcloud.example.com.conf
</pre>
#* And add the following:
<pre>
[nextcloud.example.com]
user = nextcloud
group = www
listen = /var/run/nextcloud.example.com-php-fpm.sock
listen.owner = nextcloud
listen.group = www
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
php_admin_value[session.save_path] = "/usr/local/www/nextcloud/tmp"
</pre>
* Change the ownership of the nextcloud directory:
<pre>
chown -R nextcloud:www /usr/local/www/nextcloud
</pre>
* Restart nginx and start php-fpm:
<pre>
service nginx restart
service php-fpm start
</pre>
---
h1. NextCloud With PHP7 (Optional)
* Uninstall the default php56 version:
<pre>
pkg remove php56
pkg autoremove
</pre>
* Install portmaster:
<pre>
pkg install portmaster
</pre>
* Update ports tree:
<pre>
portsnap fetch extract
</pre>
* Edit the make.conf file:
<pre>
vi /etc/make.conf
</pre>
#* And add the following to the file:
<pre>
DEFAULT_VERSIONS+= php=71
</pre>
* Build nextcloud with php71 as the default version:
<pre>
portmaster www/nextcloud
</pre>
---
h1. Redis
* Install Redis and PHP extension:
<pre>
pkg install redis pecl-redis
</pre>
* Create the directory which contains the socket
<pre>
mkdir -p /var/run/redis
chown redis:redis /var/run/redis
chmod 755 /var/run/redis
</pre>
* Edit the redis config:
<pre>
vi /usr/local/etc/redis.conf
</pre>
#* And modify the following parameters in the config:
<pre>
port 0
unixsocket /var/run/redis/redis.sock
unixsocketperm 770
</pre>
* Add nextcloud user to redis group
<pre>
pw groupmod redis -m nextcloud
</pre>
* Start and enable Redis at boot:
<pre>
echo 'redis_enable="YES"' >> /etc/rc.conf
service redis.start
</pre>
* Edit the NextCloud config:
<pre>
vi /usr/local/www/nextcloud/config/config.php
</pre>
#* And add the following *before* the ending @);@:
<pre>
'memcache.locking' => '\OC\Memcache\Redis',
'memcache.local' => '\OC\Memcache\Redis',
'redis' => array(
'host' => '/tmp/redis.sock',
'port' => 0,
),
</pre>
---
h1. Resources
* https://docs.nextcloud.com/server/12/admin_manual/installation/index.html
* https://docs.nextcloud.com/server/12/admin_manual/configuration_database/linux_database_configuration.html
* https://docs.nextcloud.com/server/12/admin_manual/configuration_server/caching_configuration.html