<p>We have a multisite setup. We're wanting to use the same codebase, but deploy to two different servers with some of the sites on server 1 and the rest on server 2.</p>
<p>For example:</p>
<ul>
<li>Server 1
<ul>
<li>Site 1 [Primary site]</li>
<li>Site 2</li>
</ul>
</li>
<li>Server 2
<ul>
<li>Site 3 [Primary site]</li>
<li>Site 4</li>
</ul>
</li>
</ul>
<p>Currently sites 1-4 are all enabled and Site 1 is set as the Primary site.</p>
<p>On deployment we want to enable/disable the relevant sites, so that within the Admin Control Panel users will only see the sites available on that server. Similarly, we want to set the primary site for each server.</p>
<p>We're trying to do this programmatically so that it can be run as part of our deployment scripts. However, when we call <code>saveSite</code> although it is returning <code>true</code> the database and config files are not actually updating.</p>
<p>For example, to update the primary site we're trying the following:</p>
<pre class="lang-php prettyprint-override"><code>$site = Craft::$app-&gt;sites-&gt;getSiteByHandle('site3');
$site-&gt;primary = true;
Craft::$app-&gt;sites-&gt;saveSite($site); // returns true
</code></pre>
<p>Looking at the Craft code, it looks like this should be moving the primary site flag from the current primary site to site3, but this isn't happening.</p>
<p>What do we need to do in order to make this update correctly?</p>

CC BY-SA 4.0
2021-12-17T23:03:23.550Z
Running into the same issue in my codebase. Haven't had any success either. Running `$app->sites->saveSite` doesn't seem to actually save anything.
tekstrand
CC BY-SA 4.0
2023-12-12T13:27:05.333Z
@drmonkeyninja did you eventually solve it? I have a similar task and looking for an solution. if it worked with the code above, how did you put into to your deployment scripts? via a module? Thanks!
Henrik
<multi-site><deployment>
How to change the primary site on a multi-site setup?
# How to change the primary site on a multi-site setup?

(Asked by: username_0 on 2021-09-21)

We have a multisite setup. We're wanting to use the same codebase, but deploy to two different servers with some of the sites on server 1 and the rest on server 2.
For example:

Server 1

Site 1 [Primary site]
Site 2


Server 2

Site 3 [Primary site]
Site 4



Currently sites 1-4 are all enabled and Site 1 is set as the Primary site.
On deployment we want to enable/disable the relevant sites, so that within the Admin Control Panel users will only see the sites available on that server. Similarly, we want to set the primary site for each server.
We're trying to do this programmatically so that it can be run as part of our deployment scripts. However, when we call saveSite although it is returning true the database and config files are not actually updating.
For example, to update the primary site we're trying the following:
$site = Craft::$app->sites->getSiteByHandle('site3');
$site->primary = true;
Craft::$app->sites->saveSite($site); // returns true

Looking at the Craft code, it looks like this should be moving the primary site flag from the current primary site to site3, but this isn't happening.
What do we need to do in order to make this update correctly?

**Tags:** multi-site, deployment
> **Comments:**
> **username_1**: Running into the same issue in my codebase. Haven't had any success either. Running `$app->sites->saveSite` doesn't seem to actually save anything.  
> **username_2**: @drmonkeyninja did you eventually solve it? I have a similar task and looking for an solution. if it worked with the code above, how did you put into to your deployment scripts? via a module? Thanks!  

---

## 0 answers

