在 Microsoft IIS 中托管 SvelteKit 项目

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

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

  1. C:\inetpub\projectname\wwroot 中创建构建的物理路径。
  2. 在根目录中添加 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>
  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 文件:

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="&quot;C:\Program Files\nodejs\node.exe&quot;"
			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(如果有)复制到构建文件夹

安装包

  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

import adapter from &#39;@sveltejs/adapter-node&#39;;
import { vitePreprocess } from &#39;@sveltejs/kit/vite&#39;;

/** @type {import(&#39;@sveltejs/kit&#39;).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

  1. Create physical path for built in C:\inetpub\projectname\wwroot
  2. Add index.html inside of root with boilerplate
&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta charset=&quot;utf-8&quot;&gt;
&lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1&quot;&gt;
&lt;title&gt;Hello World&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;p&gt;Hello World!&lt;/p&gt;&#160;
&lt;/body&gt;
&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:

process.env.SOCKET_PATH = process.env.PORT;
delete process.env.PORT

import(&#39;./index.js&#39;)

web.config file:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;configuration&gt;
    &lt;system.webServer&gt;
        &lt;handlers&gt;
            &lt;add name=&quot;iisnode&quot; path=&quot;server.cjs&quot; verb=&quot;*&quot; modules=&quot;iisnode&quot; /&gt;
        &lt;/handlers&gt;
        &lt;iisnode
			nodeProcessCommandLine=&quot;&amp;quot;C:\Program Files\nodejs\node.exe&amp;quot;&quot;
			watchedFiles=&quot;web.config;*.js;*.cjs&quot; 
		/&gt;
		&lt;rewrite&gt;
			&lt;rules&gt;
				&lt;!-- All other URLs are mapped to the node.js site entry point --&gt;
				&lt;rule name=&quot;node&quot;&gt;
					&lt;match url=&quot;.*&quot; /&gt;
					&lt;action type=&quot;Rewrite&quot; url=&quot;server.cjs&quot; /&gt;
				&lt;/rule&gt;
			&lt;/rules&gt;
		  &lt;!-- Change it back if Location Header got rewrited--&gt;
			&lt;outboundRules&gt;
				&lt;rule name=&quot;back&quot;&gt;
					&lt;match serverVariable=&quot;RESPONSE_Location&quot; pattern=&quot;(.*)/server.cjs&quot; /&gt;
					&lt;action type=&quot;Rewrite&quot; value=&quot;{R:1}&quot; /&gt;
				&lt;/rule&gt;
		&lt;/outboundRules&gt;
		&lt;/rewrite&gt;
        &lt;defaultDocument&gt;
            &lt;files&gt;
                &lt;add value=&quot;server.cjs&quot; /&gt;
            &lt;/files&gt;
        &lt;/defaultDocument&gt;
    &lt;/system.webServer&gt;
&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:

确定