Skip to main content

Nextcloud - LAMP Stack on Ubuntu 22.04

875 words·
Nextcloud LAMP Stack Apache MySQL

Note: This is just a basic setup for Nextcloud based on a LAMP stack. For a more extensive configuration check out my older post “Nextcloud: Docker Compose Stack, HTTPS, S3 Storage, LDAPS Active Directory Authentication, Maintenence & other Settings”.

Nextcloud LAMP Stack
#

Install Packages
#

# Update package index
sudo apt update
# Install Apache2, MySQL, PHP
sudo apt install apache2 mysql-server php libapache2-mod-php php-mysql -y

# Install additional php packages
sudo apt install php-gd php-curl php-mbstring php-intl php-gmp php-bcmath php-xml php-imagick php-zip -y
  • libapache2-mod-php Allows Apache to interpret and execute PHP files

  • php-mysql Allows PHP to communicate with MySQL databases


MySQL
#

Start & Enable SQL server
#

# Start MySQL server
sudo systemctl start mysql

# Enable service after boot
sudo systemctl enable mysql

Configure SQL server
#

# Run SQL client
sudo mysql

# Set root pw and authentication method to "mysql_native_password"
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'sql-root-pw';

# Exit SQL client
exit
# Run secure installation script
sudo mysql_secure_installation

Create Database & User
#

# Connect to SQL server: Prompt for PW
mysql -u root -p
# Create "nextcloud" database
CREATE DATABASE IF NOT EXISTS nextcloud CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;

# Create "nextclouduser" user and set PW
CREATE USER 'nextclouduser'@'localhost' IDENTIFIED BY 'mysql-pw';


# Give nextclouduser full access to database
GRANT ALL PRIVILEGES ON nextcloud.* TO 'nextclouduser'@'localhost';
# Reload & apply changes
FLUSH PRIVILEGES;

# Exit SQL client
exit

Nextcloud Setup
#

Create Website Directory
#

# Create website directory
sudo mkdir /var/www/nextcloud.jklug.work

Download Nextcloud
#

# Change to tmp directory
cd /tmp

# Download wordpress: Latest version
sudo wget https://download.nextcloud.com/server/releases/latest.tar.bz2

# Extract files
sudo tar -xjvf latest.tar.bz2
  • Move files into website directory
# Move files
sudo mv nextcloud/* /var/www/nextcloud.jklug.work/
  • Set owner and permissions
# Change owner
sudo chown -R www-data:www-data /var/www/nextcloud.jklug.work

# Set directory permissions
sudo find /var/www/nextcloud.jklug.work/ -type d -exec chmod 750 {} \;

# Set file permissions
sudo find /var/www/nextcloud.jklug.work/ -type f -exec chmod 640 {} \;

Apache
#

Domain Name
#

Define the domain name for the server in the main configuration file.

# Open main configuration
sudo vi /etc/apache2/apache2.conf

# Add domain name
ServerName jklug.work

Enable SSL
#

# Enable SSL module
sudo a2enmod ssl

Enable Rewrite Module
#

  • Necessary for Nextcloud to work correctly
# Enable Apache Rewrite module
sudo a2enmod rewrite

Other Recommended Modules #

sudo a2enmod headers &&
sudo a2enmod env &&
sudo a2enmod dir &&
sudo a2enmod mime &&
sudo a2enmod setenvif

Adjust Apache config: PHP
#

Adjust Apache2 configuration to prefer PHP Files.

# Open dir module
sudo vi /etc/apache2/mods-enabled/dir.conf
  • Set index.php on the beginning of the list, it should look like this:
# /etc/apache2/mods-enabled/dir.conf
<IfModule mod_dir.c>
        DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm
</IfModule>

Virtual Host Configuration
#

# Create virtual host configuration
sudo vi /etc/apache2/sites-available/nextcloud.jklug.work.conf
# nextcloud.jklug.work.conf
<VirtualHost *:80>
    ServerName nextcloud.jklug.work
    ServerAlias www.nextcloud.jklug.work
    Redirect permanent / https://nextcloud.jklug.work/ # Redirect HTTP to HTTPS
</VirtualHost>

<VirtualHost *:443>
    ServerName nextcloud.jklug.work
    ServerAlias www.nextcloud.jklug.work
    DocumentRoot /var/www/nextcloud.jklug.work/

    <Directory /var/www/nextcloud/>
      Require all granted
      AllowOverride All
      Options FollowSymLinks MultiViews
      Satisfy Any
  
      <IfModule mod_dav.c>
        Dav off
      </IfModule>
    </Directory>

    SSLEngine on
    SSLCertificateFile /etc/letsencrypt/live/nextcloud.jklug.work/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/nextcloud.jklug.work/privkey.pem

    # Add Strict-Transport-Security header
    Header always set Strict-Transport-Security "max-age=15552000; includeSubDomains"

    # Allow only TLS version 1.2 & 1.3
    SSLProtocol -all +TLSv1.2 +TLSv1.3

    # Define cipher suites for SSL/TLS handshake
    SSLCipherSuite "ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:DHE-RSA-AES256-GCM-SHA384"

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Enable Virtual Host Configuration
#

# Enable virtual host configuration
sudo a2ensite nextcloud.jklug.work

# Disable default host configuration
sudo a2dissite 000-default
# Test configuration
sudo apache2ctl configtest

# Restart Apache
sudo systemctl restart apache2

Nextcloud Installation
#

Install Nextcloud via OCC
#

  • Note the Nextcloud initial installation can also be done via the GUI
# Change into Nextcloud directory
cd /var/www/nextcloud.jklug.work
# Install Nextcloud
sudo -u www-data php occ  maintenance:install \
--database='mysql' --database-name='nextcloud' \
--database-user='nextclouduser' --database-pass='mysql-pw' \
--admin-user='admin' --admin-pass='admin-pw'

# Shell output
Nextcloud was successfully installed

Add Domain Name
#

  • Add the domain name to conig.php
# Open config.php
sudo vi /var/www/nextcloud.jklug.work/config/config.php
# config.php
<?php
$CONFIG = array (
  'instanceid' => 'ockxrll2qxlp',
  'passwordsalt' => 'oaItwhYXOLuy89X1/NhUFTRrYvKVf8',
  'secret' => 'E/Z6cZl+nji3KM+oNViD6tSktm2AyDynQ5VrAk4mgLDCFr8Z',
  'trusted_domains' =>
  array (
    0 => 'nextcloud.jklug.work', # Define domain name
  ),
  'datadirectory' => '/var/www/nextcloud.jklug.work/data',
  'dbtype' => 'mysql',
  'version' => '28.0.1.1',
  'overwrite.cli.url' => 'http://localhost',
  'dbname' => 'nextcloud',
  'dbhost' => 'localhost',
  'dbport' => '',
  'dbtableprefix' => 'oc_',
  'mysql.utf8mb4' => true,
  'dbuser' => 'nextclouduser',
  'dbpassword' => 'mysql-pw',
  'installed' => true,
);

PHP Settings
#

# Find php version
php --ini 

# Open php.ini
sudo vi /etc/php/8.1/apache2/php.ini
  • Memory Limit
# Default value
memory_limit = 512M

# Set Memory Limit to at least 512 MB
memory_limit = 1G
  • Output Buffering
# Default value
output_buffering = 4096

# Disable output buffering
output_buffering = Off

Move Data Directory
#

  • Place data directory outside of the web directory
# Enable Maintenance Mode
sudo -u www-data php /var/www/nextcloud.jklug.work/occ maintenance:mode --on
# Create directory
sudo mkdir -p /var/nextcloud

# Move data directory
sudo mv /var/www/nextcloud.jklug.work/data /var/nextcloud/
# Open config.php
sudo vi /var/www/nextcloud.jklug.work/config/config.php

# Define new data directory
  'datadirectory' => '/var/nextcloud/data',
# Disable Maintenance Mode
sudo -u www-data php /var/www/nextcloud.jklug.work/occ maintenance:mode --off

Restart Apache
#

# Restart Apache
sudo systemctl restart apache2

Nextcloud Login
#

# Open Nextcloud webinterface
nextcloud.jklug.work

# User:
admin

# PW:
admin-pw

Links #

# Download Nextcloud: Go to: DOWNLOAD SERVER > COMMUNITY PROJECTS > Archive
https://nextcloud.com/install/

# Apache configuration
https://docs.nextcloud.com/server/latest/admin_manual/installation/source_installation.html#apache-configuration-label