asp.net core 1.0.1发布已有些日子了,怀着好奇的心情体验了把ubuntu下的asp.net core

系统运行环境:ubuntu 16.0.4 for developer

首先搭建.net core的运行环境,可参见微软网站介绍:

sudo sh -c 'echo "deb [arch=amd64] https://apt-mo.trafficmanager.net/repos/dotnet-release/ xenial main" > /etc/apt/sources.list.d/dotnetdev.list'
sudo apt-key adv --keyserver apt-mo.trafficmanager.net --recv-keys 417A0893
sudo apt-get update
sudo apt-get install dotnet-dev-1.0.0-preview2-003131

然后新建目录用于本地存储文件夹,我的放在/var/www/helloworld下,

cd /var/www
sudo mkdir helloworld

创建新项目,并选择web模板,然后还原项目:

dotnet new -t web
dotnet resotre

然后执行dotnet run 就可以运行该项目了,默认情况下asp.net core监听的是5000的端口

dotnet run
dotnet publish

然后我们执行发布命令 dotnet publish

默认情况下是发布的文件夹为当前项目目录下/bin/Debug/netcoreapp1.0,我的完整发布目录为/var/www/helloworld/bin/Release/netcoreapp1.0,

进入该目录,然后执行dotnet run helloworld.dll就可以运行发布版本的asp.net core示例项目了,但是光这样怎么行呢,我们可不想每次重启系统时都敲上一大堆代码去启动监听,这时supervisor就派上用场了,supervisor是基于python的进程管理工具,貌似windows下没法用,

首先去安装supervisor

sudo apt-get install supervisor

等待安装完成,期间如有y/n的等待输入,记得按y

然后去/etc/supervisor/conf.d文件夹下去新增一个

图形界面可用
sudo gedit /etc/supervisor/conf.d/hellomvc.conf(需要安装gedit)
服务端用vim
sudo vim /etc/supervisor/conf.d/hellomvc.conf
当没有该文件时会创建该文件
内容如下
[program:hellomvc]
command=/usr/bin/dotnet /var/www/helloworld/bin/Debug/netcoreapp1.0/publish/helloworld.dll
directory=/var/www/helloworld/bin/Debug/netcoreapp1.0/publish
autostart=true
autorestart=true
stderr_logfile=/var/log/hellomvc.err.log
stdout_logfile=/var/log/hellomvc.out.log
environment=HOME=/var/www/,ASPNETCORE_ENVIRONMENT=Production
user=www-data
stopsignal=INT
stopasgroup=true
killasgroup=true
然后保存并退出 gedit点击右上角的save保存,vim按esc 然后出入:wq保存并退出
然后想改变自定义监听端口时需要修改项目program.cs,传递args参数
public class Program
{
public static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build(); host.Run();
}
}
修改为
public class Program
{
public static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseKestrel()
.UseUrls(args[0])
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build(); host.Run();
}
}
然后在启动时传入参数 dotnet run helloworld "http://*.5000"
helloworld.conf也需修改为
[program:hellomvc]
command=/usr/bin/dotnet /var/www/helloworld/bin/Debug/netcoreapp1.0/publish/helloworld.dll "http://*5000"
directory=/var/www/helloworld/bin/Debug/netcoreapp1.0/publish
autostart=true
autorestart=true
stderr_logfile=/var/log/hellomvc.err.log
stdout_logfile=/var/log/hellomvc.out.log
environment=HOME=/var/www/,ASPNETCORE_ENVIRONMENT=Production
user=www-data
stopsignal=INT
stopasgroup=true
killasgroup=true

或者您也可以在项目中加上配置文件如host.json,内容如下

{
"server.urls": "http://*:5000"
}

    public class Program
{
public static void Main(string[] args)
{
var config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("host.json", optional: true)
.Build();
var host = new WebHostBuilder()
.UseConfiguration(config)
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build();
host.Run();
}
}
}
helloworld.conf为就可以直接监听你host.json所传的端口了
[program:hellomvc]
command=/usr/bin/dotnet /var/www/helloworld/bin/Debug/netcoreapp1./publish/helloworld.dll
directory=/var/www/helloworld/bin/Debug/netcoreapp1./publish
autostart=true
autorestart=true
stderr_logfile=/var/log/hellomvc.err.log
stdout_logfile=/var/log/hellomvc.out.log
environment=HOME=/var/www/,ASPNETCORE_ENVIRONMENT=Production
user=www-data
stopsignal=INT
stopasgroup=true
killasgroup=true

然后program.cs修改为

然后开启supervisor服务

sudo service supervisor stop
sudo service supervisor start

如果是想开机启动自动启动服务时需要创建自启动脚本

sudo vim /etc/init.d/supervisord

内容如下:

#! /bin/sh
### BEGIN INIT INFO
# Provides: supervisord
# Required-Start: $remote_fs
# Required-Stop: $remote_fs
# Default-Start:
# Default-Stop:
# Short-Description: Example initscript
# Description: This file should be used to construct scripts to be
# placed in /etc/init.d.
### END INIT INFO # Author: Dan MacKinlay <danielm@phm.gov.au>
# Based on instructions by Bertrand Mathieu
# http://zebert.blogspot.com/2009/05/installing-django-solr-varnish-and.html # Do NOT "set -e" # PATH should only include /usr/* if it runs after the mountnfs.sh script
PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC="Description of the service"
NAME=supervisord
DAEMON=/usr/local/bin/supervisord
DAEMON_ARGS=""
PIDFILE=/tmp/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME # Exit if the package is not installed
[ -x "$DAEMON" ] || exit 0 # Read configuration variable file if it is present
[ -r /etc/default/$NAME ] && . /etc/default/$NAME # Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh # Define LSB log_* functions.
# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
. /lib/lsb/init-functions #
# Function that starts the daemon/service
#
do_start()
{
# Return
# 0 if daemon has been started
# 1 if daemon was already running
# 2 if daemon could not be started
start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --test > /dev/null \
|| return 1
start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON -- \
$DAEMON_ARGS \
|| return 2
# Add code here, if necessary, that waits for the process to be ready
# to handle requests from services started subsequently which depend
# on this one. As a last resort, sleep for some time.
} #
# Function that stops the daemon/service
#
do_stop()
{
# Return
# 0 if daemon has been stopped
# 1 if daemon was already stopped
# 2 if daemon could not be stopped
# other if a failure occurred
start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --name $NAME
RETVAL="$?"
[ "$RETVAL" = 2 ] && return 2
# Wait for children to finish too if this is a daemon that forks
# and if the daemon is only ever run from this initscript.
# If the above conditions are not satisfied then add some other code
# that waits for the process to drop all resources that could be
# needed by services started subsequently. A last resort is to
# sleep for some time.
start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $DAEMON
[ "$?" = 2 ] && return 2
# Many daemons don't delete their pidfiles when they exit.
rm -f $PIDFILE
return "$RETVAL"
} #
# Function that sends a SIGHUP to the daemon/service
#
do_reload() {
#
# If the daemon can reload its configuration without
# restarting (for example, when it is sent a SIGHUP),
# then implement that here.
#
start-stop-daemon --stop --signal 1 --quiet --pidfile $PIDFILE --name $NAME
return 0
} case "$1" in
start)
[ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
do_start
case "$?" in
0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
esac
;;
stop)
[ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
do_stop
case "$?" in
0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
esac
;;
#reload|force-reload)
#
# If do_reload() is not implemented then leave this commented out
# and leave 'force-reload' as an alias for 'restart'.
#
#log_daemon_msg "Reloading $DESC" "$NAME"
#do_reload
#log_end_msg $?
#;;
restart|force-reload)
#
# If the "reload" option is implemented then remove the
# 'force-reload' alias
#
log_daemon_msg "Restarting $DESC" "$NAME"
do_stop
case "$?" in
0|1)
do_start
case "$?" in
0) log_end_msg 0 ;;
1) log_end_msg 1 ;; # Old process is still running
*) log_end_msg 1 ;; # Failed to start
esac
;;
*)
# Failed to stop
log_end_msg 1
;;
esac
;;
*)
#echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2
echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
exit 3
;;
esac

然后修改权限,并更新系统

sudo chmod +x /etc/init.d/supervisord
sudo update-rc.d supervisord defaults
sudo service supervisord start

这样就实现了开机自启动

kestrel 作为一个asp.net良好的动态内容服务部件,然而不是类似iis apache,weblogic全功能服务器,所以一个反向代理服务器也就必要了,而nginx作为代理服务器也是很多web应用的选择,

首先安装nginx:

sudo apt-get install nginx

然后

配置nginx的代理转发,修改/etc/nginx/sites-available/default,修改内容如下

server {
listen ;
location / {
proxy_pass http://localhost:5000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection keep-alive;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}

保存退出,然后重新加载nginx

sudo nginx -t

sudo nginx -s reload

nginx代理就实现了不信,去你的浏览器打开localhost,是不是就是我们的helloworld项目了,nice !

如果想要本机外的访问,记得开启80 以及5000端口

sudo ufw enable

sudo ufw allow 80
sudo ufw allow 5000
如果遇到文件访问被拒绝记得修改项目的权限
sudo chmod 777 xxx(xxx表示文件夹路径)
参考asp.net官方文档

ubuntu下发布asp.net core并用nginx代理之旅的更多相关文章

  1. ubuntu下发布asp.net core并用nginx代理之旅(续)

    前面实现了ubuntu下的发布,然而实际项目一般为visual studio中发布文件系统,然后上传至生产环境中,(部分参考:上传文件到linux - ubuntu) 这节就发布到生产环境中的: 1. ...

  2. 在Linux(Ubuntu)下搭建ASP.NET Core环境并运行 继续跨平台

    最新教程:http://www.cnblogs.com/linezero/p/aspnetcoreubuntu.html 无需安装mono,在Linux(Ubuntu)下搭建ASP.NET Core环 ...

  3. 发布Asp.net core到nginx 使用nginx代理

    In this guide, we will cover setting up a production-ready ASP.NET environment on an Ubuntu 16.04 Se ...

  4. Linux(Ubuntu)下搭建ASP.NET Core环境

    今天来学习一下ASP.NET Core 运行在Ubuntu中.无需安装mono . 环境 Ubuntu 14.04.4 LTS 服务器版 全新安装系统. 下载地址:http://mirrors.neu ...

  5. docker 初识之二(简单发布ASP.NET Core 网站)

    在发布ASP.NET Core网站以前,先介绍一下DaoCloud 一个免费的docker云容器服务平台.登陆官方网站,创建一台docker主机,这台主机有120分钟的使用时间,对于鄙人学习使用正好合 ...

  6. 在Ubuntu下搭建ASP.NET 5开发环境

    在Ubuntu下搭建ASP.NET 5开发环境 0x00 写在前面的废话 年底这段时间实在太忙了,各种事情都凑在这个时候,没时间去学习自己感兴趣的东西,所以博客也好就没写了.最近工作上有个小功能要做成 ...

  7. Ubuntu下搭建ASP.NET 5

    在Ubuntu下搭建ASP.NET 5开发环境   0x00 写在前面的废话 年底这段时间实在太忙了,各种事情都凑在这个时候,没时间去学习自己感兴趣的东西,所以博客也好就没写了.最近工作上有个小功能要 ...

  8. Windows下构建ASP.NET Core+Code First+Docker

    背景介绍 本文将会示范如何在Windows系统下基于ASP.NET Core构建跨平台服务,并通过Docker容器运行发布. 首先说一下为什么选择这一套组合: 我本人和我们Code4Thought团队 ...

  9. 10分钟学会在windows/Linux下设置ASP.Net Core开发环境并部署应用

    创建和开发ASP.NET Core应用可以有二种方式:最简单的方式是通过Visual Studio 2017 来创建,其优点是简单方便,但需要安装最新版本Visual Studio 2017 prev ...

随机推荐

  1. HashMap的存储原理

    HashMap是java中相当重要的数据结构,使用HashMap的场景非常之多,因此,了解HashMap实现的过程和原理,是非常有必要的,在一些面试中也会经常被问到.好了,我们赶紧来研究java内部是 ...

  2. 单片机C语言基础编程源码六则2

    1.某单片机系统的P2口接一数模转换器DAC0832输出模拟量,现在要求从DAC0832输出连续的三角波,实现的方法是从P2口连续输出按照三角波变化的数值,从0开始逐渐增大,到某一最大值后逐渐减小,直 ...

  3. flume 1.7在windows下的安装与测试

    一.安装 安装java,配置环境变量. 安装flume,下载地址,下载后直接解压即可. 二.运行 创建配置文件:在解压后的文件 apache-flume-1.7.0-bin\conf下创建一个exam ...

  4. docker~大叔对术语的解释

    回到目录 接触和使用docker已经有1年多了,起初对概念本身理解的不是很好,或者说,没有找到一本比较好的书,在自己的研究下,对docker一些基本的概念有了自己的一些认识和看法,现在分享给正在学习d ...

  5. JStorm与Storm源码分析(二)--任务分配,assignment

    mk-assignments主要功能就是产生Executor与节点+端口的对应关系,将Executor分配到某个节点的某个端口上,以及进行相应的调度处理.代码注释如下: ;;参数nimbus为nimb ...

  6. 连接池报错 Proxool Provider unable to load JAXP configurator file: proxool.xml

    上篇博文讲到简易配置 proxool 连接池:http://www.cnblogs.com/linnuo/p/7232380.html 由于把说明注释留在了 proxool.xml 配置文件里导致配置 ...

  7. OpenCV探索之路(二十一)如何生成能在无opencv环境下运行的exe

    我们经常遇到这样的需求:我们在VS写好的程序,需要在一个没有装opencv甚至没有装vs的电脑下运行,跑出效果.比如,你在你的电脑用opencv+vs2015写出一个程序,然后老师叫你把程序发给他,他 ...

  8. ORACLE - 管理控制文件

    在oracle中,控制文件极其重要,如果该文件损坏并在没有备份的情况下,数据库将无法启动,里面的数据也将无法读取恢复,一般情况下,对数据库配置好了以后备份控制文件: 1. 查看控制文件 SQL> ...

  9. 用vue写添加数据、删除数据、筛选数据表格

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...

  10. Boyer-Moore Majority Vote Algorithm

    介绍算法之前, 我们来看一个场景, 假设您有一个未排序的列表.您想知道列表中是否存在一个数量占列表的总数一半以上的元素, 我们称这样一个列表元素为 Majority 元素.如果有这样一个元素, 求出它 ...