An open-source, self-hostable PaaS alternative to Vercel, Heroku & Netlify that lets you easily deploy static sites, databases, full-stack applications and 360+ one-click services on your own servers.
PHP coolifydatabasesdeploymentdockerdocker-composeinertiajslaravelmariadbmysqlnextjsnodejsphppostgresredisself-hostedself-hostingserverstatic-sitesveltesvelte5
fix: proxy check for ports, do not kill anything listening on port 80/443
Andras Bacsai59eae3a44e72
diff --git a/app/Actions/Proxy/CheckProxy.php b/app/Actions/Proxy/CheckProxy.phpnew file mode 100644index 0000000000..84dc4e955d--- /dev/null+++ b/app/Actions/Proxy/CheckProxy.php@@ -0,0 +1,42 @@+<?php++namespace App\Actions\Proxy;++use App\Enums\ProxyTypes;+use App\Models\Server;+use Illuminate\Support\Str;+use Lorisleiva\Actions\Concerns\AsAction;+use Spatie\Activitylog\Models\Activity;++class CheckProxy+{+ use AsAction;+ public function handle(Server $server)+ {+ if (!$server->isProxyShouldRun()) {+ throw new \Exception("Proxy should not run");+ }+ $status = getContainerStatus($server, 'coolify-proxy');+ if ($status === 'running') {+ $server->proxy->set('status', 'running');+ $server->save();+ return 'OK';+ }+ $ip = $server->ip;+ if ($server->id === 0) {+ $ip = 'host.docker.internal';+ }++ $connection = @fsockopen($ip, '80');+ $connection = @fsockopen($ip, '443');+ $port80 = is_resource($connection) && fclose($connection);+ $port443 = is_resource($connection) && fclose($connection);+ ray($ip);+ if ($port80) {+ throw new \Exception("Port 80 is in use.<br>You must stop the process using this port.<br>Docs: <a target='_blank' href='https://coolify.io/docs'>https://coolify.io/docs</a> <br> Discord: <a target='_blank' href='https://coollabs.io/discord'>https://coollabs.io/discord</a>");+ }+ if ($port443) {+ throw new \Exception("Port 443 is in use.<br>You must stop the process using this port.<br>Docs: <a target='_blank' href='https://coolify.io/docs'>https://coolify.io/docs</a> <br> Discord: <a target='_blank' href='https://coollabs.io/discord'>https://coollabs.io/discord</a>>");+ }+ }+}diff --git a/app/Actions/Proxy/StartProxy.php b/app/Actions/Proxy/StartProxy.phpindex d6ae7eec85..14b7fefb53 100644--- a/app/Actions/Proxy/StartProxy.php+++ b/app/Actions/Proxy/StartProxy.php@@ -2,7 +2,6 @@namespace App\Actions\Proxy;-use App\Enums\ProxyTypes;use App\Models\Server;use Illuminate\Support\Str;use Lorisleiva\Actions\Concerns\AsAction;@@ -11,56 +10,49 @@ use Spatie\Activitylog\Models\Activity;class StartProxy{use AsAction;- public function handle(Server $server, bool $async = true): Activity|string+ public function handle(Server $server, bool $async = true): string|Activity{- $commands = collect([]);- $proxyType = $server->proxyType();- if ($proxyType === ProxyTypes::NONE->value) {- return 'OK';- }- $proxy_path = get_proxy_path();- $configuration = CheckConfiguration::run($server);- if (!$configuration) {- throw new \Exception("Configuration is not synced");- }- SaveConfiguration::run($server, $configuration);- $docker_compose_yml_base64 = base64_encode($configuration);- $server->proxy->last_applied_settings = Str::of($docker_compose_yml_base64)->pipe('md5')->value;- $server->save();+ try {+ CheckProxy::run($server);- $commands = $commands->merge([- "apt-get update > /dev/null 2>&1 || true",- "command -v lsof >/dev/null || echo '####### Installing lsof.'",- "command -v lsof >/dev/null || apt install -y lsof",- "command -v lsof >/dev/null || command -v fuser >/dev/null || apt install -y psmisc",- "mkdir -p $proxy_path && cd $proxy_path",- "echo '####### Creating Docker Compose file.'",- "echo '####### Pulling docker image.'",- 'docker compose pull',- "echo '####### Stopping existing coolify-proxy.'",- "docker compose down -v --remove-orphans > /dev/null 2>&1",- "command -v fuser >/dev/null || command -v lsof >/dev/null || echo '####### Could not kill existing processes listening on port 80 & 443. Please stop the process holding these ports...'",- "command -v lsof >/dev/null && lsof -nt -i:80 | xargs -r kill -9 || true",- "command -v lsof >/dev/null && lsof -nt -i:443 | xargs -r kill -9 || true",- "command -v fuser >/dev/null && fuser -k 80/tcp || true",- "command -v fuser >/dev/null && fuser -k 443/tcp || true",- "systemctl disable nginx > /dev/null 2>&1 || true",- "systemctl disable apache2 > /dev/null 2>&1 || true",- "systemctl disable apache > /dev/null 2>&1 || true",- "echo '####### Starting coolify-proxy.'",- 'docker compose up -d --remove-orphans',- "echo '####### Proxy installed successfully.'"- ]);- $commands = $commands->merge(connectProxyToNetworks($server));- if ($async) {- $activity = remote_process($commands, $server);- return $activity;- } else {- instant_remote_process($commands, $server);- $server->proxy->set('status', 'running');- $server->proxy->set('type', $proxyType);+ $proxyType = $server->proxyType();+ $commands = collect([]);+ $proxy_path = get_proxy_path();+ $configuration = CheckConfiguration::run($server);+ if (!$configuration) {+ throw new \Exception("Configuration is not synced");+ }+ SaveConfiguration::run($server, $configuration);+ $docker_compose_yml_base64 = base64_encode($configuration);+ $server->proxy->last_applied_settings = Str::of($docker_compose_yml_base64)->pipe('md5')->value;$server->save();- return 'OK';+ $commands = $commands->merge([+ "mkdir -p $proxy_path && cd $proxy_path",+ "echo 'Creating required Docker Compose file.'",+ "echo 'Pulling docker image.'",+ 'docker compose pull',+ "echo 'Stopping existing coolify-proxy.'",+ "docker compose down -v --remove-orphans > /dev/null 2>&1",+ "echo 'Starting coolify-proxy.'",+ 'docker compose up -d --remove-orphans',+ "echo 'Proxy started successfully.'"+ ]);+ $commands = $commands->merge(connectProxyToNetworks($server));+ if ($async) {+ $activity = remote_process($commands, $server);+ return $activity;+ } else {+ instant_remote_process($commands, $server);+ $server->proxy->set('status', 'running');+ $server->proxy->set('type', $proxyType);+ $server->save();+ return 'OK';+ }+ } catch(\Throwable $e) {+ ray($e);+ throw $e;}++}}diff --git a/app/Http/Livewire/Server/Proxy/Deploy.php b/app/Http/Livewire/Server/Proxy/Deploy.phpindex ad5884e186..44ff4b917e 100644--- a/app/Http/Livewire/Server/Proxy/Deploy.php+++ b/app/Http/Livewire/Server/Proxy/Deploy.php@@ -2,6 +2,7 @@namespace App\Http\Livewire\Server\Proxy;+use App\Actions\Proxy\CheckProxy;use App\Actions\Proxy\StartProxy;use App\Models\Server;use Livewire\Component;@@ -11,18 +12,29 @@ class Deploy extends Componentpublic Server $server;public bool $traefikDashboardAvailable = false;public ?string $currentRoute = null;- protected $listeners = ['proxyStatusUpdated', 'traefikDashboardAvailable', 'serverRefresh' => 'proxyStatusUpdated'];+ protected $listeners = ['proxyStatusUpdated', 'traefikDashboardAvailable', 'serverRefresh' => 'proxyStatusUpdated', "checkProxy", "startProxy"];- public function mount() {+ public function mount()+ {$this->currentRoute = request()->route()->getName();}- public function traefikDashboardAvailable(bool $data) {+ public function traefikDashboardAvailable(bool $data)+ {$this->traefikDashboardAvailable = $data;}public function proxyStatusUpdated(){$this->server->refresh();}+ public function checkProxy() {+ try {+ CheckProxy::run($this->server);+ $this->emit('startProxyPolling');+ $this->emit('proxyChecked');+ } catch (\Throwable $e) {+ return handleError($e, $this);+ }+ }public function startProxy(){try {diff --git a/app/Http/Livewire/Server/Proxy/Modal.php b/app/Http/Livewire/Server/Proxy/Modal.phpindex ef99489107..2674abe3d1 100644--- a/app/Http/Livewire/Server/Proxy/Modal.php+++ b/app/Http/Livewire/Server/Proxy/Modal.php@@ -11,8 +11,6 @@ class Modal extends Componentpublic function proxyStatusUpdated(){- $this->server->proxy->set('status', 'running');- $this->server->save();$this->emit('proxyStatusUpdated');}}diff --git a/app/Http/Livewire/Server/Proxy/Status.php b/app/Http/Livewire/Server/Proxy/Status.phpindex 5cfd220823..90deb14554 100644--- a/app/Http/Livewire/Server/Proxy/Status.php+++ b/app/Http/Livewire/Server/Proxy/Status.php@@ -2,6 +2,7 @@namespace App\Http\Livewire\Server\Proxy;+use App\Actions\Proxy\CheckProxy;use App\Jobs\ContainerStatusJob;use App\Models\Server;use Livewire\Component;@@ -9,26 +10,49 @@ use Livewire\Component;class Status extends Component{public Server $server;+ public bool $polling = false;+ public int $numberOfPolls = 0;- protected $listeners = ['proxyStatusUpdated'];+ protected $listeners = ['proxyStatusUpdated', 'startProxyPolling'];+ public function startProxyPolling()+ {+ $this->polling = true;+ }public function proxyStatusUpdated(){$this->server->refresh();}- public function getProxyStatus()+ public function checkProxy(bool $notification = false){try {- dispatch_sync(new ContainerStatusJob($this->server));+ if ($this->polling) {+ if ($this->numberOfPolls >= 10) {+ $this->polling = false;+ $this->numberOfPolls = 0;+ $notification && $this->emit('error', 'Proxy is not running.');+ return;+ }+ $this->numberOfPolls++;+ }+ CheckProxy::run($this->server);$this->emit('proxyStatusUpdated');+ if ($this->server->proxy->status === 'running') {+ $this->polling = false;+ $notification && $this->emit('success', 'Proxy is running.');+ } else {+ $notification && $this->emit('error', 'Proxy is not running.');+ }} catch (\Throwable $e) {return handleError($e, $this);}}- public function getProxyStatusWithNoti()+ public function getProxyStatus(){- if ($this->server->isFunctional()) {- $this->emit('success', 'Refreshed proxy status.');- $this->getProxyStatus();+ try {+ dispatch_sync(new ContainerStatusJob($this->server));+ $this->emit('proxyStatusUpdated');+ } catch (\Throwable $e) {+ return handleError($e, $this);}}}diff --git a/app/Jobs/ContainerStatusJob.php b/app/Jobs/ContainerStatusJob.phpindex 9b4ef56994..00e6b31fb2 100644--- a/app/Jobs/ContainerStatusJob.php+++ b/app/Jobs/ContainerStatusJob.php@@ -27,11 +27,6 @@ class ContainerStatusJob implements ShouldQueue, ShouldBeEncryptedpublic $tries = 1;public $timeout = 120;- public function __construct(public Server $server)- {-- }-public function middleware(): array{return [(new WithoutOverlapping($this->server->uuid))->dontRelease()];@@ -41,6 +36,11 @@ class ContainerStatusJob implements ShouldQueue, ShouldBeEncrypted{return $this->server->uuid;}++ public function __construct(public Server $server)+ {+ }+public function handle(){try {diff --git a/bootstrap/helpers/proxy.php b/bootstrap/helpers/proxy.phpindex 81cb92c496..08f134d151 100644--- a/bootstrap/helpers/proxy.php+++ b/bootstrap/helpers/proxy.php@@ -22,7 +22,7 @@ function connectProxyToNetworks(Server $server)}$commands = $networks->map(function ($network) {return [- "echo '####### Connecting coolify-proxy to $network network...'",+ "echo 'Connecting coolify-proxy to $network network...'","docker network ls --format '{{.Name}}' | grep '^$network$' >/dev/null || docker network create --attachable $network >/dev/null","docker network connect $network coolify-proxy >/dev/null 2>&1 || true",];diff --git a/resources/views/livewire/server/proxy/deploy.blade.php b/resources/views/livewire/server/proxy/deploy.blade.phpindex 41444fde4d..933133408f 100644--- a/resources/views/livewire/server/proxy/deploy.blade.php+++ b/resources/views/livewire/server/proxy/deploy.blade.php@@ -20,8 +20,9 @@@endif<x-forms.button isModal noStyle modalId="stopProxy"class="flex items-center gap-2 cursor-pointer hover:text-white text-neutral-400">- <svg xmlns="http://www.w3.org/2000/svg" class="w-5 h-5 text-error" viewBox="0 0 24 24" stroke-width="2"- stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">+ <svg xmlns="http://www.w3.org/2000/svg" class="w-5 h-5 text-error" viewBox="0 0 24 24"+ stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round"+ stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"></path><path d="M6 5m0 1a1 1 0 0 1 1 -1h2a1 1 0 0 1 1 1v12a1 1 0 0 1 -1 1h-2a1 1 0 0 1 -1 -1z"></path><path d="M14 5m0 1a1 1 0 0 1 1 -1h2a1 1 0 0 1 1 1v12a1 1 0 0 1 -1 1h-2a1 1 0 0 1 -1 -1z"></path>@@ -30,7 +31,7 @@</x-forms.button></div>@else- <button wire:click='startProxy' onclick="startProxy.showModal()"+ <button onclick="checkProxy()"class="flex items-center gap-2 cursor-pointer hover:text-white text-neutral-400"><svg xmlns="http://www.w3.org/2000/svg" class="w-5 h-5 text-warning" viewBox="0 0 24 24"stroke-width="1.5" stroke="currentColor" fill="none" stroke-linecap="round"@@ -42,4 +43,13 @@</button>@endif@endif+ <script>+ function checkProxy() {+ Livewire.emit('checkProxy')+ }+ Livewire.on('proxyChecked', () => {+ startProxy.showModal();+ Livewire.emit('startProxy');+ })+ </script></div>diff --git a/resources/views/livewire/server/proxy/status.blade.php b/resources/views/livewire/server/proxy/status.blade.phpindex b477752d7b..65eebe0370 100644--- a/resources/views/livewire/server/proxy/status.blade.php+++ b/resources/views/livewire/server/proxy/status.blade.php@@ -1,6 +1,6 @@<div>@if ($server->isFunctional())- <div class="flex gap-2" x-init="$wire.getProxyStatus">+ <div class="flex gap-2" @if ($polling) wire:poll.2000ms='checkProxy' @endif>@if (data_get($server, 'proxy.status') === 'running')<x-status.running status="Proxy Running" />@elseif (data_get($server, 'proxy.status') === 'restarting')@@ -8,7 +8,9 @@@else<x-status.stopped status="Proxy Stopped" />@endif- <x-forms.button wire:click='getProxyStatusWithNoti'>Refresh </x-forms.button>+ @if (data_get($server, 'proxy.status') === 'running')+ <x-forms.button wire:click='checkProxy(true)'>Refresh</x-forms.button>+ @endif</div>@endif</div>