Server Preparation for Ubuntu 18.04


The target audience for this document are more sophisticated admins with additional needs and setup scenarios. Ubuntu 18.04, by design, only provides PHP 7.2. Because PHP 7.2 security support ends in November 2020, you may want to consider using PHP 7.3. If you want to install the PHP 7.3 package and other required PHP extensions for PHP 7.3 for Ubuntu 18.04, you need to use the well known, custom, PPA ondrej/php. Independent of the PHP version used, all other items, such as installing extensions or comments, and notes, remain the same.

If you are looking installing PHP 7.4, read the Ubuntu 20.04 preparation guide as it contains special notes and guides regarding PHP 7.4

Apache Web Server and PHP

The following command installs the Apache web server and PHP.

sudo apt install php libapache2-mod-php apache2

If you want to install in two steps, first PHP and then Apache, run the following commands:

sudo apt install php-cgi
sudo apt install php
sudo apt install apache2 libapache2-mod-php

Use php-fpm when opting for the FastCGI Process Manager. This requires a non-standard setup.

Multiple Concurrent PHP Versions

If you have multiple concurrent PHP versions installed, which will happen when using ondrej/php, you need to tell your Web Server and your CLI environment which one to use. To list all available versions and choose one from them, use the following command:

sudo update-alternatives --config php

Here is an example output:

There are 2 choices for the alternative php (providing /usr/bin/php).

  Selection    Path             Priority   Status
  0            /usr/bin/php7.4   74        auto mode
* 1            /usr/bin/php7.3   73        manual mode
  2            /usr/bin/php7.4   74        manual mode

Press <enter> to keep the current choice[*], or type selection number:

You can also directly set the required PHP version:

sudo update-alternatives --set php /usr/bin/php7.3
After selecting your PHP version, it is highly recommended to switch to the correct compiling environment which is essential e.g. when using PECL!
sudo update-alternatives --set phar /usr/bin/phar7.3
sudo update-alternatives --set phar.phar /usr/bin/phar.phar7.3
sudo update-alternatives --set phpize /usr/bin/phpize7.3
sudo update-alternatives --set php-config /usr/bin/php-config7.3

When using Apache PHP-FPM, you have to configure your PHP version in an Apache config file manually. In any case, the PHP version used must always be the same.

You can read more about the update-alternatives command on the man page.

When switching a PHP version, you MUST also configure the corresponding php.ini files and modules used accordingly!
You may get completely unexpected behaviour if the PHP version of the Web Server and the CLI are different!

Avoiding a Nasty Pitfall

If you are using a PECL install later on, check the output of the command soon after it has started. You will find text like:

PHP Api Version:         20190902
Zend Module Api No:      20190902

Then do a test by just entering the following command:

php -i | grep extension_dir

If the output is different in than the example below, there is a problem that needs fixing:

extension_dir => /usr/lib/php/20180731 => /usr/lib/php/20180731

This is the output that shows a fix is needed:

PHP Warning:  PHP Startup: smbclient: Unable to initialize module
Module compiled with module API=20190902
PHP    compiled with module API=20180731
These options need to match

As you see above, the API modules do not match and have been compiled with different versions and therefore will not work. To fix this, uninstall the module with pecl uninstall <extension_name> then set the correct update-alternatives as described above, and reinstall it.

Common Prerequisites

# Applications suggested for the use with ownCloud
sudo apt install smbclient
sudo apt install redis-server

# Applications needed for the use with ownCloud
sudo apt install unzip
sudo apt install openssl

# PHP extensions needed to use ownCloud
sudo apt install php-mysql php-mbstring php-gettext php-intl php-redis \
     php-imagick php-igbinary php-gmp php-bcmath php-curl php-gd php-zip \
     php-imap php-ldap php-bz2 php-ssh2 php-phpseclib

Useful Commands For Managing PHP Extensions

List Enabled PHP Extensions

If you want to retrieve a list of enabled PHP extensions run following command:

ls `php -i | grep "^extension_dir" | sed -e 's/.*=> //'` | sort

Enabling and Disabling PHP Extensions

To enable or disable a PHP extension, use the commands phpenmod or phpdismod.


sudo phpenmod php-ldap
# or
sudo phpdismod php-ldap

Exif Library

This library should already be part of PHP 7.2. However, you can check if it is by running the following command.

grep -q -P 'EXIF Support => enabled' <(php -i) && echo "Exif is installed and enabled"

Notes for PHP Library phpseclib

phpseclib’s BigInteger uses the php-gmp (GNU Multiple Precision Arithmetic Library), php-bcmath and OpenSSL extensions, if they’re available, for speed, but doesn’t require them to maintain maximum compatibility. The GMP library uses an internal resource type, while the BCMath library uses strings as datatype. The most salient difference is, that GMP works on [arbitrary precision] integer values, whereas BCMath allows [arbitrary precision] decimal / float-like values.

Cryptography Library

The sodium and OpenSSL PHP libraries are cryptographic libraries which are part of the PHP core from PHP 7.2 onwards. Consequently, there is no need to install additional cryptographic libraries, such as php-libsodium or mcrypt.

mcrypt was deprecated in PHP 7.2. If you need it, it is still available via PECL.

mcrypt is only used if it’s available. If mcrypt is not available, either a pure-PHP implementation or OpenSSL is used instead.

The prioritization is as follows: OpenSSL > mcrypt > pure-PHP. mcrypt and OpenSSL load faster than the pure-PHP implementation. mcrypt offers a 45x speedup over pure-PHP. OpenSSL offers a greater than 6.5x speedup over mcrypt.

Mcrypt PHP Library

Here are the steps for installing mcrypt, when it is explicitly required.

Some steps were borrowed from the website for student’s guide to installing the PHP 7.2-Mcrypt module.

First, determine the mcrypt version you want to use in PECL’s mcrypt documentation. Then, run the following commands to install it.

sudo apt install php-dev libmcrypt-dev php-pear
sudo pecl channel-update
sudo pecl install mcrypt-<desired mcrypt version>

When the commands complete, you then have to:

  • Create /etc/php/7.2/mods-available/mcrypt.ini with the following content:

  • Enable the module by running phpenmod mcrypt.

  • Restart your web server, by running the following commands:

    sudo service apache2 restart


From PHP 7.2 onwards, libsodium is part of PHP, so installing this extension is no longer necessary.

If you see the message "PHP Fatal error: sodium_init() in Unknown on line 0" when invoking the command php -v or when running PHP programs, php-libsodium may have been installed unnecessarily. This error does not do any harm, but it can be annoying, and it fills up your logs. To prevent this, you can uninstall php-libsodium. This can be done by invoking the following command:

sudo apt remove php-libsodium

You can check success by invoking the command php -v, which should no longer return the error message. You can also check the existence of sodium with the following command:

grep -P "sodium support => enabled" <( php -i )

libsmbclient-php Library

libsmbclient-php is a PHP extension that uses Samba’s libsmbclient library to provide Samba-related functions to PHP programs. You need to install it, if you have installed smbclient as described above. smbclient is e.g. necessary, if you are using the Windows Network Drives app from ownClouds Enterprise Edition. To install it, run the commands described below. You can find more information about smbclient and the latest version on PECL.

sudo apt install php-dev libsmbclient-dev php-pear
sudo pecl channel-update
sudo pecl install smbclient

When the commands complete, you then have to (assuming you use PHP 7.2):

  • Create /etc/php/7.2/mods-available/smbclient.ini with following content

  • Enable the module by running phpenmod smbclient.

  • Restart PHP and your web server by running the following command:

    sudo service apache2 restart
Do not get confused by the name smbclient. It is on the one hand a command-line SMB/CIFS client for Unix, and on the other hand the package name for the PECL libsmbclient-php` PHP extension.
Alternatively you can install it from source.

Due to a change in the minimum protocol version used in the Samba client in Ubuntu 18.04, you may not get a valid connection in ownCloud. This error is identified by a red box at the mount definition or being unable to list directory content. In this case, you have to add the following to /etc/samba/smb.cnf, below the workgroup = statement:

client max protocol = NT1

For more information see: Bionic Beaver can not discover Samba hosts

Database Installation

To install a database, see the Manual Installation Databases guide.

Follow the procedure described in Useful Tips, if you want to Disable Transparent Huge Pages (THP),Transparent Huge Pages

Useful Tips

Start a Service After a Resource is Mounted

If you have network resources, such as NFS or iSCSI based mounts, and you want to make sure that the database or web server only starts after the resource is mounted, then consider the following example setup when configuring your system.

The example below is based on an NFS mount which you want to be available before the service with <name.service> starts. The same procedure can be used for iSCSI. For details setting up an iSCSI mount see the Ubuntu iSCSI Initiator guide.

The name in <name.service> could be any valid service, including apache2, mysql or mariadb.

  • Add _netdev to the list of NFS mount point options in /etc/fstab.

    This option ensures, that the mount happens after the network is up:

    resource:foreign_path local_path nfs (<your options>),_netdev
  • Make sure that all mounts in /etc/fstab are mounted by running:

    sudo mount -a
  • Run the following command to list mounts which must be up first:

    systemctl list-units | grep -nP "\.mount"

    You should see lines printed to the console. Look for the mount you want to be up in the command’s output.

      loaded active mounted <local_path>

    where <folder.mount> and <local_path> are examples!

  • Edit the service you want to change:

    sudo systemctl edit <name>.service

    Add the following directive in the editor opened, using your chosen folder.mount from above:


    You can add more than one dependency if needed by separating them with spaces. This procedure keeps <name>.service in its original state but makes it possible to override the current setup with new parameters. This is necessary, because on updates, the original service data will be overwritten. It automatically creates a directory in /etc/systemd/system, named <name>.service.d, and a file in that directory called override.conf. In the example above, the parameter is added to the existing list of parameters of the After directive.

    For more details please read section Example 2. Overriding vendor settings

    Please keep the following points in mind, regarding if <name>.service is linked or not:

    • If the file is linked from /lib/systemd/system, it is for packaged unit files. They are overwritten when Systemd (or whatever package provides them) is upgraded.

    • If the file originates in /etc/systemd/system, it is for your own and customised unit files. Unit files you place in here override the package-provided file and will not be replaced on upgrade.

    It is recommended to keep things simple and future proof by creating an override file via systemctl edit.

  • Run the following command to apply your changes:

    sudo systemctl daemon-reload
  • Check if <name>.service has been properly added:

    sudo systemctl show <name>.service | grep "After="

    folder.mount should be part of the parameter list.

  • Restart your service by invoking:

    sudo system <name> restart

Disable Transparent Huge Pages (THP)

Transparent Huge Pages should be disabled when using databases. This is applicable when using Redis, as well as MariaDB. For more information read: Why THP (Transparent Huge Pages) are not recommended for Databases.

To disable Transparent Huge Pages, follow these steps:

  • Create in /etc/systemd/system a file like disable-thp.service add the following content:

    Description=Disable Transparent Huge Pages
    ExecStart=/bin/sh -c '/bin/echo never > /sys/kernel/mm/transparent_hugepage/enabled'
    ExecStart=/bin/sh -c '/bin/echo never > /sys/kernel/mm/transparent_hugepage/defrag'
  • Run following command to apply and activate your changes and start it automatically at boot time:

    sudo systemctl daemon-reload
    sudo systemctl enable disable-thp
    sudo service disable-thp start