Ubuntu18+mono+Nginx部署ASP.NET项目

项目环境:

  系统:Ubuntu 18.04.5 LTS (GNU/Linux 4.15.0-135-generic x86_64)

  ASP.NET项目:WebApplication工程,框架是.NET Framework 4.5,启动文件是Default.aspx

一,安装

1.安装 nginx:

root@root1:~# apt-get install nginx

root@root1:~# nginx -v  #查看版本
nginx version: nginx/1.14.0 (Ubuntu)

局域网其他主机输入:http://192.168.17.149/index.nginx-debian.html,查看安装效果

2.安装 mono:

root@root1:~# sudo apt-get install mono-complete

root@root1:~# mono --version  #查看版本
Mono JIT compiler version 5.10.1.20 (tarball Thu Mar 29 10:48:35 UTC 2018)
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
Interpreter: yes
LLVM: supported, not enabled.
GC: sgen (concurrent by default)

3.安装 mono-fastcgi-server:

apt-get install mono-fastcgi-server2  #找不到就略过
apt-get install mono-fastcgi-server4  #安装到/usr/bin/fastcgi-mono-server4
#版本如下
mono-fastcgi-server4 is already the newest version (4.2-2.1).

4.nginx 配置

1)编辑etc/nginx/sites-available/default文件(默认配置,可以添加Server节点,这里不建议直接复制(有行符号错误我没去找),改原文件对应的行就好)

##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# https://www.nginx.com/resources/wiki/start/
# https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/
# https://wiki.debian.org/Nginx/DirectoryStructure
#
# In most cases, administrators will remove this file from sites-enabled/ and
# leave it as reference inside of sites-available where it will continue to be
# updated by the nginx packaging team.
#
# This file will automatically load configuration files provided by other
# applications, such as Drupal or Wordpress. These applications will be made
# available underneath a path with that package name, such as /drupal8.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##


# Default server configuration
#
server {
listen 80 default_server;
listen [::]:80 default_server;

server_name localhost;


location / {
  # First attempt to serve request as file, then
  # as directory, then fall back to displaying a 404.
  # try_files $uri $uri/ =404;
  root /home/root1/www/;#改成自己的路径

  index index.html index.htm default.aspx Default.aspx;
  fastcgi_index Default.aspx;
  fastcgi_pass 127.0.0.1:8000;
  fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  include /etc/nginx/fastcgi_params;
  }
}

补充default例子:

 1 ##
 2 # You should look at the following URL's in order to grasp a solid understanding
 3 # of Nginx configuration files in order to fully unleash the power of Nginx.
 4 # https://www.nginx.com/resources/wiki/start/
 5 # https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/
 6 # https://wiki.debian.org/Nginx/DirectoryStructure
 7 #
 8 # In most cases, administrators will remove this file from sites-enabled/ and
 9 # leave it as reference inside of sites-available where it will continue to be
10 # updated by the nginx packaging team.
11 #
12 # This file will automatically load configuration files provided by other
13 # applications, such as Drupal or Wordpress. These applications will be made
14 # available underneath a path with that package name, such as /drupal8.
15 #
16 # Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
17 ##
18 
19 # Default server configuration
20 #
21 server {
22     listen 80 default_server;
23     listen [::]:80 default_server;
24 
25     # SSL configuration
26     #
27     # listen 443 ssl default_server;
28     # listen [::]:443 ssl default_server;
29     #
30     # Note: You should disable gzip for SSL traffic.
31     # See: https://bugs.debian.org/773332
32     #
33     # Read up on ssl_ciphers to ensure a secure configuration.
34     # See: https://bugs.debian.org/765782
35     #
36     # Self signed certs generated by the ssl-cert package
37     # Don't use them in a production server!
38     #
39     # include snippets/snakeoil.conf;
40 
41      # root /var/www/html;
42         root /home/root1/www/;
43     # Add index.php to the list if you are using PHP
44     index index.html index.htm index.nginx-debian.html default.aspx Default.aspx;
45 
46     server_name localhost;
47 
48     location / {
49         # First attempt to serve request as file, then
50         # as directory, then fall back to displaying a 404.
51         # try_files $uri $uri/ =404;
52             root /home/root1/www/;
53                 index index.html index.htm default.aspx Default.aspx;
54                 fastcgi_index Default.aspx;
55                 fastcgi_pass 127.0.0.1:8000;
56                 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
57                 include /etc/nginx/fastcgi_params;
58     }
59 
60     # pass PHP scripts to FastCGI server
61     #
62     #location ~ .php$ {
63     #    include snippets/fastcgi-php.conf;
64     #
65     #    # With php-fpm (or other unix sockets):
66     #    fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
67     #    # With php-cgi (or other tcp sockets):
68     #    fastcgi_pass 127.0.0.1:9000;
69     #}
70 
71     # deny access to .htaccess files, if Apache's document root
72     # concurs with nginx's one
73     #
74     #location ~ /.ht {
75     #    deny all;
76     #}
77 }
78 
79 
80 # Virtual Host configuration for example.com
81 #
82 # You can move that to a different file under sites-available/ and symlink that
83 # to sites-enabled/ to enable it.
84 #
85 #server {
86 #    listen 80;
87 #    listen [::]:80;
88 #
89 #    server_name example.com;
90 #
91 #    root /var/www/example.com;
92 #    index index.html;
93 #
94 #    location / {
95 #        try_files $uri $uri/ =404;
96 #    }
97 #}
View Code

  上述文件适用于启动文件是htm、html、aspx等。

2)编辑etc/nginx/fastcgi_params,加入如下功能

fastcgi_param PATH_INFO "";
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

3)编辑etc/nginx/nginx.conf文件(服务器群做负载均衡时用到这个(我的k8s服务器集群是不是要开始弄下了ꉂꉂ(ᵔᗜᵔ*)))

 不用修改

5.重启nginx

sudo service nginx restart
/etc/init.d/nginx restart

  补充:启动为sudo service nginx start

6.手动启动mono-fastcgi-server服务

sudo fastcgi-mono-server4 /applications=/:/home/root1/www/ /socket=tcp:127.0.0.1:8000

7.mono-fastcgi-server设置开机自启

新建monoserve文件(WEBAPPS位置改成自己的网站文件所在目录):

touch monoserve.txt
#!/bin/sh
 
### BEGIN INIT INFO
# Provides:          monoserve.sh
# Required-Start:    $local_fs $syslog $remote_fs
# Required-Stop:     $local_fs $syslog $remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start fastcgi mono server with hosts
### END INIT INFO
 
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/local/bin/mono
NAME=monoserver
DESC=monoserver
 
MONOSERVER=$(which fastcgi-mono-server4)
MONOSERVER_PID=$(ps auxf | grep fastcgi-mono-server4.exe | grep -v grep | awk '{print $2}')
 
# WEBAPPS="www.domain1.xyz:/:/var/www/domain1.xyz/,www.domain2.xyz:/:/var/www/domain2.xyz/"
WEBAPPS="/:/home/root1/www/"

case "$1" in
        start)
                if [ -z "${MONOSERVER_PID}" ]; then
                        echo "starting mono server4"
                        ${MONOSERVER} /applications=${WEBAPPS} /socket=tcp:127.0.0.1:8000 &
                        echo "mono server started"
                else
                        echo ${WEBAPPS}
                        echo "mono server is running"
                fi
        ;;
        stop)
                if [ -n "${MONOSERVER_PID}" ]; then
                        kill ${MONOSERVER_PID}
                        echo "mono server stopped"
                else
                        echo "mono server is not running"
                fi
        ;;
esac
 
exit 0

 上面文件放到/etc/init.d/文件夹下面(cp monoserve /etc/init.d/),然后添加执行权限

chmod +x /etc/init.d/monoserve

再然后安装脚本

update-rc.d monoserve defaults

  这里参考了mono fastcgi server自动启动脚本,脚本由fastcgi-mono-server2改为fastcgi-mono-server4,还有一篇文章也有参考价值:http://www.etwiki.cn/ubuntu/16747.html

 

补充:

  1.mono是.net framework的linux实现,也是微软支持的开源的平台。可以用来开发部署运行.net framework程序,与之前的.net core是差不多的东西。现在用.NET5就好了(用了.NET Core我还没试过.NET5,在ks8服务器集群或用容器时实验吧)

  出现时间:

    .net framework(MS,运行平台Windows)

    mono(Xamarin——被MS收购,运行平台Windows丶linux等)

    .Net Core(MS,运行平台Windows丶linux等)

    .Net5(MS,运行平台Windows丶linux等)

  2.我没弄请mono与mono-fastcgi-server的关系,我个人理解是,mono-fastcgi-server是mono+ngiux环境中用来部署server服务的一个插件,把服务部署到8000端口,即:

    nignx+mono时,使用mono-fastcgi-server(现在是mono-fastcgi-server4,上个版本可能是mono-fastcgi-server2),

    Apache+mono时,使用mono_mod。

  然后nginx把8000端口映射到80端口(好像是这么个意思)。

  3.nginx是开机自启的,我们把mono-fastcgi-server4也弄成开机自启就好了(7中设置了需要启动的服务,记得改成自己的)

  4.Nginx部署文件(一)-nginx.conf文件

  5.Nginx部署文件(二)-default文件

  6.Nginx部署文件(三)-fastcgi_params文件

  7.Nginx-Server常规配置

 

365个夜晚,我希望做到两天更一篇博客。加油,小白!
原文地址:https://www.cnblogs.com/qq2806933146xiaobai/p/14425465.html