“在 Linux 作業系統中運行 ASP.NET 4 (中)”中已經配置好了 openSUSE 11.3 作業系統。
現在,我們進入“GNOME 終端”,使用 ifconfig 命令驗證一下本虛擬 IP 位址就是一開始我們在寬頻路由器中為它分配的靜態地址。
ben@linux-nyhn:~> /sbin/ifconfigeth0 Link encap:Ethernet HWaddr 08:00:27:51:5B:0B inet addr:192.168.1.251 Bcast:192.168.1.255 Mask:255.255.255.0 inet6 addr: fe80::a00:27ff:fe51:5b0b/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:853 errors:0 dropped:0 overruns:0 frame:0 TX packets:807 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:853227 (833.2 Kb) TX bytes:90376 (88.2 Kb)lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:16436 Metric:1 RX packets:22 errors:0 dropped:0 overruns:0 frame:0 TX packets:22 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:1300 (1.2 Kb) TX bytes:1300 (1.2 Kb)ben@linux-nyhn:~> sudo vim /etc/hosts
如上所示,再使用 vim 編輯器編輯 /etc/hosts 檔案,增加一個網域名稱 ben.skyiv.com,如所示:
注意中的 IP 位址必須是 127.0.0.1,而不能是 192.168.1.251。在宿主機的 Windows Vista 作業系統中以管理員身份進入命令提示字元,使用記事本編輯 C:\Windows\System32\drivers\etc\hosts 檔案,增加一個同樣的網域名稱 ben.skyiv.com,注意此時 IP 位址必須是 192.168.1.251,而不能是 127.0.0.1,如所示:
我們的 openSUSE 11.3 作業系統已經安裝好了 apache 軟體,但尚未啟用,如下所示:
ben@linux-nyhn:~> sudo /sbin/service apache2 statusChecking for httpd2: unused
那麼,現在,讓我們設定 apache 為開機隨系統啟動,並且現在就啟動它吧:
ben@linux-nyhn:~> sudo /sbin/chkconfig -add apache2apache2 0:off 1:off 2:off 3:on 4:off 5:on 6:offben@linux-nyhn:~> sudo /sbin/service apache2 startStarting httpd2 (prefork) doneben@linux-nyhn:~> sudo /sbin/service apache2 statusChecking for httpd2: running
現在讓我們在宿主機的 Windows Vista 作業系統的Google瀏覽器中開啟 ben.skyiv.com 網站試試看吧:
如所示,我們的 Apache 2.2.15 已經正常工作在 Linux/SUSE 作業系統上了。只不過現在該網站還沒有內容而已。
在 openSUSE 作業系統中,Apache 的預設文件是放在 /srv/www/htdocs 目錄下的。
openSUSE 作業系統已經為每個使用者建立了 public_html 目錄,例如我的使用者就是 /home/ben/public_html 目錄,用以放置該使用者的網站的內容。可以通過 ben.skyiv.com/~ben 訪問,這裡 ben 是我的使用者名稱,如所示:
從中可以看出,這個目錄是空的,需要使用者自己放裡放東東。那麼,我們就編寫如下一個 index.html 吧:
ben@linux-nyhn:~> cd public_htmlben@linux-nyhn:~/public_html> vim index.html
再次在 Windows Vista 作業系統中訪問 ben.skyiv.com/~ben 網頁:
現在讓我們來看看 mono 的版本:
ben@linux-nyhn:~> mono --versionMono JIT compiler version 2.6.4 (tarball Mon Jul 5 13:53:45 UTC 2010)Copyright (C) 2002-2010 Novell, Inc and Contributors. www.mono-project.comTLS: __threadGC: Included Boehm (with typed GC and Parallel Mark)SIGSEGV: altstackNotifications: epollArchitecture: amd64Disabled: noneben@linux-nyhn:~> ben@linux-nyhn:~> gmcs --versionMono C# compiler version 2.6.4.0
這個 mono 2.6.4 的版本太低了。根據 mono 官方下載頁面的指示,按如下步驟進行升級:
ben@linux-nyhn:~> sudo zypper addrepo http://ftp.novell.com/pub/mono/download-stable/openSUSE_11.3 mono-stable正在添加安裝源 'mono-stable' [done]Repository 'mono-stable' successfully addedEnabled: YesAutorefresh: NoURI: http://ftp.novell.com/pub/mono/download-stable/openSUSE_11.3ben@linux-nyhn:~> sudo zypper refresh --repo mono-stableRetrieving repository 'mono-stable' metadata [done]正在構建 'mono-stable' 安裝源緩衝 [done]Specified repositories have been refreshed.ben@linux-nyhn:~> zypper dist-upgrade --repo mono-stable=================> 在這裡進行漫長的升級 <=================ben@linux-nyhn:~>
升級完成,再次查看 mono 的版本:
ben@linux-nyhn:~> mono --versionMono JIT compiler version 2.8.1 (tarball Fri Nov 12 14:37:21 UTC 2010)Copyright (C) 2002-2010 Novell, Inc and Contributors. www.mono-project.comTLS: __threadSIGSEGV: altstackNotifications: epollArchitecture: amd64Disabled: noneMisc: debugger softdebug LLVM: yes(2.8svn-mono)GC: Included Boehm (with typed GC and Parallel Mark)ben@linux-nyhn:~> dmcs --versionMono C# compiler version 2.8.1.0
果然已經升級到最新的 mono 2.8.1 版本了。
在 Apache mod_mono configuration tool 網頁可以產生運行 ASP.NET 所需要的 Apache 軟體的設定檔,如所示:
產生的 ben.skyiv.com.conf 檔案如下所示:
<virtualhost *:80> ServerName ben.skyiv.com DocumentRoot /home/ben/www # MonoServerPath can be changed to specify which version of ASP.NET is hosted # mod-mono-server1 = ASP.NET 1.1 / mod-mono-server2 = ASP.NET 2.0 # For SUSE Linux Enterprise Mono Extension, uncomment the line below: # MonoServerPath ben.skyiv.com "/opt/novell/mono/bin/mod-mono-server2" # For Mono on openSUSE, uncomment the line below instead: MonoServerPath ben.skyiv.com "/usr/bin/mod-mono-server2" # To obtain line numbers in stack traces you need to do two things: # 1) Enable Debug code generation in your page by using the Debug="true" # page directive, or by setting in the # application's Web.config # 2) Uncomment the MonoDebug true directive below to enable mod_mono debugging MonoDebug ben.skyiv.com true # The MONO_IOMAP environment variable can be configured to provide platform abstraction # for file access in Linux. Valid values for MONO_IOMAP are: # case # drive # all # Uncomment the line below to alter file access behavior for the configured application MonoSetEnv ben.skyiv.com MONO_IOMAP=all # # Additional environtment variables can be set for this server instance using # the MonoSetEnv directive. MonoSetEnv takes a string of 'name=value' pairs # separated by semicolons. For instance, to enable platform abstraction *and* # use Mono's old regular expression interpreter (which is slower, but has a # shorter setup time), uncomment the line below instead: # MonoSetEnv ben.skyiv.com MONO_IOMAP=all;MONO_OLD_RX=1 MonoApplications ben.skyiv.com "/:/home/ben/www" <location "/"> Allow from all Order allow,deny MonoSetServerAlias ben.skyiv.com SetHandler mono SetOutputFilter DEFLATE SetEnvIfNoCase Request_URI "\.(?:gif|jpe?g|png)$" no-gzip dont-vary </location> <ifmodule mod_deflate.c> AddOutputFilterByType DEFLATE text/html text/plain text/xml text/javascript </ifmodule></virtualhost>
這個 ben.skyiv.com.conf 檔案需要放在 /etc/apache2/conf.d 目錄下:
ben@ben:~> ll /etc/apache2/conf.d總計 8-rw-r--r-- 1 ben users 2037 01-04 01:21 ben.skyiv.com.conf-rw-r--r-- 1 root root 1239 10-01 02:12 mod_mono.conf
然後建立 /home/ben/www 目錄,並重啟 Apache 服務:
ben@ben:~> mkdir wwwben@ben:~> sudo /etc/init.d/apache2 restartSyntax OKShutting down httpd2 (waiting for all children to terminate) doneStarting httpd2 (prefork) doneben@ben:~>
現在讓我們建立一個網站:
ben@ben:~> cd wwwben@ben:~/www> vim index.html
在 openSUSE 11.3 作業系統中訪問 ben.skyiv.com 網站,如所示:
點擊中的“伺服器資訊”,出現“The resource cannot be found”錯誤:
這是預料之中的,因為我們還沒有編寫 info.aspx 檔案呢。從中可以看出,ASP.NET 是 2.0 版的。
現在,我們修改 /etc/apache2/conf.d/ben.skyiv.com.conf 檔案,如下所示:
ben@ben:~/www> sudo vim /etc/apache2/conf.d/ben.skyiv.com.conf
如所示,將 mod-mono-server2 改為 mod-mono-server4 。然後重啟 Apache 服務:
ben@ben:~> sudo /etc/init.d/apache2 restartSyntax OKShutting down httpd2 (waiting for all children to terminate) doneStarting httpd2 (prefork) doneben@ben:~>
然後重新訪問 ben.skyiv.com/aspx/info.aspx 網頁:
從中可以看出,已經運行在 ASP.NET 4 了。
現在,讓我們來編寫 info.aspx 程式吧:
ben@linux-nyhn:~/www> mkdir aspx src binben@linux-nyhn:~/www> vim aspx/info.aspxben@linux-nyhn:~/www> vim src/info.aspx.csben@linux-nyhn:~/www> vim src/info.rsp
網頁檔案 aspx/info.aspx 的內容如下所示:
01: <%@ Page Language="C#" inherits="Skyiv.Ben.Web.InfoPage" %>02: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 03: <html xmlns="http://www.w3.org/1999/xhtml" >04: <head runat="server">05: <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 06: <title>伺服器資訊</title>07: </head>08: <body>09: <form id="form1" runat="server">10: <a href="http://ben.skyiv.com">返回</a>11: <hr />12: <asp:GridView id="dgMain" runat="Server" >13: <AlternatingRowStyle BackColor="#FFFFCD" />14: <HeaderStyle BackColor="#AAAADD" />15: </asp:GridView>16: </form>17: </body>18: </html>
對應的 C# 來源程式 src/info.aspx.cs 的內容如下所示:
01: using System;02: using System.Data;03: using System.Web.UI;04: using System.Web.UI.WebControls;05: 06: namespace Skyiv.Ben.Web07: {08: public class InfoPage : Page09: {10: protected GridView dgMain;11: 12: public void Page_Load(object sender, EventArgs e)13: {14: if (IsPostBack) return;15: dgMain.DataSource = GetInfo();16: dgMain.DataBind();17: }18: 19: DataTable GetInfo()20: {21: DataTable dt = new DataTable();22: dt.Columns.Add("項目", typeof(string));23: dt.Columns.Add("內容", typeof(string));24: AddRow(dt, "作業系統", Environment.OSVersion);25: AddRow(dt, "公用語言運行庫", Environment.Version);26: return dt;27: }28: 29: void AddRow(DataTable dt, string name, object value)30: {31: DataRow dr = dt.NewRow();32: dr[0] = name;33: dr[1] = value.ToString();34: dt.Rows.Add(dr);35: }36: }37: }
編譯回應檔 src/info.rsp 的內容如下所示:
-out:../bin/info.dll-t:library-r:System.Data-r:System.Webinfo.aspx.cs
然後就是使用以下命令進行編譯了:
ben@linux-nyhn:~/www> cd srcben@linux-nyhn:~/www/src> dmcs @info.rsp
編譯完成,網站的運行效果如所示:
至此,我們成功地在 Linux 作業系統中運行 ASP.NET 4。
參考資料
- Mod mono
- Apache mod_mono configuration tool