2019独角兽企业重金招聘Python工程师标准>>>
其实铺垫了那么久,终于到重点了,迫不及待了吧,那么我们用重量级工具Visual Studio 2015,安装Update3, 安装DotNetCore.1.0.1-VS2015Tools.Preview2.0.2.exe,打开Visual Studio 2015 点击,创建,修改自寄宿代码,
var host = new WebHostBuilder().UseKestrel().UseStartup<Startup>().UseUrls("http://*:5000/") //配置监听端口.Build();host.Run();
编译并发布到开发环境,并上传到服务器/test目录下,执行命令
# 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.
访问http://192.168.91.128:5000/ 可以看到运行成功了,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 reload
Restarted supervisord
访问http://192.168.91.128:9001/ 打开/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 5000
tcp LISTEN 2 128 :::5000 :::* users:(("dotnet",pid=8210,fd=204))
# kill 8210
发现一直kill不掉,执行命令
# 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
访问http://192.168.91.128:9001/ 排查文章向上,可以看到取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 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启动成功了,访问http://192.168.91.128/ 查看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-pythonsudo cat /var/log/audit/audit.log | grep nginx | grep denied | audit2allow -M mynginxsudo semodule -i mynginx.pp
访问http://192.168.91.128/