Linux操作系统中运行ASP.NET4

《在openSUSE下ASP.NET 4开发环境配置》中已经配置好了 openSUSE 11.3 操作系统。

创新互联是一家集网站建设,衡南企业网站建设,衡南品牌网站建设,网站定制,衡南网站建设报价,网络营销,网络优化,衡南网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。

现在,我们进入“GNOME 终端”,使用 ifconfig 命令验证一下本虚拟的 IP 地址就是一开始我们在宽带路由器中为它分配的静态地址。

 
 
 
 
  1. ben@linux-nyhn:~> /sbin/ifconfig
  2. eth0      Link encap:Ethernet  HWaddr 08:00:27:51:5B:0B  
  3.           inet addr:192.168.1.251  Bcast:192.168.1.255  Mask:255.255.255.0
  4.           inet6 addr: fe80::a00:27ff:fe51:5b0b/64 Scope:Link
  5.           UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
  6.           RX packets:853 errors:0 dropped:0 overruns:0 frame:0
  7.           TX packets:807 errors:0 dropped:0 overruns:0 carrier:0
  8.           collisions:0 txqueuelen:1000 
  9.           RX bytes:853227 (833.2 Kb)  TX bytes:90376 (88.2 Kb)
  10. lo        Link encap:Local Loopback  
  11.           inet addr:127.0.0.1  Mask:255.0.0.0
  12.           inet6 addr: ::1/128 Scope:Host
  13.           UP LOOPBACK RUNNING  MTU:16436  Metric:1
  14.           RX packets:22 errors:0 dropped:0 overruns:0 frame:0
  15.           TX packets:22 errors:0 dropped:0 overruns:0 carrier:0
  16.           collisions:0 txqueuelen:0 
  17.           RX bytes:1300 (1.2 Kb)  TX bytes:1300 (1.2 Kb)
  18. 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 软件,但尚未启用,如下所示:

 
 
 
 
  1. ben@linux-nyhn:~> sudo /sbin/service apache2 status
  2. Checking for httpd2:                                              unused

那么,现在,让我们设置 apache 为开机随系统启动,并且现在就启动它吧:

 
 
 
 
  1. ben@linux-nyhn:~> sudo /sbin/chkconfig -add apache2
  2. apache2                   0:off  1:off  2:off  3:on   4:off  5:on   6:off
  3. ben@linux-nyhn:~> sudo /sbin/service apache2 start
  4. Starting httpd2 (prefork)                                            done
  5. ben@linux-nyhn:~> sudo /sbin/service apache2 status
  6. Checking for httpd2:                                              running

现在让我们在宿主机的 Windows Vista 操作系统的谷歌浏览器中打开 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 吧:

 
 
 
 
  1. ben@linux-nyhn:~> cd public_html
  2. ben@linux-nyhn:~/public_html> vim index.html

再次在 Windows Vista 操作系统中访问 ben.skyiv.com/~ben 网页:

现在让我们来看看 mono 的版本:

 
 
 
 
  1. ben@linux-nyhn:~> mono --version
  2. Mono JIT compiler version 2.6.4 (tarball Mon Jul  5 13:53:45 UTC 2010)
  3. Copyright (C) 2002-2010 Novell, Inc and Contributors. www.mono-project.com
  4.     TLS:           __thread
  5.     GC:            Included Boehm (with typed GC and Parallel Mark)
  6.     SIGSEGV:       altstack
  7.     Notifications: epoll
  8.     Architecture:  amd64
  9.     Disabled:      none
  10. ben@linux-nyhn:~> 
  11. ben@linux-nyhn:~> gmcs --version
  12. Mono C# compiler version 2.6.4.0

这个 mono 2.6.4 的版本太低了。根据 mono 官方下载页面的指示,按如下步骤进行升级:

 
 
 
 
  1. ben@linux-nyhn:~> sudo zypper addrepo http://ftp.novell.com/pub/mono/download-stable/openSUSE_11.3 mono-stable
  2. 正在添加安装源 'mono-stable' [done]
  3. Repository 'mono-stable' successfully added
  4. Enabled: Yes
  5. Autorefresh: No
  6. URI: http://ftp.novell.com/pub/mono/download-stable/openSUSE_11.3
  7. ben@linux-nyhn:~> sudo zypper refresh --repo mono-stable
  8. Retrieving repository 'mono-stable' metadata [done]
  9. 正在构建 'mono-stable' 安装源缓存 [done]
  10. Specified repositories have been refreshed.
  11. ben@linux-nyhn:~> zypper dist-upgrade --repo mono-stable
  12. =================> 在这里进行漫长的升级 <=================
  13. ben@linux-nyhn:~> 

升级完成,再次查看 mono 的版本:

 
 
 
 
  1. ben@linux-nyhn:~> mono --version
  2. Mono JIT compiler version 2.8.1 (tarball Fri Nov 12 14:37:21 UTC 2010)
  3. Copyright (C) 2002-2010 Novell, Inc and Contributors. www.mono-project.com
  4.     TLS:           __thread
  5.     SIGSEGV:       altstack
  6.     Notifications: epoll
  7.     Architecture:  amd64
  8.     Disabled:      none
  9.     Misc:          debugger softdebug 
  10.     LLVM:          yes(2.8svn-mono)
  11.     GC:            Included Boehm (with typed GC and Parallel Mark)
  12. ben@linux-nyhn:~> dmcs --version
  13. Mono C# compiler version 2.8.1.0

果然已经升级到最新的 mono 2.8.1 版本了。

在 Apache mod_mono configuration tool 网页可以生成运行 ASP.NET 所需要的 Apache 软件的配置文件,如下图所示:

生成的 ben.skyiv.com.conf 文件如下所示:

 
 
 
 
  1.   ServerName ben.skyiv.com
  2.   DocumentRoot /home/ben/www
  3.   # MonoServerPath can be changed to specify which version of ASP.NET is hosted
  4.   # mod-mono-server1 = ASP.NET 1.1 / mod-mono-server2 = ASP.NET 2.0
  5.   # For SUSE Linux Enterprise Mono Extension, uncomment the line below:
  6.   # MonoServerPath ben.skyiv.com "/opt/novell/mono/bin/mod-mono-server2"
  7.   # For Mono on openSUSE, uncomment the line below instead:
  8.   MonoServerPath ben.skyiv.com "/usr/bin/mod-mono-server2"
  9.   # To obtain line numbers in stack traces you need to do two things: 
  10.   # 1) Enable Debug code generation in your page by using the Debug="true" 
  11.   #    page directive, or by setting  in the 
  12.   #    application's Web.config
  13.   # 2) Uncomment the MonoDebug true directive below to enable mod_mono debugging
  14.   MonoDebug ben.skyiv.com true
  15.   
  16.   # The MONO_IOMAP environment variable can be configured to provide platform abstraction
  17.   # for file access in Linux.  Valid values for MONO_IOMAP are:
  18.   #    case
  19.   #    drive
  20.   #    all
  21.   # Uncomment the line below to alter file access behavior for the configured application
  22.   MonoSetEnv ben.skyiv.com MONO_IOMAP=all
  23.   #
  24.   # Additional environtment variables can be set for this server instance using 
  25.   # the MonoSetEnv directive.  MonoSetEnv takes a string of 'name=value' pairs 
  26.   # separated by semicolons.  For instance, to enable platform abstraction *and* 
  27.   # use Mono's old regular expression interpreter (which is slower, but has a
  28.   # shorter setup time), uncomment the line below instead:
  29.   # MonoSetEnv ben.skyiv.com MONO_IOMAP=all;MONO_OLD_RX=1
  30.   MonoApplications ben.skyiv.com "/:/home/ben/www"
  31.   
  32.     Allow from all
  33.     Order allow,deny
  34.     MonoSetServerAlias ben.skyiv.com
  35.     SetHandler mono
  36.     SetOutputFilter DEFLATE
  37.     SetEnvIfNoCase Request_URI "\.(?:gif|jpe?g|png)$" no-gzip dont-vary
  38.   
  39.   
  40.     AddOutputFilterByType DEFLATE text/html text/plain text/xml text/javascript
  41.   

这个 ben.skyiv.com.conf 文件需要放在 /etc/apache2/conf.d 目录下:

 
 
 
 
  1. ben@ben:~> ll /etc/apache2/conf.d
  2. 总计 8
  3. -rw-r--r-- 1 ben  users 2037 01-04 01:21 ben.skyiv.com.conf
  4. -rw-r--r-- 1 root root  1239 10-01 02:12 mod_mono.conf

然后创建 /home/ben/www 目录,并重启 Apache 服务:

 
 
 
 
  1. ben@ben:~> mkdir www
  2. ben@ben:~> sudo /etc/init.d/apache2 restart
  3. Syntax OK
  4. Shutting down httpd2 (waiting for all children to terminate)      done
  5. Starting httpd2 (prefork)                                         done
  6. ben@ben:~> 

现在让我们建立一个网站:

 
 
 
 
  1. ben@ben:~> cd www
  2. ben@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 文件,如下所示:

 
 
 
 
  1. ben@ben:~/www> sudo vim /etc/apache2/conf.d/ben.skyiv.com.conf

如上图所示,将 mod-mono-server2 改为 mod-mono-server4 。然后重启 Apache 服务:

 
 
 
 
  1. ben@ben:~> sudo /etc/init.d/apache2 restart
  2. Syntax OK
  3. Shutting down httpd2 (waiting for all children to terminate)      done
  4. Starting httpd2 (prefork)                                         done
  5. ben@ben:~> 

然后重新访问 ben.skyiv.com/aspx/info.aspx 网页:

从上图中可以看出,已经运行在 ASP.NET 4 了。

现在,让我们来编写 info.aspx 程序吧:

 
 
 
 
  1. ben@linux-nyhn:~/www> mkdir aspx src bin
  2. ben@linux-nyhn:~/www> vim aspx/info.aspx
  3. ben@linux-nyhn:~/www> vim src/info.aspx.cs
  4. ben@linux-nyhn:~/www> vim src/info.rsp

网页文件 aspx/info.aspx 的内容如下所示:

 
 
 
 
  1.  <%@ Page Language="C#" inherits="Skyiv.Ben.Web.InfoPage" %>
  2.  
  3.  
  4.  
  5.    
  6.   服务器信息
  7.  
  8.   返回
  9.  
  10.  
  11.  
  12.      
  13.    
  14.    
  15.  
  16.  

对应的 C# 源程序 src/info.aspx.cs 的内容如下所示:

 
 
 
 
  1. using System;
  2. using System.Data;
  3. using System.Web.UI;
  4. using System.Web.UI.WebControls;
  5.  namespace Skyiv.Ben.Web
  6.  {
  7.   public class InfoPage : Page
  8.  {
  9.  protected GridView dgMain;
  10.    public void Page_Load(object sender, EventArgs e)
  11.    {
  12.      if (IsPostBack) return;
  13.       dgMain.DataSource = GetInfo();
  14.       dgMain.DataBind();
  15.      }
  16.    DataTable GetInfo()
  17.      {
  18.       DataTable dt = new DataTable();
  19.       dt.Columns.Add("项目", typeof(string));
  20.        dt.Columns.Add("内容", typeof(string));
  21.      AddRow(dt, "操作系统", Environment.OSVersion);
  22.      AddRow(dt, "公共语言运行库", Environment.Version);
  23.       return dt;
  24.      }
  25.     void AddRow(DataTable dt, string name, object value)
  26.    {
  27.      DataRow dr = dt.NewRow();
  28.      dr[0] = name;
  29.      dr[1] = value.ToString();
  30.     dt.Rows.Add(dr);
  31.     }
  32.    }
  33.   }

编译响应文件 src/info.rsp 的内容如下所示:

 
 
 
 
  1. -out:../bin/info.dll
  2. -t:library
  3. -r:System.Data
  4. -r:System.Web
  5. info.aspx.cs

然后就是使用以下命令进行编译了:

 
 
 
 
  1. ben@linux-nyhn:~/www> cd src
  2. ben@linux-nyhn:~/www/src> dmcs @info.rsp

编译完成,网站的运行效果如下图所示:

至此,我们成功地在 Linux 操作系统中运行 ASP.NET 4。

当前文章:Linux操作系统中运行ASP.NET4
文章位置:http://www.csdahua.cn/qtweb/news25/238775.html

网站建设、网络推广公司-快上网,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 快上网