ASP.NET基于Centos 6.5的跨平台实践
1、前言
应技术主管要求,为公司实现ASP.NET框架跨平台运行于Linux服务器上。笔者读书时候也学过C#,现在是一名运维工程师,工作跟C#开发并没有半点关系,但看到C#可以跑在linux下,笔者还是非常高兴的。
2、理论部分
-- mono
是指由Novell公司(由Xamarin发起,并由Miguel de lcaza领导的,一个致力于开创·NET在Linux上使用的开源工程。
-- Jexus(免费非开源)
即 Jexus Web Server,简称JWS,是Linux平台上 的一款ASP.NET WEB服务器。它是 Linux、Unix、FreeBSD 等非Windows系统架设 ASP.NET WEB 服务器的核心程序。
2.1、Jexus
2.1.1、Jexus主配置文件位置
配置文件的查找
1
|
find / -name jws.conf
|
一般显示如下:
1
|
/usr/jexus/jws .conf
|
2.1.2、Jexus主配置文件
1
2
3
4
5
6
7
8
|
SiteLogDir=log #网站及Jexus系统日志存放目录(必填,基于jws相对路径)
SiteConfigDir=siteconf #子配置文件存放目录(必填,绝对路径或相对jws.conf路径)
Runtime=v4.0.30319 #设定Jexus的.NET版本(可选)
httpd.processes=1 #开启进程数(可选,建议6-8核CPU一个进程,最大4进程)
httpd.user=www-data #运行进程身份(可选,默认root)
php-fcgi. set = /usr/bin/php-cgi ,6 #逗号前指定php-cgi路径,逗号后指定php进程数(可选PHP)
CertificateFile= /xxxx/xx .crt #SSL证书路径(可选)
CertificateKeyFile= /xxxx/xx .key #SSL**文件路径(可选)
|
2.1.3、子配置文件
1)子配置文件路径
1
|
SiteConfigDir=siteconf #主配置文件定义的绝对路径或相对jws.conf路径
|
注意:子配置文件名称不能含空格
2)配置文件选项
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
port=80 #倾听端口(必填)
root=/ /var/www/www .cmdschool.org #网站跟目录(必填)
hosts=www.cmdschool.org #站点域名(默认网站只一个)
addr=0.0.0.0 #端口倾听地址
CheckQuery= false #关闭URL安全检测
NoLog= true #禁用日志功能
NoFile= /index .aspx #没文件首页回应
Keep_Alive= false #关闭长连接
UseGZIP= true #启用传输压缩
UseHttps= true #启用ssl加密
DenyFrom=192.168.0.233, 192.168.1.*, 192.168.2.0 /24 #拒绝访问的主机或网段
AllowFrom=192.168.*.* #允许访问的主机或网段
DenyDirs=~ /cgi , ~ /upfiles #拒绝访问的文件或文件夹
indexes=index.aspx,index.html #首页文件(选填)
rewrite=^/.+?\.(asp|php|cgi|pl|sh)$ /index .aspx #URL重写
reproxy= /bbs/ http: //192 .168.1.112 /bbs/ #反向代理
# Jexus php fastcgi address is '/var/run/jexus/phpsvr' ####################################################### fastcgi.add=php|socket: /var/run/jexus/phpsvr #php的fastcgi网关
# php-fpm listen address is '127.0.0.1:9000' ############################################ fastcgi.add=php|tcp:127.0.0.1:9000 #php的fastcgi网关
|
3、实践部分
3.1、主机信息
ser:
ipaddress=10.168.0.165
client:
ipaddress=10.168.0.8
3.2、yum源安装
1
2
3
|
yum -y update yum -y install gcc gcc-c++ bison pkgconfig glib2-devel gettext make libpng-devel libjpeg-devel libtiff-devel libexif-devel giflib-devel libX11-devel freetype-devel fontconfig-devel cairo-devel
yum install -y ntp wget
|
3.3、安装步骤
In ser:
3.3.1、创建工作文件夹
1
2
|
mkdir ~ /ctmj
cd ~ /ctmj
|
3.3.2、ntp时间校准
1
2
|
cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
ntpdate -u pool.ntp.org |
3.3.3、关闭selinux
1
2
|
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
setenforce 0 |
3.3.4、下载安装包
1
2
3
|
wget -c http: //download .mono-project.com /sources/libgdiplus/libgdiplus-2 .10.9. tar .bz2
wget -c http: //download .mono-project.com /sources/mono/mono-3 .10.0. tar .bz2
wget -c http: //linuxdot .net /down/jexus-5 .6.3. tar .gz
|
3.3.5、GDI+组件编译安装
1
2
3
4
5
6
|
tar jxf libgdiplus-2.10.9. tar .bz2
cd libgdiplus-2.10.9/
. /configure --prefix= /usr
make && make install
ldconfig cd ..
|
3.3.6、mono编译安装
1
2
3
4
5
6
7
8
|
tar jxf mono-3.10.0. tar .bz2
cd mono-3.10.0
. /configure --prefix= /usr
sed -i 's/#define HAVE_LOCALCHARSET_H 1/#define HAVE_LOCALCHARSET_H 0/g' eglib /config .h
make && make install
cd ..
mono -V |
显示如下:
1
2
3
4
5
6
7
8
9
10
|
Mono JIT compiler version 3.10.0 (tarball Thu Jan 7 21:58:07 EST 2016) Copyright (C) 2002-2014 Novell, Inc, Xamarin Inc and Contributors. www.mono-project.com TLS: __thread
SIGSEGV: altstack
Notifications: epoll
Architecture: amd64
Disabled: none
Misc: softdebug
LLVM: supported, not enabled.
GC: sgen
|
3.3.7、jexus编译安装
1
2
3
4
5
|
tar zxf jexus-5.6.3. tar .gz
cd jexus-5.6.3
. /install
cd ..
/usr/jexus/jws start
|
3.3.8、设置服务管理脚本
1)创建服务脚本
vim编辑/etc/rc.d/init.d/jexus
输入如下内容:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
#!/bin/bash #chkconfig: 2345 10 90 #description: Jexus Server case "$1" in
start) echo "Jexus Start.."
/usr/jexus/jws start
;;
stop) echo "Jexus Stop.."
/usr/jexus/jws stop
;;
restart) echo "Jexus Restart"
/usr/jexus/jws restart
;;
status) /usr/jexus/jws status
;;
*) echo "Usage: jexus {start|stop|restart|status}"
exit 1
;;
esac |
注,以上脚本必须包含2-3行,否则脚本无法注册:
其中2345是默认启动级别,级别有0-6共7个级别。
-- 等级0表示:表示关机
-- 等级1表示:单用户模式
-- 等级2表示:无网络连接的多用户命令行模式
-- 等级3表示:有网络连接的多用户命令行模式
-- 等级4表示:不可用
-- 等级5表示:带图形界面的多用户模式
-- 等级6表示:重新启动
10是启动优先级,90是停止优先级,优先级范围是0-100,数字越大,优先级越低。
2)注册服务启动脚本
1
|
chkconfig --add jexus |
3.4、jws服务的配置
3.4.1、配置应用程序目录
1
2
|
mkdir -p /var/www/www .cmdschool.org
echo "www.cmdschool.org" > /var/www/www .cmdschool.org /index .html
|
3.4.2、配置虚拟目录
vim编辑/usr/jexus/siteconf/www.cmdschool.org
1
2
3
4
|
port=80 root=/ /var/www/www .cmdschool.org
hosts=www.cmdschool.org indexes=Default.aspx,index.aspx,index.html |
3.4.3、重启服务
1
|
/etc/init .d /jexus restart
|
3.4.4、配置防火墙
1)编辑防火墙
vim编辑/etc/sysconfig/iptables
1
|
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT |
2)重启防火墙
1
|
/etc/init .d /iptables restart
|
3.4.5、静态测试
In Client:
1)模拟dns配置
vim编辑/etc/hosts
1
|
10.168.0.165 www.cmdschool.org |
2)测试服务
1
|
curl www.cmdschool.org |
显示如下:
1
|
www.cmdschool.org |
3.4.6、C#项目测试
1)下载开源的博客网站代码:
http://sourceforge.net/projects/tblogger/
或者直接下载:
1
|
wget http: //nchc .dl.sourceforge.net /project/tblogger/tblogger/v0 .23b /tBloggerV0 .23bSource.zip
|
2)复制代码到测试目录下
1
2
3
|
unzip tBloggerV0.23bSource.zip rm -rf /var/www/www .cmdschool.org/*
cp -rf tBloggerV0.23bSource/* /var/www/www .cmdschool.org/
|
3)编辑windows系统的hosts(模拟DNS配置):
1
|
notepad %SystemRoot%\System32\drivers\etc\hosts |
加入如下内容:
1
|
10.168.0.165 www.cmdschool.org |
4)浏览器测试