在 Microsoft IIS 中托管 SvelteKit 项目

huangapple go评论195阅读模式
英文:

Host SvelteKit project in Microsoft IIS

问题

我一直在尝试将Svelte应用部署到我公司提供的IIS服务器上。我尝试使用这个适配器,但我无法使其工作。

我应该继续尝试,或者可能建议一些其他解决方案,比如Express吗?

谢谢您的帮助。

英文:

I've been trying to deploy a svelte application into an IIS server my company has provided. I've tried using this adapter, but i couldn't manage to make it work.

Should I keep on trying on maybe suggest some other solution, like express?

Thanks for the help.

答案1

得分: 2

部署 Sveltekit 应用到 IIS

构建应用

在 IIS 上部署 Sveltekit 应用需要使用适配器 node 进行构建。

  • npm i -D @sveltejs/adapter-node

安装后,打开 svelte.config 文件并将顶部行更改为使用 adapter-node:import adapter from '@sveltejs/adapter-node';

在你的 svelte.config 中确保使用以下代码:

  1. import adapter from '@sveltejs/adapter-node';
  2. import { vitePreprocess } from '@sveltejs/kit/vite';
  3. const config = {
  4. kit: {
  5. adapter: adapter()
  6. },
  7. preprocess: vitePreprocess()
  8. };
  9. export default config;

构建应用:npm run build

部署到服务器

> 服务器上必须安装 Node 和 IISNode

设置站点 URL

  1. C:\inetpub\projectname\wwroot 中创建构建的物理路径。
  2. 在根目录中添加 index.html,内容为模板:
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1">
  6. <title>Hello World</title>
  7. </head>
  8. <body>
  9. <p>Hello World!</p>
  10. </body>
  11. </html>
  1. 添加站点:
    • 打开 IIS 管理器
    • 点击服务器名称 -> 站点
    • 右键单击站点 -> 添加网站
    • 为其命名并指定主机名(网址 sitedomain)
    • 物理路径选择 inetpub -> wwroot
  2. 为站点添加写入权限
    • 右键单击项目文件夹(inetpub->projectfolder) -> 属性 -> 安全 -> 编辑 -> 添加
    • 点击位置
    • 在位置窗口的顶部选择服务器 -> 点击确定
    • 在对象名称框中添加站点名称(如果忘记,可以在 IIS 管理器中看到)
    • 点击检查名称
    • 如果正确输入,站点名称将被添加并下划线标记
    • 点击确定
    • 在权限框中点击站点名称 -> 允许写入权限 -> 确定

测试 URL

在本地 PC 上转到 C:\Windows\System32\drivers\etc -&gt; host,添加 IP 和 Web 地址。

  • 将域名添加到 host 文件
    • 10.39.2.50 sitedomain

访问 http://sitedomain
应该看到模板 HTML

添加构建到服务器

在项目的 wwwroot 文件夹中添加构建文件夹

在构建文件夹中,您需要添加两个文件。

server.cjs 文件:

  1. process.env.SOCKET_PATH = process.env.PORT;
  2. delete process.env.PORT
  3. import('./index.js')

web.config 文件:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <configuration>
  3. <system.webServer>
  4. <handlers>
  5. <add name="iisnode" path="server.cjs" verb="*" modules="iisnode" />
  6. </handlers>
  7. <iisnode
  8. nodeProcessCommandLine="&quot;C:\Program Files\nodejs\node.exe&quot;"
  9. watchedFiles="web.config;*.js;*.cjs"
  10. />
  11. <rewrite>
  12. <rules>
  13. <!-- 所有其他 URL 映射到 node.js 站点入口点 -->
  14. <rule name="node">
  15. <match url=".*" />
  16. <action type="Rewrite" url="server.cjs" />
  17. </rule>
  18. </rules>
  19. <!-- 如果 Location Header 被重写,则将其改回 -->
  20. <outboundRules>
  21. <rule name="back">
  22. <match serverVariable="RESPONSE_Location" pattern="(.*)/server.cjs" />
  23. <action type="Rewrite" value="{R:1}" />
  24. </rule>
  25. </outboundRules>
  26. </rewrite>
  27. <defaultDocument>
  28. <files>
  29. <add value="server.cjs" />
  30. </files>
  31. </defaultDocument>
  32. </system.webServer>
  33. </configuration>
  • 将构建文件复制到服务器的构建文件夹
  • 将 package.json、packagelock.json 和 .env(如果有)复制到构建文件夹

安装包

  1. 以管理员身份打开 cmd
  2. 进入构建文件夹
  3. 运行 npm ci --omit dev

Prisma 应用

如果您使用 Prisma,需要将本地 PC 中的 .prisma 文件夹复制到服务器的 node-modules 中。如果有多个数据库连接,您还需要复制每个生成的 Prisma 节点 @generated-db

更改站点位置

在完成所有这些步骤后,回到 IIS 管理器,点击新站点 -> 基本设置,并将物理路径从 wwwroot 更改为构建文件夹。

英文:

Deploying Sveltekit App IIS

Building App

Sveltekit apps being deployed on IIS need to be built using adapter node

  • npm i -D @sveltejs/adapter-node

After install open svelte.config and change top line to use adapter-node: import adapter from @sveltejs/adapter-node&#39;;

Inside your svelte.config make sure to use this node

  1. import adapter from &#39;@sveltejs/adapter-node&#39;;
  2. import { vitePreprocess } from &#39;@sveltejs/kit/vite&#39;;
  3. /** @type {import(&#39;@sveltejs/kit&#39;).Config} */
  4. const config = {
  5. kit: {
  6. adapter: adapter()
  7. },
  8. preprocess: vitePreprocess()
  9. };
  10. export default config;

Build app: npm run build

Deploying on server

> Node and IISNode must be installed on server

Set up site url

  1. Create physical path for built in C:\inetpub\projectname\wwroot
  2. Add index.html inside of root with boilerplate
  1. &lt;!DOCTYPE html&gt;
  2. &lt;html&gt;
  3. &lt;head&gt;
  4. &lt;meta charset=&quot;utf-8&quot;&gt;
  5. &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1&quot;&gt;
  6. &lt;title&gt;Hello World&lt;/title&gt;
  7. &lt;/head&gt;
  8. &lt;body&gt;
  9. &lt;p&gt;Hello World!&lt;/p&gt;&#160;
  10. &lt;/body&gt;
  11. &lt;/html&gt;
  1. Add site:
    • Open IIS Manager
    • Click on Server Name -> Sites
    • Right click on sites -> Add Website
    • Give it a name and host name (web address sitedomain)
  • Physical path inetpub -> wwroot
  1. Add write permissions for site
    • Right click on project folder(inetpub->projectfolder) -> Properties -> secutiry -> edit -> add
    • Click on locations
    • Select server at top of location window -> Click OK
    • In the object names box add the site name (you can see it in the IIS Manager if you forget)
    • Click Check Names
    • The site name should be added and underlined if entered correctly
    • Click OK
    • In Permissions box click on site name -> Allow write permissions -> OK

Test URL

On local pc go to C:\Windows\System32\drivers\etc -&gt; host add ip and webaddress

  • add domain to host file
    • 10.39.2.50 sitedomain

Navigate to http://sitedomain
Should see boilerplate html

Add build to server

Add build folder inside project wwwroot folder

Inside your build folder you need to add two files.

server.cjs file:

  1. process.env.SOCKET_PATH = process.env.PORT;
  2. delete process.env.PORT
  3. import(&#39;./index.js&#39;)

web.config file:

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  2. &lt;configuration&gt;
  3. &lt;system.webServer&gt;
  4. &lt;handlers&gt;
  5. &lt;add name=&quot;iisnode&quot; path=&quot;server.cjs&quot; verb=&quot;*&quot; modules=&quot;iisnode&quot; /&gt;
  6. &lt;/handlers&gt;
  7. &lt;iisnode
  8. nodeProcessCommandLine=&quot;&amp;quot;C:\Program Files\nodejs\node.exe&amp;quot;&quot;
  9. watchedFiles=&quot;web.config;*.js;*.cjs&quot;
  10. /&gt;
  11. &lt;rewrite&gt;
  12. &lt;rules&gt;
  13. &lt;!-- All other URLs are mapped to the node.js site entry point --&gt;
  14. &lt;rule name=&quot;node&quot;&gt;
  15. &lt;match url=&quot;.*&quot; /&gt;
  16. &lt;action type=&quot;Rewrite&quot; url=&quot;server.cjs&quot; /&gt;
  17. &lt;/rule&gt;
  18. &lt;/rules&gt;
  19. &lt;!-- Change it back if Location Header got rewrited--&gt;
  20. &lt;outboundRules&gt;
  21. &lt;rule name=&quot;back&quot;&gt;
  22. &lt;match serverVariable=&quot;RESPONSE_Location&quot; pattern=&quot;(.*)/server.cjs&quot; /&gt;
  23. &lt;action type=&quot;Rewrite&quot; value=&quot;{R:1}&quot; /&gt;
  24. &lt;/rule&gt;
  25. &lt;/outboundRules&gt;
  26. &lt;/rewrite&gt;
  27. &lt;defaultDocument&gt;
  28. &lt;files&gt;
  29. &lt;add value=&quot;server.cjs&quot; /&gt;
  30. &lt;/files&gt;
  31. &lt;/defaultDocument&gt;
  32. &lt;/system.webServer&gt;
  33. &lt;/configuration&gt;
  • Copy build files to server build folder
  • Copy package.json, packagelock.json, and .env(if there is one) into build folder

Install packages

  1. Open cmd as administrator
  2. Cd into build folder
  3. Run npm ci --omit dev

Prisma apps

If you are using prisma you will need to copy the .prisma folder from local pc node-modules into server node-modules. Also if you have more than one database connection you will need to copy each generated prisma node @generated-db

Change Site location

After all of these steps are completed go back into IIS Manager and click on the new site -> basic settings and change the physical path from wwwroot to build folder.

huangapple
  • 本文由 发表于 2023年7月18日 16:45:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76710978.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定