英文:
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 中确保使用以下代码:
import adapter from '@sveltejs/adapter-node';
import { vitePreprocess } from '@sveltejs/kit/vite';
const config = {
kit: {
adapter: adapter()
},
preprocess: vitePreprocess()
};
export default config;
构建应用:npm run build
部署到服务器
> 服务器上必须安装 Node 和 IISNode
设置站点 URL
- 在
C:\inetpub\projectname\wwroot
中创建构建的物理路径。 - 在根目录中添加 index.html,内容为模板:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Hello World</title>
</head>
<body>
<p>Hello World!</p>
</body>
</html>
- 添加站点:
- 打开 IIS 管理器
- 点击服务器名称 -> 站点
- 右键单击站点 -> 添加网站
- 为其命名并指定主机名(网址 sitedomain)
- 物理路径选择 inetpub -> wwroot
- 为站点添加写入权限
- 右键单击项目文件夹(inetpub->projectfolder) -> 属性 -> 安全 -> 编辑 -> 添加
- 点击位置
- 在位置窗口的顶部选择服务器 -> 点击确定
- 在对象名称框中添加站点名称(如果忘记,可以在 IIS 管理器中看到)
- 点击检查名称
- 如果正确输入,站点名称将被添加并下划线标记
- 点击确定
- 在权限框中点击站点名称 -> 允许写入权限 -> 确定
测试 URL
在本地 PC 上转到 C:\Windows\System32\drivers\etc -> host
,添加 IP 和 Web 地址。
- 将域名添加到 host 文件
- 10.39.2.50 sitedomain
访问 http://sitedomain
应该看到模板 HTML
添加构建到服务器
在项目的 wwwroot 文件夹中添加构建文件夹
在构建文件夹中,您需要添加两个文件。
server.cjs 文件:
process.env.SOCKET_PATH = process.env.PORT;
delete process.env.PORT
import('./index.js')
web.config 文件:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<handlers>
<add name="iisnode" path="server.cjs" verb="*" modules="iisnode" />
</handlers>
<iisnode
nodeProcessCommandLine=""C:\Program Files\nodejs\node.exe""
watchedFiles="web.config;*.js;*.cjs"
/>
<rewrite>
<rules>
<!-- 所有其他 URL 映射到 node.js 站点入口点 -->
<rule name="node">
<match url=".*" />
<action type="Rewrite" url="server.cjs" />
</rule>
</rules>
<!-- 如果 Location Header 被重写,则将其改回 -->
<outboundRules>
<rule name="back">
<match serverVariable="RESPONSE_Location" pattern="(.*)/server.cjs" />
<action type="Rewrite" value="{R:1}" />
</rule>
</outboundRules>
</rewrite>
<defaultDocument>
<files>
<add value="server.cjs" />
</files>
</defaultDocument>
</system.webServer>
</configuration>
- 将构建文件复制到服务器的构建文件夹
- 将 package.json、packagelock.json 和 .env(如果有)复制到构建文件夹
安装包
- 以管理员身份打开 cmd
- 进入构建文件夹
- 运行
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';
Inside your svelte.config make sure to use this node
import adapter from '@sveltejs/adapter-node';
import { vitePreprocess } from '@sveltejs/kit/vite';
/** @type {import('@sveltejs/kit').Config} */
const config = {
kit: {
adapter: adapter()
},
preprocess: vitePreprocess()
};
export default config;
Build app: npm run build
Deploying on server
> Node and IISNode must be installed on server
Set up site url
- Create physical path for built in
C:\inetpub\projectname\wwroot
- Add index.html inside of root with boilerplate
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Hello World</title>
</head>
<body>
<p>Hello World!</p> 
</body>
</html>
- 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
- 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 -> 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:
process.env.SOCKET_PATH = process.env.PORT;
delete process.env.PORT
import('./index.js')
web.config file:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<handlers>
<add name="iisnode" path="server.cjs" verb="*" modules="iisnode" />
</handlers>
<iisnode
nodeProcessCommandLine="&quot;C:\Program Files\nodejs\node.exe&quot;"
watchedFiles="web.config;*.js;*.cjs"
/>
<rewrite>
<rules>
<!-- All other URLs are mapped to the node.js site entry point -->
<rule name="node">
<match url=".*" />
<action type="Rewrite" url="server.cjs" />
</rule>
</rules>
<!-- Change it back if Location Header got rewrited-->
<outboundRules>
<rule name="back">
<match serverVariable="RESPONSE_Location" pattern="(.*)/server.cjs" />
<action type="Rewrite" value="{R:1}" />
</rule>
</outboundRules>
</rewrite>
<defaultDocument>
<files>
<add value="server.cjs" />
</files>
</defaultDocument>
</system.webServer>
</configuration>
- Copy build files to server build folder
- Copy package.json, packagelock.json, and .env(if there is one) into build folder
Install packages
- Open cmd as administrator
- Cd into build folder
- 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论