|
| 1 | +# Config for NextJS |
| 2 | + |
| 3 | +```{note} |
| 4 | +NextJS is not officially supported on the Hypernode Platform. So please use this at your own risk,. |
| 5 | +``` |
| 6 | + |
| 7 | +Configuration to use as Hypernode Deploy deploy.php for a NextJS webserver application: |
| 8 | + |
| 9 | +```php |
| 10 | +<?php |
| 11 | + |
| 12 | +declare(strict_types=1); |
| 13 | + |
| 14 | +namespace Hypernode\DeployConfiguration; |
| 15 | + |
| 16 | +use function Deployer\run; |
| 17 | +use function Deployer\task; |
| 18 | +use function Deployer\test; |
| 19 | +use function Deployer\within; |
| 20 | + |
| 21 | +task('node:install', static function () { |
| 22 | + run('npm ci'); |
| 23 | +}); |
| 24 | + |
| 25 | +task('node:build', static function () { |
| 26 | + run('npm run build'); |
| 27 | +}); |
| 28 | + |
| 29 | +task('deploy:pm2:install', static function() { |
| 30 | + if (!test('grep -q "/data/web/.local/bin" ~/.bashrc')) { |
| 31 | + run('echo "export PATH=/data/web/.local/bin:$PATH" >> ~/.bashrc'); |
| 32 | + run('source ~/.bashrc'); |
| 33 | + } |
| 34 | + |
| 35 | + run('npm install -g pm2 --prefix /data/web/.local'); |
| 36 | +}); |
| 37 | + |
| 38 | +task('deploy:pm2:restart', static function() { |
| 39 | + within('{{release_path}}', function () { |
| 40 | + run('pm2 startOrRestart ecosystem.config.js --env prod'); |
| 41 | + }); |
| 42 | +}); |
| 43 | + |
| 44 | +$configuration = new Configuration(); |
| 45 | + |
| 46 | +$configuration->setPlatformConfigurations([ |
| 47 | + new PlatformConfiguration\NginxConfiguration('etc/nginx'), |
| 48 | + new PlatformConfiguration\HypernodeSettingConfiguration('supervisor_enabled', 'False'), |
| 49 | + new PlatformConfiguration\HypernodeSettingConfiguration('rabbitmq_enabled', 'False'), |
| 50 | + new PlatformConfiguration\HypernodeSettingConfiguration('elasticsearch_enabled', 'False'), |
| 51 | + new PlatformConfiguration\HypernodeSettingConfiguration('opensearch_enabled', 'False'), |
| 52 | + new PlatformConfiguration\HypernodeSettingConfiguration('varnish_enabled', 'False'), |
| 53 | + new PlatformConfiguration\HypernodeSettingConfiguration('nodejs_version', '20'), |
| 54 | +]); |
| 55 | + |
| 56 | +$configuration->addBuildTask('node:install'); |
| 57 | +$configuration->addBuildTask('node:build'); |
| 58 | + |
| 59 | +$configuration->addDeployTask('deploy:pm2:install'); |
| 60 | +$configuration->addDeployTask('deploy:pm2:restart'); |
| 61 | + |
| 62 | +$configuration->setSharedFiles([ |
| 63 | + '.env' |
| 64 | +]); |
| 65 | + |
| 66 | +$configuration->setDeployExclude([ |
| 67 | + './.git', |
| 68 | + './deploy.php', |
| 69 | + '.gitignore', |
| 70 | + './etc' |
| 71 | +]); |
| 72 | + |
| 73 | +$productionStage = $configuration->addStage('production', 'my-next-application.nl'); |
| 74 | +$productionStage->addServer('app.hypernode.io'); |
| 75 | + |
| 76 | +$acceptanceStage = $configuration->addStage('acceptance', 'my-next-application.nl'); |
| 77 | +$acceptanceStage->addBrancherServer('app') |
| 78 | + ->setLabels(['stage=acceptance']); |
| 79 | + |
| 80 | +return $configuration; |
| 81 | +``` |
| 82 | + |
| 83 | +This will: |
| 84 | + |
| 85 | +- Configure the Hypernode settings optimal for the least load of non-used applications and servers. |
| 86 | +- Install [PM2](https://pm2.keymetrics.io/) if not present on the server. |
| 87 | +- Sets up a vhost for application. |
| 88 | +- Sets up the nginx configuration to proxy to the NextJS server. |
| 89 | + |
| 90 | +You will still need to add the nginx configuration file, so create the `./etc/nginx/server.headless.conf` file: |
| 91 | + |
| 92 | +```nginx |
| 93 | +location / { |
| 94 | + proxy_pass http://localhost:3000; |
| 95 | + proxy_http_version 1.1; |
| 96 | + proxy_set_header Upgrade $http_upgrade; |
| 97 | + proxy_set_header Connection 'upgrade'; |
| 98 | + proxy_set_header Host $host; |
| 99 | + proxy_set_header X-Real-IP $remote_addr; |
| 100 | + proxy_cache_bypass $http_upgrade; |
| 101 | +
|
| 102 | + # Optional: set maximum response time to 60 seconds |
| 103 | + proxy_read_timeout 60s; |
| 104 | + proxy_connect_timeout 60s; |
| 105 | +} |
| 106 | +
|
| 107 | +# Optional: specific location for static assets if they are served by Next.js |
| 108 | +location /_next/ { |
| 109 | + proxy_pass http://localhost:3000; |
| 110 | + proxy_cache_bypass $http_upgrade; |
| 111 | +} |
| 112 | +``` |
| 113 | + |
| 114 | +And to save configuration about pm2, add the `ecosystem.config.js` file: |
| 115 | + |
| 116 | +```js |
| 117 | +module.exports = { |
| 118 | + apps: [ |
| 119 | + { |
| 120 | + name: 'my-next-app', |
| 121 | + exec_mode: 'cluster', |
| 122 | + instances: 'max', // Or a number of instances |
| 123 | + script: 'node_modules/next/dist/bin/next', |
| 124 | + args: 'start' |
| 125 | + } |
| 126 | + ] |
| 127 | + } |
| 128 | +``` |
0 commit comments