.net core入门之web应用
2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
其實(shí)鋪墊了那么久,終于到重點(diǎn)了,迫不及待了吧,那么我們用重量級(jí)工具Visual Studio 2015,安裝Update3, 安裝DotNetCore.1.0.1-VS2015Tools.Preview2.0.2.exe,打開(kāi)Visual Studio 2015 點(diǎn)擊,創(chuàng)建,修改自寄宿代碼,
var host = new WebHostBuilder().UseKestrel().UseStartup<Startup>().UseUrls("http://*:5000/") //配置監(jiān)聽(tīng)端口.Build();host.Run();編譯并發(fā)布到開(kāi)發(fā)環(huán)境,并上傳到服務(wù)器/test目錄下,執(zhí)行命令
# cd test # dotnet WebApplication1.dll Hosting environment: Production Content root path: /test Now listening on: http://*:5000 Application started. Press Ctrl+C to shut down.訪問(wèn)http://192.168.91.128:5000/ 可以看到運(yùn)行成功了,Control+Z取消web網(wǎng)站運(yùn)行,可知我們的網(wǎng)站運(yùn)行在bash中,那么web網(wǎng)站 可不可以開(kāi)機(jī)啟動(dòng),并且異常重啟嗎? 當(dāng)然可以,這就用上我們我們上節(jié)說(shuō)到的守護(hù)進(jìn)程了,添加文件WebApplication1.conf
[program:WebApplication1] command=dotnet WebApplication1.dll ; 運(yùn)行程序的命令 directory=/test ; 命令執(zhí)行的目錄 autorestart=true ; 程序意外退出是否自動(dòng)重啟 stderr_logfile=/var/log/WebApplication1.err.log ; 錯(cuò)誤日志文件 stdout_logfile=/var/log/WebApplication1.out.log ; 輸出日志文件 environment=ASPNETCORE_ENVIRONMENT=Production ; 進(jìn)程環(huán)境變量 user=root ; 進(jìn)程執(zhí)行上傳至/etc/supervisord.d,執(zhí)行命令
# supervisorctl reload Restarted supervisord訪問(wèn)http://192.168.91.128:9001/ 打開(kāi)/var/log/WebApplication1.err.log,發(fā)現(xiàn) Unhandled Exception: System.AggregateException: One or more errors occurred. (Error -98 EADDRINUSE address already in use) ---> Microsoft.AspNetCore.Server.Kestrel.Internal.Networking.UvException: Error -98 EADDRINUSE address already in use 由此可知此端口已被占用,查看誰(shuí)占用了此端口
# ss -lnp | grep 5000 tcp LISTEN 2 128 :::5000 :::* users:(("dotnet",pid=8210,fd=204)) # kill 8210發(fā)現(xiàn)一直kill不掉,執(zhí)行命令
# systemctl status 8210 ● session-1.scope - Session 1 of user rootLoaded: loadedDrop-In: /run/systemd/system/session-1.scope.d└─50-After-systemd-logind\x2eservice.conf, 50-After-systemd-user-sessions\x2eservice.conf, 50-Description.conf, 50-SendSIGHUP.conf, 50-Slice.confActive: active (running) since 六 2016-09-17 05:43:12 CST; 6h agoCGroup: /user.slice/user-0.slice/session-1.scope├─ 2262 sshd: root@pts/0├─ 2266 -bash├─ 8210 dotnet WebApplication1.dll└─12212 systemctl status 8210 # systemctl kill session-1.scope訪問(wèn)http://192.168.91.128:9001/ 排查文章向上,可以看到取Application started. Press Ctrl+C to shut down, 但是我按了Ctrl+Z,實(shí)際上沒(méi)進(jìn)行shut down
我們都知道nginx是一個(gè)輕量級(jí)高性能web反向代理服務(wù)器,做緩存和負(fù)載均衡都很方便, 下面來(lái)安裝,執(zhí)行命令
# yum install -y nginx # systemctl start nginx.service # systemctl enable nginx.service # systemctl status nginx.service # nginx.service - The nginx HTTP and reverse proxy serverLoaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)Active: active (running) since 六 2016-09-17 12:59:37 CST; 30s agoMain PID: 4197 (nginx)CGroup: /system.slice/nginx.service├─4197 nginx: master process /usr/sbin/nginx└─4198 nginx: worker process由此可以看到nginx啟動(dòng)成功了,訪問(wèn)http://192.168.91.128/ 查看nginx配置文件位置
# nginx -V #查看配置參數(shù) --conf-path=/etc/nginx/nginx.conf注釋掉原有配置,添加配置
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;}}執(zhí)行命令
# nginx -t #測(cè)試配置文件 # nginx -s reload #重新加載配置文件將nginx添加至SELinux的白名單。
yum install policycoreutils-pythonsudo cat /var/log/audit/audit.log | grep nginx | grep denied | audit2allow -M mynginxsudo semodule -i mynginx.pp訪問(wèn)http://192.168.91.128/
轉(zhuǎn)載于:https://my.oschina.net/weidedong/blog/747431
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的.net core入门之web应用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: twitter storm源码走读(五)
- 下一篇: python引入模块时import与fr