【在本文后面有測試地址,歡迎大家測試。】
之前博客園上面有一個活動,就是139元買3個月的linux阿里雲主機,我也是通過本次活動體驗了一下linux的阿里雲,這里感謝博客園。首先我的阿里雲選擇的是Ubuntu 12操作系統,默認情況下阿里雲主機里面是什么都沒有安裝的,都需要自己安裝,下面就說一下怎么搭建Mono.NET系統:
我這里選擇的是mono+nginx+fast-cgi-server,這里給出我的阿里雲mono測試地址(估計2012.3月份過期):
http://42.121.129.207/Default.aspx
下面是firebug的調試截圖:
查看磁盤信息
我們買的系統默認情況下只是安裝了系統,而數據盤需要自己掛載,例如我這里的系統占用20多G,還有40多G的數據盤默認是沒有掛載的,首先我們運行df -h查看:
root@AY1212241134392134698:~# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/xvda1 20G 1.4G 18G 7% /
udev 237M 4.0K 237M 1% /dev
tmpfs 99M 260K 98M 1% /run
none 5.0M 0 5.0M 0% /run/lock
none 246M 0 246M 0% /run/shm
可以看到文件系統里面只有一個/dev/xvda1大小是20G,我們是用fdisk -l 可以看到所有的硬盤:
root@AY1212241134392134698:~# fdisk -l
Disk /dev/xvda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders, total 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0002bfb2
Device Boot Start End Blocks Id System
/dev/xvda1 * 2048 41940991 20969472 83 Linux
Disk /dev/xvdb: 42.9 GB, 42949672960 bytes
255 heads, 63 sectors/track, 5221 cylinders, total 83886080 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
Disk /dev/xvdb doesn't contain a valid partition table
上面顯示了2塊盤,其中第一塊盤是Disk /dev/xvda 有21.5G的容量,已經裝上了系統,而第二塊Disk /dev/xvdb: 42.9 GB是沒有掛載的,Disk /dev/xvdb doesn't contain a valid partition table表明沒有分區表。
分區與掛載
下面對/dev/xvdb進行分區與掛載,使用fdisk /dev/xvdb 命令
root@AY1212241134392134698:~# fdisk /dev/xvdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0xabc9a42f.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
Command (m for help): n ===========> 這里輸入n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p ===========> 這里輸入p
Partition number (1-4, default 1): 1 ===========> 這里輸入1
First sector (2048-83886079, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-83886079, default 83886079):
Using default value 83886079
Command (m for help): wq ===========> 這里輸入wq保存設置
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
再次輸入fdisk -l可以查看是否分區成功
root@AY1212241134392134698:~# fdisk -l
Disk /dev/xvda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders, total 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0002bfb2
Device Boot Start End Blocks Id System
/dev/xvda1 * 2048 41940991 20969472 83 Linux
Disk /dev/xvdb: 42.9 GB, 42949672960 bytes
171 heads, 5 sectors/track, 98112 cylinders, total 83886080 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xabc9a42f
Device Boot Start End Blocks Id System
/dev/xvdb1 2048 83886079 41942016 83 Linux
可以看到/dev/xvdb1已經有內容了,System為Linux。
為分區指定文件系統
下面對分區進行格式化,我們將分區格式化為ext4文件系統。可以使用df -hT 查看分區使用的文件系統,我們可以看到Disk /dev/xvda使用的文件系統就是ext4,所以我們將/dev/xvdb1也格式化為同樣的ext4文件系統
sudo mkfs -t ext4 /dev/xvdb1 #格式化分區/dev/xvdb1並指定文件系統為ext4
將分區掛載到目錄
/dev/xvdb1 /root/test ext4 defaults, 0 2 # 掛載分區到目錄/root/test中,此目錄可以自己指定,此命令會將掛載信息寫到/etc/fstab中
安裝mono
sudo apt-get install mono-complete 這個是安裝mono的完整版所以可能要安裝很多東西,包括桌面開發的東西。mono-utils mono-xsp monodoc-http mono-gmcs
安裝Nginx
sudo apt-get update
sudo apt-get install nginx
Nginx的啟動與停止:
sudo /etc/init.d/nginx start
sudo /etc/init.d/nginx stop
配置nginx:
vi /etc/nginx/nginx.conf #這個文件是關於nginx服務器的配置,這里面配置的網站應該是服務器的默認網站
在http節點中加上:
server {
listen 80;
server_name localhost;
location ~ {
root /root/test/www; # 這個目錄也是你自己指定的,表示你的網站的根目錄
index index.html index.htm;
fastcgi_pass 127.0.0.1:8000; #此處端口要與fastcgi映射的端口一致
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
}
vi /etc/nginx/sites-enabled/default #這個文件是關於nginx虛擬主機的配置
修改里面的server節點中的location ~節點與 nginx.conf中的server節點中一致,並修改root的值與 location ~ 中root的值一致
安裝svn
這里要安裝svn主要是我們需要安裝fastcgi-mono-server,而fastcgi-mono-server不能通過apt-get install進行安裝,只能通過源碼安裝,所以這里我們先安裝svn通過svn命令得到fastcgi-mono-server的源碼,再自己編譯,安裝。因為nginx要解析aspx的網頁必須通過fastcgi-mono-server去調用mono的運行時。
sudo apt-get install subversion
獲取fastcgi-mono-server 源碼
svn co http://mono-soc-2007.googlecode.com/svn/trunk/brian/FastCgi/ fastcgi-mono-server
安裝編譯環境
阿里雲默認是沒有編譯環境的,需要自己安裝
sudo apt-get install automake
sudo apt-get install gcc g++ make # 會自動安裝libc的庫以及一些需要的工具,例如binutils等
編譯fastcgi-mono-server
你或許要cd到fastcgi-mono-server目錄下去運行./autogen.sh腳本:
./autogen.sh # 如果運行有問題,請使用bash執行此腳本,一種方法是修改/autogen.sh的第一行#!/bin/sh修改為#!/bin/bash
上面的shell會生成Makefile,下面進行編譯:
make
安裝fastcgi-mono-server
sudo make install
安裝好之后,輸入 fastcgi-mono-server2 --help 和 fastcgi-mono-server --help 都會有相關選項出來
或者直接輸入fastcgi-mono-server 連續按2個tab鍵也會列出兩個fastcgi-mono-server和fastcgi-mono-server2命令,表示安裝成功。
啟動fast-cgi
要注意下面的目錄/root/test/www與上面配置的目錄是一致的:
sudo fastcgi-mono-server2 /socket=tcp:8000 /address=127.0.0.1 /applications=/:/root/test/www >/dev/null 2>&1 &
其實到此為止你的環境應該搭建好了,你可以在/root/test/www下面添加網頁,例如Default.aspx,內容如下進行測試,這也是我的測試網頁的內容。
<%@ Page Language="C#" %>
<%
int number = 0;
if(Application["number"]!=null)
{
number = int.Parse(Application["number"].ToString());
number ++;
}
Application["number"] = number;
%>
<%="Hello Mono.net "+number.ToString() %>
安裝ftp服務器
我們使用的是vsftpd:
sudo apt-get update
sudo apt-get install vsftpd
啟動與停止:
/etc/init.d/vsftpd start
/etc/init.d/vsftpd stop
/etc/init.d/vsftpd restart
ftp的配置:
vi /etc/vsftpd.conf
打開這么幾個屬性:
local_enable=YES #允許linux系統上的本機用戶作為ftp賬戶訪問ftp
write_enable=YES #允許寫,這個是針對非匿名用戶的
local_umask=022 # 本機用戶的umask
connect_from_port_20=YES #使用20端口傳遞數據
idle_session_timeout=600 # 會話超時時間
data_connection_timeout=120 # 數據連接超時時間
chroot_local_user=YES #如果設為YES,本地用戶登錄后將被(默認地)鎖定在虛根下,並被放在他的home目錄下。
vsftpd的用戶有幾種,一種是使用linux操作系統的系統賬戶當作ftp用戶使用,另一種是使用叫做ftp虛擬用戶的用戶,此用戶是vsftpd識別的用戶,但是不能登錄系統,也不是系統本身的賬戶。當然所有這種虛擬用戶登錄到ftp之后對文件的讀寫權限是寄宿在一個真實的linux系統用戶上面額。這里我們僅僅說明直接使用系統本身就有的賬戶去登錄ftp,所以需要打開local_enbale選項。
添加一個系統賬戶專門用來操作ftp,ftp不能使用root登錄。
useradd -d /home/ftpdir -s /sbin/nologin ftptest #添加一個叫做ftptest的系統賬戶,該用戶不能通過ssh或者telnet登錄系統,只能通過ftp訪問其home目錄,該home目錄就是此用戶
在ftp共享文件的目錄,其home目錄是/home/ftpdir ,這個可以自己指定,但是一定要對該目錄有訪問權限,否則ftp登陸的時候進不去.
passwd ftptest #為該用戶指定密碼
要注意的是:指定chroot_local_user=YES之后,必須設定ftp的虛根目錄不能有寫權限,這是新版的vsftpd為了安全設定的,否則會出現登錄不進去的情況,報如下錯誤:
500 OOPS: vsftpd: refusing to run with writable root inside chroot()
意思是,如果開啟了chroot來控制用戶路徑,則用戶不能再具有該用戶根目錄的寫的權限。
解決辦法:
去掉該用戶根目錄的寫的權限,再新建一目錄,以后用新目錄
#sudo chmod a-w /home/用戶名
#sudo mkdir /home/用戶名/新目錄名
其實很不方便,用戶登陸進去會發現必須要再進一層目錄才能找到自己的文件
/etc/init.d/vsftpd restart #重啟vsftpd就可以了
提示:如果已經存在用戶,但是需要禁止該用戶登錄liux系統,可以直接使用root用戶編輯/etc/passwd文件,修改該用戶對應的行的最后面的shell路徑就可以了。
安裝mysql
sudo apt-get install mysql-server
輸入mysql的root密碼
如果需要在linux下面使用Mono連接MySql數據庫,就需要使用支持Mono的Mysql數據提供程序,此程序在MySql官方網站上面可以找到:http://dev.mysql.com/downloads/connector/net/ 在下拉列表中可以選擇Mono環境。
歡迎大家回復,如果有疑問,我會盡我說能給與解釋。如果覺得本文章對您有幫助,麻煩點擊一下推薦,謝謝支持哦。