CentOS Linux搭建.NET环境

作者: gaohailin 分类: 网站技术 发布时间: 2011-09-16 09:22

Mono是一个跨平台的、开源的.NET开发框架。支持Linux系统,它是Novell公司基于SUSE Linux操作系统开发的。
官网:http://mono-project.com/Main_Page

网上查了下,在Linux上要使用ASP.NET环境的Web服务器可以使用Apache+mod_mono方式或Mono+Jexus方式(Jexus是国人开发的一个免费软件),当然Nginx也可以支持。http://www.mono-project.com/ASP.NET

官方软件包下载地址:http://ftp.novell.com/pub/mono/sources-stable/

Apache+mod_mono方式
安装必备软件:

yum install bison make gcc gcc-c++ libstdc++-devel gettext pkgconfig glib2-devel

1、安装libgdiplus
libgdiplus是mono中的System.Drawing依赖的一个组件,用于显示web页面基本颜色等。目前最新是libgdiplus-2.10。

cd /tmp
wget http://ftp.novell.com/pub/mono/sources/libgdiplus/libgdiplus-2.10.tar.bz2
tar -xjvf libgdiplus-2.10.tar.bz2
cd libgdiplus-2.10
./configure –prefix=/usr/local

生成makefile时提示失败:
configure: WARNING: *** TIFF plug-in will not be built (TIFF library not found) ***
checking for jpeg_destroy_decompress in -ljpeg… no
configure: WARNING: *** JPEG loader will not be built (JPEG library not found) ***
checking for DGifOpenFileName in -lgif… no
configure: WARNING: *** GIF loader will not be built (giflibrary not found) ***
checking for DGifOpenFileName in -lungif… no
configure: WARNING: *** GIF loader will not be built (ungiflibrary not found) ***
checking for libpng12… no
checking for png_read_info in -lpng… no
configure: error: *** libpng12 not found. See http://www.libpng.org/pub/png/libpng.html.

缺少libtiff、libjpeg、libgif、libpng库。补了这些库之后还会提示有库缺少,一起装了:

yum install libtiff libtiff-devel libjpeg libjpeg-devel giflib giflib-devel libpng libpng-devel libX11 libX11-devel freetype freetype-devel fontconfig fontconfig-devel libexif libexif-devel

继续安装libgdiplus:

./configure –prefix=/usr/local
make
make install

2、安装Mono

cd /tmp
wget http://ftp.novell.com/pub/mono/sources/mono/mono-2.10.2.tar.bz2
tar -xjvf mono-2.10.2.tar.bz2
cd mono-2.10.2
./configure –prefix=/usr/local
make
make install

Mono安装之后,可以用命令mono -V查看一下mono的安装情况,如果能够看到mono版本号等信息,说明Mono安装成功。

3、安装Apache

cd /tmp
wget http://labs.renren.com/apache-mirror//httpd/httpd-2.2.21.tar.gz
tar -xzvf httpd-2.2.21.tar.gz
cd httpd-2.2.21
./configure –prefix=/usr/local/apache –enable-mods-shared=most
make
make install

4、安装mod_mono

cd /tmp
wget http://ftp.novell.com/pub/mono/sources/mod_mono/mod_mono-2.10.tar.bz2
tar -xjvf mod_mono-2.10.tar.bz2
cd mod_mono-2.10
./configure
make
make install

5、安装xsp
xsp就是mod-mono-server。

cd /tmp
wget http://ftp.novell.com/pub/mono/sources/xsp/xsp-2.10.2.tar.bz2
tar -xjvf xsp-2.10.2.tar.bz2
cd xsp-2.10.2
./configure –prefix=/usr/local
make
make install

如果生成makefile时提示:
checking for MONO_MODULE… configure: error: Package requirements (mono >= 2.10.0) were not met:

No package ‘mono’ found

Consider adjusting the PKG_CONFIG_PATH environment variable if you
installed software in a non-standard prefix.

Alternatively, you may set the environment variables MONO_MODULE_CFLAGS
and MONO_MODULE_LIBS to avoid the need to call pkg-config.
See the pkg-config man page for more details.

需要设置环境变量,就能编译通过了:

export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig

6、配置Apache
查看/usr/local/apache/conf里已经有mod_mono.conf文件了。

编辑httpd.conf:

vim /usr/local/apache/conf/httpd.conf

1)添加mod_mono.conf配置文件
找到

# Server-pool management (MPM specific)
#Include conf/extra/httpd-mpm.conf

在前面加入:

Include /usr/local/apache/conf/mod_mono.conf

2)将以下一句前的注释去掉:

#ServerName www.example.com:80

不设置ServerName,Apache在启动时会因为domain name报错。

3)建立测试页面:

vim /usr/local/apache/htdocs/test.aspx

网上找了一个,输入:

<%@ Page Language=”C#” %>
<html>
<head>
<title>hello world</title>
</head>
<body>
<%
for (int i=1; i<=7; i++)
{
Response.Write(“<font size=” + i.ToString() + “>”);
Response.Write(“hello world”);
Response.Write(“</font><br />”);
}
%>
</body>
</html>

4)重启Apache:

/usr/local/apache/bin/apachectl restart

5)访问测试页面:

http://IP/test.aspx

显示出以下内容表示成功。

6)设置Apache开机自启动:

vim /etc/rc.local

加入:

/usr/local/apache/bin/apachectl start

7)原理说明:
上面所搭建asp.net运行环境,是以apache作为通讯框架,mod_mono作为接口桥(apache的插件),连接apache与xsp的通讯,xsp最终负责asp.net的服务处理,xsp会监听本地端口用来与mod_mono通讯,后者将处理结果返回给apache,apache再返回给客户端。

参考资料:
http://hi.baidu.com/xc_hai/blog/item/a585bba96e0d62bdca130ca2.html
http://www.xiaojb.com/archives/it/mono.shtml

如果觉得我的文章对您有用,请随意赞赏。您的支持将鼓励我继续创作!

发表评论

电子邮件地址不会被公开。 必填项已用*标注