话说那天跟大叔说博客搭好了,问大叔说要不要也闹一个。大叔说,觉得还是自己写一个博客程序比较靠谱。说实话哈,我觉得WordPress还是挺好用的,但是程序员嘛,大家都有这个习惯,自己写的东西才觉得靠谱(本人也一样,只是懒得写T_T||)。好了言归正传,今天把在Ubuntu Server 11.10上配置Mono 2.10 + Nginx环境的过程展示一下。
首先安装Ubuntu Server,这次使用了10月新发表的11.10,第一次装这个版本,果然遇到点问题,那就是安装时选择中文语言,结果在命令执行过程中提示
locale: Cannot set LC_CTYPE to default locale: No such file or directory locale: Cannot set LC_MESSAGES to default locale: No such file or directory locale: Cannot set LC_ALL to default locale: No such file or directory
百度了一下发现,是语言设置的问题,如果你也遇到了这个问题,解决方案请参考这篇文章,我就不粘贴过来了
http://wiki.ubuntu.org.cn/%E4%BF%AE%E6%94%B9locale
重点的步骤还是说一下,生成一下缺失的区域文件即可解决问题:
qii@ubuntu:~$ cd /usr/share/locales qii@ubuntu:/usr/share/locales$ ls install-language-pack remove-language-pack qii@ubuntu:/usr/share/locales$ sudo ./install-language-pack en_US Generating locales... en_US.UTF-8... done Generation complete. dpkg-trigger: dpkg-trigger must be called from a maintainer script (or with a --by-package option)
接下来安装mono运行时环境。在查找资料时发现,Ubuntu里面经常使用的啊帖便笺居然是拿C#些的,于是Ubuntu自己的发行版里面就包含了mono,由于是server版本,并没有被默认安装,可以通过以下命令来安装:
sudo apt-get install mono-runtime
APT工具将自动从软件仓库中下载并且安装就绪mono环境,完成后可以如下验证:
mono --version
回显是这样子的
root@ubuntu:/usr/share/nginx/www# mono --version Mono JIT compiler version 2.10.5 (Debian 2.10.5-1) Copyright (C) 2002-2011 Novell, Inc, Xamarin, Inc and Contributors. www.mono-project.com TLS: __thread SIGSEGV: altstack Notifications: epoll Architecture: x86 Disabled: none Misc: softdebug LLVM: supported, not enabled. GC: Included Boehm (with typed GC and Parallel Mark)
接下来需要安装一些基本的支持库,来支撑我们程序的运行
sudo apt-get install mono-gmcs libmono-system-data2.0-cil libmono-system-messaging2.0-cil libmono-system-ldap2.0-cil libmono-system-messaging2.0-cil libmono-system-runtime2.0-cil libmono-system-web2.0-cil libmono-system-web-mvc1.0-cil libmono-wcf3.0-cil libmono-winforms2.0-cil
可选:
libmono-oracle2.0-cil libmono-npgsql2.0-cil libmono-nunit2.4-cil libmono-sharpzip2.84-cil libmono-sqlite2.0-cil
这些库被安装完成后,我们可以来安装mono的.Net平台的FastCGI接口,为运行.Net的Web程序做准备了。
sudo apt-get install mono-fastcgi-server2 mono-fastcgi-server4
安装完成后我们来测试下:
fastcgi-mono-server4 /version
回显为:
root@ubuntu:/usr/share/nginx/www# fastcgi-mono-server4 /version fastcgi-mono-server4.exe 2.10.0.0 (c) 2007 Brian Nickel FastCGI Backend for XSP
接下来,我们配置Nginx环境,从软件仓库下载安装Nginx,完成后启动nginx:
sudo apt-get install nginx sudo /etc/init.d/nginx start
安装完成后,用编辑器打开编辑nginx的虚拟主机配置文件/etc/nginx/sites-available/default,server部分参考以下设置:
server { listen 80; ## listen for ipv4; this line is default and implied root /usr/share/nginx/www; index index.php index.aspx Default.aspx index.html index.htm; location / { fastcgi_pass 127.0.0.1:9000; include /etc/nginx/fastcgi_params; } }
其中/usr/share/nginx/www是Web应用的路径,接下来,我们启动fastcgi服务器:
sudo fastcgi-mono-server4 /applications=/:/usr/share/nginx/www /socket=tcp:127.0.0.1:9000
至此,基本的Mono .NET环境已经搭建成功了,我们来部署一个简单的HelloWorld页面做测试。打开vi编辑器创建/usr/share/nginx/www/index.aspx文件,输入以下内容:
<%="hello, world!"%>
打开浏览器访问一下我们刚才创建的页面:
http://[your.ubuntu.server]/index.aspx
OK,运行成功~
据大叔说,mono神马的似乎会非常占内存,不过我觉得一个.NET程序也不至于和Java那么样吃内存吧,于是验证一番,先找到mono的进程号:
ps aux | grep mono
root@ubuntu:~# ps aux | grep mono root 839 0.5 3.9 43960 20188 pts/0 Sl+ 09:35 0:02 /usr/bin/mono /usr/lib/mono/4.0/fastcgi-mono-server4.exe /applications=/:/usr/share/nginx/www /socket=tcp:127.0.0.1:9001
进程号是839,接下来使用top命令跟踪进程活动状态:
top -p 839
top - 09:45:37 up 14 min, 2 users, load average: 0.00, 0.02, 0.05 Tasks: 1 total, 0 running, 1 sleeping, 0 stopped, 0 zombie Cpu(s): 0.0%us, 0.0%sy, 0.0%ni,100.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st Mem: 507612k total, 106348k used, 401264k free, 16132k buffers Swap: 522236k total, 0k used, 522236k free, 51528k cached PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 839 root 20 0 43960 19m 9632 S 0.0 4.0 0:02.82 mono
在静止状态下,mono进程只占用了19m内存,刷新页面,CPU可以跳到1%,至于在生产环境下的表现,还需要进一步的测试。
等有时间,再折腾折腾详细的调优神马的。不知道哪儿还有点问题,运行VS自动生成的.NET 4.0还不成,继续研究研究。PS: 由于我对.NET神马的也不太了解,部署应用时出了点配置错误,现在自动生成的已经可以完美运行了,截图为证:
参考文章
《Mono on FastCGI Nginx》
http://www.mono-project.com/FastCGI_Nginx
《Ubuntu 10.04 server mono2.4.4 nginx (实战成功)》
http://hi.baidu.com/xc_hai/blog/item/a8d8a99679a5a86054fb9624.html
《Ubuntu11.04+Mono+Nginx运行asp.net之HelloWorld》
http://www.cnblogs.com/mszhangxuefei/archive/2011/09/23/monohelloworld.htmlhttp://www.cnblogs.com/mszhangxuefei/archive/2011/09/23/mono_1.html
《Ubuntu11.04+Mono+Nginx运行asp.net之HelloWorld》链接有更改如下:
http://www.cnblogs.com/mszhangxuefei/archive/2011/09/23/mono_1.html
链接已更新,感谢通知!
杯具,偶照做后访问ASPX就直接下载了。。
sudo fastcgi-mono-server4 /applications=/:/usr/share/nginx/www /socket=tcp:127.0.0.1:9000
这条执行时没反应。。。
没有把fastcgi的请求映射过去应该是。 这条是启动一个mono的fastcgi server,监听/usr/share/nginx/www,启动之后应该只是一个控制台,阻塞输出
嗯,今天重装系统再次设置。。。。那条可以执行了
不过又出了个新问题:
======================
执行 /etc/init.d/nginx start
后提示:
Starting nginx: nginx: [emerg] “server” directive is not allowed here in /etc/nginx/sites-enabled/default:78
nginx: configuration file /etc/nginx/nginx.conf test failed
于是我进入nginx的目录,发现 /usr/share/nginx 目录下面居然没有 nginx.conf 这个文件。。。汗,整三天了,还没整好这环境,哎。。。
=================
另外冒似博主少了一条命令 sudo /etc/init.d/nginx start 要行启到NGINX服务,再开启 mono的fastcgi server 监听?(因为我反复安装测试时发现,有时候执行 sudo apt-get install nginx 后,nginx 并没有自动启动,或者 nginx 目录下根本没有 nginx.conf 文件。。。。。
刚用 winscp 搜 nginx 发现只在 /usr/local/nginx 下发现 nginx.conf….搜了一段时间之后发现 nginx 下面又有了 nginx.conf ,邪门(莫非一开始nginx 在后安装或运行初始化配置。。。。我手动启动时配置过程还没完成?)现在使用默认的 nginx.conf 可以启动了,嘿嘿,接下来再配置下 fastcgi 试试
终于成功了,汗,呵呵
nginx.conf 不做任何配置
只在 sites-available/default
中添加
location ~ \.aspx$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.aspx;
fastcgi_param SCRIPT_FILENAME $document_root$stcgi_script_name;
include /etc/nginx/fastcgi_params;
}
就可以运行ASPX 页面,么非是和 nginx 版本有关。。。fa 😳
哈,多谢提醒,我更新了文章内容加上了启动nginx的命令
我使用Ubuntu Server 12.04,玩了几天了都不行,aspx文件不执行,而是直接下载了;在网上搜刮遍了,配置方法都大同小异,但都是之前的老一点的版本,各种配置都尝试了一遍;但总是直接下载aspx文件。看log也没用动态执行的记录。到底啥原因了??
aspx文件不执行,应该是nginx的转发没有完成。检查location里面的正则表达式设置是否科学。
我之前都是用VirtualBox安装的虚拟机Ubuntu Server 12.04,宿主系统Win7,一直出现下载aspx文件的问题,后来换了宿主系统Ubuntu Desktop 12.04,虚拟机还是原来Win7下的那个,没做任何改动,再执行info.aspx探针,正常显示了。心想:问题就算这么解决了,具体原因留给系统开发人员去研究去吧。
可是进一步测试,发现服务器对复杂一点的求情路径还不能正确识别,比如:地址栏输入127.0.0.1/info.aspx,可以正常执行;但是输入127.0.0.1/info.aspx/abc?id=123这样的路径,就报错404:找不到请求路径127.0.0.1/info.aspx/abc;这样请求是使用RestFull风格的架构,在IIS中一直都能正常解析,而且mono 2.10.8.1已经支持MVC了,真不容易,这又是个棘手的问题。
基于安全的考虑,Ngnix默认不支持127.0.0.1/info.aspx/abc path_info这种风格的,需要进行特别的修改,可以仿照PHP使用path_info的方式进行配置。
执行aspx的问题还是比较容易解决,关键在/etc/nginx/sites-available/default 文件的配置上,无论宿主系统是Win7还是Ubuntu Desktop,都一样能正确执行;
可是干掉一个问题,又来新的问题。项目中使用了html5的Websocket协议进行实时通信;服务端用直接用socket实现;在IIS正常,在MonoDev中调试也正常;部署到服务器上,就是不能建立连接;把WEB服务nginx+mono换成apache+mono;都一样,一发起连接请求,立马就收到close消息,服务器端监听进程却未收到连接请求。不知道在哪个环节就被阻断了。
debian 系统 mono2.11.1 和xsp 都是编译安装。
fastcgi-mono-server2 是好的 运行.net 2.0
fastcgi-mono-server4 有进程,但是不工作,nginx报502错误。
问题都解决了,nginx+mono和 apache+mono两个配置下都运行正常;改结贴了,有时间再对比测试一下两个配置的性能。
感谢manlsea为此类问题提供了一个解决方案。
我用ubuntu server12.04也是直接下载了,配置如下
root /usr/share/nginx/www;
index index.aspx Default.aspx index.html index.htm;
server_name localhost;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to index.html
fastcgi_pass 127.0.0.1:9000;
include /etc/nginx/fastcgi_params;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
什么情况?