Common Issues with Zoneminder Installation on Ubuntu

From ZoneMinder Wiki
Jump to navigationJump to search

08AUG16

With some changes “in-the-works” for Ubuntu installs by the hard working volunteers who keep Zoneminder up-to-date, it was recommended that we post fixes/instructions for some issues that continue to surface. This document will likely grow over time but may become obsolete as procedures become better documented.

The upgrade procedures to 1.30.0 have not been fully tested for Ubuntu 15x but have been written with the knowledge of the behavior of the 1.30.0 install package. Post concerns or failures on the forum.

Zoneminder "Master" PPA This is to remind you that software installed from the iconnor zoneminder-master PPA is BETA software and NOT recommended for production systems! BETA software may have some features that do not work correctly!


Zoneminder Database

See Database Tips and Tricks for more topics: http://www.zoneminder.com/wiki/index.php/General_Notes#Database_Tricks.2FTips

Enable and convert MySQL to innodb_file_per_table for Zoneminder

Note: You may wish to convert MyISAM tables to InnoDB tables before you proceed. Upgrading Zoneminder to 1.26 or newer should do this for you.

innodb_file_per_table is by default ON Mysql 5.6.6 and onwards. There is plenty of stuff on Google about pros & cons of innodb_file_per_table. This post details how to enable innodb_file_per_table on an existing database. Because innodb_file_per_table affects new tables only, created after innodb_file_per_table is enabled, we need to recreate old databases to force innodb_file_per_table on old tables and reclaim some disk space.

Become root

sudo su

Backup First Create a dir to take backups:

cd ~

Note: I found it helpful to create a file which contained the MySQL user and password. Otherwise you will have to enter the user and password for every operation.

nano .my.cnf

Enter this content

[client]
user=root
password=mysqlpass

Ctrl+o Enter to save

CTRL+x to exit


Make backup directory

mkdir backup
cd backup

Copy MySQL data files (raw) (If all goes well, we will not need this)

Stop Zoneminder

service zoneminder stop

If you have other services that use MySQL you will want to stop them and possibly Apache.

service mysql stop && cp -ra /var/lib/mysql mysqldata && service mysql start

Take mysqldump As soon as above line completes, take a mysqldump of all databases

mysqldump --routines --events --flush-privileges --all-databases > all-db.sql

Drop Databases Create a sql file to drop all databases EXCEPT mysql database

mysql -e "SELECT DISTINCT CONCAT ('DROP DATABASE ',TABLE_SCHEMA,' ;') FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA <> 'mysql' AND TABLE_SCHEMA <> 'information_schema';" | tail -n+2 > drop.sql

Verify if drop.sql has correct database names and then execute drop.sql queries.

mysql < drop.sql

Verify all InnoDB tables gone

SELECT table_name, table_schema, engine FROM information_schema.tables WHERE engine = 'InnoDB';

Remove InnoDB files Stop mysql server first

service mysql stop

Then

rm /var/lib/mysql/ibdata1 && rm /var/lib/mysql/ib_logfile0 && rm /var/lib/mysql/ib_logfile1

At this point most likely you will have only /var/lib/mysql/mysql directory only.

Enable innodb_file_per_table

Open my.cnf file

nano /etc/mysql/my.cnf

Add following line after [mysqld]

innodb_file_per_table

Ctrl+o Enter to save

CTRL+x to exit

Time to import from mysqldump Start mysql server now

service mysql start

Run mysql import

mysql < all-db.sql

Force mysql_upgrade (to generate performance_schema)

mysql_upgrade --force

That’s All!

Restart Zoneminder (and any other services you have stopped)

service zoneminder start

Check for proper operation and that all your events are present.

When you are satisfied that all is worling well remove the backup directory and password filr=e

cd ~
rm -r backup
rm .my.cnf

You are finished!

This procedure has been adopted from https://rtcamp.com/tutorials/mysql/enable-innodb-file-per-table. Thanks to Rahul Bansal!


Adding Permissions to allow Database Upgrade

Versions of Zoneminder up to and including 1.28.1 in Ubuntu would automatically add the zm database to MySQL. Beginning about August 2015 the development version from the iconnor-master PPA omitted this step becoming like the Debian install process. Manually adding the database to MySQL will be necessary.


To install the zm database (after doing apt-get install zoneminder)

Create Zoneminder database in MySQL (Note: this also creates the default Zoneminder user and permissions in MySQL)

This next step creates a file which contained the MySQL user and password. Otherwise you will have to enter the user and password on the command line which is not secure!

Go to the root directory

cd ~

Create a hidden password file

nano .my.cnf

Enter this content (but use your MySQL root password!)

[client]
user=root
password=(mysqlpass)

Ctrl+o Enter to save

CTRL+x to exit

Create database permissions

mysql -e "grant select,insert,update,delete,create,alter,lock tables on zm.* to 'zmuser'@localhost identified by 'zmpass';"


Remove password file

rm .my.cnf

Continue with the Zoneminder installation

Upgrades

Ubuntu 14.04 with MySQL 5.6 and Zoneminder from PPA

This procedure starts with Ubuntu Server 14.04-3 with LAMP added. Zoneminder is not installed. It is possible to upgrade a system running Zoneminder but it is not covered in this procedure.

Become root

sudo su

Stop MySQL

service mysql stop

Remove MySQL

apt-get purge mysql

Install MySQL 5.6 (will upgrade the MySQL client)

apt-get install mysql-server-5.6

Note: You may need to edit my.cnf and change the value: sql_mode= to sql_mode=NO_ENGINE_SUBSTITUTION

Follow the install instructions: https://wiki.zoneminder.com/Ubuntu_Server_14.04_64-bit_with_Zoneminder_1.29.0_the_easy_way

Ubuntu 14.04 - ZM 1.26.5 or later to 1.30.x or the latest PPA version

Note: this has been tested with ZM 1.30.2 RC1 11FEB17 using the repository ppa:iconnor/zoneminder-master

Become root

sudo su

Add Repository

Note: if you need to remove a repository use: add-apt-repository --remove ppa:iconnor/zoneminder

add-apt-repository ppa:iconnor/zoneminder
apt-get update

Stop Zoneminder

service zoneminder stop

Upgrade the installation

apt-get upgrade  (may return nothing to upgrade)
apt-get dist-upgrade

You will be asked if you want to replace /etc/init.d/zoneminder. Choose "Y"

With ZM 1.30.3 the database will automatically upgrade.

Add back the delay to allow MySQL to start before Zoneminder

nano /etc/init.d/zoneminder

Add sleep 15 as shown:

start() {
     sleep 15
     echo -n "Starting $prog: "

And add the following after mkdir -p "$TMPDIR" && chown www-data:www-data "$TMPDIR" to allow the API to work

mkdir /tmp/zm/cache
mkdir /tmp/zm/cache/models
mkdir /tmp/zm/cache/persistent
chown -R www-data:www-data /tmp/zm/cache

Ctrl+o Enter to save

CTRL+x to exit

Remove symbolic link

rm /etc/apache2/conf-enabled/zoneminder.conf

Set permissions of /etc/zm/zm.conf to root:www-data 740

chmod 740 /etc/zm/zm.conf
chown root:www-data /etc/zm/zm.conf

Enable Zoneminder and rewrite

a2enconf zoneminder
a2enmod rewrite

Start Zoneminder (Note: Zoneminder may have started automatically after the dist-upgrade)

service zoneminder start

Add timezone to PHP

nano /etc/php5/apache2/php.ini

Search for [Date] and make changes as follows for your time zone

[Date]
; Defines the default timezone used by the date functions
; http://php.net/date.timezone
date.timezone = America/New_York

Ctrl+o Enter to save

CTRL+x to exit

Restart Apache

service apache2 restart

Open Zoneminder. Click on Options - Paths

Change PATH_ZMS to /zm/cgi-bin/nph-zms

Reboot the server to set the API tmp directories.

Ubuntu 16.04 - ZM 1.30.x Upgrade

Ubuntu 16.04 contains Zoneminder 1.29.0.

Note: 31MAR17 This was tested with ZM 1.30.2 from ppa:iconnor/zoneminder

To upgrade to Zoneminder 1.30.2

Become root

sudo su

Add Repository. Note:You may need to run: "apt install software-properties-common" if you did the minimal net install of Ubuntu.

add-apt-repository ppa:iconnor/zoneminder

Update package list

apt update

Stop Zoneminder

systemctl stop zoneminder.service

Note: It may be a good idea to backup your database. This procedure was tested with MySQL 5.7 but should work with Mariadb 10.


Upgrade the installation (Zoneminder will upgrade with the "apt upgrade" command. Your database will automatically be upgraded.

apt upgrade 
apt dist-upgrade

Start Zoneminder if necessary

systemctl start zoneminder.service


Ubuntu 14.04 with ZM 1.29.0 from the PPA to Ubuntu 16.04

02MAY16 This upgrade procedure has been tested successfully! It may not be necessary to take a dump of your zm database but I did it just-in-case MySQL messed something up.

Make sure your install is up to date!

sudo apt-get update
sudo apt-get upgrade
sudo apt-get dist-upgrade

Reboot if needed

Login to a console and stop Zoneminder

sudo service zoneminder stop

Take a dump of the Zoneminder (zm) database (the zm.sql will be saved in your "home" directory)

mysqldump -u root -p zm > zm.sql 

Drop the "zm" database

mysqladmin -uroot -p drop zm

Run the Ubuntu upgrade (note: the "-d" is required until Ubuntu 16.04-1 is released)

sudo do-release-upgrade -d

When prompted, overwrite the zoneminder configuration with the package maintainers version.

My upgrade informed me MySQL server upgrade failed. I had to continue and reboot (Ctrl+Alt+Del) the server manually.

Log in and become root

sudo su 

Clean up packages that were not removed when the upgrade failed (may not be needed in the future)

apt-get autoremove

Install PHP7

apt-get install php php-gd libapache2-mod-php php7.0-mysql

Secure MySQL

mysql_secure_installation

Create Zoneminder (zm) database in MySQL

mysqladmin -uroot -p create zm

Import the zm database

mysql -uroot -p zm < zm.sql

Set permissions

mysql -uroot -p -e "grant all on zm.* to 'zmuser'@localhost identified by 'zmpass';"

To better manage the MySQL server I recommend you copy the sample config file and replace the default my.cnf symbolic link.

rm /etc/mysql/my.cnf  (this removes the current symbolic link)
cp /etc/mysql/mysql.conf.d/mysqld.cnf /etc/mysql/my.cnf

To change MySQL settings:

nano /etc/mysql/my.cnf

In the [mysqld] section add the following

sql_mode = NO_ENGINE_SUBSTITUTION

Ctrl+o Enter to save

CTRL+x to exit

Restart MySQL

systemctl restart mysql

Set the Timezone on PHP7

nano /etc/php/7.0/apache2/php.ini

Search for [Date] (Ctrl + w then type Date and press Enter) and make changes as follows for your time zone

[Date]
; Defines the default timezone used by the date functions
; http://php.net/date.timezone
date.timezone = America/New_York

Ctrl+o Enter to save

CTRL+x to exit

Fix Permissions

chown -R www-data:www-data /usr/share/zoneminder/

Fix to allow API to work

nano /etc/apache2/apache2.conf

Change "AllowOverride None" to "AllowOverride All" two places as shown below

<Directory /usr/share>
        AllowOverride All
        Require all granted
</Directory>
<Directory /var/www/>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
</Directory>

Enable and start Zoneminder

systemctl enable zoneminder
service zoneminder start

Restart Apache

service apache2 reload

You should be able to connect to Zoneminder in your web browser!

Ubuntu with ZM 1.28.1 or Earlier Upgrade to Ubuntu 16.04

Upgrade Ubuntu running Zoneminder 1.28.1 or earlier.

05MAY16 This has been run on Ubuntu 14.04 with Zoneminder 1.26.5 and Ubuntu 15.10 with Zoneminder 1.28.1. You have two database options: (1)MySQL 5.7 or (2)Mariadb 10.1. Switching to Mariadb Server is a good thing as it seems to work smoother than MySQL 5.7.

Make sure your install is up to date!

sudo apt-get update
sudo apt-get upgrade
sudo apt-get dist-upgrade

Reboot if needed

Login to a console and stop Zoneminder

sudo service zoneminder stop

Take a dump of the Zoneminder (zm) database (the zm.sql will be saved in your "home" directory)You will need this if you choose Mariadb or if something goes wrong with the upgrade.

mysqldump -u root -p zm > zm.sql 


Run the Ubuntu upgrade (note: the "-d" is required until Ubuntu 16.04-1 is released)

sudo do-release-upgrade -d

When prompted, overwrite the zoneminder configuration with the package maintainers version.

If, for some reason, you get an error message that MySQL did not upgrade, continue to reboot, manually if needed, and run on restart

apt-get autoremove

Log in and become root

sudo su 

Install PHP7

apt-get install php php-gd libapache2-mod-php php7.0-mysql

Use the default MySQL 5.7

To allow the database upgrade to work you will need to edit one sql file

nano /usr/share/zoneminder/db/zm_update-1.28.99.sql

Search for [ALTER IGNORE TABLE] (Ctrl + w then type ALTER IGNORE TABLE and press Enter. Remove IGNORE then

Ctrl+o Enter to save

CTRL+x to exit

To better manage the MySQL server I recommend you copy the sample config file and replace the default my.cnf symbolic link.

rm /etc/mysql/my.cnf  (this removes the current symbolic link)
cp /etc/mysql/mysql.conf.d/mysqld.cnf /etc/mysql/my.cnf

To change MySQL settings:

nano /etc/mysql/my.cnf

In the [mysqld] section add the following

sql_mode = NO_ENGINE_SUBSTITUTION

Ctrl+o Enter to save

CTRL+x to exit

Restart MySQL

systemctl restart mysql

Recommended: Secure MySQL.

mysql_secure_installation

Scroll down to Continue Installation and skip Mariadb

Replace MySQL 5.7 with Mariadb 10

apt-get install mariadb-server

Secure Mariadb (set root password in Mariadb and etc.)

mysql_secure_installation

Create Zoneminder (zm) database in MySQL

mysqladmin -uroot -p create zm

Import the zm database

mysql -uroot -p zm < zm.sql

Set permissions

mysql -uroot -p -e "grant all on zm.* to 'zmuser'@localhost identified by 'zmpass';"

Run the database upgrade

/usr/bin/zmupdate.pl  

To better manage the MySQL server I recommend you copy the sample config file and replace the default my.cnf symbolic link.

rm /etc/mysql/my.cnf  (this removes the current symbolic link)
cp /etc/mysql/mariadb.conf.d/50-server.cnf /etc/mysql/my.cnf

When you want to change MySQL settings:

nano /etc/mysql/my.cnf

Continue Installation

Set permissions

mysql -uroot -p -e "grant all on zm.* to 'zmuser'@localhost identified by 'zmpass';"

Run the database upgrade

/usr/bin/zmupdate.pl  

Set the Timezone on PHP7

nano /etc/php/7.0/apache2/php.ini

Search for [Date] (Ctrl + w then type Date and press Enter) and make changes as follows for your time zone

[Date]
; Defines the default timezone used by the date functions
; http://php.net/date.timezone
date.timezone = America/New_York

Ctrl+o Enter to save

CTRL+x to exit

Fix Permissions

chown -R www-data:www-data /usr/share/zoneminder/

Fix to allow API to work

nano /etc/apache2/apache2.conf

Change "AllowOverride None" to "AllowOverride All" two places as shown below

<Directory /usr/share>
        AllowOverride All
        Require all granted
</Directory>
<Directory /var/www/>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
</Directory>

Enable and start Zoneminder

systemctl enable zoneminder
service zoneminder start

Restart Apache

service apache2 reload

Connect to Zoneminder in your web browser, open Options and set PATH_ZMS to /zm/cgi-bin/nph-zms

Ubuntu 16.10 - ZM 1.30.0 Upgrade to 1.30.x

11FEB17 - Ubuntu 16.10 upgrade to 1.30.x tested with ppa:iconnor/zoneminder-master Become root

sudo su

Add Repository

Note: if you need to remove a repository use: add-apt-repository --remove ppa:iconnor/zoneminder

add-apt-repository ppa:iconnor/zoneminder
apt update

Stop Zoneminder

service zoneminder stop

Upgrade the installation

apt upgrade


Ubuntu Swap File

I have been puzzled for some time as to why Ubuntu will write to the swap file when there is plenty of RAM installed. I've discovered that writing to the swap file is controlled by the "swappiness" setting! By default, Ubuntu uses a swappiness value of 60 which means that when 40% of memory is used, the system will start swapping data from memory to the swap file. Here is some info you can use to check and/or change swappiness:

To check the swappiness value

cat /proc/sys/vm/swappiness

To change the swappiness value A temporary change (lost on reboot) with a swappiness value of 10 can be made with

sudo sysctl vm.swappiness=10

To make a change permanent, edit the configuration file with your favorite editor:

sudo nano /etc/sysctl.conf

Search for vm.swappiness and change its value as desired. If vm.swappiness does not exist, add it to the end of the file like so:

vm.swappiness=10

The more RAM your system has the lower the swappiness value can be. Possibly even "0" My production system with 16 GIG of RAM works well with a value of "10"

Save the file and reboot.

Use Systemd to Mount Internal Drive or NAS

Mount units with Systemd Ubuntu 16.xx or newer.

Thanks to knnniggett for the effort to make this work!

Background One of the first steps the end user must perform after installing ZoneMinder is to dedicate an entire partition, drive, or network share for ZoneMinder's event storage. The reason being, ZoneMinder will, by design, fill up your hard disk, and you don't want to do that to your root volume! The traditional method for accomplishing this is documented here, and that method continues to work well.

However, due to the mass adoption of Systemd we now have a new way to accomplish this, which happens to give us something the former method did not. Read on to learn more!

Systemd natively integrates all sorts of system admin functions that the legacy sys v init didn't have anything to do with. One of those functions is the ability to create mount points, much in the same way one would create a service (a.k.a. unit) file.

Note: This is especially easy on a new system. Just create your mount (/var/cache/zoneminder for Ubuntu) then install Zoneminder. How easy is that?

Collect Information We need to know the following before we get started:

   Find the ZoneMinder events and images folders on your filesystem
   Determine the name of the web account user
   For local volumes, determine the device name of the volume
   For local volumes, determine the uuid of the volume
   For remote volumes, determine the sharing protocol to be used e.g. nfs, smb, etc
   For remote volumes, determine the share name


The location of the events and images folder will vary by Linux distro. You are looking for an actual folder, NOT a symlink! Debian (Ubuntu) distros prefer /var/cache/zoneminder. These folder locations are chosen by each distros' packaging guidelines.


On most Linux distros, you can view the uuid of each of your drives like so:

ls -l /dev/disk/by-uuid

On my system with two hard drives, the items above are as follows:

   /var/cache/zoneminder
   www-data
   /dev/sdb1
   7ec50da2-fc67-4ade-89c7-6047710afae2

Your configuration will be different. Options to connect to a network share will be noted herein.

Migrate existing data I'm not going to get into the details of this since the necessary steps will vary with each system. What you need to do is migrate whatever happens to be in the ZoneMinder events and images folders over to the new partition, disk, or network share. You do this by mounting the target from the command line via the normal fashion to a temporary folder, issue the appropriate move commands, and finally unmount the target.

This procedure has been modified for Ubuntu 16.xx and later

Log in and become root

sudo su

Stop Zoneminder

service zoneminder stop

Note: The next step will remove the Zoneminder events and images directories! This is safe to do if your system is new and you have not added cameras that have recorded events.

Remove or events and images directories

rm -r /var/cache/zoneminder/events
rm -r /var/cache/zoneminder/images

If you have an operational system you may rename the directories (mv /home/user/oldname /home/user/newname) then move the data to the new drive directories.

mv /var/cache/zoneminder/events /var/cache/zoneminder/oldevents
mv /var/cache/zoneminder/images /var/cache/zoneminder/oldimages

Create the first Systemd Mount Unit You will be creating a total of three mount units. The first mount unit mounts the drive or partition to your system. To do that create a new folder. I prefer to put mount points under /mnt and give the name of the subfolder the same name as the device or share. In my case, that would be sdb1.

mkdir /mnt/sdb1 

When creating mount units with systemd, the filename describing the mount point has to be named in a specific manner. Since the folder I just created is at /mnt/sdb1, one has to name the mount unit mnt-sdb1.mount. Create that file in the /etc/systemd/system folder.

nano /etc/systemd/system/mnt-sdb1.mount

Note: the next step you use is dependent upon the use of an internal drive or a network drive. Read carefully!


Internal drive: add the following contents, changing the path and uuid to match that of your system:

# systemd mount unit for ZoneMinder event storage
 
[Unit]
Description=systemd mount unit for ZoneMinder event storage
Before=zoneminder
 
[Mount]
What=/dev/disk/by-uuid/7ec50da2-fc67-4ade-89c7-6047710afae2
Where=/mnt/sdb1
Type=ext4
Options=defaults,noatime,commit=120,data=writeback
 
[Install]
WantedBy=multi-user.target

Save and close the editor.

Go to *Next


Network drive/NAS SMB or CIFS Mount

Note: your network share must recognize symbolic links Add the following contents, changing the What= and Options= to match that of your system:

# systemd mount unit for ZoneMinder event storage
 
[Unit]
Description=systemd mount unit for ZoneMinder event storage
After=networking.service
Before=zoneminder
 
[Mount]
What=//192.168.50.4/zm
Where=/mnt/sdb1
Type=
Options=credentials=/root/.smbcredentials,uid=33,gid=33,rw
 
[Install]
WantedBy=multi-user.target

Save and close the editor.

Install smbclient if you are mounting a Windows or SMB share

apt install smbclient cifs-utils

Create a file for your remote servers logon credential, This is the user and password you would use to access the Windows share and is located in the /root directory.

nano ~/.smbcredentials

Enter your Windows username, password and workgroup or domain in the file:

username=msusername
password=mspassword
domain=workgroup

Save the file, exit the editor.

Change the permissions of the file to prevent unwanted access to your credentials:

chmod 600 ~/.smbcredentials

  • Next

Now enable and start the unit:

systemctl enable mnt-sdb1.mount
systemctl start mnt-sdb1.mount 

Make and Set Folder Permissions We've created our primary mount point, but we aren't done yet. First, let's create some folders and set the correct permissions:

mkdir -p /mnt/sdb1/zoneminder/events
mkdir -p /mnt/sdb1/zoneminder/images
chown -R www-data:www-data /mnt/sdb1/zoneminder
   

Remember that, if you are not running a Ubuntu distro, the web user account might be named something other than www-data on your system.

Create two Systemd Bind Mount Units With the events and images folders created, we want to create two mount units, which bind mount those folders into the desired places.

Create the file /etc/systemd/system/var-cache-zoneminder-events.mount

nano /etc/systemd/system/var-cache-zoneminder-events.mount

Next add the following content to it:

# systemd bind mount unit for ZoneMinder event storage

[Unit]
Description=systemd bind mount unit for ZoneMinder event storage
After=mnt-sdb1.mount
Requires=mnt-sdb1.mount

[Mount]
What=/mnt/sdb1/zoneminder/events
Where=/var/cache/zoneminder/events
Type=none
Options=bind

[Install]
WantedBy=local-fs.target

Save and close the editor.

Now enable and start the unit:

systemctl enable  var-cache-zoneminder-events.mount
systemctl start  var-cache-zoneminder-events.mount 

We now need to do the same thing to the images folder. Create the file /etc/systemd/system/var-lib-zoneminder-images.mount

nano /etc/systemd/system/var-cache-zoneminder-images.mount

Next add the following content to it:

# systemd bind mount unit for ZoneMinder image storage

[Unit]
Description=systemd bind mount unit for ZoneMinder image storage
After=mnt-sdb1.mount
Requires=mnt-sdb1.mount

[Mount]
What=/mnt/sdb1/zoneminder/images
Where=/var/cache/zoneminder/images
Type=none
Options=bind

[Install]
WantedBy=local-fs.target

Save and close the editor.

Now enable and start the unit:

systemctl enable var-cache-zoneminder-images.mount
systemctl start var-cache-zoneminder-images.mount

Now reboot and verify all three mount points were successful:

Bonus Points. Leverage the Power of Systemd If you recall, I mentioned that using systemd to manage your mount points has an advantage over the previous method. With your mount points configured with systemd, you can easily prevent ZoneMinder from starting, should the mount point fail for any reason. Anyone who has ever started ZoneMinder without realizing there was a problem reading from the events folder can tell you what happens when this occurs. You lose all your events, and yes this is by design.

To prevent that from happening we need to modify our zoneminder service file. But first make a copy just in case..

cp /lib/systemd/system/zoneminder.service /lib/systemd/system/zoneminder.service.sav
nano /lib/systemd/system/zoneminder.service

Add entries to the After= and Requires=

# ZoneMinder systemd unit file
# This file is intended to work with Debian distributions

[Unit]
Description=ZoneMinder CCTV recording and surveillance system
After=network.target mysql.service mnt-sdb1.mount var-cache-zoneminder-images.mount var-cache-zoneminder-events.mount
# Remarked out so that it will start ZM on machines that don't have mysql installed
#Requires=mysql.service
Requires=mnt-sdb1.mount var-cache-zoneminder-images.mount var-cache-zoneminder-events.mount

[Service]
#User=www-data
Type=forking
ExecStart=/usr/bin/zmpkg.pl start
ExecReload=/usr/bin/zmpkg.pl restart
ExecStop=/usr/bin/zmpkg.pl stop
PIDFile=/var/run/zm/zm.pid
Restart=on-abnormal
[Install]
WantedBy=multi-user.target

Save and close the editor.


Now issue a daemon reload to tell systemd to pick up the change:

systemctl daemon-reload

Should the mount point fail during startup, systemd will prevent the ZoneMinder service from starting with a message stating a failed dependency. Your events are saved from deletion!

Oh, restart Zoneminder

service zoneminder start

Zoneminder Multi Server on Ubuntu

I developed this procedure using two Ubuntu 16.04 servers set up with Zoneminder 1.30.4 per the instructions in the WIKI (https://wiki.zoneminder.com/Ubuntu_Server_16.04_64-bit_with_Zoneminder_1.30.4_the_easy_way). I used the default MySQL server but Mariadb can also be used with a couple of modifications.

For this procedure my servers were:

Name - u2 IP address 192.168.50.2 This is the storage and database server

Name - u3 IP address 192.168.50.3 This is the remote zoneminder server

It is possible to use a separate database server and a seperate storage server. Your storage server does need to have a Linux file system on it as Zoneminder uses symbolic links and a Windows (NTFS, FAT32) does not know about symbolic links. You can also use Samba to mount the storage drive but this is written to use NFS.

"Official" Multi Server Install is at: http://zoneminder.readthedocs.io/en/stable/installationguide/multiserver.html

I use Putty from a Windows PC to connect to the servers as I can copy the commands from this web page and paste them into Putty. This is the easy way for me!


Database and Storage Server

Stop the Zoneminder service

service zoneminder stop

Edit the my.conf. Change bind-address to: bind-address = 0.0.0.0

nano /etc/mysql/my.cnf

Ctrl+o Enter to save

CTRL+x to exit

Restart MySQL

systemctl restart mysql

Grant remote user access (Note: for additional remote servers add them using their IP address)

mysql -uroot -p -e "grant all on zm.* to 'zmuser'@192.168.50.2 identified by 'zmpass';"
mysql -uroot -p -e "grant all on zm.* to 'zmuser'@192.168.50.3 identified by 'zmpass';"

Install the NFS server

apt install nfs-kernel-server

Add the export directory

nano /etc/exports

Add to the end of the file (Note: for additional remote servers add additional lines using their IP address)

/var/cache/zoneminder/events 192.168.50.3(rw,sync,no_subtree_check)

Ctrl+o Enter to save

CTRL+x to exit

Restart the NFS server

systemctl restart nfs-kernel-server

Edit the Zoneminder config file

nano /etc/zm/zm.conf

Change these two lines

ZM_DB_HOST=192.168.50.2

ZM_SERVER_HOST=u2

Ctrl+o Enter to save

CTRL+x to exit

Start Zoneminder service

service zoneminder start

Local Zoneminder Server(s)

If you have more than one remote server repeat this procedure on each one adjusting the host name for each.

Stop Zoneminder

service zoneminder stop

Edit the Zoneminder config file

nano /etc/zm/zm.conf

Change these two lines

ZM_DB_HOST=192.168.50.2

ZM_SERVER_HOST=u3

Ctrl+o Enter to save

CTRL+x to exit

Install NFS client

apt install nfs-client
modprobe nfs
echo NFS | tee -a /etc/modules

Create a systemd file for the mount

nano /etc/systemd/system/var-cache-zoneminder-events.mount

Copy and paste everything between the lines into the systemd file


  1. systemd mount unit for ZoneMinder event storage

[Unit] Description=systemd mount unit for ZoneMinder event storage After=network.target Before=zoneminder

[Mount] What=192.168.50.2:/var/cache/zoneminder/events Where=/var/cache/zoneminder/events Type=nfs

[Install] WantedBy=multi-user.target


Ctrl+o Enter to save

CTRL+x to exit

Save and start the mount

systemctl enable var-cache-zoneminder-events.mount
systemctl start var-cache-zoneminder-events.mount

Start the Zoneminder service

service zoneminder start

In a web browser open Zoneminder on one of the servers. (Note: you may want to check all your servers to insure Zoneminder is started on each. You may have to reboot each server to get Zoneminder to start)

Click on Options - Servers - Add New Server

In the Name block add a name of one of your servers. In my example the first server name is u2.

In the Hostname block enter the IP address of the first server. In my example it is 192.168.50.2

Click Save

Add the rest of your servers in the same way. Close the Options window when your servers have been added.

When you add a camera make sure you select a server you want to add it to (Source - General - Server dropdown) All of your cameras can be managed and viewed from one server Zoneminder screen.