其实铺垫了那么久,终于到重点了,迫不及待了吧,那么我们用重量级工具Visual Studio 2015,安装Update3, 安装DotNetCore.1.0.1-VS2015Tools.Preview2.0.2.exe,打开Visual Studio 2015 点击,创建,修改自寄宿代码,
var host = new WebHostBuilder() .UseKestrel() .UseStartup() .UseUrls("http://*:5000/") //配置监听端口 .Build(); host.Run();
编译并发布到开发环境,并上传到服务器/test目录下,执行命令
# cd test# dotnet WebApplication1.dllHosting environment: ProductionContent root path: /testNow listening on: http://*:5000Application started. Press Ctrl+C to shut down.
访问 可以看到运行成功了,Control+Z取消web网站运行,可知我们的网站运行在bash中,那么web网站 可不可以开机启动,并且异常重启吗? 当然可以,这就用上我们我们上节说到的守护进程了,添加文件WebApplication1.conf
[program:WebApplication1]command=dotnet WebApplication1.dll ; 运行程序的命令directory=/test ; 命令执行的目录autorestart=true ; 程序意外退出是否自动重启stderr_logfile=/var/log/WebApplication1.err.log ; 错误日志文件stdout_logfile=/var/log/WebApplication1.out.log ; 输出日志文件environment=ASPNETCORE_ENVIRONMENT=Production ; 进程环境变量user=root ; 进程执行
上传至/etc/supervisord.d,执行命令
# supervisorctl reloadRestarted supervisord
访问 打开/var/log/WebApplication1.err.log,发现 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 由此可知此端口已被占用,查看谁占用了此端口
# ss -lnp | grep 5000tcp LISTEN 2 128 :::5000 :::* users:(("dotnet",pid=8210,fd=204))# kill 8210
发现一直kill不掉,执行命令
# systemctl status 8210● session-1.scope - Session 1 of user root Loaded: loaded Drop-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.conf Active: active (running) since 六 2016-09-17 05:43:12 CST; 6h ago CGroup: /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
访问 排查文章向上,可以看到取Application started. Press Ctrl+C to shut down, 但是我按了Ctrl+Z,实际上没进行shut down
我们都知道nginx是一个轻量级高性能web反向代理服务器,做缓存和负载均衡都很方便, 下面来安装,执行命令
# yum install -y nginx# systemctl start nginx.service# systemctl enable nginx.service# systemctl status nginx.service# nginx.service - The nginx HTTP and reverse proxy server Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled) Active: active (running) since 六 2016-09-17 12:59:37 CST; 30s ago Main PID: 4197 (nginx) CGroup: /system.slice/nginx.service ├─4197 nginx: master process /usr/sbin/nginx └─4198 nginx: worker process
由此可以看到nginx启动成功了,访问 查看nginx配置文件位置
# nginx -V #查看配置参数--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; } }
执行命令
# nginx -t #测试配置文件# nginx -s reload #重新加载配置文件
将nginx添加至SELinux的白名单。
yum install policycoreutils-python sudo cat /var/log/audit/audit.log | grep nginx | grep denied | audit2allow -M mynginx sudo semodule -i mynginx.pp
访问