在Ubuntu上运行ASP.NET MVC项目,Apache,XSP

安装Mono,测试aspx页面

第一步:Add the Mono repository to your system

按照

https://www.mono-project.com/download/stable/#download-lin-ubuntu

比如Ubuntu 18.04使用以下命令

sudo apt install gnupg ca-certificates
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF
echo "deb https://download.mono-project.com/repo/ubuntu stable-bionic main" | sudo tee /etc/apt/sources.list.d/mono-official-stable.list
sudo apt update

第二步:Install Mono

sudo apt install mono-devel

sudo apt install mono-devel mono-complete mono-dbg referenceassemblies-pcl ca-certificates-mono mono-xsp4 

第三步:测试

参考https://www.mono-project.com/docs/getting-started/mono-basics/

建一个项目文件夹

mkdir projectname

新建一个hello.aspx页面

vim hello.aspx

复制以下内容:

<%@ Page Language="C#" %>
<html>
<head>
   <title>Sample Calendar</title>
</head>
<asp:calendar showtitle="true" runat="server">
</asp:calendar>

然后在当前目录运行以下命令

xsp4 --port 9000

然后就可以访问这个页面了,会显示一个日历:

 http://localhost:9000/hello.aspx

如果只是ASPX网页,可以采用自动配置:

https://www.mono-project.com/docs/web/mod_mono-autoconfiguration/

只要启用Apache2模块,不需要其他任何配置:

sudo a2enmod mod_mono_auto

 在conf文件<VirtualHost *:9000></VirtualHost>中指定项目地址就可以。

配置ASP.NET MVC

如果是ASP.NET MVC,有两种方法:

一、直接使用XSP4运行

xsp4 --port 9000

参考页面:

https://www.mono-project.com/docs/web/aspnet/

ASP.NET hosting with XSP

XSP is a standalone web server written in C# that can be used to run your ASP.NET applications with minimal effort. XSP works under both the Mono and Microsoft runtimes. The code is available from our download page (look for XSP web server) or from the git repository (module name: xsp).

The easiest way to start XSP is to run it from within the root directory of your application. It will serve requests on port 8080. Place additional assemblies in the bin directory. Other XSP options can be set on the command line, such as the application directory and the port to listen on.

XSP comes with a set of pages, controls and web services that you can use to test the server and see what ASP.NET looks like.

For example, once you install XSP, you can try some samples like this:

 $ cd /usr/lib/xsp/test
 $ xsp
 Listening on port: 8080
 Listening on address: 0.0.0.0
 Root directory: /home/cvs/mcs/class/corlib/Microsoft.Win32
 Hit Return to stop the server.

You can now browse to http://localhost:8080 and see various sample programs

二、使用Apache2中配置

检查mod_mono.conf可能在下面的地址:

/etc/apache2/mods-available/mod_mono.conf

将以下语句注释掉:

#AddType application/x-asp-net .aspx .ashx .asmx .ascx .asax .config .ascx
#DirectoryIndex index.aspx

只保留:

Include /etc/mono-server4/mono-server4-hosts.conf

修改ports.conf

vim /etc/apache2/ports.conf

加入Listen 9000

查看错误

cat /var/log/apache2/error.log

如果出现Failed running '/usr/bin/mod-mono-server2 --filename /tmp/mod_mono_server_global --nonstop --master (null) (null) (null) (null) (null) (null) (null) (null)'. Reason: No such file or directory之类的错误

参考stackoverflow上的答案:

https://stackoverflow.com/questions/23617568/how-to-resolve-failed-running-usr-lib-pkgconfig-bin-mod-mono-server2-f

安装mono-apache-server4

apt-get update
sudo apt-get install mono-apache-server4


create a symbolic link from /usr/bin/mod-mono-server2 to /usr/bin/mod-mono-server4 like this:

sudo ln -s /usr/bin/mod-mono-server4 /usr/bin/mod-mono-server2

在网站的conf文件配置中

vim /etc/apache2/sites-available/000-default.conf

加入:

<VirtualHost *:9000>
        ServerName IP地址:9000
        DocumentRoot /home/username/www

MonoAutoApplication disabled
AddHandler mono .aspx .ascx .asax .ashx .config .cs .asmx .axd
MonoApplications "/:/home/username/www"

  <Directory />
    Allow from all
    SetHandler mono
    Require all granted
  </Directory>

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

参考https://www.mono-project.com/docs/web/mod_mono/

Configuring Mod_Mono

A basic setup is as follows (with line numbers added for convenience):

MonoAutoApplication disabled
AddHandler mono .aspx .ascx .asax .ashx .config .cs .asmx .axd
MonoApplications "/:/home/username/www"

The first line disables the AutoHosting feature. (If inside a VirtualHost section, it disables it just for that virtual host).

The second line instructs Apache that processing of files with .aspx, etc. extensions should be delegated to mod_mono (rather than Apache processing them itself as plain-text files).

The third line instructs mod_mono that an ASP.NET application exists at the root directory of the web site (i.e. at http://www.example.com/), and that this virtual path corresponds to the physical path on disk of /home/username/www. Normally, the physical path will match whatever Apache would map the given virtual path to. So if the virtual path is /, as in this example, the physical path matches what is in the DocumentRoot directive for Apache. This is important because in that virtual path, Apache itself will continue to serve images, static HTML, and other files, based on the physical path it knows in DocumentRoot, while mod_mono will handle files with .aspx, etc. extensions (or whatever was specified in AddHandler) based on the physical path provided in the MonoApplications directive.

如果还有别的位置请参考:

Here is another configuration that sets up the ASP.NET test suite that comes with mod_mono.

Let’s say you want those file to be available under the virtual path /test. Edit your httpd.conf file (hint: /etc/httpd, /etc/apache2) and append the following lines (remove the numbers ;-):

1   Alias /test "/usr/share/doc/xsp/test"
2   MonoApplications "/test:/usr/share/doc/xsp/test"
3   <Location /test>
4       SetHandler mono
5   </Location>

Unlike the first example, this example assumes that the virtual path “/test” does not already correspond to the physical path /usr/share/doc/xsp/test. The Alias directive is a standard Apache directive to map a virtual path to a physical path. The second line creates an ASP.NET application in something other than the root virtual path. Lines 3-5 instruct Apache that absolutely all files in the /test virtual path are to be handled by mod_mono. (mod_mono can handle non-ASP.NET pages as well. It will just send other files to the client directly without special processing.)

Now start/restart Apache and browse http://hostname/test/index.aspx (where hostname is the name of the server, or 127.0.0.1 if you’re running Apache locally). If you cannot see the test page, go to the troubleshooting section. Otherwise, welcome to ASP.NET!

最后,启动运行

a2ensite 000-default.conf
service apache2 restart

查看error

cat /var/log/apache2/error.log

三、在Nginx中配置,使用FastCGI

没尝试,参见:https://www.mono-project.com/docs/web/aspnet/

ASP.NET hosting with Nginx

Nginx is a high-performance HTTP server which support running ASP.NET and ASP.NET MVC web applications through FastCGI protocol. See the FastCGI Nginx page for details on installation and configuration.

以下文章可参考:

https://blog.csdn.net/u012925833/article/details/80451229

但主要还是要参考官方页面:

https://www.mono-project.com/download/stable/

https://www.mono-project.com/docs/web/aspnet/

特别是:

https://www.mono-project.com/docs/web/mod_mono/

原文地址:https://www.cnblogs.com/yanghao2008/p/13047915.html