You can either increase the time spans for timeouts, on larger systems it is better to start the upgrade from command line where normally no timeouts apply. E.g. for Debian:
sudo -u www-data php /var/www/nextcloud/updater/updater.phar
After that you have perhaps to start the occ upgrade command as well and turn off maintenance mode:
sudo -u www-data php /var/www/nextcloud/occ upgrade
sudo -u www-data php /var/www/nextcloud/occ maintenance:mode --off