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.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
然后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: 2 3 4 5 # Default-Stop: 0 1 6 # 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 80; 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官方文檔